diff options
author | Marc Espie <espie@cvs.openbsd.org> | 2008-10-06 09:36:18 +0000 |
---|---|---|
committer | Marc Espie <espie@cvs.openbsd.org> | 2008-10-06 09:36:18 +0000 |
commit | 6f5139fc20ddbc5d63363d79c128d6b617cb09f6 (patch) | |
tree | 102ef6dae6ca5866614e11f6d077efd4539d7132 /usr.sbin | |
parent | 4a693d6809683a8df9acd9ee68e6864a4c20a4e0 (diff) |
turn the checksums stored in packing elements into real objects, so that
pkg_add becomes mostly independent of the type of checksum stored.
separate md5 into an abstract OpenBSD::digest class and an md5 subclass
with specific methods to compute checksums and serialize results, and create
an sha class that does the same thing with sha256 (with a base64 serializer)
Diffstat (limited to 'usr.sbin')
-rw-r--r-- | usr.sbin/pkg_add/OpenBSD/Add.pm | 20 | ||||
-rw-r--r-- | usr.sbin/pkg_add/OpenBSD/CollisionReport.pm | 14 | ||||
-rw-r--r-- | usr.sbin/pkg_add/OpenBSD/Delete.pm | 20 | ||||
-rw-r--r-- | usr.sbin/pkg_add/OpenBSD/PackingElement.pm | 54 | ||||
-rw-r--r-- | usr.sbin/pkg_add/OpenBSD/PackingList.pm | 4 | ||||
-rw-r--r-- | usr.sbin/pkg_add/OpenBSD/md5.pm | 118 | ||||
-rw-r--r-- | usr.sbin/pkg_add/pkg_create | 14 | ||||
-rw-r--r-- | usr.sbin/pkg_add/pkg_merge | 2 |
8 files changed, 185 insertions, 61 deletions
diff --git a/usr.sbin/pkg_add/OpenBSD/Add.pm b/usr.sbin/pkg_add/OpenBSD/Add.pm index 7df6f2701e6..7aa4d2f1caf 100644 --- a/usr.sbin/pkg_add/OpenBSD/Add.pm +++ b/usr.sbin/pkg_add/OpenBSD/Add.pm @@ -1,5 +1,5 @@ # ex:ts=8 sw=4: -# $OpenBSD: Add.pm,v 1.83 2008/06/11 09:43:25 espie Exp $ +# $OpenBSD: Add.pm,v 1.84 2008/10/06 09:36:17 espie Exp $ # # Copyright (c) 2003-2007 Marc Espie <espie@openbsd.org> # @@ -75,14 +75,14 @@ sub record_partial_installation # last file may have not copied correctly my $last = $n->{state}->{lastfile}; - if (defined $last && defined($last->{md5})) { + if (defined $last && defined($last->{d})) { - my $old = $last->{md5}; + my $old = $last->{d}; my $lastname = $last->realname($state); - $last->{md5} = $last->compute_md5($lastname); - if ($old ne $last->{md5}) { - print "Adjusting md5 for $lastname from ", - unpack('H*', $old), " to ", unpack('H*', $last->{md5}), "\n"; + $last->{d} = $last->compute_digest($lastname, $old); + if (!$old->equals($last->{d})) { + print "Adjusting ", $old->keyword, " for $lastname from ", + $old->stringize, " to ", $last->{d}->stringize, "\n"; } } register_installation($n); @@ -508,11 +508,11 @@ sub install if (-e $filename) { if ($state->{verbose}) { print "The existing file $filename has NOT been changed\n"; - if (defined $orig->{md5}) { + if (defined $orig->{d}) { # XXX assume this would be the same type of file - my $md5 = $self->compute_md5($filename); - if ($md5 eq $orig->{md5}) { + my $d = $self->compute_digest($filename, $orig->{d}); + if ($d->equals($orig->{d})) { print "(but it seems to match the sample file $origname)\n"; } else { print "It does NOT match the sample file $origname\n"; diff --git a/usr.sbin/pkg_add/OpenBSD/CollisionReport.pm b/usr.sbin/pkg_add/OpenBSD/CollisionReport.pm index f0cf0969fb4..0ec4e9518b8 100644 --- a/usr.sbin/pkg_add/OpenBSD/CollisionReport.pm +++ b/usr.sbin/pkg_add/OpenBSD/CollisionReport.pm @@ -1,5 +1,5 @@ # ex:ts=8 sw=4: -# $OpenBSD: CollisionReport.pm,v 1.18 2008/06/21 14:01:10 espie Exp $ +# $OpenBSD: CollisionReport.pm,v 1.19 2008/10/06 09:36:17 espie Exp $ # # Copyright (c) 2003-2006 Marc Espie <espie@openbsd.org> # @@ -68,7 +68,7 @@ sub collision_report($$) } return; } - my %todo = map {($_->fullname, $_->{md5})} @$list; + my %todo = map {($_->fullname, $_->{d})} @$list; my $clueless_bat; my $clueless_bat2; my $found = 0; @@ -90,16 +90,16 @@ sub collision_report($$) } } if (%todo) { - require OpenBSD::md5; my $destdir = $state->{destdir}; for my $item (sort keys %todo) { if (defined $todo{$item}) { - my $md5 = OpenBSD::md5::fromfile($destdir.$item); - if ($md5 eq $todo{$item}) { - print "\t$item (same md5)\n"; + my $old = $todo{$item}; + my $d = $old->new($destdir.$item); + if ($d->equals($old)) { + print "\t$item (same checksum)\n"; } else { - print "\t$item (different md5)\n"; + print "\t$item (different checksum)\n"; } } else { print "\t$item\n"; diff --git a/usr.sbin/pkg_add/OpenBSD/Delete.pm b/usr.sbin/pkg_add/OpenBSD/Delete.pm index 81b0d5a886d..d21971afd64 100644 --- a/usr.sbin/pkg_add/OpenBSD/Delete.pm +++ b/usr.sbin/pkg_add/OpenBSD/Delete.pm @@ -1,5 +1,5 @@ # ex:ts=8 sw=4: -# $OpenBSD: Delete.pm,v 1.76 2008/06/11 09:43:25 espie Exp $ +# $OpenBSD: Delete.pm,v 1.77 2008/10/06 09:36:17 espie Exp $ # # Copyright (c) 2003-2007 Marc Espie <espie@openbsd.org> # @@ -243,12 +243,13 @@ sub do_not_delete delete $self->{symlink}; delete $self->{link}; - delete $self->{md5}; + my $algo = $self->{d}; + delete $self->{d}; if (-l $realname) { $self->{symlink} = readlink $realname; } elsif (-f _) { - $self->{md5} = $self->compute_md5($realname); + $self->{d} = $self->compute_digest($realname, $algo); } elsif (-d _) { # what should we do ? } @@ -393,15 +394,16 @@ sub delete return; } unless (defined($self->{link}) or $self->{nochecksum} or $state->{quick}) { - if (!defined $self->{md5}) { + if (!defined $self->{d}) { print "Problem: ", $self->fullname, " does not have a checksum\n"; print "NOT deleting: $realname\n"; $state->print("Couldn't delete $realname (no checksum)\n"); return; } - my $md5 = $self->compute_md5($realname); - if ($md5 ne $self->{md5}) { + my $d = $self->compute_digest($realname, + $self->{d}); + if (!$d->equals($self->{d})) { print "Problem: checksum doesn't match for ", $self->fullname, "\n"; print "NOT deleting: $realname\n"; @@ -502,7 +504,7 @@ sub delete return; } - if (!defined $orig->{md5}) { + if (!defined $orig->{d}) { $state->print("Couldn't delete $realname (no checksum)\n"); return; } @@ -514,8 +516,8 @@ sub delete return; } } else { - my $md5 = $self->compute_md5($realname); - if ($md5 eq $orig->{md5}) { + my $d = $self->compute_digest($realname, $orig->{d}); + if ($d->equals($orig->{d})) { print "File $realname identical to sample\n" if $state->{not} or $state->{verbose}; } else { unless ($state->{extra}) { diff --git a/usr.sbin/pkg_add/OpenBSD/PackingElement.pm b/usr.sbin/pkg_add/OpenBSD/PackingElement.pm index 12567b5142b..31bc5b3247e 100644 --- a/usr.sbin/pkg_add/OpenBSD/PackingElement.pm +++ b/usr.sbin/pkg_add/OpenBSD/PackingElement.pm @@ -1,5 +1,5 @@ # ex:ts=8 sw=4: -# $OpenBSD: PackingElement.pm,v 1.148 2008/06/11 12:21:03 espie Exp $ +# $OpenBSD: PackingElement.pm,v 1.149 2008/10/06 09:36:17 espie Exp $ # # Copyright (c) 2003-2007 Marc Espie <espie@openbsd.org> # @@ -250,11 +250,11 @@ sub realname return $state->{destdir}.$name; } -sub compute_md5 +sub compute_digest { - my ($self, $filename) = @_; + my ($self, $filename, $class) = @_; require OpenBSD::md5; - return OpenBSD::md5::fromfile($filename); + return $class->new($filename); } sub write @@ -319,8 +319,8 @@ sub write my ($self, $fh) = @_; print $fh "\@comment no checksum\n" if defined $self->{nochecksum}; $self->SUPER::write($fh); - if (defined $self->{md5}) { - print $fh "\@md5 ", unpack('H*', $self->{md5}), "\n"; + if (defined $self->{d}) { + $self->{d}->write($fh); } if (defined $self->{size}) { print $fh "\@size ", $self->{size}, "\n"; @@ -349,12 +349,11 @@ sub destate } } -sub add_md5 +sub add_digest { - my ($self, $md5) = @_; - $self->{md5} = $md5; + my ($self, $d) = @_; + $self->{d} = $d; } - sub add_size { my ($self, $sz) = @_; @@ -595,8 +594,25 @@ __PACKAGE__->register_with_factory('md5'); sub add { my ($class, $plist, $args) = @_; + + require OpenBSD::md5; + + $plist->{state}->{lastchecksummable}->add_digest(OpenBSD::md5->fromstring($args)); + return; +} + +package OpenBSD::PackingElement::sha; +our @ISA=qw(OpenBSD::PackingElement::Annotation); + +__PACKAGE__->register_with_factory('sha'); + +sub add +{ + my ($class, $plist, $args) = @_; + + require OpenBSD::md5; - $plist->{state}->{lastchecksummable}->add_md5(pack('H*', $args)); + $plist->{state}->{lastchecksummable}->add_digest(OpenBSD::sha->fromstring($args)); return; } @@ -852,14 +868,14 @@ sub write { my ($self, $fh) = @_; $self->SUPER::write($fh); - if (defined $self->{md5}) { - print $fh "\@md5 ", unpack('H*', $self->{md5}), "\n"; + if (defined $self->{d}) { + $self->{d}->write($fh); } } -sub add_md5 +sub add_digest { - &OpenBSD::PackingElement::FileBase::add_md5; + &OpenBSD::PackingElement::FileBase::add_digest; } package OpenBSD::PackingElement::PkgPath; @@ -1348,9 +1364,9 @@ our @ISA=qw(OpenBSD::PackingElement::Unique); sub exec_on_add { 0 } sub exec_on_delete { 0 } -sub add_md5 +sub add_digest { - &OpenBSD::PackingElement::FileBase::add_md5; + &OpenBSD::PackingElement::FileBase::add_digest; } sub add_size @@ -1358,9 +1374,9 @@ sub add_size &OpenBSD::PackingElement::FileBase::add_size; } -sub compute_md5 +sub compute_digest { - &OpenBSD::PackingElement::FileObject::compute_md5; + &OpenBSD::PackingElement::FileObject::compute_digest; } sub write diff --git a/usr.sbin/pkg_add/OpenBSD/PackingList.pm b/usr.sbin/pkg_add/OpenBSD/PackingList.pm index 4d3606dba75..aa86d7ed84b 100644 --- a/usr.sbin/pkg_add/OpenBSD/PackingList.pm +++ b/usr.sbin/pkg_add/OpenBSD/PackingList.pm @@ -1,5 +1,5 @@ # ex:ts=8 sw=4: -# $OpenBSD: PackingList.pm,v 1.83 2008/06/06 15:02:57 espie Exp $ +# $OpenBSD: PackingList.pm,v 1.84 2008/10/06 09:36:17 espie Exp $ # # Copyright (c) 2003-2007 Marc Espie <espie@openbsd.org> # @@ -275,7 +275,7 @@ MAINLOOP: if (m/^\@shared\b/o) { &$cont($_); while(<$fh>) { - redo MAINLOOP unless m/^\@(?:md5|size|symlink|link)\b/o; + redo MAINLOOP unless m/^\@(?:sha|md5|size|symlink|link)\b/o; m/^\@size\b/o || m/^\@symlink\b/o || m/^\@link\b/o; &$cont($_); diff --git a/usr.sbin/pkg_add/OpenBSD/md5.pm b/usr.sbin/pkg_add/OpenBSD/md5.pm index b9713c05d02..6a89f47c5fb 100644 --- a/usr.sbin/pkg_add/OpenBSD/md5.pm +++ b/usr.sbin/pkg_add/OpenBSD/md5.pm @@ -1,5 +1,5 @@ # ex:ts=8 sw=4: -# $OpenBSD: md5.pm,v 1.6 2008/06/11 09:42:40 espie Exp $ +# $OpenBSD: md5.pm,v 1.7 2008/10/06 09:36:17 espie Exp $ # # Copyright (c) 2003-2007 Marc Espie <espie@openbsd.org> # @@ -18,26 +18,126 @@ use strict; use warnings; -package OpenBSD::md5; -use Digest::MD5; +package OpenBSD::digest; -sub fromfile +sub new { - my $fname = shift; + my ($class, $filename) = @_; + $class = ref($class) || $class; + my $digest = $class->digest_file($filename); + bless \$digest, $class; +} + +sub write +{ + my ($self, $fh) = @_; + print $fh "\@", $self->keyword, " ", $self->stringize, "\n"; +} + +sub digest_file +{ + my ($self, $fname) = @_; open(my $file, '<', $fname) or die "can't open $fname: $!"; - my $digest = fromfh($file); + my $digest = $self->digest_fh($file); close($file) or die "problem closing $fname: $!"; return $digest; } + +sub digest_fh +{ + my ($self, $file) = @_; + + my $d = $self->algo; + + $d->addfile($file); + return $d->digest; +} + +sub fromstring +{ + my ($class, $arg) = @_; + $class = ref($class) || $class; + my $d = $class->unstringize($arg); + bless \$d, $class; +} + +sub equals +{ + my ($a, $b) = @_; + return ref($a) eq ref($b) && $$a eq $$b; +} + +package OpenBSD::md5; +our @ISA=(qw(OpenBSD::digest)); + +sub fromfile +{ + my $fname = shift; + return OpenBSD::md5->digest_file($fname); +} + sub fromfh { my $file = shift; + return OpenBSD::md5->digest_fh($file); +} + +sub algo +{ + my $self = shift; + require Digest::MD5; + + return Digest::MD5->new; +} + +sub stringize +{ + my $self = shift; + return unpack('H*', $$self); +} + +sub unstringize +{ + my ($class, $arg) = @_; + return pack('H*', $arg); +} + +sub keyword +{ + return "md5"; +} - my $md5 = new Digest::MD5; +package OpenBSD::sha; +our @ISA=(qw(OpenBSD::digest)); - $md5->addfile($file); - return $md5->digest; +sub algo +{ + my $self = shift; + require Digest::SHA; + + return Digest::SHA->new(256); +} + +sub stringize +{ + my $self = shift; + require MIME::Base64; + + return MIME::Base64::encode_base64($$self, ''); +} + +sub unstringize +{ + my ($class, $arg) = @_; + require MIME::Base64; + + return MIME::Base64::decode_base64($arg); +} + +sub keyword +{ + return "sha"; } 1; diff --git a/usr.sbin/pkg_add/pkg_create b/usr.sbin/pkg_add/pkg_create index 4eeaf88d923..0343fb4dc90 100644 --- a/usr.sbin/pkg_add/pkg_create +++ b/usr.sbin/pkg_add/pkg_create @@ -1,6 +1,6 @@ #! /usr/bin/perl # ex:ts=8 sw=4: -# $OpenBSD: pkg_create,v 1.117 2008/06/11 09:43:25 espie Exp $ +# $OpenBSD: pkg_create,v 1.118 2008/10/06 09:36:17 espie Exp $ # # Copyright (c) 2003-2007 Marc Espie <espie@openbsd.org> # @@ -21,7 +21,6 @@ use warnings; use OpenBSD::PackingList; use OpenBSD::PackageInfo; use OpenBSD::Getopt; -use OpenBSD::md5; use OpenBSD::Temp; use OpenBSD::Error; use OpenBSD::Ustar; @@ -94,7 +93,7 @@ sub compute_checksum $result->make_hardlink($stash->{"$dev/$ino"}); } else { $stash->{"$dev/$ino"} = $name; - $result->add_md5($self->compute_md5($fname)); + $result->add_digest($self->compute_digest($fname, 'OpenBSD::md5')); $result->add_size($size); } } else { @@ -116,7 +115,7 @@ sub verify_checksum_with_base my $check = ref($self)->new($self->{name}); $self->compute_checksum($check, $base, $stash); - for my $field (qw(symlink link md5 size)) { + for my $field (qw(symlink link size)) { # md5 if ((defined $check->{$field} && defined $self->{$field} && $check->{$field} ne $self->{$field}) || (defined $check->{$field} xor defined $self->{$field})) { @@ -125,6 +124,13 @@ sub verify_checksum_with_base $main::errors++; } } + if ((defined $check->{d} && defined $self->{d} && + !$check->{d}->equals($self->{d})) || + (defined $check->{d} xor defined $self->{d})) { + print STDERR "Error: checksum inconsistency for ", + $self->fullname, "\n"; + $main::errors++; + } } diff --git a/usr.sbin/pkg_add/pkg_merge b/usr.sbin/pkg_add/pkg_merge index c8bd09d61b9..aef85498b9c 100644 --- a/usr.sbin/pkg_add/pkg_merge +++ b/usr.sbin/pkg_add/pkg_merge @@ -221,7 +221,7 @@ while(1) { my @todo = (); my @merged = (); for my $cmp (@mergeable) { - if (defined $ref->{md5} && $cmp->{tocopy}->[0]->{md5} eq $ref->{md5}) { + if (defined $ref->{d} && $cmp->{tocopy}->[0]->{d}->equals($ref->{d})) { push(@merged, $cmp); $currentprefix .= $cmp->{prefix}; $copies++; |