aboutsummaryrefslogtreecommitdiffstats
path: root/lib/Youri/Check/Input/Conflicts.pm
blob: 9ffc9865da6911efcef06151315538be51effc7b (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
# $Id: Conflicts.pm 885 2006-04-17 22:25:00Z guillomovitch $
package Youri::Check::Input::Conflicts;

=head1 NAME

Youri::Check::Input::Conflicts - Check file conflicts

=head1 DESCRIPTION

This plugin checks packages files, and report conflict and duplications.

=cut

use warnings;
use strict;
use Carp;
use constant;
use Youri::Package;
use base 'Youri::Check::Input';

use constant TYPE_MASK => 0170000; 
use constant TYPE_DIR  => 0040000;

use constant PACKAGE => 0;
use constant MODE    => 1;
use constant MD5SUM  => 2;

my $compatibility = {
    x86_64  => 'i586',
    i586    => 'x86_64',
    sparc64 => 'sparc',
    sparc   => 'sparc64',
    ppc64   => 'ppc',
    ppc     => 'ppc64'
};

sub columns {
    return qw/
        arch
        file
        error
        level
    /;
}

sub links {
    return qw//;
}

=head2 new(%args)

Creates and returns a new Youri::Check::Input::Conflicts object.

No specific parameters.

=cut

sub prepare {
    my ($self, @medias) = @_;
    croak "Not a class method" unless ref $self;

    my $index = sub {
        my ($package) = @_;

        # index files
        foreach my $file ($package->get_files()) {
            push(
                @{$self->{_files}->{$file->[Youri::Package::FILE_NAME]}},
                [ $package, $file->[Youri::Package::FILE_MODE], $file->[Youri::Package::FILE_MD5SUM] ]
            );
        }
    };

    foreach my $media (@medias) {
        # don't index source media files
        next unless $media->get_type() eq 'binary';

        my $media_id = $media->get_id();
        $self->{_medias}->{$media_id} = 1;
        print STDERR "Indexing media $media_id files\n"
            if $self->{_verbose};

        $media->traverse_headers($index);
    }
}

sub run {
    my ($self, $media, $result) = @_;
    croak "Not a class method" unless ref $self;

    # this is a binary media check only
    return unless $media->get_type() eq 'binary';

    my $check = sub {
        my ($package) = @_;

        return if $package->get_arch() eq 'src';

        my $arch = $package->get_arch();
        my $name = $package->get_name();

        foreach my $file ($package->get_files()) {

            my $found =
                $self->{_files}->{$file->[Youri::Package::FILE_NAME]};

            my @found = $found ? @$found : ();

            foreach my $found (@found) {
                next if $found->[PACKAGE] == $package;
                next unless compatible($found->[PACKAGE], $package);
                next if conflict($found->[PACKAGE], $package);
                next if replace($found->[PACKAGE], $package);
                if (
                    ($file->[Youri::Package::FILE_MODE] & TYPE_MASK) == TYPE_DIR &&
                    ($found->[MODE] & TYPE_MASK) == TYPE_DIR
                ) {
                    $result->add_result($self->{_id}, $media, $package, {
                        arch  => $arch,
                        file  => $name,
                        error => "directory $file->[Youri::Package::FILE_NAME] duplicated with package " . $found->[PACKAGE]->get_name(),
                        level => Youri::Check::Input::WARNING
                    }) unless $self->_directory_duplicate_exception(
                        $package,
                        $found->[PACKAGE],
                        $file
                    );
                } else {
                    if ($found->[MD5SUM] eq $file->[Youri::Package::FILE_MD5SUM]) {
                        $result->add_result($self->{_id}, $media, $package, {
                            arch  => $arch,
                            file  => $name,
                            error => "file $file->[Youri::Package::FILE_NAME] duplicated with package " . $found->[PACKAGE]->get_name(),
                            level => Youri::Check::Input::WARNING
                        }) unless $self->_file_duplicate_exception(
                            $package,
                            $found->[PACKAGE],
                            $file
                        );
                    } else {
                        $result->add_result($self->{_id}, $media, $package, {
                            arch  => $arch,
                            file  => $name,
                            error => "non-explicit conflict on file $file->[Youri::Package::FILE_NAME] with package " . $found->[PACKAGE]->get_name(),
                            level => Youri::Check::Input::ERROR
                        }) unless $self->_file_conflict_exception(
                            $package,
                            $found->[PACKAGE],
                            $file
                        );
                    }
                }
            }
        }
    };

    $media->traverse_headers($check);
}

# return true if $package1 is arch-compatible with $package2
sub compatible {
    my ($package1, $package2) = @_;

    my $arch1 = $package1->get_arch();
    my $arch2 = $package2->get_arch();

    return 1 if $arch1 eq $arch2;

    return 1 if $compatibility->{$arch1} && $compatibility->{$arch1} eq $arch2;

    return 0;
}

# return true if $package1 conflict with $package2
# or the other way around
sub conflict {
    my ($package1, $package2) = @_;

    my $name2 = $package2->get_name();

    foreach my $conflict ($package1->get_conflicts()) {
        return 1 if $conflict eq $name2;
    }

    my $name1 = $package1->get_name();

    foreach my $conflict ($package2->get_conflicts()) {
        return 1 if $conflict eq $name1;
    }

    return 0;
}

# return true if $package1 replace $package2
sub replace {
    my ($package1, $package2) = @_;


    my $name1 = $package1->get_name();
    my $name2 = $package2->get_name();

    return 1 if $name1 eq $name2;

    foreach my $obsolete ($package1->get_obsoletes()) {
        return 1 if $obsolete->[Youri::Package::DEPENDENCY_NAME] eq $name2;
    }

    return 0;
}

sub _directory_duplicate_exception {
    return 0;
}

sub _file_duplicate_exception {
    return 0;
}

sub _file_conflict_exception {
    return 0;
}

=head1 COPYRIGHT AND LICENSE

Copyright (C) 2002-2006, YOURI project

This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.

=cut

1;