File: jetring-checksum

package info (click to toggle)
jetring 0.20
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd, wheezy
  • size: 464 kB
  • ctags: 26
  • sloc: perl: 462; sh: 171; makefile: 25
file content (70 lines) | stat: -rwxr-xr-x 1,369 bytes parent folder | download | duplicates (5)
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
#!/usr/bin/perl
use warnings;
use strict;
use Getopt::Long;

sub usage {
	die "usage: jetring-checksum [-ug] dir\n";
}

my $upgrade=0;
GetOptions(
	"-u", \$upgrade,
) || usage;

usage() unless @ARGV;
my $dir=shift;

chdir($dir) || die "chdir $dir: $!";

open (INDEX, "<", "index") || die "$dir/index: $!";
if ($upgrade) {
	open (OUT, ">", "index.new") || die "$dir/index.new: $!";
}
while (<INDEX>) {
	chomp;
	if (/^([a-zA-Z0-9]+)+\s+(.*)$/) {
		runcheck($_, "sha1sum", "-c", "-w");
		if ($upgrade) {
			print OUT genchecksum($2);
		}
	}
	elsif (/^(\w+)-(.*\s+.*)/) {
		my $type=$1;
		my $rest=$2;
		if ($type=~/^sha(1|256|224|384|512)$/) {
			runcheck($rest, $type."sum", "-c", "-w")
		}
		else {
			die "unsupported checksum type \ยจ$type\" in $dir/index\n";
		}
		if ($upgrade) {
			print OUT $_."\n";
		}
	}
	else {
		die "invalid line in $dir/index: $_\n";
	}
}
close INDEX;
if ($upgrade) {
	close OUT;
	rename("index.new", "index") || die "rename: $!";
}

sub runcheck {
	my $line=shift;
	my $command=shift;
	close STDOUT;
	open(STDOUT, ">/dev/null");
	open (PROG, "|-", $command, @_) || die "error running $command: $!";
	print PROG $line."\n" || die "$command did not read line";
	close PROG || die "$dir/index contains bad checksum!\n";
}

sub genchecksum {
	my $file=shift;
	my $checksum=`sha256sum '$file'`;
	chomp $checksum;
	return "sha256-$checksum\n";
}