summaryrefslogtreecommitdiffstats
path: root/lib/Lintian/Check/Md5sums.pm
blob: c62d9cdc9004983044039fa323f743f5b3fd4e00 (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
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
128
129
130
131
132
133
# md5sums -- lintian check script -*- perl -*-

# Copyright (C) 1998 Christian Schwarz and Richard Braakman
# Copyright (C) 2020 Felix Lechner
# Copyright (C) 2018, 2020 Chris Lamb <lamby@debian.org>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, you can find it on the World Wide
# Web at https://www.gnu.org/copyleft/gpl.html, or write to the Free
# Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
# MA 02110-1301, USA.

package Lintian::Check::Md5sums;

use v5.20;
use warnings;
use utf8;

use List::Compare;
use Path::Tiny;

use Lintian::Util qw(read_md5sums drop_relative_prefix);

use Moo;
use namespace::clean;

with 'Lintian::Check';

has only_conffiles => (is => 'rw', default => 1);

sub visit_installed_files {
    my ($self, $item) = @_;

    # check if package contains non-conffiles
    # debhelper doesn't create entries in md5sums
    # for conffiles since this information would
    # be redundant

    # Skip non-files, they will not appear in the md5sums file
    return
      unless $item->is_regular_file;

    $self->only_conffiles(0)
      unless $self->processable->declared_conffiles->is_known($item->name);

    return;
}

sub binary {
    my ($self) = @_;

    my $control = $self->processable->control->lookup('md5sums');
    unless (defined $control) {

        # ignore if package contains no files
        return
          unless @{$self->processable->installed->sorted_list};

        $self->hint('no-md5sums-control-file')
          unless $self->only_conffiles;

        return;
    }

    # The md5sums file should not be a symlink.  If it is, the best
    # we can do is to leave it alone.
    return
      if $control->is_symlink;

    return
      unless $control->is_open_ok;

    # Is it empty? Then skip it. Tag will be issued by control-files
    return
      if $control->size == 0;

    my $text = $control->bytes;

    my ($md5sums, $errors) = read_md5sums($text);

    $self->pointed_hint('malformed-md5sums-control-file',$control->pointer, $_)
      for @{$errors};

    my %noprefix
      = map { drop_relative_prefix($_) => $md5sums->{$_} } keys %{$md5sums};

    my @listed = keys %noprefix;
    my @found
      = grep { $_->is_file} @{$self->processable->installed->sorted_list};

    my $lc = List::Compare->new(\@listed, \@found);

    # find files that should exist but do not
    $self->pointed_hint('md5sums-lists-nonexistent-file',$control->pointer, $_)
      for $lc->get_Lonly;

    # find files that should be listed but are not
    for my $name ($lc->get_Ronly) {

        $self->pointed_hint('file-missing-in-md5sums', $control->pointer,$name)
          unless $self->processable->declared_conffiles->is_known($name)
          || $name =~ m{^var/lib/[ai]spell/.};
    }

    # checksum should match for common files
    for my $name ($lc->get_intersection) {

        my $item = $self->processable->installed->lookup($name);

        $self->pointed_hint('md5sum-mismatch', $control->pointer, $name)
          unless $item->md5sum eq $noprefix{$name};
    }

    return;
}

1;

# Local Variables:
# indent-tabs-mode: nil
# cperl-indent-level: 4
# End:
# vim: syntax=perl sw=4 sts=4 sr et