summaryrefslogtreecommitdiffstats
path: root/urpm/md5sum.pm
blob: 7d39f8cb4b670eb8c65134beff55524d1bc56234 (plain)
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
package urpm::md5sum; # $Id: $

use urpm;
use urpm::util;
use urpm::msg;


#- parse an MD5SUM file from a mirror
sub from_MD5SUM {
    my ($md5sum_file, $f) = @_;  
    my $basename = basename($f);

    my ($retrieved_md5sum) = map {
	my ($md5sum, $file) = m|(\S+)\s+(?:\./)?(\S+)|;
	$file && $file eq $basename ? $md5sum : @{[]};
    } cat_($md5sum_file);

    $retrieved_md5sum;
}

sub from_MD5SUM__or_warn {
    my ($urpm, $md5sum_file, $basename) = @_;
    $urpm->{log}(N("examining %s file", $md5sum_file));
    my $retrieved_md5sum = from_MD5SUM($md5sum_file, $basename) 
      or $urpm->{log}(N("warning: md5sum for %s unavailable in MD5SUM file", $basename));
    return $retrieved_md5sum;
}

sub on_local_medium {
    my ($urpm, $medium, $force) = @_;
    if ($force) {
	#- force downloading the file again, else why a force option has been defined ?
	delete $medium->{md5sum};
    } else {
	$medium->{md5sum} ||= compute_on_local_medium($urpm, $medium);
    }
    $medium->{md5sum};
}

sub compute_on_local_medium {
    my ($urpm, $medium) = @_;

    require urpm; #- help perl_checker
    my $f = urpm::statedir_hdlist_or_synthesis($urpm, $medium);
    $urpm->{log}(N("computing md5sum of existing source hdlist (or synthesis) [%s]", $f));
    -e $f && compute($f);
}

sub compute {
    my ($file) = @_;
    eval { require Digest::MD5 };
    if ($@) {
	#- Use an external command to avoid depending on perl
	return (split ' ', `/usr/bin/md5sum '$file'`)[0];
    } else {
	my $ctx = Digest::MD5->new;
	open my $fh, $file or return '';
	$ctx->addfile($fh);
	close $fh;
	return $ctx->hexdigest;
    }
}

1;