Debian Codegraph @

Debbugs/Status.pm

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