summaryrefslogtreecommitdiffstats
path: root/lib/Lintian/Check/GroupChecks.pm
blob: 79150a166a5357d9f862049bb47efcd6f1850a7c (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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
# group-checks -- lintian check script -*- perl -*-

# Copyright (C) 2011 Niels Thykier <niels@thykier.net>
# Copyright (C) 2018 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::GroupChecks;

use v5.20;
use warnings;
use utf8;

use List::SomeUtils qw(any);

use Lintian::Relation;

use Moo;
use namespace::clean;

with 'Lintian::Check';

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

    my $group = $self->group;

    ## To find circular dependencies, we will first generate Strongly
    ## Connected Components using Tarjan's algorithm
    ##
    ## We are not using DepMap, because it cannot tell how the circles
    ## are made - only that there exists at least 1 circle.

    # The packages a.k.a. nodes
    my (@nodes, %edges, $sccs);
    my @installables = grep { $_->type ne 'udeb' } $group->get_installables;

    $self->check_file_overlap(@installables);

    for my $installable (@installables) {

        my $deps = $group->direct_dependencies($installable);
        if (scalar @{$deps} > 0) {
            # it depends on another package - it can cause
            # a circular dependency
            my $pname = $installable->name;
            push @nodes, $pname;
            $edges{$pname} = [map { $_->name } @{$deps}];
            $self->check_multiarch($installable, $deps);
        }
    }

    # Bail now if we do not have at least two packages depending
    # on some other package from this source.
    return if scalar @nodes < 2;

    $sccs= Lintian::Check::GroupChecks::Graph->new(\@nodes, \%edges)->tarjans;

    for my $comp (@{$sccs}) {
        # It takes two to tango... erh. make a circular dependency.
        next if scalar @{$comp} < 2;

        $self->hint('intra-source-package-circular-dependency',
            (sort @{$comp}));
    }

    return;
}

sub check_file_overlap {
    my ($self, @processables) = @_;

    # make a local copy to be modified
    my @remaining = @processables;

    # avoids checking the same combo twice
    while (@remaining > 1) {

        # avoids checking the same combo twice
        my $one = shift @remaining;

        my @provides_one = $one->fields->trimmed_list('Provides', qr{,});
        my $relation_one = Lintian::Relation->new->load(
            join(' | ', $one->name, @provides_one));

        for my $two (@remaining) {

            # poor man's work-around for "Multi-arch: same"
            next
              if $one->name eq $two->name;

            my @provides_two = $two->fields->trimmed_list('Provides', qr{,});
            my $relation_two = Lintian::Relation->new->load(
                join(' | ', $two->name, @provides_two));

            # $two conflicts/replaces with $one
            next
              if $two->relation('Conflicts')->satisfies($relation_one);
            next
              if $two->relation('Replaces')->satisfies($one->name);

            # $one conflicts/replaces with $two
            next
              if $one->relation('Conflicts')->satisfies($relation_two);
            next
              if $one->relation('Replaces')->satisfies($two->name);

            for my $one_file (@{$one->installed->sorted_list}) {

                my $name = $one_file->name;

                $name =~ s{/$}{};
                my $two_file = $two->installed->lookup($name)
                  // $two->installed->lookup("$name/");
                next
                  unless defined $two_file;

                next
                  if $one_file->is_dir && $two_file->is_dir;

                $self->hint('binaries-have-file-conflict',
                    sort($one->name, $two->name), $name);
            }
        }
    }

    return;
}

sub check_multiarch {
    my ($self, $processable, $deps) = @_;

    my $KNOWN_DBG_PACKAGE= $self->data->load('common/dbg-pkg',qr/\s*\~\~\s*/);

    my $ma = $processable->fields->value('Multi-Arch') || 'no';
    if ($ma eq 'same') {
        for my $dep (@{$deps}) {
            my $dma = $dep->fields->value('Multi-Arch') || 'no';
            if ($dma eq 'same' or $dma eq 'foreign') {
                1; # OK
            } else {
                $self->hint(
                    'dependency-is-not-multi-archified',
                    join(q{ },
                        $processable->name, 'depends on',
                        $dep->name, "(multi-arch: $dma)")
                );
            }
        }
    } elsif ($ma ne 'same'
        and ($processable->fields->value('Section') || 'none')
        =~ m{(?:^|/)debug$}) {
        # Debug package that isn't M-A: same, exploit that (non-debug)
        # dependencies is (almost certainly) a package for which the
        # debug carries debug symbols.
        for my $dep (@{$deps}) {
            my $dma = $dep->fields->value('Multi-Arch') || 'no';
            if ($dma eq 'same'
                && ($dep->fields->value('Section') || 'none')
                !~ m{(?:^|/)debug$}){

                # Debug package isn't M-A: same, but depends on a
                # package that is from same source that isn't a debug
                # package and that is M-A same.  Thus it is not
                # possible to install debug symbols for all
                # (architecture) variants of the binaries.
                $self->hint(
                    'debug-package-for-multi-arch-same-pkg-not-coinstallable',
                    $processable->name . ' => ' . $dep->name
                  )
                  unless any { $processable->name =~ m/$_/xms }
                  $KNOWN_DBG_PACKAGE->all;
            }
        }
    }
    return;
}

## Encapsulate Tarjan's algorithm in a class/object to keep
## the run sub somewhat sane.  Allow this "extra" package as
## it is not a proper subclass.
#<<< no Perl tidy (it breaks the no critic comment)
package Lintian::Check::GroupChecks::Graph;  ## no critic (Modules::ProhibitMultiplePackages)
#>>>

use Const::Fast;

const my $EMPTY => q{};

sub new {
    my ($type, $nodes, $edges) = @_;
    my $self = { nodes => $nodes, edges => $edges};
    bless $self, $type;
    return $self;
}

sub tarjans {
    my ($self) = @_;
    my $nodes = $self->{nodes};
    $self->{index} = 0;
    $self->{scc} = [];
    $self->{stack} = [];
    $self->{on_stack} = {};
    # The information for each node:
    #  $self->{node_info}{$node}[X], where X is:
    #    0 => index
    #    1 => low_index
    $self->{node_info} = {};
    for my $node (@{$nodes}) {
        $self->_tarjans_sc($node)
          unless defined $self->{node_info}{$node};
    }
    return $self->{scc};
}

sub _tarjans_sc {
    my ($self, $node) = @_;
    my $index = $self->{index};
    my $stack = $self->{stack};
    my $ninfo = [$index, $index];
    my $on_stack = $self->{on_stack};
    $self->{node_info}{$node} = $ninfo;
    $index++;
    $self->{index} = $index;
    push(@{$stack}, $node);
    $on_stack->{$node} = 1;

    foreach my $neighbour (@{ $self->{edges}{$node} }){
        my $nb_info;
        $nb_info = $self->{node_info}{$neighbour};
        if (!defined $nb_info){
            # First time visit
            $self->_tarjans_sc($neighbour);
            # refresh $nb_info
            $nb_info = $self->{node_info}{$neighbour};
            # min($node.low_index, $neigh.low_index)
            $ninfo->[1] = $nb_info->[1] if $nb_info->[1] < $ninfo->[1];
        } elsif (exists $on_stack->{$neighbour})  {
            # Node is in this component
            # min($node.low_index, $neigh.index)
            $ninfo->[1] = $nb_info->[0] if $nb_info->[0] < $ninfo->[1];
        }
    }
    if ($ninfo->[0] == $ninfo->[1]){
        # the "root" node - create the SSC.
        my $component = [];
        my $scc = $self->{scc};
        my $elem = $EMPTY;

        do {
            $elem = pop @{$stack};
            delete $on_stack->{$elem};
            push(@{$component}, $elem);

        } until $node eq $elem;

        push(@{$scc}, $component);
    }
    return;
}

1;

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