ソースを参照

Dpkg::Checksums: Switch from using programs to Digest modules

This was one cause for portability problems, as the checksum programs
have different names on different systems, they also have different
output.

Obsolete the program property, now that we are not making using any of
those programs.
Guillem Jover 11 年 前
コミット
d5bbe517c0
共有2 個のファイルを変更した34 個の追加26 個の削除を含む
  1. 2 0
      debian/changelog
  2. 32 26
      scripts/Dpkg/Checksums.pm

+ 2 - 0
debian/changelog

@@ -45,6 +45,8 @@ dpkg (1.18.0) UNRELEASED; urgency=low
     - Use --add-location=file in msgmerge and xgettext commands.
     - Use --porefs=noline for po4a command.
   * Wrap file references in man page PO files.
+  * Switch Dpkg::Checksums from using checksum programs to the more portable
+    Digest modules. Obsolete the 'program' property, and add a 'name' one.
 
  -- Guillem Jover <guillem@debian.org>  Tue, 09 Dec 2014 23:53:18 +0100
 

+ 32 - 26
scripts/Dpkg/Checksums.pm

@@ -1,5 +1,5 @@
 # Copyright © 2008 Frank Lichtenheld <djpig@debian.org>
-# Copyright © 2008, 2012-2014 Guillem Jover <guillem@debian.org>
+# Copyright © 2008, 2012-2015 Guillem Jover <guillem@debian.org>
 # Copyright © 2010 Raphaël Hertzog <hertzog@debian.org>
 #
 # This program is free software; you can redistribute it and/or modify
@@ -20,11 +20,13 @@ package Dpkg::Checksums;
 use strict;
 use warnings;
 
-our $VERSION = '1.01';
+our $VERSION = '1.02';
+
+use Carp;
+use Digest;
 
 use Dpkg::Gettext;
 use Dpkg::ErrorHandling;
-use Dpkg::IPC;
 
 use Exporter qw(import);
 our @EXPORT = qw(checksums_get_list checksums_is_supported
@@ -50,15 +52,15 @@ about supported checksums.
 
 my $CHECKSUMS = {
     md5 => {
-	program => [ 'md5sum' ],
+	name => 'MD5',
 	regex => qr/[0-9a-f]{32}/,
     },
     sha1 => {
-	program => [ 'sha1sum' ],
+	name => 'SHA-1',
 	regex => qr/[0-9a-f]{40}/,
     },
     sha256 => {
-	program => [ 'sha256sum' ],
+	name => 'SHA-256',
 	regex => qr/[0-9a-f]{64}/,
     },
 };
@@ -90,16 +92,15 @@ sub checksums_is_supported($) {
 
 Returns the requested property of the checksum algorithm. Returns undef if
 either the property or the checksum algorithm doesn't exist. Valid
-properties currently include "program" (returns an array reference with
-a program name and parameters required to compute the checksum of the
-filename given as last parameter) and "regex" for the regular expression
-describing the common string representation of the checksum (as output
-by the program that generates it).
+properties currently include "name" (returns the name of the digest
+algorithm) and "regex" for the regular expression describing the common
+string representation of the checksum.
 
 =cut
 
 sub checksums_get_property($$) {
     my ($alg, $property) = @_;
+    carp 'obsolete checksums program property' if $property eq 'program';
     return unless checksums_is_supported($alg);
     return $CHECKSUMS->{lc($alg)}{$property};
 }
@@ -179,21 +180,18 @@ sub add_from_file {
     $self->{size}{$key} = $s[7];
 
     foreach my $alg (@alg) {
-	my @exec = (@{$CHECKSUMS->{$alg}{program}}, $file);
-	my $regex = $CHECKSUMS->{$alg}{regex};
-	my $output;
-	spawn(exec => \@exec, to_string => \$output);
-	if ($output =~ /^($regex)(\s|$)/m) {
-	    my $newsum = $1;
-	    if (not $opts{update} and exists $self->{checksums}{$key}{$alg} and
-		$self->{checksums}{$key}{$alg} ne $newsum) {
-		error(g_('file %s has checksum %s instead of expected %s (algorithm %s)'),
-		      $file, $newsum, $self->{checksums}{$key}{$alg}, $alg);
-	    }
-	    $self->{checksums}{$key}{$alg} = $newsum;
-	} else {
-	    error(g_("checksum program gave bogus output `%s'"), $output);
-	}
+        my $digest = Digest->new($CHECKSUMS->{$alg}{name});
+        open my $fh, '<', $file or syserr(g_('cannot open file %s'), $file);
+        $digest->addfile($fh);
+        close $fh;
+
+        my $newsum = $digest->hexdigest;
+        if (not $opts{update} and exists $self->{checksums}{$key}{$alg} and
+            $self->{checksums}{$key}{$alg} ne $newsum) {
+            error(g_('file %s has checksum %s instead of expected %s (algorithm %s)'),
+                  $file, $newsum, $self->{checksums}{$key}{$alg}, $alg);
+        }
+        $self->{checksums}{$key}{$alg} = $newsum;
     }
 }
 
@@ -376,6 +374,14 @@ sub export_to_control {
 
 =head1 CHANGES
 
+=head2 Version 1.02
+
+Obsolete property: Getting the 'program' checksum property will carp() and
+return undef, the Digest module is used internally now.
+
+New property: Add new 'name' property with the name of the Digest algorithm
+to use.
+
 =head2 Version 1.01
 
 New argument: Accept an options argument in $ck->export_to_string().