~xnox/debian-cd/cleanup

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
#!/usr/bin/perl -w 

exit 0;

use Digest::MD5;

my $mirror = shift || $ENV{'MIRROR'};
my $nonus = shift || $ENV{'NONUS'};
my $security = shift || $ENV{'SECURITY'};
my ($p, $filename, $size, $md5sum, $file) = ('', '', 0, '', '');

my $ok = 1;
my ($ctx, $digest);

$/=''; # Browse paragraph by paragraph

while (defined($_ = <>)) {
	# Get the infos about a package
	m/^Package: (\S+)/mi  and $p = $1;
	m/^Size: (\d+)/mi     and $size = $1;
	m/^Filename: (\S+)/mi and $filename = $1;
	m/^MD5sum: (\S+)/mi   and $md5sum = $1;

	next if not $p;
	
	$file = ( ( -e "$mirror/$filename" ) ? "$mirror/$filename" :
		  ( exists $ENV{LOCALDEBS} && -e "$ENV{LOCALDEBS}/$filename" ) ? "$ENV{LOCALDEBS}/$filename" :
		  ( -e "$nonus/$filename" ) ? "$nonus/$filename" :
		  "$security/$filename" );
	
	# Check if the file exists
	if (! -e $file) {
		print STDERR "File $filename can't be found ...\n";
		$ok = 0;
		next;
	}
	
	# Check for the size
	if (-s _ != $size) {
		print STDERR "The size doesn't match for $file.\n";
		print STDERR "It's `@{ [ -s _ ]}' instead of `$size'.\n";
		$ok = 0;
	}

	# Compute the MD5sum
	open(FILE, "< $file") || die "Can't open $file : $!\n";
	$ctx = Digest::MD5->new;
	$ctx->addfile(*FILE);
	$digest = $ctx->hexdigest;
	undef $ctx;
	close FILE;
	
	# Check the MD5sum
	if ($digest ne $md5sum) {
		print STDERR "MD5sum does not match for $file ...\n";
		print STDERR "It's `$digest' instead of `$md5sum'.\n";
		$ok = 0;
	}
}

if ($ok) {
	exit 0;
} else {
	print STDERR <<EOF;
	
The mirror has some problems. Please correct them before trying to build
Debian CDs. To do so you could sync your mirror from one of the primary
mirror sites listed on the Debian web pages.
EOF
	exit 1;
}