105,6 → 105,52 |
|
### Meaningful functions |
|
# Compute_Checksums($basepath, $filename) |
# |
# Compute MD5 and SHA1 checksums of a file. The checksums are stored, |
# together with the filename and the file size, in a hash, a reference |
# to which is returned. |
# |
# Returns undef on error. |
|
sub Compute_Checksums { |
my($basepath, $filename) = @_; |
my($fh); |
|
# Open the file and read in the contents. This could be a very |
# large amount of data, but unfortunately, both Digets routines |
# require the entire thing at once. |
|
unless (open($fh, '<', "$basepath/$filename") && binmode($fh)) { |
$Error = "Couldn't open file '$basepath/$filename' for reading."; |
return undef; |
} |
|
# Grab the filesize from stat() |
my(@stat) = stat($fh); |
my $ret = { |
'File' => $filename, |
'Size' => $stat[7], |
}; |
|
my %digesters = ('MD5Sum' => new Digest::MD5, |
'SHA1' => new Digest::SHA1); |
|
# Now calculate the checksums and put them into the hashes. |
while (my ($algo, $dig) = each %digesters) { |
eval { |
seek $fh, 0, 0 or die $!; |
$ret->{$algo} = $dig->addfile($fh)->hexdigest; |
}; |
if ($@) { |
$Error = "Failed to compute $algo for '$basepath/$filename': $@"; |
close $fh; |
return undef; |
} |
} |
return $ret; |
} |
|
# Generate_Release_Triple($archive, $component, $architecture, $version) |
# |
# Generate a Release file for a specific dist/component/arch, in the |
144,37 → 190,10 |
if (0 == grep(/^$ck_file$/, @SigFiles)) { # We don't care about it. |
next; |
} |
|
# Grab the filesize from stat() |
|
my(@stat) = stat("${dirpath}/${ck_file}"); |
my($size) = $stat[7]; |
|
# Open the file and read in the contents. This could be a very |
# large amount of data, but unfortunately, both Digest routines |
# require the entire thing at once. |
|
if (!open(CK_FILE, '<', "${dirpath}/${ck_file}")) { |
$Error = "Couldn't open file '${dirpath}/${ck_file}' for reading."; |
return undef; |
my $checksum = Compute_Checksums($dirpath, $ck_file) or return undef; |
push @Checksums, $checksum; |
} |
|
my(@filetext) = <CK_FILE>; |
close(CK_FILE); |
|
# Now calculate the checksums and put them into the hashes. |
|
my($md5) = Digest::MD5::md5_hex(@filetext); |
my($sha1) = Digest::SHA1::sha1_hex(@filetext); |
|
push @Checksums, { |
'File' => $ck_file, |
'Size' => $size, |
'MD5' => $md5, |
'SHA1' => $sha1, |
}; |
} |
|
# Open a secure tempfile, and write the headers to it. |
|
my($tmpfile_handle, $tmpfile_name) = tempfile(); |
197,17 → 216,14 |
|
# Now print MD5 and SHA1 checksum lists. |
|
print $tmpfile_handle "MD5Sum:\n"; |
foreach my $checksum (@Checksums) { |
printf $tmpfile_handle " %s %8d %s\n", $checksum->{'MD5'}, |
$checksum->{'Size'}, $checksum->{'File'}; |
} |
foreach my $algo ('MD5Sum', 'SHA1') { |
print $tmpfile_handle "$algo:\n"; |
|
print $tmpfile_handle "SHA1:\n"; |
foreach my $checksum (@Checksums) { |
printf $tmpfile_handle " %s %8d %s\n", $checksum->{'SHA1'}, |
printf $tmpfile_handle " %s %8d %s\n", $checksum->{$algo}, |
$checksum->{'Size'}, $checksum->{'File'}; |
} |
} |
|
close($tmpfile_handle); |
|
240,34 → 256,12 |
|
my($file); |
for $file (@files) { |
my($fullfile) = "${dists_dir}/${archive}/${file}"; |
|
# Now, for each file, generate MD5 and SHA1 checksums, and put them |
# into Checksums for later use (assuming it's a file we care about). |
|
my(@stat) = stat($fullfile); |
my($size) = $stat[7]; |
|
if (!open(HASH_FILE, '<', $fullfile)) { |
$Error = "Couldn't open file '${fullfile} for reading."; |
return undef; |
my $checksum = Compute_Checksums("${dists_dir}/${archive}", $file) or return undef; |
push @Checksums, $checksum; |
} |
my(@filetext) = <HASH_FILE>; |
close(HASH_FILE); |
|
# Now calculate the checksums and put them into the hashes. |
|
my($md5) = Digest::MD5::md5_hex(@filetext); |
my($sha1) = Digest::SHA1::sha1_hex(@filetext); |
|
push @Checksums, { |
'File' => $file, |
'Size' => $size, |
'MD5' => $md5, |
'SHA1' => $sha1, |
}; |
} |
|
# Open a secure tempfile, and set up some variables. |
|
my($tmpfile_handle, $tmpfile_name) = tempfile(); |
289,18 → 283,14 |
|
# Now print MD5 and SHA1 checksum lists. |
|
print $tmpfile_handle "MD5Sum:\n"; |
foreach $file (@Checksums) { |
printf $tmpfile_handle " %s %8d %s\n", $file->{'MD5'}, |
$file->{'Size'}, $file->{'File'}; |
} |
foreach my $algo ('MD5Sum', 'SHA1') { |
print $tmpfile_handle "$algo:\n"; |
|
print $tmpfile_handle "SHA1:\n"; |
foreach $file (@Checksums) { |
printf $tmpfile_handle " %s %8d %s\n", $file->{'SHA1'}, |
$file->{'Size'}, $file->{'File'}; |
foreach my $checksum (@Checksums) { |
printf $tmpfile_handle " %s %8d %s\n", $checksum->{$algo}, |
$checksum->{'Size'}, $checksum->{'File'}; |
} |
|
} |
close($tmpfile_handle); |
|
return $tmpfile_name; |