This file is indexed.

/usr/share/perl5/Lintian/Lab/Manifest.pm is in lintian 2.5.81ubuntu1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
# Lintian::Lab::Manifest -- Lintian Lab manifest

# Copyright (C) 2011 Niels Thykier
#
# 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 http://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::Lab::Manifest;

use strict;
use warnings;
use autodie;

use parent qw(Class::Accessor::Fast Clone);

use Carp qw(croak);

=head1 NAME

Lintian::Lab::Manifest -- Lintian Lab manifest

=head1 SYNOPSIS

 use Lintian::Lab::Manifest;
 
 my $plist = Lintian::Lab::Manifest->new ('binary');
 # Read the file
 $plist->read_list('info/binary-packages');
 # fetch the entry for lintian (if any)
 my $entry = $plist->get('lintian', '2.5.2', 'all');
 if ( $entry && exits $entry->{'version'} ) {
    print "Lintian has version $entry->{'version'}\n";
 }
 # delete all lintian entries
 $plist->delete('lintian');
 # Write to file if changed
 if ($plist->dirty) {
    $plist->write_list('info/binary-packages');
 }

=head1 DESCRIPTION

Instances of this class provide access to the packages list used by
the lab as caches.

The data structure is basically a tree (using hashes).  For binaries
is looks (something) like:

 $self->{'state'}{$name}{$version}{$architecture}

The (order of the) fields used in the tree are listed in the
@{BIN,SRC,CHG}_QUERY lists below.  The fields may (and generally do)
differ between package types.

=head1 CLASS METHODS

=over 4

=cut

# these banner lines have to be changed with every incompatible change of the
# binary and source list file formats
use constant BINLIST_FORMAT =>
  q{Lintian's list of binary packages in the archive--V5};
use constant SRCLIST_FORMAT =>
  q{Lintian's list of source packages in the archive--V5};
use constant BLDLIST_FORMAT =>
  q{Lintian's list of buildinfo packages in the archive--V1};
use constant CHGLIST_FORMAT =>
  q{Lintian's list of changes packages in the archive--V1};

# List of fields in the formats and the order they appear in
#  - for internal usage to read and write the files

# source package lists
my @SRC_FILE_FIELDS = (
    'source','version','maintainer','uploaders',
    'area','binary','file','timestamp',
);

# binary/udeb package lists
my @BIN_FILE_FIELDS = (
    'package','version','source','source-version',
    'architecture','file','timestamp','area',
);

# buildinfo packages lists
my @BLD_FILE_FIELDS = ('source','version','architecture','file','timestamp',);

# changes packages lists
my @CHG_FILE_FIELDS = ('source','version','architecture','file','timestamp',);

# List of fields (in order) of fields used to look up the package in the
# manifest.  The field names matches those used in the list above.

my @SRC_QUERY = ('source','version',);

my @GROUP_QUERY = ('source','version','identifier',);

my @BIN_QUERY = ('package','version','architecture',);

my @BLD_QUERY = ('source','version','architecture',);

my @CHG_QUERY = ('source','version','architecture',);

my %TYPE2INFO = (
    'source' => {
        'file-fields'  => \@SRC_FILE_FIELDS,
        'file-header'  => SRCLIST_FORMAT,
        'query-fields' => \@SRC_QUERY
    },
    'binary' => {
        'file-fields'  => \@BIN_FILE_FIELDS,
        'file-header'  => BINLIST_FORMAT,
        'query-fields' => \@BIN_QUERY
    },
    'buildinfo' => {
        'file-fields'  => \@BLD_FILE_FIELDS,
        'file-header'  => BLDLIST_FORMAT,
        'query-fields' => \@BLD_QUERY
    },
    'changes' => {
        'file-fields'  => \@CHG_FILE_FIELDS,
        'file-header'  => CHGLIST_FORMAT,
        'query-fields' => \@CHG_QUERY
    },
    'GROUP' => {
        'file-fields'  => undef, # Never written to disk
        'file-header'  => undef, # Never written to disk
        'query-fields' => \@GROUP_QUERY
    },
);

# udeb behave exactly like binary, so share the underlying table
#  \o/  ~50 bytes saved!
$TYPE2INFO{'udeb'} = $TYPE2INFO{'binary'};

=item new (TYPE[, GROUPING])

Creates a new packages list for a certain type of packages.  This type
defines the format of the files.

The known types are:
 * binary
 * changes
 * source
 * udeb
 * GROUP

If TYPE is GROUP, then GROUPING should be omitted.

=cut

sub new {
    my ($class, $pkg_type, $grouping) = @_;
    my $self = {
        'type'  => $pkg_type,
        'dirty' => 0,
        'state' => {},
        'grouping' => $grouping,
    };
    bless $self, $class;
    return $self;
}

=back

=head1 INSTANCE METHODS

=over 4

=item dirty

Returns a truth value if the manifest has changed since it was last
written.

=item type

Returns the type of packages that this manifest has information about.
(one of binary, udeb, source or changes)

=cut

Lintian::Lab::Manifest->mk_ro_accessors(qw(dirty type));

=item read_list (FILE)

Reads a manifest from FILE.  Any records already in the manifest will
be discarded before reading the contents.

On success, this will clear the L</dirty> flag and on error it will
croak.

=cut

sub read_list {
    my ($self, $file) = @_;
    my $type = $self->type;
    croak 'Cannot read a GROUP manifest' if $type eq 'GROUP';

    # Accept a scalar (as an "in-memory file") - write_list does the same
    if (my $r = ref $file) {
        croak 'Attempt to pass non-scalar ref to read_list'
          unless $r eq 'SCALAR';
    } else {
        # FIXME: clear the manifest if -s $file
        return unless -s $file;
    }

    my ($header, $fields, $qf)
      = @{$TYPE2INFO{$type}}{'file-header', 'file-fields', 'query-fields'};

    $self->{'state'} = $self->_do_read_file($file, $header, $fields, $qf);
    $self->_mark_dirty(0);
    return 1;
}

=item write_list (FILE)

Writes the manifest to FILE.

On success, this will clear the L</dirty> flag and on error it will
croak.

On error, the contents of FILE are undefined.

=cut

sub write_list {
    my ($self, $file) = @_;
    my $type = $self->type;
    croak 'Cannot write a GROUP manifest' if $type eq 'GROUP';
    my ($header, $fields) = @{$TYPE2INFO{$type}}{'file-header', 'file-fields'};
    my $visitor;

    open(my $fd, '>', $file);
    print $fd "$header\n";

    $visitor = sub {
        my ($entry) = @_;
        return if exists $entry->{'_transient'};
        my %values = %$entry;
        print $fd join(';', @values{@$fields}) . "\n";
    };

    $self->visit_all($visitor);

    close($fd);
    $self->_mark_dirty(0);
    return 1;
}

=item visit_all (VISITOR[, KEY1, ..., KEYN])

Visits entries and passes them to VISITOR.  If any keys are passed they
are used to reduce the search.  See get for a list of (common) keys.

The VISITOR is called as:

 VISITOR->(ENTRY, KEYS)

where ENTRY is the entry and KEYS are the keys to be used to look up
this entry via get method.  So for the lintian 2.5.2 binary the keys
would be something like:
 ('lintian', '2.5.2', 'all')

=cut

sub visit_all {
    my ($self, $visitor, @keys) = @_;
    my $root;
    my $type = $self->type;
    my $qf = $TYPE2INFO{$type}{'query-fields'};

    if (@keys) {
        $root = $self->get(@keys);
        return unless $root;
        if (scalar @$qf == scalar @keys) {
            # If we are given an exact match, just visit that and
            # stop.
            $visitor->($root, @keys);
            return;
        }
    } else {
        $root = $self->{'state'};
    }

    $self->_recurse_visit($root, $visitor, scalar @$qf - 1, @keys);
    return;
}

=item get (KEYS...)

Fetches the entry for KEYS (if any).  Returns C<undef> if the entry is
not known.  If KEYS is exactly one item, it is assumed to be a
L<Lintian::Processable> and the correct keys are extracted from it.

Otherwise, the keys are (in general and in order):

=over 4

=item package/source

=item version

=item architecture

except for source packages

=back

=cut

sub get {
    my ($self, @keys) = @_;
    my $cur = $self->{'state'};
    my $type = $self->type;
    my $qf = $TYPE2INFO{$type}{'query-fields'};
    my $max = scalar @$qf;
    @keys = $self->_make_keys($keys[0])
      if scalar(@keys) == 1 && ref($keys[0]);
    $max = scalar @keys if scalar @keys < $max;
    for (my $i = 0 ; $i < $max ; $i++) {
        my $key = $keys[$i];
        $cur = $cur->{$key};
        return unless defined $cur;
    }
    return $cur;
}

sub _make_keys {
    my ($self, $entry) = @_;
    my @keys = ($entry->pkg_name, $entry->pkg_version);
    push @keys, $entry->pkg_arch if $entry->pkg_type ne 'source';
    return @keys;
}

=item set (ENTRY)

Inserts ENTRY into the manifest.  This may replace an existing entry.

Note: The interesting fields from ENTRY are copied, so later changes
to ENTRY will not affect the data in the manifest.

=cut

sub set {
    my ($self, $entry) = @_;
    my $type = $self->type;
    croak 'Cannot alter a GROUP manifest directly'
      if $type eq 'GROUP';
    my %pdata;
    my ($fields, $qf) = @{$TYPE2INFO{$type}}{'file-fields', 'query-fields'};

    # Copy the relevant fields - ensuring all fields are defined.
    foreach my $field (@$fields) {
        my $val = $entry->{$field} // '';
        # Avoid some problematic characters that would break the file
        # format.
        $val =~ tr/;\n/_ /;
        $pdata{$field} = $val;
    }
    $self->_make_alias_fields(\%pdata);

    $self->_do_set($qf, \%pdata);
    $self->_mark_dirty(1);
    return 1;
}

=item set_transient_marker (TRANSIENT, KEYS...)

Set or clear transient flag.  Transient entries are not written to the
disk (i.e. They will not appear in the file created/written by
L</write_list (FILE)>).  KEYS is passed as is passed to
L</get (KEYS...)>.

By default all entries are persistent.

=cut

sub set_transient_marker {
    my ($self, $marker, @keys) = @_;
    my $entry = $self->get(@keys);
    return unless $entry;
    $self->{'_transient'} = 1 if $marker;
    delete $self->{'_transient'} unless $marker;
    return;
}

=item delete (KEYS...)

Removes the entry/entries found by KEYS (if any).  KEYS must contain
at least one item - if the list of keys cannot uniquely identify a single
element, all "matching" elements will be removed.  Examples:

 # Delete the gcc-4.6 entry at version 4.6.1-4 that is also architecture i386
 $manifest->delete ('gcc-4.6', '4.6.1-4', 'i386');
 
 # Delete all gcc-4.6 entries at version 4.6.1-4 regardless of their
 # architecture
 $manifest->delete ('gcc-4.6', '4.6.1-4');
 
 # Delete all gcc-4.6 entries regardless of version and architecture
 $manifest->delete ('gcc-4.6')

If KEYS is exactly one item, it is assumed to be a
L<Lintian::Processable>.  If so, the proper keys will be extracted
from that processable and (if present) that one element will be
removed.

This will mark the list as dirty if an element was removed.  If it returns
a truth value, an element was removed - otherwise it will return 0.

See L</get (KEYS...)> for the key names.

=cut

sub delete {
    my ($self, @keys) = @_;
    croak 'Cannot alter a GROUP manifest directly'
      if $self->type eq 'GROUP';
    return $self->_do_delete(@keys);
}

sub _do_delete {
    my ($self, @keys) = @_;
    @keys = $self->_make_keys($keys[0]) if scalar @keys == 1;
    # last key, that is what we will remove :)
    my $lk = pop @keys;
    my $hash;

    return 0 unless defined $lk;

    if (@keys) {
        $hash = $self->get(@keys);
    } else {
        $hash = $self->{'state'};
    }

    if (defined $hash && exists $hash->{$lk}) {
        my $entry = delete $hash->{$lk};
        $self->_mark_dirty(1);
        if (my $grouping = $self->{'grouping'}) {
            my @keys = (
                $entry->{'source'},
                $entry->{'source-version'},
                $entry->{'identifier'});
            $grouping->_do_delete(@keys);
        }
        return 1;
    }
    return 0;
}

=item diff (MANIFEST)

Returns a L<diff|Lintian::Lab::ManifestDiff> between this manifest and
MANIFEST.

This instance is considered the "original" and MANIFEST is "new"
version of the manifest.  (See the olist and nlist methods of
L<Lintian::Lab::ManifestDiff> for more information.

=cut

sub diff {
    my ($self, $other) = @_;
    croak 'Cannot diff a GROUP manifest' if $self->type eq 'GROUP';
    my ($copy, @changed, @added, @removed, $visitor);
    unless ($self->{'type'} eq $other->{'type'}) {
        my $st = $self->{'type'};
        my $ot = $other->{'type'};
        croak "Diffing incompatible types ($st != $ot)";
    }
    $copy = $self->clone;

    $visitor = sub {
        my ($ov, @keys) = @_;
        my $sv = $copy->get(@keys);
        unless (defined $sv) {
            push @added, \@keys;
            return;
        }
        if (  $sv->{'version'} ne $ov->{'version'}
            ||$sv->{'timestamp'} ne $ov->{'timestamp'}) {
            push @changed, \@keys;
        }
        # Remove the entry from $copy
        $copy->delete(@keys);
    }; # End of visitor sub

    # Find all the added and changed entries - since $visitor removes
    # all entries it finds from $copy, $copy will contain the elements
    # that are only in $self after this call.
    $other->visit_all($visitor);
    # Thus we can just add all of these entries to @removed.  :)
    $copy->visit_all(sub { my (undef, @keys) = @_; push @removed, \@keys; });

    require Lintian::Lab::ManifestDiff;

    return Lintian::Lab::ManifestDiff->_new($self->{'type'}, $other, $self,
        \@added, \@removed, \@changed);
}

### Internal methods ###

# $plist->_mark_dirty($val)
#
# Internal sub to alter the dirty flag. 1 for dirty, 0 for "not dirty"
sub _mark_dirty {
    my ($self, $dirty) = @_;
    $self->{'dirty'} = $dirty;
    return;
}

# $plist->_do_read_file($file, $header, $fields)
#
# internal sub to actually load the pkg list from $file.
# $header is the expected header (first line excl. newline)
# $fields is a ref to the relevant field list (see @*_FILE_FIELDS)
#  - croaks on error
sub _do_read_file {
    my ($self, $file, $header, $fields, $qf) = @_;
    my $count = scalar @$fields;
    my $root = {};
    open(my $fd, '<', $file);
    my $hd = <$fd>;
    chop $hd;
    unless ($hd eq $header) {
        # The interesting part here is the format is invalid, so
        # "ignore" errors from close
        no autodie qw(close);
        close($fd);
        croak "Unknown/unsupported file format ($hd)";
    }

    while (my $line = <$fd>) {
        chop($line);
        next if $line =~ m/^\s*+$/o;
        my (@values) = split m/\;/o, $line, $count;
        my $entry = {};
        unless ($count == scalar @values) {
            # The interesting part here is the format is invalid, so
            # "ignore" errors from close
            no autodie qw(close);
            close($fd);
            croak "Invalid line in $file at line $. ($_)";
        }
        for (my $i = 0 ; $i < $count ; $i++) {
            $entry->{$fields->[$i]} = $values[$i]//'';
        }
        $self->_make_alias_fields($entry);
        $self->_do_set($qf, $entry, $root);
    }
    close($fd);
    return $root;
}

sub _make_alias_fields {
    my ($self, $entry) = @_;

    # define source-version as alias of version for
    # source packages.
    $entry->{'source-version'} = $entry->{'version'}
      unless defined $entry->{'source-version'};
    # For compat with Lintian::Processable->new_from_metadata
    $entry->{'pkg_path'} = $entry->{'file'};
    $entry->{'package'} = $entry->{'source'}
      unless defined $entry->{'package'};

    $entry->{'pkg_type'} = $self->type;

    unless (defined $entry->{'identifier'}) {
        my $pkg = $entry->{'package'};
        my $version = $entry->{'version'};
        my $id = $self->type . ":$pkg/$version";
        $id .= '/' . $entry->{'architecture'} if $self->type ne 'source';
        $entry->{'identifier'} = $id;
    }
    return;
}

sub _do_set {
    my ($self, $qf, $entry, $root) = @_;
    my $qfl = scalar @$qf - 1; # exclude the last element (see below)
    my $cur = $root // $self->{'state'};
    my $k;

    # Find the hash where the entry should be stored
    # - The basic structure is "$root->{key1}->...->{keyN-1}{keyN} = $entry"
    # - This loop is supposed to find the "n-1"th hash and save that in $cur.
    # - After the loop, a simple "$cur->{$keyN} = $entry" inserts the element.
    for (my $i = 0 ; $i < $qfl ; $i++) {
        # Current key
        my $curk = $entry->{$qf->[$i]};
        my $element = $cur->{$curk};
        unless (defined $element) {
            $element = {};
            $cur->{$curk} = $element;
        }
        $cur = $element;
    }
    $k = $entry->{$qf->[$qfl]};
    $cur->{$k} = $entry;
    if (my $grouping = $self->{'grouping'}) {
        $grouping->_do_set(\@GROUP_QUERY, $entry);
    }
    return 1;
}

# Self-recursing method powering visit_all
sub _recurse_visit {
    my ($self, $hash, $visitor, $vdep, @keys) = @_;
    # if false, we recurse, if true we pass it to $visitor
    my $visit = $vdep == scalar @keys;
    foreach my $k (sort keys %$hash) {
        my $v = $hash->{$k};
        # Should we recurse into $v?
        $self->_recurse_visit($v, $visitor, $vdep, @keys, $k) unless $visit;
        # ... or is it the value to be visited?
        $visitor->($v, @keys, $k) if $visit;
    }
    return;
}

=back

=head1 AUTHOR

Originally written by Niels Thykier <niels@thykier.net> for Lintian.

=head1 SEE ALSO

lintian(1)

=cut

1;

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