| 1 |
#!/usr/bin/perl |
| 2 |
use warnings; |
| 3 |
use strict; |
| 4 |
use Getopt::Long; |
| 5 |
|
| 6 |
sub usage { |
| 7 |
die "usage: jetring-checksum [-ug] dir\n"; |
| 8 |
} |
| 9 |
|
| 10 |
my $upgrade=0; |
| 11 |
GetOptions( |
| 12 |
"-u", \$upgrade, |
| 13 |
) || usage; |
| 14 |
|
| 15 |
usage() unless @ARGV; |
| 16 |
my $dir=shift; |
| 17 |
|
| 18 |
chdir($dir) || die "chdir $dir: $!"; |
| 19 |
|
| 20 |
open (INDEX, "<", "index") || die "$dir/index: $!"; |
| 21 |
if ($upgrade) { |
| 22 |
open (OUT, ">", "index.new") || die "$dir/index.new: $!"; |
| 23 |
} |
| 24 |
while (<INDEX>) { |
| 25 |
chomp; |
| 26 |
if (/^([a-zA-Z0-9]+)+\s+(.*)$/) { |
| 27 |
runcheck($_, "sha1sum", "-c", "-w"); |
| 28 |
if ($upgrade) { |
| 29 |
print OUT genchecksum($2); |
| 30 |
} |
| 31 |
} |
| 32 |
elsif (/^(\w+)-(.*\s+.*)/) { |
| 33 |
my $type=$1; |
| 34 |
my $rest=$2; |
| 35 |
if ($type=~/^sha(1|256|224|384|512)$/) { |
| 36 |
runcheck($rest, $type."sum", "-c", "-w") |
| 37 |
} |
| 38 |
else { |
| 39 |
die "unsupported checksum type \¨$type\" in $dir/index\n"; |
| 40 |
} |
| 41 |
if ($upgrade) { |
| 42 |
print OUT $_."\n"; |
| 43 |
} |
| 44 |
} |
| 45 |
else { |
| 46 |
die "invalid line in $dir/index: $_\n"; |
| 47 |
} |
| 48 |
} |
| 49 |
close INDEX; |
| 50 |
if ($upgrade) { |
| 51 |
close OUT; |
| 52 |
rename("index.new", "index") || die "rename: $!"; |
| 53 |
} |
| 54 |
|
| 55 |
sub runcheck { |
| 56 |
my $line=shift; |
| 57 |
my $command=shift; |
| 58 |
close STDOUT; |
| 59 |
open(STDOUT, ">/dev/null"); |
| 60 |
open (PROG, "|-", $command, @_) || die "error running $command: $!"; |
| 61 |
print PROG $line."\n" || die "$command did not read line"; |
| 62 |
close PROG || die "$dir/index contains bad checksum!\n"; |
| 63 |
} |
| 64 |
|
| 65 |
sub genchecksum { |
| 66 |
my $file=shift; |
| 67 |
my $checksum=`sha256sum '$file'`; |
| 68 |
chomp $checksum; |
| 69 |
return "sha256-$checksum\n"; |
| 70 |
} |