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

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

sub parse {
    my ($md5sum_file) = @_;

    my %h = map {
	my ($md5sum, $file) = m|(\S+)\s+(?:\./)?(\S+)|;
	$file => $md5sum;
    } cat_($md5sum_file);

    \%h;
}

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

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;