File: apt-cacher-lib-cs.pl

package info (click to toggle)
apt-cacher 0.9.4sarge1
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 268 kB
  • ctags: 224
  • sloc: perl: 1,862; makefile: 495; sh: 37
file content (82 lines) | stat: -rwxr-xr-x 1,608 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#! /usr/bin/perl

# this are hooks the overload the methods in apt-cacher-lib.pl and implement
# data checksumming methods

use DBI;
use Fcntl ':flock';
use IO::Handle;
use POSIX;
use Digest::MD5 qw(md5_hex);


my $ctx;
my $dbfile;
my $dbh;
my $dblck;

sub dbauf {
   open($dblck, $dbfile);
   flock($dblck, LOCK_EX);
   $dbh = DBI->connect("dbi:SQLite:dbname=$dbfile","", "",
   { RaiseError => 1, AutoCommit => 0 } );
}

sub dbzu {
   $dbh->disconnect;
   flock($dblck, LOCK_UN);
   close($dblck);
}

# arg: file to be scanned and added to DB
sub import_sums {
   my %sumhash;
   extract_sums(shift, \%sumhash);
   dbauf();
   for(keys %sumhash) {
      #$dbh->do("insert or ignore into sums values(\"$_\", \"$sumhash{sum}\");");
      $dbh->do("replace into sums values(\"$_\", \"".$sumhash{$_}."\");");
   }
   $dbh->commit;
   dbzu();
}

sub db_init {
   $dbfile=shift;
   if(!-s $dbfile) { 
      open $db, ">$dbfile"; close $db; # touch it
      dbauf();
      $dbh->do("CREATE TABLE sums (file varchar PRIMARY KEY, sum varchar(32) NOT NULL);");
      $dbh->commit;
      dbzu();
   }
}

# purpose: create hasher object
sub data_init {
   $ctx = Digest::MD5->new;
   return 1;
}

# purpose: append data to be scanned
sub data_feed {
   $ref=shift;
   $ctx->add($$ref);
}

# arg: filename
sub check_sum {
   my $file=shift;
   my $digest = $ctx->hexdigest;
   dbauf();
   my @sqlar = $dbh->selectrow_array("select sum from sums where file='$file'");
   dbzu();
   if(defined($sqlar[0]) && $sqlar[0] ne '') {
      # now find the faulty deb
      return ($sqlar[0] eq $digest);
   }
   return 1;
}


1;