summaryrefslogtreecommitdiffstats
path: root/lib/Lintian/Check/Conffiles.pm
blob: 076c17f45f52976c5e65127db28b7c2f177400d1 (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
134
135
136
# conffiles -- lintian check script -*- perl -*-

# Copyright (C) 1998 Christian Schwarz
# Copyright (C) 2000 Sean 'Shaleh' Perry
# Copyright (C) 2017 Chris Lamb <lamby@debian.org>
# Copyright (C) 2021 Felix Lechner
#
# 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::Conffiles;

use v5.20;
use warnings;
use utf8;

use Const::Fast;
use List::Compare;
use List::SomeUtils qw(any none);
use Path::Tiny;

const my $SPACE => q{ };

const my @KNOWN_INSTRUCTIONS => qw(remove-on-upgrade);

use Moo;
use namespace::clean;

with 'Lintian::Check';

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

    return
      if $self->processable->type =~ 'udeb';

    my $declared_conffiles = $self->processable->declared_conffiles;

    unless ($item->is_file) {
        $self->pointed_hint('conffile-has-bad-file-type', $item->pointer)
          if $declared_conffiles->is_known($item->name);
        return;
    }

    # files /etc must be conffiles, with some exceptions).
    $self->pointed_hint('file-in-etc-not-marked-as-conffile',$item->pointer)
      if $item->name =~ m{^etc/}
      && !$declared_conffiles->is_known($item->name)
      && $item->name !~ m{/README$}
      && $item->name !~ m{^ etc/init[.]d/ (?: skeleton | rc S? ) $}x;

    return;
}

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

    my $declared_conffiles = $self->processable->declared_conffiles;
    for my $relative ($declared_conffiles->all) {

        my $item = $self->processable->conffiles_item;

        my @entries = @{$declared_conffiles->by_file->{$relative}};

        my @positions = map { $_->position } @entries;
        my $lines = join($SPACE, (sort { $a <=> $b } @positions));

        $self->pointed_hint('duplicate-conffile', $item->pointer,
            $relative, "(lines $lines)")
          if @entries > 1;

        for my $entry (@entries) {

            my $conffiles_item = $self->processable->conffiles_item;
            my $pointer = $conffiles_item->pointer($entry->position);

            $self->pointed_hint('relative-conffile', $pointer,$relative)
              if $entry->is_relative;

            $self->pointed_hint('file-in-etc-rc.d-marked-as-conffile',
                $pointer, $relative)
              if $relative =~ m{^etc/rc.\.d/};

            $self->pointed_hint('file-in-usr-marked-as-conffile',
                $pointer, $relative)
              if $relative =~ m{^usr/};

            $self->pointed_hint('non-etc-file-marked-as-conffile',
                $pointer, $relative)
              unless $relative =~ m{^etc/};

            my @instructions = @{$entry->instructions};

            my $instruction_lc
              = List::Compare->new(\@instructions, \@KNOWN_INSTRUCTIONS);
            my @unknown = $instruction_lc->get_Lonly;

            $self->pointed_hint('unknown-conffile-instruction', $pointer, $_)
              for @unknown;

            my $should_exist= none { $_ eq 'remove-on-upgrade' } @instructions;
            my $may_not_exist= any { $_ eq 'remove-on-upgrade' } @instructions;

            my $shipped = $self->processable->installed->lookup($relative);

            $self->pointed_hint('missing-conffile', $pointer, $relative)
              if $should_exist && !defined $shipped;

            $self->pointed_hint('unexpected-conffile', $pointer, $relative)
              if $may_not_exist && defined $shipped;
        }
    }

    return;
}

1;

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