]> git.donarmstrong.com Git - debbugs.git/blob - Debbugs/Status.pm
Add support for outlook in control
[debbugs.git] / Debbugs / Status.pm
1 # This module is part of debbugs, and is released
2 # under the terms of the GPL version 2, or any later
3 # version at your option.
4 # See the file README and COPYING for more information.
5 #
6 # [Other people have contributed to this file; their copyrights should
7 # go here too.]
8 # Copyright 2007-9 by Don Armstrong <don@donarmstrong.com>.
9
10 package Debbugs::Status;
11
12 =head1 NAME
13
14 Debbugs::Status -- Routines for dealing with summary and status files
15
16 =head1 SYNOPSIS
17
18 use Debbugs::Status;
19
20
21 =head1 DESCRIPTION
22
23 This module is a replacement for the parts of errorlib.pl which write
24 and read status and summary files.
25
26 It also contains generic routines for returning information about the
27 status of a particular bug
28
29 =head1 FUNCTIONS
30
31 =cut
32
33 use warnings;
34 use strict;
35
36 use vars qw($VERSION $DEBUG %EXPORT_TAGS @EXPORT_OK @EXPORT);
37 use base qw(Exporter);
38
39 use Params::Validate qw(validate_with :types);
40 use Debbugs::Common qw(:util :lock :quit :misc :utf8);
41 use Debbugs::Config qw(:config);
42 use Debbugs::MIME qw(decode_rfc1522 encode_rfc1522);
43 use Debbugs::Packages qw(makesourceversions make_source_versions getversions get_versions binary_to_source);
44 use Debbugs::Versions;
45 use Debbugs::Versions::Dpkg;
46 use POSIX qw(ceil);
47 use File::Copy qw(copy);
48 use Encode qw(decode encode is_utf8);
49
50 use Storable qw(dclone);
51 use List::Util qw(min max);
52
53 use Carp qw(croak);
54
55 BEGIN{
56      $VERSION = 1.00;
57      $DEBUG = 0 unless defined $DEBUG;
58
59      @EXPORT = ();
60      %EXPORT_TAGS = (status => [qw(splitpackages get_bug_status buggy bug_archiveable),
61                                 qw(isstrongseverity bug_presence split_status_fields),
62                                ],
63                      read   => [qw(readbug read_bug lockreadbug lockreadbugmerge),
64                                 qw(lock_read_all_merged_bugs),
65                                ],
66                      write  => [qw(writebug makestatus unlockwritebug)],
67                      new => [qw(new_bug)],
68                      versions => [qw(addfoundversions addfixedversions),
69                                   qw(removefoundversions removefixedversions)
70                                  ],
71                      hook     => [qw(bughook bughook_archive)],
72                      fields   => [qw(%fields)],
73                     );
74      @EXPORT_OK = ();
75      Exporter::export_ok_tags(keys %EXPORT_TAGS);
76      $EXPORT_TAGS{all} = [@EXPORT_OK];
77 }
78
79
80 =head2 readbug
81
82      readbug($bug_num,$location)
83      readbug($bug_num)
84
85 Reads a summary file from the archive given a bug number and a bug
86 location. Valid locations are those understood by L</getbugcomponent>
87
88 =cut
89
90 # these probably shouldn't be imported by most people, but
91 # Debbugs::Control needs them, so they're now exportable
92 our %fields = (originator     => 'submitter',
93               date           => 'date',
94               subject        => 'subject',
95               msgid          => 'message-id',
96               'package'      => 'package',
97               keywords       => 'tags',
98               done           => 'done',
99               forwarded      => 'forwarded-to',
100               mergedwith     => 'merged-with',
101               severity       => 'severity',
102               owner          => 'owner',
103               found_versions => 'found-in',
104               found_date     => 'found-date',
105               fixed_versions => 'fixed-in',
106               fixed_date     => 'fixed-date',
107               blocks         => 'blocks',
108               blockedby      => 'blocked-by',
109               unarchived     => 'unarchived',
110               summary        => 'summary',
111               outlook        => 'outlook',
112               affects        => 'affects',
113              );
114
115
116 # Fields which need to be RFC1522-decoded in format versions earlier than 3.
117 my @rfc1522_fields = qw(originator subject done forwarded owner);
118
119 sub readbug {
120      return read_bug(bug => $_[0],
121                      (@_ > 1)?(location => $_[1]):()
122                     );
123 }
124
125 =head2 read_bug
126
127      read_bug(bug => $bug_num,
128               location => 'archive',
129              );
130      read_bug(summary => 'path/to/bugnum.summary');
131      read_bug($bug_num);
132
133 A more complete function than readbug; it enables you to pass a full
134 path to the summary file instead of the bug number and/or location.
135
136 =head3 Options
137
138 =over
139
140 =item bug -- the bug number
141
142 =item location -- optional location which is passed to getbugcomponent
143
144 =item summary -- complete path to the .summary file which will be read
145
146 =item lock -- whether to obtain a lock for the bug to prevent
147 something modifying it while the bug has been read. You B<must> call
148 C<unfilelock();> if something not undef is returned from read_bug.
149
150 =item locks -- hashref of already obtained locks; incremented as new
151 locks are needed, and decremented as locks are released on particular
152 files.
153
154 =back
155
156 One of C<bug> or C<summary> must be passed. This function will return
157 undef on failure, and will die if improper arguments are passed.
158
159 =cut
160
161 sub read_bug{
162     if (@_ == 1) {
163          unshift @_, 'bug';
164     }
165     my %param = validate_with(params => \@_,
166                               spec   => {bug => {type => SCALAR,
167                                                  optional => 1,
168                                                  # something really
169                                                  # stupid passes
170                                                  # negative bugnumbers
171                                                  regex    => qr/^-?\d+/,
172                                                 },
173                                          location => {type => SCALAR|UNDEF,
174                                                       optional => 1,
175                                                      },
176                                          summary  => {type => SCALAR,
177                                                       optional => 1,
178                                                      },
179                                          lock     => {type => BOOLEAN,
180                                                       optional => 1,
181                                                      },
182                                          locks    => {type => HASHREF,
183                                                       optional => 1,
184                                                      },
185                                         },
186                              );
187     die "One of bug or summary must be passed to read_bug"
188          if not exists $param{bug} and not exists $param{summary};
189     my $status;
190     my $log;
191     my $location;
192     if (not defined $param{summary}) {
193          my $lref;
194          ($lref,$location) = @param{qw(bug location)};
195          if (not defined $location) {
196               $location = getbuglocation($lref,'summary');
197               return undef if not defined $location;
198          }
199          $status = getbugcomponent($lref, 'summary', $location);
200          $log    = getbugcomponent($lref, 'log'    , $location);
201          return undef unless defined $status;
202          return undef if not -e $status;
203     }
204     else {
205          $status = $param{summary};
206          $log = $status;
207          $log =~ s/\.summary$/.log/;
208          ($location) = $status =~ m/(db-h|db|archive)/;
209     }
210     if ($param{lock}) {
211         filelock("$config{spool_dir}/lock/$param{bug}",exists $param{locks}?$param{locks}:());
212     }
213     my $status_fh = IO::File->new($status, 'r');
214     if (not defined $status_fh) {
215         warn "Unable to open $status for reading: $!";
216         if ($param{lock}) {
217                 unfilelock(exists $param{locks}?$param{locks}:());
218         }
219         return undef;
220     }
221     binmode($status_fh,':encoding(UTF-8)');
222
223     my %data;
224     my @lines;
225     my $version = 2;
226     local $_;
227
228     while (<$status_fh>) {
229         chomp;
230         push @lines, $_;
231         $version = $1 if /^Format-Version: ([0-9]+)/i;
232     }
233
234     # Version 3 is the latest format version currently supported.
235     if ($version > 3) {
236          warn "Unsupported status version '$version'";
237          if ($param{lock}) {
238              unfilelock(exists $param{locks}?$param{locks}:());
239          }
240          return undef;
241     }
242
243     my %namemap = reverse %fields;
244     for my $line (@lines) {
245         if ($line =~ /(\S+?): (.*)/) {
246             my ($name, $value) = (lc $1, $2);
247             # this is a bit of a hack; we should never, ever have \r
248             # or \n in the fields of status. Kill them off here.
249             # [Eventually, this should be superfluous.]
250             $value =~ s/[\r\n]//g;
251             $data{$namemap{$name}} = $value if exists $namemap{$name};
252         }
253     }
254     for my $field (keys %fields) {
255         $data{$field} = '' unless exists $data{$field};
256     }
257     if ($version < 3) {
258         for my $field (@rfc1522_fields) {
259             $data{$field} = decode_rfc1522($data{$field});
260         }
261     }
262     $data{severity} = $config{default_severity} if $data{severity} eq '';
263     for my $field (qw(found_versions fixed_versions found_date fixed_date)) {
264          $data{$field} = [split ' ', $data{$field}];
265     }
266     for my $field (qw(found fixed)) {
267          # create the found/fixed hashes which indicate when a
268          # particular version was marked found or marked fixed.
269          @{$data{$field}}{@{$data{"${field}_versions"}}} =
270               (('') x (@{$data{"${field}_date"}} - @{$data{"${field}_versions"}}),
271                @{$data{"${field}_date"}});
272     }
273
274     my $status_modified = (stat($status))[9];
275     # Add log last modified time
276     $data{log_modified} = (stat($log))[9];
277     $data{last_modified} = max($status_modified,$data{log_modified});
278     $data{location} = $location;
279     $data{archived} = (defined($location) and ($location eq 'archive'))?1:0;
280     $data{bug_num} = $param{bug};
281
282     return \%data;
283 }
284
285 =head2 split_status_fields
286
287      my @data = split_status_fields(@data);
288
289 Splits splittable status fields (like package, tags, blocks,
290 blockedby, etc.) into arrayrefs (use make_list on these). Keeps the
291 passed @data intact using dclone.
292
293 In scalar context, returns only the first element of @data.
294
295 =cut
296
297 our $ditch_empty = sub{
298     my @t = @_;
299     my $splitter = shift @t;
300     return grep {length $_} map {split $splitter} @t;
301 };
302
303 my $ditch_empty_space = sub {return &{$ditch_empty}(' ',@_)};
304 my %split_fields =
305     (package        => \&splitpackages,
306      affects        => \&splitpackages,
307      blocks         => $ditch_empty_space,
308      blockedby      => $ditch_empty_space,
309      # this isn't strictly correct, but we'll split both of them for
310      # the time being until we ditch all use of keywords everywhere
311      # from the code
312      keywords       => $ditch_empty_space,
313      tags           => $ditch_empty_space,
314      found_versions => $ditch_empty_space,
315      fixed_versions => $ditch_empty_space,
316      mergedwith     => $ditch_empty_space,
317     );
318
319 sub split_status_fields {
320     my @data = @{dclone(\@_)};
321     for my $data (@data) {
322         next if not defined $data;
323         croak "Passed an element which is not a hashref to split_status_field".ref($data) if
324             not (ref($data) and ref($data) eq 'HASH');
325         for my $field (keys %{$data}) {
326             next unless defined $data->{$field};
327             if (exists $split_fields{$field}) {
328                 next if ref($data->{$field});
329                 my @elements;
330                 if (ref($split_fields{$field}) eq 'CODE') {
331                     @elements = &{$split_fields{$field}}($data->{$field});
332                 }
333                 elsif (not ref($split_fields{$field}) or
334                        UNIVERSAL::isa($split_fields{$field},'Regex')
335                       ) {
336                     @elements = split $split_fields{$field}, $data->{$field};
337                 }
338                 $data->{$field} = \@elements;
339             }
340         }
341     }
342     return wantarray?@data:$data[0];
343 }
344
345 =head2 join_status_fields
346
347      my @data = join_status_fields(@data);
348
349 Handles joining the splitable status fields. (Basically, the inverse
350 of split_status_fields.
351
352 Primarily called from makestatus, but may be useful for other
353 functions after calling split_status_fields (or for legacy functions
354 if we transition to split fields by default).
355
356 =cut
357
358 sub join_status_fields {
359     my %join_fields =
360         (package        => ', ',
361          affects        => ', ',
362          blocks         => ' ',
363          blockedby      => ' ',
364          tags           => ' ',
365          found_versions => ' ',
366          fixed_versions => ' ',
367          found_date     => ' ',
368          fixed_date     => ' ',
369          mergedwith     => ' ',
370         );
371     my @data = @{dclone(\@_)};
372     for my $data (@data) {
373         next if not defined $data;
374         croak "Passed an element which is not a hashref to split_status_field: ".
375             ref($data)
376                 if ref($data) ne 'HASH';
377         for my $field (keys %{$data}) {
378             next unless defined $data->{$field};
379             next unless ref($data->{$field}) eq 'ARRAY';
380             next unless exists $join_fields{$field};
381             $data->{$field} = join($join_fields{$field},@{$data->{$field}});
382         }
383     }
384     return wantarray?@data:$data[0];
385 }
386
387
388 =head2 lockreadbug
389
390      lockreadbug($bug_num,$location)
391
392 Performs a filelock, then reads the bug; the bug is unlocked if the
393 return is undefined, otherwise, you need to call unfilelock or
394 unlockwritebug.
395
396 See readbug above for information on what this returns
397
398 =cut
399
400 sub lockreadbug {
401     my ($lref, $location) = @_;
402     return read_bug(bug => $lref, location => $location, lock => 1);
403 }
404
405 =head2 lockreadbugmerge
406
407      my ($locks, $data) = lockreadbugmerge($bug_num,$location);
408
409 Performs a filelock, then reads the bug. If the bug is merged, locks
410 the merge lock. Returns a list of the number of locks and the bug
411 data.
412
413 =cut
414
415 sub lockreadbugmerge {
416      my ($bug_num,$location) = @_;
417      my $data = lockreadbug(@_);
418      if (not defined $data) {
419           return (0,undef);
420      }
421      if (not length $data->{mergedwith}) {
422           return (1,$data);
423      }
424      unfilelock();
425      filelock("$config{spool_dir}/lock/merge");
426      $data = lockreadbug(@_);
427      if (not defined $data) {
428           unfilelock();
429           return (0,undef);
430      }
431      return (2,$data);
432 }
433
434 =head2 lock_read_all_merged_bugs
435
436      my ($locks,@bug_data) = lock_read_all_merged_bugs($bug_num,$location);
437
438 Performs a filelock, then reads the bug passed. If the bug is merged,
439 locks the merge lock, then reads and locks all of the other merged
440 bugs. Returns a list of the number of locks and the bug data for all
441 of the merged bugs.
442
443 Will also return undef if any of the merged bugs failed to be read,
444 even if all of the others were read properly.
445
446 =cut
447
448 sub lock_read_all_merged_bugs {
449     my %param = validate_with(params => \@_,
450                               spec   => {bug => {type => SCALAR,
451                                                  regex => qr/^\d+$/,
452                                                 },
453                                          location => {type => SCALAR,
454                                                       optional => 1,
455                                                      },
456                                          locks    => {type => HASHREF,
457                                                       optional => 1,
458                                                      },
459                                         },
460                              );
461     my $locks = 0;
462     my @data = read_bug(bug => $param{bug},
463                         lock => 1,
464                         exists $param{location} ? (location => $param{location}):(),
465                         exists $param{locks} ? (locks => $param{locks}):(),
466                        );
467     if (not @data or not defined $data[0]) {
468         return ($locks,());
469     }
470     $locks++;
471     if (not length $data[0]->{mergedwith}) {
472         return ($locks,@data);
473     }
474     unfilelock(exists $param{locks}?$param{locks}:());
475     $locks--;
476     filelock("$config{spool_dir}/lock/merge",exists $param{locks}?$param{locks}:());
477     $locks++;
478     @data = read_bug(bug => $param{bug},
479                      lock => 1,
480                      exists $param{location} ? (location => $param{location}):(),
481                      exists $param{locks} ? (locks => $param{locks}):(),
482                     );
483     if (not @data or not defined $data[0]) {
484         unfilelock(exists $param{locks}?$param{locks}:()); #for merge lock above
485         $locks--;
486         return ($locks,());
487     }
488     $locks++;
489     my @bugs = split / /, $data[0]->{mergedwith};
490     push @bugs, $param{bug};
491     for my $bug (@bugs) {
492         my $newdata = undef;
493         if ($bug != $param{bug}) {
494             $newdata =
495                 read_bug(bug => $bug,
496                          lock => 1,
497                          exists $param{location} ? (location => $param{location}):(),
498                          exists $param{locks} ? (locks => $param{locks}):(),
499                         );
500             if (not defined $newdata) {
501                 for (1..$locks) {
502                     unfilelock(exists $param{locks}?$param{locks}:());
503                 }
504                 $locks = 0;
505                 warn "Unable to read bug: $bug while handling merged bug: $param{bug}";
506                 return ($locks,());
507             }
508             $locks++;
509             push @data,$newdata;
510             # perform a sanity check to make sure that the merged bugs
511             # are all merged with eachother
512             my $expectmerge= join(' ',grep {$_ != $bug } sort { $a <=> $b } @bugs);
513             if ($newdata->{mergedwith} ne $expectmerge) {
514                 for (1..$locks) {
515                     unfilelock(exists $param{locks}?$param{locks}:());
516                 }
517                 die "Bug $param{bug} differs from bug $bug: ($newdata->{bug_num}: '$newdata->{mergedwith}') vs. ('$expectmerge') (".join(' ',@bugs).")";
518             }
519         }
520     }
521     return ($locks,@data);
522 }
523
524 =head2 new_bug
525
526         my $new_bug_num = new_bug(copy => $data->{bug_num});
527
528 Creates a new bug and returns the new bug number upon success.
529
530 Dies upon failures.
531
532 =cut
533
534 sub new_bug {
535     my %param =
536         validate_with(params => \@_,
537                       spec => {copy => {type => SCALAR,
538                                         regex => qr/^\d+/,
539                                         optional => 1,
540                                        },
541                               },
542                      );
543     filelock("nextnumber.lock");
544     my $nn_fh = IO::File->new("nextnumber",'r') or
545         die "Unable to open nextnuber for reading: $!";
546     local $\;
547     my $nn = <$nn_fh>;
548     ($nn) = $nn =~ m/^(\d+)\n$/ or die "Bad format of nextnumber; is not exactly ".'^\d+\n$';
549     close $nn_fh;
550     overwritefile("nextnumber",
551                   ($nn+1)."\n");
552     unfilelock();
553     my $nn_hash = get_hashname($nn);
554     if ($param{copy}) {
555         my $c_hash = get_hashname($param{copy});
556         for my $file (qw(log status summary report)) {
557             copy("db-h/$c_hash/$param{copy}.$file",
558                  "db-h/$nn_hash/${nn}.$file")
559         }
560     }
561     else {
562         for my $file (qw(log status summary report)) {
563             overwritefile("db-h/$nn_hash/${nn}.$file",
564                            "");
565         }
566     }
567
568     # this probably needs to be munged to do something more elegant
569 #    &bughook('new', $clone, $data);
570
571     return($nn);
572 }
573
574
575
576 my @v1fieldorder = qw(originator date subject msgid package
577                       keywords done forwarded mergedwith severity);
578
579 =head2 makestatus
580
581      my $content = makestatus($status,$version)
582      my $content = makestatus($status);
583
584 Creates the content for a status file based on the $status hashref
585 passed.
586
587 Really only useful for writebug
588
589 Currently defaults to version 2 (non-encoded rfc1522 names) but will
590 eventually default to version 3. If you care, you should specify a
591 version.
592
593 =cut
594
595 sub makestatus {
596     my ($data,$version) = @_;
597     $version = 3 unless defined $version;
598
599     my $contents = '';
600
601     my %newdata = %$data;
602     for my $field (qw(found fixed)) {
603          if (exists $newdata{$field}) {
604               $newdata{"${field}_date"} =
605                    [map {$newdata{$field}{$_}||''} keys %{$newdata{$field}}];
606          }
607     }
608     %newdata = %{join_status_fields(\%newdata)};
609
610     %newdata = encode_utf8_structure(%newdata);
611
612     if ($version < 3) {
613         for my $field (@rfc1522_fields) {
614             $newdata{$field} = encode_rfc1522($newdata{$field});
615         }
616     }
617
618     # this is a bit of a hack; we should never, ever have \r or \n in
619     # the fields of status. Kill them off here. [Eventually, this
620     # should be superfluous.]
621     for my $field (keys %newdata) {
622         $newdata{$field} =~ s/[\r\n]//g if defined $newdata{$field};
623     }
624
625     if ($version == 1) {
626         for my $field (@v1fieldorder) {
627             if (exists $newdata{$field} and defined $newdata{$field}) {
628                 $contents .= "$newdata{$field}\n";
629             } else {
630                 $contents .= "\n";
631             }
632         }
633     } elsif ($version == 2 or $version == 3) {
634         # Version 2 or 3. Add a file format version number for the sake of
635         # further extensibility in the future.
636         $contents .= "Format-Version: $version\n";
637         for my $field (keys %fields) {
638             if (exists $newdata{$field} and defined $newdata{$field}
639                 and $newdata{$field} ne '') {
640                 # Output field names in proper case, e.g. 'Merged-With'.
641                 my $properfield = $fields{$field};
642                 $properfield =~ s/(?:^|(?<=-))([a-z])/\u$1/g;
643                 my $data = $newdata{$field};
644                 $contents .= "$properfield: $data\n";
645             }
646         }
647     }
648     return $contents;
649 }
650
651 =head2 writebug
652
653      writebug($bug_num,$status,$location,$minversion,$disablebughook)
654
655 Writes the bug status and summary files out.
656
657 Skips writting out a status file if minversion is 2
658
659 Does not call bughook if disablebughook is true.
660
661 =cut
662
663 sub writebug {
664     my ($ref, $data, $location, $minversion, $disablebughook) = @_;
665     my $change;
666
667     my %outputs = (1 => 'status', 3 => 'summary');
668     for my $version (keys %outputs) {
669         next if defined $minversion and $version < $minversion;
670         my $status = getbugcomponent($ref, $outputs{$version}, $location);
671         die "can't find location for $ref" unless defined $status;
672         my $sfh;
673         if ($version >= 3) {
674             open $sfh,">","$status.new"  or
675                 die "opening $status.new: $!";
676         }
677         else {
678             open $sfh,">","$status.new"  or
679                 die "opening $status.new: $!";
680         }
681         print {$sfh} makestatus($data, $version) or
682             die "writing $status.new: $!";
683         close($sfh) or die "closing $status.new: $!";
684         if (-e $status) {
685             $change = 'change';
686         } else {
687             $change = 'new';
688         }
689         rename("$status.new",$status) || die "installing new $status: $!";
690     }
691
692     # $disablebughook is a bit of a hack to let format migration scripts use
693     # this function rather than having to duplicate it themselves.
694     &bughook($change,$ref,$data) unless $disablebughook;
695 }
696
697 =head2 unlockwritebug
698
699      unlockwritebug($bug_num,$status,$location,$minversion,$disablebughook);
700
701 Writes a bug, then calls unfilelock; see writebug for what these
702 options mean.
703
704 =cut
705
706 sub unlockwritebug {
707     writebug(@_);
708     unfilelock();
709 }
710
711 =head1 VERSIONS
712
713 The following functions are exported with the :versions tag
714
715 =head2 addfoundversions
716
717      addfoundversions($status,$package,$version,$isbinary);
718
719 All use of this should be phased out in favor of Debbugs::Control::fixed/found
720
721 =cut
722
723
724 sub addfoundversions {
725     my $data = shift;
726     my $package = shift;
727     my $version = shift;
728     my $isbinary = shift;
729     return unless defined $version;
730     undef $package if $package =~ m[(?:\s|/)];
731     my $source = $package;
732     if ($package =~ s/^src://) {
733         $isbinary = 0;
734         $source = $package;
735     }
736
737     if (defined $package and $isbinary) {
738         my @srcinfo = binary_to_source(binary => $package,
739                                        version => $version);
740         if (@srcinfo) {
741             # We know the source package(s). Use a fully-qualified version.
742             addfoundversions($data, $_->[0], $_->[1], '') foreach @srcinfo;
743             return;
744         }
745         # Otherwise, an unqualified version will have to do.
746         undef $source;
747     }
748
749     # Strip off various kinds of brain-damage.
750     $version =~ s/;.*//;
751     $version =~ s/ *\(.*\)//;
752     $version =~ s/ +[A-Za-z].*//;
753
754     foreach my $ver (split /[,\s]+/, $version) {
755         my $sver = defined($source) ? "$source/$ver" : '';
756         unless (grep { $_ eq $ver or $_ eq $sver } @{$data->{found_versions}}) {
757             push @{$data->{found_versions}}, defined($source) ? $sver : $ver;
758         }
759         @{$data->{fixed_versions}} =
760             grep { $_ ne $ver and $_ ne $sver } @{$data->{fixed_versions}};
761     }
762 }
763
764 =head2 removefoundversions
765
766      removefoundversions($data,$package,$versiontoremove)
767
768 Removes found versions from $data
769
770 If a version is fully qualified (contains /) only versions matching
771 exactly are removed. Otherwise, all versions matching the version
772 number are removed.
773
774 Currently $package and $isbinary are entirely ignored, but accepted
775 for backwards compatibilty.
776
777 =cut
778
779 sub removefoundversions {
780     my $data = shift;
781     my $package = shift;
782     my $version = shift;
783     my $isbinary = shift;
784     return unless defined $version;
785
786     foreach my $ver (split /[,\s]+/, $version) {
787          if ($ver =~ m{/}) {
788               # fully qualified version
789               @{$data->{found_versions}} =
790                    grep {$_ ne $ver}
791                         @{$data->{found_versions}};
792          }
793          else {
794               # non qualified version; delete all matchers
795               @{$data->{found_versions}} =
796                    grep {$_ !~ m[(?:^|/)\Q$ver\E$]}
797                         @{$data->{found_versions}};
798          }
799     }
800 }
801
802
803 sub addfixedversions {
804     my $data = shift;
805     my $package = shift;
806     my $version = shift;
807     my $isbinary = shift;
808     return unless defined $version;
809     undef $package if defined $package and $package =~ m[(?:\s|/)];
810     my $source = $package;
811
812     if (defined $package and $isbinary) {
813         my @srcinfo = binary_to_source(binary => $package,
814                                        version => $version);
815         if (@srcinfo) {
816             # We know the source package(s). Use a fully-qualified version.
817             addfixedversions($data, $_->[0], $_->[1], '') foreach @srcinfo;
818             return;
819         }
820         # Otherwise, an unqualified version will have to do.
821         undef $source;
822     }
823
824     # Strip off various kinds of brain-damage.
825     $version =~ s/;.*//;
826     $version =~ s/ *\(.*\)//;
827     $version =~ s/ +[A-Za-z].*//;
828
829     foreach my $ver (split /[,\s]+/, $version) {
830         my $sver = defined($source) ? "$source/$ver" : '';
831         unless (grep { $_ eq $ver or $_ eq $sver } @{$data->{fixed_versions}}) {
832             push @{$data->{fixed_versions}}, defined($source) ? $sver : $ver;
833         }
834         @{$data->{found_versions}} =
835             grep { $_ ne $ver and $_ ne $sver } @{$data->{found_versions}};
836     }
837 }
838
839 sub removefixedversions {
840     my $data = shift;
841     my $package = shift;
842     my $version = shift;
843     my $isbinary = shift;
844     return unless defined $version;
845
846     foreach my $ver (split /[,\s]+/, $version) {
847          if ($ver =~ m{/}) {
848               # fully qualified version
849               @{$data->{fixed_versions}} =
850                    grep {$_ ne $ver}
851                         @{$data->{fixed_versions}};
852          }
853          else {
854               # non qualified version; delete all matchers
855               @{$data->{fixed_versions}} =
856                    grep {$_ !~ m[(?:^|/)\Q$ver\E$]}
857                         @{$data->{fixed_versions}};
858          }
859     }
860 }
861
862
863
864 =head2 splitpackages
865
866      splitpackages($pkgs)
867
868 Split a package string from the status file into a list of package names.
869
870 =cut
871
872 sub splitpackages {
873     my $pkgs = shift;
874     return unless defined $pkgs;
875     return grep {length $_} map lc, split /[\s,()?]+/, $pkgs;
876 }
877
878
879 =head2 bug_archiveable
880
881      bug_archiveable(bug => $bug_num);
882
883 Options
884
885 =over
886
887 =item bug -- bug number (required)
888
889 =item status -- Status hashref returned by read_bug or get_bug_status (optional)
890
891 =item version -- Debbugs::Version information (optional)
892
893 =item days_until -- return days until the bug can be archived
894
895 =back
896
897 Returns 1 if the bug can be archived
898 Returns 0 if the bug cannot be archived
899
900 If days_until is true, returns the number of days until the bug can be
901 archived, -1 if it cannot be archived. 0 means that the bug can be
902 archived the next time the archiver runs.
903
904 Returns undef on failure.
905
906 =cut
907
908 # This will eventually need to be fixed before we start using mod_perl
909 our $version_cache = {};
910 sub bug_archiveable{
911      my %param = validate_with(params => \@_,
912                                spec   => {bug => {type => SCALAR,
913                                                   regex => qr/^\d+$/,
914                                                  },
915                                           status => {type => HASHREF,
916                                                      optional => 1,
917                                                     },
918                                           days_until => {type => BOOLEAN,
919                                                          default => 0,
920                                                         },
921                                           ignore_time => {type => BOOLEAN,
922                                                           default => 0,
923                                                          },
924                                          },
925                               );
926      # This is what we return if the bug cannot be archived.
927      my $cannot_archive = $param{days_until}?-1:0;
928      # read the status information
929      my $status = $param{status};
930      if (not exists $param{status} or not defined $status) {
931           $status = read_bug(bug=>$param{bug});
932           if (not defined $status) {
933                print STDERR "Cannot archive $param{bug} because it does not exist\n" if $DEBUG;
934                return undef;
935           }
936      }
937      # Bugs can be archived if they are
938      # 1. Closed
939      if (not defined $status->{done} or not length $status->{done}) {
940           print STDERR "Cannot archive $param{bug} because it is not done\n" if $DEBUG;
941           return $cannot_archive
942      }
943      # Check to make sure that the bug has none of the unremovable tags set
944      if (@{$config{removal_unremovable_tags}}) {
945           for my $tag (split ' ', ($status->{keywords}||'')) {
946                if (grep {$tag eq $_} @{$config{removal_unremovable_tags}}) {
947                     print STDERR "Cannot archive $param{bug} because it has an unremovable tag '$tag'\n" if $DEBUG;
948                     return $cannot_archive;
949                }
950           }
951      }
952
953      # If we just are checking if the bug can be archived, we'll not even bother
954      # checking the versioning information if the bug has been -done for less than 28 days.
955      my $log_file = getbugcomponent($param{bug},'log');
956      if (not defined $log_file) {
957           print STDERR "Cannot archive $param{bug} because the log doesn't exist\n" if $DEBUG;
958           return $cannot_archive;
959      }
960      my $max_log_age = max(map {$config{remove_age} - -M $_}
961                            $log_file, map {my $log = getbugcomponent($_,'log');
962                                            defined $log ? ($log) : ();
963                                       }
964                            split / /, $status->{mergedwith}
965                        );
966      if (not $param{days_until} and not $param{ignore_time}
967          and $max_log_age > 0
968         ) {
969           print STDERR "Cannot archive $param{bug} because of time\n" if $DEBUG;
970           return $cannot_archive;
971      }
972      # At this point, we have to get the versioning information for this bug.
973      # We examine the set of distribution tags. If a bug has no distribution
974      # tags set, we assume a default set, otherwise we use the tags the bug
975      # has set.
976
977      # In cases where we are assuming a default set, if the severity
978      # is strong, we use the strong severity default; otherwise, we
979      # use the normal default.
980
981      # There must be fixed_versions for us to look at the versioning
982      # information
983      my $min_fixed_time = time;
984      my $min_archive_days = 0;
985      if (@{$status->{fixed_versions}}) {
986           my %dist_tags;
987           @dist_tags{@{$config{removal_distribution_tags}}} =
988                (1) x @{$config{removal_distribution_tags}};
989           my %dists;
990           for my $tag (split ' ', ($status->{keywords}||'')) {
991                next unless exists $config{distribution_aliases}{$tag};
992                next unless $dist_tags{$config{distribution_aliases}{$tag}};
993                $dists{$config{distribution_aliases}{$tag}} = 1;
994           }
995           if (not keys %dists) {
996                if (isstrongseverity($status->{severity})) {
997                     @dists{@{$config{removal_strong_severity_default_distribution_tags}}} =
998                          (1) x @{$config{removal_strong_severity_default_distribution_tags}};
999                }
1000                else {
1001                     @dists{@{$config{removal_default_distribution_tags}}} =
1002                          (1) x @{$config{removal_default_distribution_tags}};
1003                }
1004           }
1005           my %source_versions;
1006           my @sourceversions = get_versions(package => $status->{package},
1007                                             dist => [keys %dists],
1008                                             source => 1,
1009                                            );
1010           @source_versions{@sourceversions} = (1) x @sourceversions;
1011           # If the bug has not been fixed in the versions actually
1012           # distributed, then it cannot be archived.
1013           if ('found' eq max_buggy(bug => $param{bug},
1014                                    sourceversions => [keys %source_versions],
1015                                    found          => $status->{found_versions},
1016                                    fixed          => $status->{fixed_versions},
1017                                    version_cache  => $version_cache,
1018                                    package        => $status->{package},
1019                                   )) {
1020                print STDERR "Cannot archive $param{bug} because it's found\n" if $DEBUG;
1021                return $cannot_archive;
1022           }
1023           # Since the bug has at least been fixed in the architectures
1024           # that matters, we check to see how long it has been fixed.
1025
1026           # If $param{ignore_time}, then we should ignore time.
1027           if ($param{ignore_time}) {
1028                return $param{days_until}?0:1;
1029           }
1030
1031           # To do this, we order the times from most recent to oldest;
1032           # when we come to the first found version, we stop.
1033           # If we run out of versions, we only report the time of the
1034           # last one.
1035           my %time_versions = get_versions(package => $status->{package},
1036                                            dist    => [keys %dists],
1037                                            source  => 1,
1038                                            time    => 1,
1039                                           );
1040           for my $version (sort {$time_versions{$b} <=> $time_versions{$a}} keys %time_versions) {
1041                my $buggy = buggy(bug => $param{bug},
1042                                  version        => $version,
1043                                  found          => $status->{found_versions},
1044                                  fixed          => $status->{fixed_versions},
1045                                  version_cache  => $version_cache,
1046                                  package        => $status->{package},
1047                                 );
1048                last if $buggy eq 'found';
1049                $min_fixed_time = min($time_versions{$version},$min_fixed_time);
1050           }
1051           $min_archive_days = max($min_archive_days,ceil($config{remove_age} - (time - $min_fixed_time)/(60*60*24)))
1052                # if there are no versions in the archive at all, then
1053                # we can archive if enough days have passed
1054                if @sourceversions;
1055      }
1056      # If $param{ignore_time}, then we should ignore time.
1057      if ($param{ignore_time}) {
1058           return $param{days_until}?0:1;
1059      }
1060      # 6. at least 28 days have passed since the last action has occured or the bug was closed
1061      my $age = ceil($max_log_age);
1062      if ($age > 0 or $min_archive_days > 0) {
1063           print STDERR "Cannot archive $param{bug} because not enough days have passed\n" if $DEBUG;
1064           return $param{days_until}?max($age,$min_archive_days):0;
1065      }
1066      else {
1067           return $param{days_until}?0:1;
1068      }
1069 }
1070
1071
1072 =head2 get_bug_status
1073
1074      my $status = get_bug_status(bug => $nnn);
1075
1076      my $status = get_bug_status($bug_num)
1077
1078 =head3 Options
1079
1080 =over
1081
1082 =item bug -- scalar bug number
1083
1084 =item status -- optional hashref of bug status as returned by readbug
1085 (can be passed to avoid rereading the bug information)
1086
1087 =item bug_index -- optional tied index of bug status infomration;
1088 currently not correctly implemented.
1089
1090 =item version -- optional version(s) to check package status at
1091
1092 =item dist -- optional distribution(s) to check package status at
1093
1094 =item arch -- optional architecture(s) to check package status at
1095
1096 =item bugusertags -- optional hashref of bugusertags
1097
1098 =item sourceversion -- optional arrayref of source/version; overrides
1099 dist, arch, and version. [The entries in this array must be in the
1100 "source/version" format.] Eventually this can be used to for caching.
1101
1102 =item indicatesource -- if true, indicate which source packages this
1103 bug could belong to (or does belong to in the case of bugs assigned to
1104 a source package). Defaults to true.
1105
1106 =back
1107
1108 Note: Currently the version information is cached; this needs to be
1109 changed before using this function in long lived programs.
1110
1111 =cut
1112
1113 sub get_bug_status {
1114      if (@_ == 1) {
1115           unshift @_, 'bug';
1116      }
1117      my %param = validate_with(params => \@_,
1118                                spec   => {bug       => {type => SCALAR,
1119                                                         regex => qr/^\d+$/,
1120                                                        },
1121                                           status    => {type => HASHREF,
1122                                                         optional => 1,
1123                                                        },
1124                                           bug_index => {type => OBJECT,
1125                                                         optional => 1,
1126                                                        },
1127                                           version   => {type => SCALAR|ARRAYREF,
1128                                                         optional => 1,
1129                                                        },
1130                                           dist       => {type => SCALAR|ARRAYREF,
1131                                                          optional => 1,
1132                                                         },
1133                                           arch       => {type => SCALAR|ARRAYREF,
1134                                                          optional => 1,
1135                                                         },
1136                                           bugusertags   => {type => HASHREF,
1137                                                             optional => 1,
1138                                                            },
1139                                           sourceversions => {type => ARRAYREF,
1140                                                              optional => 1,
1141                                                             },
1142                                           indicatesource => {type => BOOLEAN,
1143                                                              default => 1,
1144                                                             },
1145                                          },
1146                               );
1147      my %status;
1148
1149      if (defined $param{bug_index} and
1150          exists $param{bug_index}{$param{bug}}) {
1151           %status = %{ $param{bug_index}{$param{bug}} };
1152           $status{pending} = $status{ status };
1153           $status{id} = $param{bug};
1154           return \%status;
1155      }
1156      if (defined $param{status}) {
1157           %status = %{$param{status}};
1158      }
1159      else {
1160           my $location = getbuglocation($param{bug}, 'summary');
1161           return {} if not defined $location or not length $location;
1162           %status = %{ readbug( $param{bug}, $location ) };
1163      }
1164      $status{id} = $param{bug};
1165
1166      if (defined $param{bugusertags}{$param{bug}}) {
1167           $status{keywords} = "" unless defined $status{keywords};
1168           $status{keywords} .= " " unless $status{keywords} eq "";
1169           $status{keywords} .= join(" ", @{$param{bugusertags}{$param{bug}}});
1170      }
1171      $status{tags} = $status{keywords};
1172      my %tags = map { $_ => 1 } split ' ', $status{tags};
1173
1174      $status{package} = '' if not defined $status{package};
1175      $status{"package"} =~ s/\s*$//;
1176
1177      $status{source} = binary_to_source(binary=>[split /\s*,\s*/, $status{package}],
1178                                         source_only => 1,
1179                                        );
1180
1181      $status{"package"} = 'unknown' if ($status{"package"} eq '');
1182      $status{"severity"} = 'normal' if (not defined $status{severity} or $status{"severity"} eq '');
1183
1184      $status{"pending"} = 'pending';
1185      $status{"pending"} = 'forwarded'       if (length($status{"forwarded"}));
1186      $status{"pending"} = 'pending-fixed'    if ($tags{pending});
1187      $status{"pending"} = 'fixed'           if ($tags{fixed});
1188
1189
1190      my $presence = bug_presence(status => \%status,
1191                                  map{(exists $param{$_})?($_,$param{$_}):()}
1192                                  qw(bug sourceversions arch dist version found fixed package)
1193                                 );
1194      if (defined $presence) {
1195           if ($presence eq 'fixed') {
1196                $status{pending} = 'done';
1197           }
1198           elsif ($presence eq 'absent') {
1199                $status{pending} = 'absent';
1200           }
1201      }
1202      return \%status;
1203 }
1204
1205 =head2 bug_presence
1206
1207      my $precence = bug_presence(bug => nnn,
1208                                  ...
1209                                 );
1210
1211 Returns 'found', 'absent', 'fixed' or undef based on whether the bug
1212 is found, absent, fixed, or no information is available in the
1213 distribution (dist) and/or architecture (arch) specified.
1214
1215
1216 =head3 Options
1217
1218 =over
1219
1220 =item bug -- scalar bug number
1221
1222 =item status -- optional hashref of bug status as returned by readbug
1223 (can be passed to avoid rereading the bug information)
1224
1225 =item bug_index -- optional tied index of bug status infomration;
1226 currently not correctly implemented.
1227
1228 =item version -- optional version to check package status at
1229
1230 =item dist -- optional distribution to check package status at
1231
1232 =item arch -- optional architecture to check package status at
1233
1234 =item sourceversion -- optional arrayref of source/version; overrides
1235 dist, arch, and version. [The entries in this array must be in the
1236 "source/version" format.] Eventually this can be used to for caching.
1237
1238 =back
1239
1240 =cut
1241
1242 sub bug_presence {
1243      my %param = validate_with(params => \@_,
1244                                spec   => {bug       => {type => SCALAR,
1245                                                         regex => qr/^\d+$/,
1246                                                        },
1247                                           status    => {type => HASHREF,
1248                                                         optional => 1,
1249                                                        },
1250                                           version   => {type => SCALAR|ARRAYREF,
1251                                                         optional => 1,
1252                                                        },
1253                                           dist       => {type => SCALAR|ARRAYREF,
1254                                                          optional => 1,
1255                                                         },
1256                                           arch       => {type => SCALAR|ARRAYREF,
1257                                                          optional => 1,
1258                                                         },
1259                                           sourceversions => {type => ARRAYREF,
1260                                                              optional => 1,
1261                                                             },
1262                                          },
1263                               );
1264      my %status;
1265      if (defined $param{status}) {
1266          %status = %{$param{status}};
1267      }
1268      else {
1269           my $location = getbuglocation($param{bug}, 'summary');
1270           return {} if not length $location;
1271           %status = %{ readbug( $param{bug}, $location ) };
1272      }
1273
1274      my @sourceversions;
1275      my $pseudo_desc = getpseudodesc();
1276      if (not exists $param{sourceversions}) {
1277           my %sourceversions;
1278           # pseudopackages do not have source versions by definition.
1279           if (exists $pseudo_desc->{$status{package}}) {
1280                # do nothing.
1281           }
1282           elsif (defined $param{version}) {
1283                foreach my $arch (make_list($param{arch})) {
1284                     for my $package (split /\s*,\s*/, $status{package}) {
1285                          my @temp = makesourceversions($package,
1286                                                        $arch,
1287                                                        make_list($param{version})
1288                                                       );
1289                          @sourceversions{@temp} = (1) x @temp;
1290                     }
1291                }
1292           } elsif (defined $param{dist}) {
1293                my %affects_distribution_tags;
1294                @affects_distribution_tags{@{$config{affects_distribution_tags}}} =
1295                     (1) x @{$config{affects_distribution_tags}};
1296                my $some_distributions_disallowed = 0;
1297                my %allowed_distributions;
1298                for my $tag (split ' ', ($status{keywords}||'')) {
1299                    if (exists $config{distribution_aliases}{$tag} and
1300                         exists $affects_distribution_tags{$config{distribution_aliases}{$tag}}) {
1301                        $some_distributions_disallowed = 1;
1302                        $allowed_distributions{$config{distribution_aliases}{$tag}} = 1;
1303                    }
1304                    elsif (exists $affects_distribution_tags{$tag}) {
1305                        $some_distributions_disallowed = 1;
1306                        $allowed_distributions{$tag} = 1;
1307                    }
1308                }
1309                my @archs = make_list(exists $param{arch}?$param{arch}:());
1310            GET_SOURCE_VERSIONS:
1311                foreach my $arch (@archs) {
1312                    for my $package (split /\s*,\s*/, $status{package}) {
1313                          my @versions = ();
1314                          my $source = 0;
1315                          if ($package =~ /^src:(.+)$/) {
1316                              $source = 1;
1317                              $package = $1;
1318                          }
1319                          foreach my $dist (make_list(exists $param{dist}?$param{dist}:[])) {
1320                               # if some distributions are disallowed,
1321                               # and this isn't an allowed
1322                               # distribution, then we ignore this
1323                               # distribution for the purposees of
1324                               # finding versions
1325                               if ($some_distributions_disallowed and
1326                                   not exists $allowed_distributions{$dist}) {
1327                                    next;
1328                               }
1329                               push @versions, get_versions(package => $package,
1330                                                            dist    => $dist,
1331                                                            ($source?(arch => 'source'):
1332                                                             (defined $arch?(arch => $arch):())),
1333                                                           );
1334                          }
1335                          next unless @versions;
1336                          my @temp = make_source_versions(package => $package,
1337                                                          arch => $arch,
1338                                                          versions => \@versions,
1339                                                         );
1340                          @sourceversions{@temp} = (1) x @temp;
1341                     }
1342                }
1343                # this should really be split out into a subroutine,
1344                # but it'd touch so many things currently, that we fake
1345                # it; it's needed to properly handle bugs which are
1346                # erroneously assigned to the binary package, and we'll
1347                # probably have it go away eventually.
1348                if (not keys %sourceversions and (not @archs or defined $archs[0])) {
1349                    @archs = (undef);
1350                    goto GET_SOURCE_VERSIONS;
1351                }
1352           }
1353
1354           # TODO: This should probably be handled further out for efficiency and
1355           # for more ease of distinguishing between pkg= and src= queries.
1356           # DLA: src= queries should just pass arch=source, and they'll be happy.
1357           @sourceversions = keys %sourceversions;
1358      }
1359      else {
1360           @sourceversions = @{$param{sourceversions}};
1361      }
1362      my $maxbuggy = 'undef';
1363      if (@sourceversions) {
1364           $maxbuggy = max_buggy(bug => $param{bug},
1365                                 sourceversions => \@sourceversions,
1366                                 found => $status{found_versions},
1367                                 fixed => $status{fixed_versions},
1368                                 package => $status{package},
1369                                 version_cache => $version_cache,
1370                                );
1371      }
1372      elsif (defined $param{dist} and
1373             not exists $pseudo_desc->{$status{package}}) {
1374           return 'absent';
1375      }
1376      if (length($status{done}) and
1377          (not @sourceversions or not @{$status{fixed_versions}})) {
1378           return 'fixed';
1379      }
1380      return $maxbuggy;
1381 }
1382
1383
1384 =head2 max_buggy
1385
1386      max_buggy()
1387
1388 =head3 Options
1389
1390 =over
1391
1392 =item bug -- scalar bug number
1393
1394 =item sourceversion -- optional arrayref of source/version; overrides
1395 dist, arch, and version. [The entries in this array must be in the
1396 "source/version" format.] Eventually this can be used to for caching.
1397
1398 =back
1399
1400 Note: Currently the version information is cached; this needs to be
1401 changed before using this function in long lived programs.
1402
1403
1404 =cut
1405 sub max_buggy{
1406      my %param = validate_with(params => \@_,
1407                                spec   => {bug       => {type => SCALAR,
1408                                                         regex => qr/^\d+$/,
1409                                                        },
1410                                           sourceversions => {type => ARRAYREF,
1411                                                              default => [],
1412                                                             },
1413                                           found          => {type => ARRAYREF,
1414                                                              default => [],
1415                                                             },
1416                                           fixed          => {type => ARRAYREF,
1417                                                              default => [],
1418                                                             },
1419                                           package        => {type => SCALAR,
1420                                                             },
1421                                           version_cache  => {type => HASHREF,
1422                                                              default => {},
1423                                                             },
1424                                          },
1425                               );
1426      # Resolve bugginess states (we might be looking at multiple
1427      # architectures, say). Found wins, then fixed, then absent.
1428      my $maxbuggy = 'absent';
1429      for my $package (split /\s*,\s*/, $param{package}) {
1430           for my $version (@{$param{sourceversions}}) {
1431                my $buggy = buggy(bug => $param{bug},
1432                                  version => $version,
1433                                  found => $param{found},
1434                                  fixed => $param{fixed},
1435                                  version_cache => $param{version_cache},
1436                                  package => $package,
1437                                 );
1438                if ($buggy eq 'found') {
1439                     return 'found';
1440                } elsif ($buggy eq 'fixed') {
1441                     $maxbuggy = 'fixed';
1442                }
1443           }
1444      }
1445      return $maxbuggy;
1446 }
1447
1448
1449 =head2 buggy
1450
1451      buggy(bug => nnn,
1452            found => \@found,
1453            fixed => \@fixed,
1454            package => 'foo',
1455            version => '1.0',
1456           );
1457
1458 Returns the output of Debbugs::Versions::buggy for a particular
1459 package, version and found/fixed set. Automatically turns found, fixed
1460 and version into source/version strings.
1461
1462 Caching can be had by using the version_cache, but no attempt to check
1463 to see if the on disk information is more recent than the cache is
1464 made. [This will need to be fixed for long-lived processes.]
1465
1466 =cut
1467
1468 sub buggy {
1469      my %param = validate_with(params => \@_,
1470                                spec   => {bug => {type => SCALAR,
1471                                                   regex => qr/^\d+$/,
1472                                                  },
1473                                           found => {type => ARRAYREF,
1474                                                     default => [],
1475                                                    },
1476                                           fixed => {type => ARRAYREF,
1477                                                     default => [],
1478                                                    },
1479                                           version_cache => {type => HASHREF,
1480                                                             optional => 1,
1481                                                            },
1482                                           package => {type => SCALAR,
1483                                                      },
1484                                           version => {type => SCALAR,
1485                                                      },
1486                                          },
1487                               );
1488      my @found = @{$param{found}};
1489      my @fixed = @{$param{fixed}};
1490      if (grep {$_ !~ m{/}} (@{$param{found}}, @{$param{fixed}})) {
1491           # We have non-source version versions
1492           @found = makesourceversions($param{package},undef,
1493                                       @found
1494                                      );
1495           @fixed = makesourceversions($param{package},undef,
1496                                       @fixed
1497                                      );
1498      }
1499      if ($param{version} !~ m{/}) {
1500           my ($version) = makesourceversions($param{package},undef,
1501                                              $param{version}
1502                                             );
1503           $param{version} = $version if defined $version;
1504      }
1505      # Figure out which source packages we need
1506      my %sources;
1507      @sources{map {m{(.+)/}; $1} @found} = (1) x @found;
1508      @sources{map {m{(.+)/}; $1} @fixed} = (1) x @fixed;
1509      @sources{map {m{(.+)/}; $1} $param{version}} = 1 if
1510           $param{version} =~ m{/};
1511      my $version;
1512      if (not defined $param{version_cache} or
1513          not exists $param{version_cache}{join(',',sort keys %sources)}) {
1514           $version = Debbugs::Versions->new(\&Debbugs::Versions::Dpkg::vercmp);
1515           foreach my $source (keys %sources) {
1516                my $srchash = substr $source, 0, 1;
1517                my $version_fh = IO::File->new("$config{version_packages_dir}/$srchash/$source", 'r');
1518                if (not defined $version_fh) {
1519                     # We only want to warn if it's a package which actually has a maintainer
1520                     my $maints = getmaintainers();
1521                     next if not exists $maints->{$source};
1522                     warn "Bug $param{bug}: unable to open $config{version_packages_dir}/$srchash/$source: $!";
1523                     next;
1524                }
1525                $version->load($version_fh);
1526           }
1527           if (defined $param{version_cache}) {
1528                $param{version_cache}{join(',',sort keys %sources)} = $version;
1529           }
1530      }
1531      else {
1532           $version = $param{version_cache}{join(',',sort keys %sources)};
1533      }
1534      return $version->buggy($param{version},\@found,\@fixed);
1535 }
1536
1537 sub isstrongseverity {
1538     my $severity = shift;
1539     $severity = $config{default_severity} if
1540          not defined $severity or $severity eq '';
1541     return grep { $_ eq $severity } @{$config{strong_severities}};
1542 }
1543
1544
1545 =head1 PRIVATE FUNCTIONS
1546
1547 =cut
1548
1549 sub update_realtime {
1550         my ($file, %bugs) = @_;
1551
1552         # update realtime index.db
1553
1554         return () unless keys %bugs;
1555         my $idx_old = IO::File->new($file,'r')
1556              or die "Couldn't open ${file}: $!";
1557         my $idx_new = IO::File->new($file.'.new','w')
1558              or die "Couldn't open ${file}.new: $!";
1559
1560         my $min_bug = min(keys %bugs);
1561         my $line;
1562         my @line;
1563         my %changed_bugs;
1564         while($line = <$idx_old>) {
1565              @line = split /\s/, $line;
1566              # Two cases; replacing existing line or adding new line
1567              if (exists $bugs{$line[1]}) {
1568                   my $new = $bugs{$line[1]};
1569                   delete $bugs{$line[1]};
1570                   $min_bug = min(keys %bugs);
1571                   if ($new eq "NOCHANGE") {
1572                        print {$idx_new} $line;
1573                        $changed_bugs{$line[1]} = $line;
1574                   } elsif ($new eq "REMOVE") {
1575                        $changed_bugs{$line[1]} = $line;
1576                   } else {
1577                        print {$idx_new} $new;
1578                        $changed_bugs{$line[1]} = $line;
1579                   }
1580              }
1581              else {
1582                   while ($line[1] > $min_bug) {
1583                        print {$idx_new} $bugs{$min_bug};
1584                        delete $bugs{$min_bug};
1585                        last unless keys %bugs;
1586                        $min_bug = min(keys %bugs);
1587                   }
1588                   print {$idx_new} $line;
1589              }
1590              last unless keys %bugs;
1591         }
1592         print {$idx_new} map {$bugs{$_}} sort keys %bugs;
1593
1594         print {$idx_new} <$idx_old>;
1595
1596         close($idx_new);
1597         close($idx_old);
1598
1599         rename("$file.new", $file);
1600
1601         return %changed_bugs;
1602 }
1603
1604 sub bughook_archive {
1605         my @refs = @_;
1606         filelock("$config{spool_dir}/debbugs.trace.lock");
1607         appendfile("$config{spool_dir}/debbugs.trace","archive ".join(',',@refs)."\n");
1608         my %bugs = update_realtime("$config{spool_dir}/index.db.realtime",
1609                                    map{($_,'REMOVE')} @refs);
1610         update_realtime("$config{spool_dir}/index.archive.realtime",
1611                         %bugs);
1612         unfilelock();
1613 }
1614
1615 sub bughook {
1616         my ( $type, %bugs_temp ) = @_;
1617         filelock("$config{spool_dir}/debbugs.trace.lock");
1618
1619         my %bugs;
1620         for my $bug (keys %bugs_temp) {
1621              my $data = $bugs_temp{$bug};
1622              appendfile("$config{spool_dir}/debbugs.trace","$type $bug\n",makestatus($data, 1));
1623
1624              my $whendone = "open";
1625              my $severity = $config{default_severity};
1626              (my $pkglist = $data->{package}) =~ s/[,\s]+/,/g;
1627              $pkglist =~ s/^,+//;
1628              $pkglist =~ s/,+$//;
1629              $whendone = "forwarded" if defined $data->{forwarded} and length $data->{forwarded};
1630              $whendone = "done" if defined $data->{done} and length $data->{done};
1631              $severity = $data->{severity} if length $data->{severity};
1632
1633              my $k = sprintf "%s %d %d %s [%s] %s %s\n",
1634                   $pkglist, $bug, $data->{date}, $whendone,
1635                        $data->{originator}, $severity, $data->{keywords};
1636              $bugs{$bug} = $k;
1637         }
1638         update_realtime("$config{spool_dir}/index.db.realtime", %bugs);
1639
1640         unfilelock();
1641 }
1642
1643
1644 1;
1645
1646 __END__