-
Notifications
You must be signed in to change notification settings - Fork 0
/
bagit-validate
executable file
·127 lines (110 loc) · 3.03 KB
/
bagit-validate
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#!/usr/bin/env perl
use strict;
use DateTime;
use Digest::MD5 qw(md5_hex);
use Digest::SHA qw(sha256_hex);
use File::Basename;
use File::Copy;
use File::Find;
use File::Path qw(remove_tree);
my (
$src,
) = @ARGV;
if (! -d $src) {
exit;
}
if (! -f "$src/bagit.txt") {
print "not ok $src (missing bagit.txt)\n";
}
# Identify payload files
my @files_found = ();
find({
wanted => \&payload_files,
no_chdir => 1,
}, "$src/data");
# Check fixity, recording files encountered
my %seen;
my @algs = ('md5', 'sha256');
my $alg;
my $mft;
my $got_sum;
foreach $alg (@algs) {
$mft = "$src/manifest-$alg.txt";
if (-f $mft) {
%seen = ();
open my $fh, '<', $mft
or die "$0: can't read $mft: $!\n";
while (<$fh>) {
chomp;
my ($expected_sum, $file) = split(/\s+/, $_);
$seen{$file} = 1;
$got_sum = do {
open my $mfh, '<:raw', "$src/$file"
or die "Can't read $src/$file: $!\n";
my $sum;
if ($alg eq 'md5') {
$sum = Digest::MD5->new;
} elsif ($alg eq 'sha256') {
$sum = Digest::SHA->new('sha256');
}
local $/ = \65536;
while (<$mfh>) {
$sum->add($_);
}
$sum->hexdigest;
};
if ($expected_sum ne $got_sum) {
print "not ok $src (file $file has $alg checksum $got_sum, expected $expected_sum instead)\n";
exit;
}
}
foreach my $file (@files_found) {
if (!$seen{$file}) {
print "not ok $src (file $file is not manifested in $mft\n";
exit;
}
}
}
}
# tag manifests
foreach $alg (@algs) {
$mft = "$src/tagmanifest-$alg.txt";
if (-f $mft) {
open my $fh, '<', $mft
or die "$0: can't read $mft: $!\n";
while (<$fh>) {
chomp;
my ($expected_sum, $file) = split(/\s+/, $_);
$got_sum = do {
open my $mfh, '<:raw', "$src/$file"
or die "Can't read $src/$file: $!\n";
my $sum;
if ($alg eq 'md5') {
$sum = Digest::MD5->new;
} elsif ($alg eq 'sha256') {
$sum = Digest::SHA->new('sha256');
}
local $/ = \65536;
while (<$mfh>) {
$sum->add($_);
}
$sum->hexdigest;
};
if ($expected_sum ne $got_sum) {
print "not ok $src (file $file has $alg checksum $got_sum, expected $expected_sum instead)\n";
exit;
}
}
}
}
print "ok $src\n";
##### END #####
sub payload_files {
my $file = $_;
my $basename = basename($file);
return if $basename =~ /^\./;
return unless -f $file;
my $path;
($path = $file) =~ s#$src/##;
push @files_found, $path;
}