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.
6 # [Other people have contributed to this file; their copyrights should
8 # Copyright 2007-9 by Don Armstrong <don@donarmstrong.com>.
10 package Debbugs::Status;
14 Debbugs::Status -- Routines for dealing with summary and status files
23 This module is a replacement for the parts of errorlib.pl which write
24 and read status and summary files.
26 It also contains generic routines for returning information about the
27 status of a particular bug
36 use vars qw($VERSION $DEBUG %EXPORT_TAGS @EXPORT_OK @EXPORT);
37 use base qw(Exporter);
39 use Params::Validate qw(validate_with :types);
40 use Debbugs::Common qw(:util :lock :quit :misc);
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;
48 use File::Copy qw(copy);
49 use Encode qw(decode encode is_utf8);
51 use Storable qw(dclone);
52 use List::Util qw(min max);
58 $DEBUG = 0 unless defined $DEBUG;
61 %EXPORT_TAGS = (status => [qw(splitpackages get_bug_status buggy bug_archiveable),
62 qw(isstrongseverity bug_presence split_status_fields),
64 read => [qw(readbug read_bug lockreadbug lockreadbugmerge),
65 qw(lock_read_all_merged_bugs),
67 write => [qw(writebug makestatus unlockwritebug)],
69 versions => [qw(addfoundversions addfixedversions),
70 qw(removefoundversions removefixedversions)
72 hook => [qw(bughook bughook_archive)],
73 indexdb => [qw(generate_index_db_line)],
74 fields => [qw(%fields)],
77 Exporter::export_ok_tags(keys %EXPORT_TAGS);
78 $EXPORT_TAGS{all} = [@EXPORT_OK];
84 readbug($bug_num,$location)
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>
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',
97 msgid => 'message-id',
98 'package' => 'package',
101 forwarded => 'forwarded-to',
102 mergedwith => 'merged-with',
103 severity => 'severity',
105 found_versions => 'found-in',
106 found_date => 'found-date',
107 fixed_versions => 'fixed-in',
108 fixed_date => 'fixed-date',
110 blockedby => 'blocked-by',
111 unarchived => 'unarchived',
112 summary => 'summary',
113 outlook => 'outlook',
114 affects => 'affects',
118 # Fields which need to be RFC1522-decoded in format versions earlier than 3.
119 my @rfc1522_fields = qw(originator subject done forwarded owner);
122 return read_bug(bug => $_[0],
123 (@_ > 1)?(location => $_[1]):()
129 read_bug(bug => $bug_num,
130 location => 'archive',
132 read_bug(summary => 'path/to/bugnum.summary');
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.
142 =item bug -- the bug number
144 =item location -- optional location which is passed to getbugcomponent
146 =item summary -- complete path to the .summary file which will be read
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.
152 =item locks -- hashref of already obtained locks; incremented as new
153 locks are needed, and decremented as locks are released on particular
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.
167 my %param = validate_with(params => \@_,
168 spec => {bug => {type => SCALAR,
172 # negative bugnumbers
175 location => {type => SCALAR|UNDEF,
178 summary => {type => SCALAR,
181 lock => {type => BOOLEAN,
184 locks => {type => HASHREF,
189 die "One of bug or summary must be passed to read_bug"
190 if not exists $param{bug} and not exists $param{summary};
195 if (not defined $param{summary}) {
197 ($lref,$location) = @param{qw(bug location)};
198 if (not defined $location) {
199 $location = getbuglocation($lref,'summary');
200 return undef if not defined $location;
202 $status = getbugcomponent($lref, 'summary', $location);
203 $log = getbugcomponent($lref, 'log' , $location);
204 $report = getbugcomponent($lref, 'report' , $location);
205 return undef unless defined $status;
206 return undef if not -e $status;
209 $status = $param{summary};
212 $log =~ s/\.summary$/.log/;
213 $report =~ s/\.summary$/.report/;
214 ($location) = $status =~ m/(db-h|db|archive)/;
215 ($param{bug}) = $status =~ m/(\d+)\.summary$/;
218 filelock("$config{spool_dir}/lock/$param{bug}",exists $param{locks}?$param{locks}:());
220 my $status_fh = IO::File->new($status, 'r');
221 if (not defined $status_fh) {
222 warn "Unable to open $status for reading: $!";
224 unfilelock(exists $param{locks}?$param{locks}:());
228 binmode($status_fh,':encoding(UTF-8)');
235 while (<$status_fh>) {
238 $version = $1 if /^Format-Version: ([0-9]+)/i;
241 # Version 3 is the latest format version currently supported.
243 warn "Unsupported status version '$version'";
245 unfilelock(exists $param{locks}?$param{locks}:());
250 my %namemap = reverse %fields;
251 for my $line (@lines) {
252 if ($line =~ /(\S+?): (.*)/) {
253 my ($name, $value) = (lc $1, $2);
254 # this is a bit of a hack; we should never, ever have \r
255 # or \n in the fields of status. Kill them off here.
256 # [Eventually, this should be superfluous.]
257 $value =~ s/[\r\n]//g;
258 $data{$namemap{$name}} = $value if exists $namemap{$name};
261 for my $field (keys %fields) {
262 $data{$field} = '' unless exists $data{$field};
265 for my $field (@rfc1522_fields) {
266 $data{$field} = decode_rfc1522($data{$field});
269 $data{severity} = $config{default_severity} if $data{severity} eq '';
270 for my $field (qw(found_versions fixed_versions found_date fixed_date)) {
271 $data{$field} = [split ' ', $data{$field}];
273 for my $field (qw(found fixed)) {
274 # create the found/fixed hashes which indicate when a
275 # particular version was marked found or marked fixed.
276 @{$data{$field}}{@{$data{"${field}_versions"}}} =
277 (('') x (@{$data{"${field}_date"}} - @{$data{"${field}_versions"}}),
278 @{$data{"${field}_date"}});
281 my $status_modified = (stat($status))[9];
282 # Add log last modified time
283 $data{log_modified} = (stat($log))[9];
284 my $report_modified = (stat($report))[9] // $data{log_modified};
285 $data{last_modified} = max($status_modified,$data{log_modified});
286 # if the date isn't set (ancient bug), use the smallest of any of the modified
287 if (not defined $data{date} or not length($data{date})) {
288 $data{date} = min($report_modified,$status_modified,$data{log_modified});
290 $data{location} = $location;
291 $data{archived} = (defined($location) and ($location eq 'archive'))?1:0;
292 $data{bug_num} = $param{bug};
297 =head2 split_status_fields
299 my @data = split_status_fields(@data);
301 Splits splittable status fields (like package, tags, blocks,
302 blockedby, etc.) into arrayrefs (use make_list on these). Keeps the
303 passed @data intact using dclone.
305 In scalar context, returns only the first element of @data.
309 our $ditch_empty = sub{
311 my $splitter = shift @t;
312 return grep {length $_} map {split $splitter} @t;
315 my $ditch_empty_space = sub {return &{$ditch_empty}(' ',@_)};
317 (package => \&splitpackages,
318 affects => \&splitpackages,
319 blocks => $ditch_empty_space,
320 blockedby => $ditch_empty_space,
321 # this isn't strictly correct, but we'll split both of them for
322 # the time being until we ditch all use of keywords everywhere
324 keywords => $ditch_empty_space,
325 tags => $ditch_empty_space,
326 found_versions => $ditch_empty_space,
327 fixed_versions => $ditch_empty_space,
328 mergedwith => $ditch_empty_space,
331 sub split_status_fields {
332 my @data = @{dclone(\@_)};
333 for my $data (@data) {
334 next if not defined $data;
335 croak "Passed an element which is not a hashref to split_status_field".ref($data) if
336 not (ref($data) and ref($data) eq 'HASH');
337 for my $field (keys %{$data}) {
338 next unless defined $data->{$field};
339 if (exists $split_fields{$field}) {
340 next if ref($data->{$field});
342 if (ref($split_fields{$field}) eq 'CODE') {
343 @elements = &{$split_fields{$field}}($data->{$field});
345 elsif (not ref($split_fields{$field}) or
346 UNIVERSAL::isa($split_fields{$field},'Regex')
348 @elements = split $split_fields{$field}, $data->{$field};
350 $data->{$field} = \@elements;
354 return wantarray?@data:$data[0];
357 =head2 join_status_fields
359 my @data = join_status_fields(@data);
361 Handles joining the splitable status fields. (Basically, the inverse
362 of split_status_fields.
364 Primarily called from makestatus, but may be useful for other
365 functions after calling split_status_fields (or for legacy functions
366 if we transition to split fields by default).
370 sub join_status_fields {
377 found_versions => ' ',
378 fixed_versions => ' ',
383 my @data = @{dclone(\@_)};
384 for my $data (@data) {
385 next if not defined $data;
386 croak "Passed an element which is not a hashref to split_status_field: ".
388 if ref($data) ne 'HASH';
389 for my $field (keys %{$data}) {
390 next unless defined $data->{$field};
391 next unless ref($data->{$field}) eq 'ARRAY';
392 next unless exists $join_fields{$field};
393 $data->{$field} = join($join_fields{$field},@{$data->{$field}});
396 return wantarray?@data:$data[0];
402 lockreadbug($bug_num,$location)
404 Performs a filelock, then reads the bug; the bug is unlocked if the
405 return is undefined, otherwise, you need to call unfilelock or
408 See readbug above for information on what this returns
413 my ($lref, $location) = @_;
414 return read_bug(bug => $lref, location => $location, lock => 1);
417 =head2 lockreadbugmerge
419 my ($locks, $data) = lockreadbugmerge($bug_num,$location);
421 Performs a filelock, then reads the bug. If the bug is merged, locks
422 the merge lock. Returns a list of the number of locks and the bug
427 sub lockreadbugmerge {
428 my ($bug_num,$location) = @_;
429 my $data = lockreadbug(@_);
430 if (not defined $data) {
433 if (not length $data->{mergedwith}) {
437 filelock("$config{spool_dir}/lock/merge");
438 $data = lockreadbug(@_);
439 if (not defined $data) {
446 =head2 lock_read_all_merged_bugs
448 my ($locks,@bug_data) = lock_read_all_merged_bugs($bug_num,$location);
450 Performs a filelock, then reads the bug passed. If the bug is merged,
451 locks the merge lock, then reads and locks all of the other merged
452 bugs. Returns a list of the number of locks and the bug data for all
455 Will also return undef if any of the merged bugs failed to be read,
456 even if all of the others were read properly.
460 sub lock_read_all_merged_bugs {
461 my %param = validate_with(params => \@_,
462 spec => {bug => {type => SCALAR,
465 location => {type => SCALAR,
468 locks => {type => HASHREF,
474 my @data = read_bug(bug => $param{bug},
476 exists $param{location} ? (location => $param{location}):(),
477 exists $param{locks} ? (locks => $param{locks}):(),
479 if (not @data or not defined $data[0]) {
483 if (not length $data[0]->{mergedwith}) {
484 return ($locks,@data);
486 unfilelock(exists $param{locks}?$param{locks}:());
488 filelock("$config{spool_dir}/lock/merge",exists $param{locks}?$param{locks}:());
490 @data = read_bug(bug => $param{bug},
492 exists $param{location} ? (location => $param{location}):(),
493 exists $param{locks} ? (locks => $param{locks}):(),
495 if (not @data or not defined $data[0]) {
496 unfilelock(exists $param{locks}?$param{locks}:()); #for merge lock above
501 my @bugs = split / /, $data[0]->{mergedwith};
502 push @bugs, $param{bug};
503 for my $bug (@bugs) {
505 if ($bug != $param{bug}) {
507 read_bug(bug => $bug,
509 exists $param{location} ? (location => $param{location}):(),
510 exists $param{locks} ? (locks => $param{locks}):(),
512 if (not defined $newdata) {
514 unfilelock(exists $param{locks}?$param{locks}:());
517 warn "Unable to read bug: $bug while handling merged bug: $param{bug}";
522 # perform a sanity check to make sure that the merged bugs
523 # are all merged with eachother
524 my $expectmerge= join(' ',grep {$_ != $bug } sort { $a <=> $b } @bugs);
525 if ($newdata->{mergedwith} ne $expectmerge) {
527 unfilelock(exists $param{locks}?$param{locks}:());
529 die "Bug $param{bug} differs from bug $bug: ($newdata->{bug_num}: '$newdata->{mergedwith}') vs. ('$expectmerge') (".join(' ',@bugs).")";
533 return ($locks,@data);
538 my $new_bug_num = new_bug(copy => $data->{bug_num});
540 Creates a new bug and returns the new bug number upon success.
548 validate_with(params => \@_,
549 spec => {copy => {type => SCALAR,
555 filelock("nextnumber.lock");
556 my $nn_fh = IO::File->new("nextnumber",'r') or
557 die "Unable to open nextnuber for reading: $!";
560 ($nn) = $nn =~ m/^(\d+)\n$/ or die "Bad format of nextnumber; is not exactly ".'^\d+\n$';
562 overwritefile("nextnumber",
565 my $nn_hash = get_hashname($nn);
567 my $c_hash = get_hashname($param{copy});
568 for my $file (qw(log status summary report)) {
569 copy("db-h/$c_hash/$param{copy}.$file",
570 "db-h/$nn_hash/${nn}.$file")
574 for my $file (qw(log status summary report)) {
575 overwritefile("db-h/$nn_hash/${nn}.$file",
580 # this probably needs to be munged to do something more elegant
581 # &bughook('new', $clone, $data);
588 my @v1fieldorder = qw(originator date subject msgid package
589 keywords done forwarded mergedwith severity);
593 my $content = makestatus($status,$version)
594 my $content = makestatus($status);
596 Creates the content for a status file based on the $status hashref
599 Really only useful for writebug
601 Currently defaults to version 2 (non-encoded rfc1522 names) but will
602 eventually default to version 3. If you care, you should specify a
608 my ($data,$version) = @_;
609 $version = 3 unless defined $version;
613 my %newdata = %$data;
614 for my $field (qw(found fixed)) {
615 if (exists $newdata{$field}) {
616 $newdata{"${field}_date"} =
617 [map {$newdata{$field}{$_}||''} keys %{$newdata{$field}}];
620 %newdata = %{join_status_fields(\%newdata)};
622 %newdata = encode_utf8_structure(%newdata);
625 for my $field (@rfc1522_fields) {
626 $newdata{$field} = encode_rfc1522($newdata{$field});
630 # this is a bit of a hack; we should never, ever have \r or \n in
631 # the fields of status. Kill them off here. [Eventually, this
632 # should be superfluous.]
633 for my $field (keys %newdata) {
634 $newdata{$field} =~ s/[\r\n]//g if defined $newdata{$field};
638 for my $field (@v1fieldorder) {
639 if (exists $newdata{$field} and defined $newdata{$field}) {
640 $contents .= "$newdata{$field}\n";
645 } elsif ($version == 2 or $version == 3) {
646 # Version 2 or 3. Add a file format version number for the sake of
647 # further extensibility in the future.
648 $contents .= "Format-Version: $version\n";
649 for my $field (keys %fields) {
650 if (exists $newdata{$field} and defined $newdata{$field}
651 and $newdata{$field} ne '') {
652 # Output field names in proper case, e.g. 'Merged-With'.
653 my $properfield = $fields{$field};
654 $properfield =~ s/(?:^|(?<=-))([a-z])/\u$1/g;
655 my $data = $newdata{$field};
656 $contents .= "$properfield: $data\n";
665 writebug($bug_num,$status,$location,$minversion,$disablebughook)
667 Writes the bug status and summary files out.
669 Skips writing out a status file if minversion is 2
671 Does not call bughook if disablebughook is true.
676 my ($ref, $data, $location, $minversion, $disablebughook) = @_;
679 my %outputs = (1 => 'status', 3 => 'summary');
680 for my $version (keys %outputs) {
681 next if defined $minversion and $version < $minversion;
682 my $status = getbugcomponent($ref, $outputs{$version}, $location);
683 die "can't find location for $ref" unless defined $status;
686 open $sfh,">","$status.new" or
687 die "opening $status.new: $!";
690 open $sfh,">","$status.new" or
691 die "opening $status.new: $!";
693 print {$sfh} makestatus($data, $version) or
694 die "writing $status.new: $!";
695 close($sfh) or die "closing $status.new: $!";
701 rename("$status.new",$status) || die "installing new $status: $!";
704 # $disablebughook is a bit of a hack to let format migration scripts use
705 # this function rather than having to duplicate it themselves.
706 &bughook($change,$ref,$data) unless $disablebughook;
709 =head2 unlockwritebug
711 unlockwritebug($bug_num,$status,$location,$minversion,$disablebughook);
713 Writes a bug, then calls unfilelock; see writebug for what these
725 The following functions are exported with the :versions tag
727 =head2 addfoundversions
729 addfoundversions($status,$package,$version,$isbinary);
731 All use of this should be phased out in favor of Debbugs::Control::fixed/found
736 sub addfoundversions {
740 my $isbinary = shift;
741 return unless defined $version;
742 undef $package if $package =~ m[(?:\s|/)];
743 my $source = $package;
744 if ($package =~ s/^src://) {
749 if (defined $package and $isbinary) {
750 my @srcinfo = binary_to_source(binary => $package,
751 version => $version);
753 # We know the source package(s). Use a fully-qualified version.
754 addfoundversions($data, $_->[0], $_->[1], '') foreach @srcinfo;
757 # Otherwise, an unqualified version will have to do.
761 # Strip off various kinds of brain-damage.
763 $version =~ s/ *\(.*\)//;
764 $version =~ s/ +[A-Za-z].*//;
766 foreach my $ver (split /[,\s]+/, $version) {
767 my $sver = defined($source) ? "$source/$ver" : '';
768 unless (grep { $_ eq $ver or $_ eq $sver } @{$data->{found_versions}}) {
769 push @{$data->{found_versions}}, defined($source) ? $sver : $ver;
771 @{$data->{fixed_versions}} =
772 grep { $_ ne $ver and $_ ne $sver } @{$data->{fixed_versions}};
776 =head2 removefoundversions
778 removefoundversions($data,$package,$versiontoremove)
780 Removes found versions from $data
782 If a version is fully qualified (contains /) only versions matching
783 exactly are removed. Otherwise, all versions matching the version
786 Currently $package and $isbinary are entirely ignored, but accepted
787 for backwards compatibility.
791 sub removefoundversions {
795 my $isbinary = shift;
796 return unless defined $version;
798 foreach my $ver (split /[,\s]+/, $version) {
800 # fully qualified version
801 @{$data->{found_versions}} =
803 @{$data->{found_versions}};
806 # non qualified version; delete all matchers
807 @{$data->{found_versions}} =
808 grep {$_ !~ m[(?:^|/)\Q$ver\E$]}
809 @{$data->{found_versions}};
815 sub addfixedversions {
819 my $isbinary = shift;
820 return unless defined $version;
821 undef $package if defined $package and $package =~ m[(?:\s|/)];
822 my $source = $package;
824 if (defined $package and $isbinary) {
825 my @srcinfo = binary_to_source(binary => $package,
826 version => $version);
828 # We know the source package(s). Use a fully-qualified version.
829 addfixedversions($data, $_->[0], $_->[1], '') foreach @srcinfo;
832 # Otherwise, an unqualified version will have to do.
836 # Strip off various kinds of brain-damage.
838 $version =~ s/ *\(.*\)//;
839 $version =~ s/ +[A-Za-z].*//;
841 foreach my $ver (split /[,\s]+/, $version) {
842 my $sver = defined($source) ? "$source/$ver" : '';
843 unless (grep { $_ eq $ver or $_ eq $sver } @{$data->{fixed_versions}}) {
844 push @{$data->{fixed_versions}}, defined($source) ? $sver : $ver;
846 @{$data->{found_versions}} =
847 grep { $_ ne $ver and $_ ne $sver } @{$data->{found_versions}};
851 sub removefixedversions {
855 my $isbinary = shift;
856 return unless defined $version;
858 foreach my $ver (split /[,\s]+/, $version) {
860 # fully qualified version
861 @{$data->{fixed_versions}} =
863 @{$data->{fixed_versions}};
866 # non qualified version; delete all matchers
867 @{$data->{fixed_versions}} =
868 grep {$_ !~ m[(?:^|/)\Q$ver\E$]}
869 @{$data->{fixed_versions}};
880 Split a package string from the status file into a list of package names.
886 return unless defined $pkgs;
887 return grep {length $_} map lc, split /[\s,()?]+/, $pkgs;
891 =head2 bug_archiveable
893 bug_archiveable(bug => $bug_num);
899 =item bug -- bug number (required)
901 =item status -- Status hashref returned by read_bug or get_bug_status (optional)
903 =item version -- Debbugs::Version information (optional)
905 =item days_until -- return days until the bug can be archived
909 Returns 1 if the bug can be archived
910 Returns 0 if the bug cannot be archived
912 If days_until is true, returns the number of days until the bug can be
913 archived, -1 if it cannot be archived. 0 means that the bug can be
914 archived the next time the archiver runs.
916 Returns undef on failure.
920 # This will eventually need to be fixed before we start using mod_perl
921 our $version_cache = {};
923 my %param = validate_with(params => \@_,
924 spec => {bug => {type => SCALAR,
927 status => {type => HASHREF,
930 days_until => {type => BOOLEAN,
933 ignore_time => {type => BOOLEAN,
938 # This is what we return if the bug cannot be archived.
939 my $cannot_archive = $param{days_until}?-1:0;
940 # read the status information
941 my $status = $param{status};
942 if (not exists $param{status} or not defined $status) {
943 $status = read_bug(bug=>$param{bug});
944 if (not defined $status) {
945 print STDERR "Cannot archive $param{bug} because it does not exist\n" if $DEBUG;
949 # Bugs can be archived if they are
951 if (not defined $status->{done} or not length $status->{done}) {
952 print STDERR "Cannot archive $param{bug} because it is not done\n" if $DEBUG;
953 return $cannot_archive
955 # Check to make sure that the bug has none of the unremovable tags set
956 if (@{$config{removal_unremovable_tags}}) {
957 for my $tag (split ' ', ($status->{keywords}||'')) {
958 if (grep {$tag eq $_} @{$config{removal_unremovable_tags}}) {
959 print STDERR "Cannot archive $param{bug} because it has an unremovable tag '$tag'\n" if $DEBUG;
960 return $cannot_archive;
965 # If we just are checking if the bug can be archived, we'll not even bother
966 # checking the versioning information if the bug has been -done for less than 28 days.
967 my $log_file = getbugcomponent($param{bug},'log');
968 if (not defined $log_file) {
969 print STDERR "Cannot archive $param{bug} because the log doesn't exist\n" if $DEBUG;
970 return $cannot_archive;
972 my $max_log_age = max(map {$config{remove_age} - -M $_}
973 $log_file, map {my $log = getbugcomponent($_,'log');
974 defined $log ? ($log) : ();
976 split / /, $status->{mergedwith}
978 if (not $param{days_until} and not $param{ignore_time}
981 print STDERR "Cannot archive $param{bug} because of time\n" if $DEBUG;
982 return $cannot_archive;
984 # At this point, we have to get the versioning information for this bug.
985 # We examine the set of distribution tags. If a bug has no distribution
986 # tags set, we assume a default set, otherwise we use the tags the bug
989 # In cases where we are assuming a default set, if the severity
990 # is strong, we use the strong severity default; otherwise, we
991 # use the normal default.
993 # There must be fixed_versions for us to look at the versioning
995 my $min_fixed_time = time;
996 my $min_archive_days = 0;
997 if (@{$status->{fixed_versions}}) {
999 @dist_tags{@{$config{removal_distribution_tags}}} =
1000 (1) x @{$config{removal_distribution_tags}};
1002 for my $tag (split ' ', ($status->{keywords}||'')) {
1003 next unless exists $config{distribution_aliases}{$tag};
1004 next unless $dist_tags{$config{distribution_aliases}{$tag}};
1005 $dists{$config{distribution_aliases}{$tag}} = 1;
1007 if (not keys %dists) {
1008 if (isstrongseverity($status->{severity})) {
1009 @dists{@{$config{removal_strong_severity_default_distribution_tags}}} =
1010 (1) x @{$config{removal_strong_severity_default_distribution_tags}};
1013 @dists{@{$config{removal_default_distribution_tags}}} =
1014 (1) x @{$config{removal_default_distribution_tags}};
1017 my %source_versions;
1018 my @sourceversions = get_versions(package => $status->{package},
1019 dist => [keys %dists],
1022 @source_versions{@sourceversions} = (1) x @sourceversions;
1023 # If the bug has not been fixed in the versions actually
1024 # distributed, then it cannot be archived.
1025 if ('found' eq max_buggy(bug => $param{bug},
1026 sourceversions => [keys %source_versions],
1027 found => $status->{found_versions},
1028 fixed => $status->{fixed_versions},
1029 version_cache => $version_cache,
1030 package => $status->{package},
1032 print STDERR "Cannot archive $param{bug} because it's found\n" if $DEBUG;
1033 return $cannot_archive;
1035 # Since the bug has at least been fixed in the architectures
1036 # that matters, we check to see how long it has been fixed.
1038 # If $param{ignore_time}, then we should ignore time.
1039 if ($param{ignore_time}) {
1040 return $param{days_until}?0:1;
1043 # To do this, we order the times from most recent to oldest;
1044 # when we come to the first found version, we stop.
1045 # If we run out of versions, we only report the time of the
1047 my %time_versions = get_versions(package => $status->{package},
1048 dist => [keys %dists],
1052 for my $version (sort {$time_versions{$b} <=> $time_versions{$a}} keys %time_versions) {
1053 my $buggy = buggy(bug => $param{bug},
1054 version => $version,
1055 found => $status->{found_versions},
1056 fixed => $status->{fixed_versions},
1057 version_cache => $version_cache,
1058 package => $status->{package},
1060 last if $buggy eq 'found';
1061 $min_fixed_time = min($time_versions{$version},$min_fixed_time);
1063 $min_archive_days = max($min_archive_days,ceil($config{remove_age} - (time - $min_fixed_time)/(60*60*24)))
1064 # if there are no versions in the archive at all, then
1065 # we can archive if enough days have passed
1068 # If $param{ignore_time}, then we should ignore time.
1069 if ($param{ignore_time}) {
1070 return $param{days_until}?0:1;
1072 # 6. at least 28 days have passed since the last action has occured or the bug was closed
1073 my $age = ceil($max_log_age);
1074 if ($age > 0 or $min_archive_days > 0) {
1075 print STDERR "Cannot archive $param{bug} because not enough days have passed\n" if $DEBUG;
1076 return $param{days_until}?max($age,$min_archive_days):0;
1079 return $param{days_until}?0:1;
1084 =head2 get_bug_status
1086 my $status = get_bug_status(bug => $nnn);
1088 my $status = get_bug_status($bug_num)
1094 =item bug -- scalar bug number
1096 =item status -- optional hashref of bug status as returned by readbug
1097 (can be passed to avoid rereading the bug information)
1099 =item bug_index -- optional tied index of bug status infomration;
1100 currently not correctly implemented.
1102 =item version -- optional version(s) to check package status at
1104 =item dist -- optional distribution(s) to check package status at
1106 =item arch -- optional architecture(s) to check package status at
1108 =item bugusertags -- optional hashref of bugusertags
1110 =item sourceversion -- optional arrayref of source/version; overrides
1111 dist, arch, and version. [The entries in this array must be in the
1112 "source/version" format.] Eventually this can be used to for caching.
1114 =item indicatesource -- if true, indicate which source packages this
1115 bug could belong to (or does belong to in the case of bugs assigned to
1116 a source package). Defaults to true.
1120 Note: Currently the version information is cached; this needs to be
1121 changed before using this function in long lived programs.
1125 Currently returns a hashref of status with the following keys.
1129 =item id -- bug number
1131 =item bug_num -- duplicate of id
1133 =item keywords -- tags set on the bug, including usertags if bugusertags passed.
1135 =item tags -- duplicate of keywords
1137 =item package -- name of package that the bug is assigned to
1139 =item severity -- severity of the bug
1141 =item pending -- pending state of the bug; one of following possible
1142 values; values listed later have precedence if multiple conditions are
1147 =item pending -- default state
1149 =item forwarded -- bug has been forwarded
1151 =item pending-fixed -- bug is tagged pending
1153 =item fixed -- bug is tagged fixed
1155 =item absent -- bug does not apply to this distribution/architecture
1157 =item done -- bug is resolved in this distribution/architecture
1161 =item location -- db-h or archive; the location in the filesystem
1163 =item subject -- title of the bug
1165 =item last_modified -- epoch that the bug was last modified
1167 =item date -- epoch that the bug was filed
1169 =item originator -- bug reporter
1171 =item log_modified -- epoch that the log file was last modified
1173 =item msgid -- Message id of the original bug report
1178 Other key/value pairs are returned but are not currently documented here.
1182 sub get_bug_status {
1186 my %param = validate_with(params => \@_,
1187 spec => {bug => {type => SCALAR,
1190 status => {type => HASHREF,
1193 bug_index => {type => OBJECT,
1196 version => {type => SCALAR|ARRAYREF,
1199 dist => {type => SCALAR|ARRAYREF,
1202 arch => {type => SCALAR|ARRAYREF,
1205 bugusertags => {type => HASHREF,
1208 sourceversions => {type => ARRAYREF,
1211 indicatesource => {type => BOOLEAN,
1218 if (defined $param{bug_index} and
1219 exists $param{bug_index}{$param{bug}}) {
1220 %status = %{ $param{bug_index}{$param{bug}} };
1221 $status{pending} = $status{ status };
1222 $status{id} = $param{bug};
1225 if (defined $param{status}) {
1226 %status = %{$param{status}};
1229 my $location = getbuglocation($param{bug}, 'summary');
1230 return {} if not defined $location or not length $location;
1231 %status = %{ readbug( $param{bug}, $location ) };
1233 $status{id} = $param{bug};
1235 if (defined $param{bugusertags}{$param{bug}}) {
1236 $status{keywords} = "" unless defined $status{keywords};
1237 $status{keywords} .= " " unless $status{keywords} eq "";
1238 $status{keywords} .= join(" ", @{$param{bugusertags}{$param{bug}}});
1240 $status{tags} = $status{keywords};
1241 my %tags = map { $_ => 1 } split ' ', $status{tags};
1243 $status{package} = '' if not defined $status{package};
1244 $status{"package"} =~ s/\s*$//;
1246 $status{source} = binary_to_source(binary=>[split /\s*,\s*/, $status{package}],
1250 $status{"package"} = 'unknown' if ($status{"package"} eq '');
1251 $status{"severity"} = 'normal' if (not defined $status{severity} or $status{"severity"} eq '');
1253 $status{"pending"} = 'pending';
1254 $status{"pending"} = 'forwarded' if (length($status{"forwarded"}));
1255 $status{"pending"} = 'pending-fixed' if ($tags{pending});
1256 $status{"pending"} = 'fixed' if ($tags{fixed});
1259 my $presence = bug_presence(status => \%status,
1260 map{(exists $param{$_})?($_,$param{$_}):()}
1261 qw(bug sourceversions arch dist version found fixed package)
1263 if (defined $presence) {
1264 if ($presence eq 'fixed') {
1265 $status{pending} = 'done';
1267 elsif ($presence eq 'absent') {
1268 $status{pending} = 'absent';
1276 my $precence = bug_presence(bug => nnn,
1280 Returns 'found', 'absent', 'fixed' or undef based on whether the bug
1281 is found, absent, fixed, or no information is available in the
1282 distribution (dist) and/or architecture (arch) specified.
1289 =item bug -- scalar bug number
1291 =item status -- optional hashref of bug status as returned by readbug
1292 (can be passed to avoid rereading the bug information)
1294 =item bug_index -- optional tied index of bug status infomration;
1295 currently not correctly implemented.
1297 =item version -- optional version to check package status at
1299 =item dist -- optional distribution to check package status at
1301 =item arch -- optional architecture to check package status at
1303 =item sourceversion -- optional arrayref of source/version; overrides
1304 dist, arch, and version. [The entries in this array must be in the
1305 "source/version" format.] Eventually this can be used to for caching.
1312 my %param = validate_with(params => \@_,
1313 spec => {bug => {type => SCALAR,
1316 status => {type => HASHREF,
1319 version => {type => SCALAR|ARRAYREF,
1322 dist => {type => SCALAR|ARRAYREF,
1325 arch => {type => SCALAR|ARRAYREF,
1328 sourceversions => {type => ARRAYREF,
1334 if (defined $param{status}) {
1335 %status = %{$param{status}};
1338 my $location = getbuglocation($param{bug}, 'summary');
1339 return {} if not length $location;
1340 %status = %{ readbug( $param{bug}, $location ) };
1344 my $pseudo_desc = getpseudodesc();
1345 if (not exists $param{sourceversions}) {
1347 # pseudopackages do not have source versions by definition.
1348 if (exists $pseudo_desc->{$status{package}}) {
1351 elsif (defined $param{version}) {
1352 foreach my $arch (make_list($param{arch})) {
1353 for my $package (split /\s*,\s*/, $status{package}) {
1354 my @temp = makesourceversions($package,
1356 make_list($param{version})
1358 @sourceversions{@temp} = (1) x @temp;
1361 } elsif (defined $param{dist}) {
1362 my %affects_distribution_tags;
1363 @affects_distribution_tags{@{$config{affects_distribution_tags}}} =
1364 (1) x @{$config{affects_distribution_tags}};
1365 my $some_distributions_disallowed = 0;
1366 my %allowed_distributions;
1367 for my $tag (split ' ', ($status{keywords}||'')) {
1368 if (exists $config{distribution_aliases}{$tag} and
1369 exists $affects_distribution_tags{$config{distribution_aliases}{$tag}}) {
1370 $some_distributions_disallowed = 1;
1371 $allowed_distributions{$config{distribution_aliases}{$tag}} = 1;
1373 elsif (exists $affects_distribution_tags{$tag}) {
1374 $some_distributions_disallowed = 1;
1375 $allowed_distributions{$tag} = 1;
1378 my @archs = make_list(exists $param{arch}?$param{arch}:());
1379 GET_SOURCE_VERSIONS:
1380 foreach my $arch (@archs) {
1381 for my $package (split /\s*,\s*/, $status{package}) {
1384 if ($package =~ /^src:(.+)$/) {
1388 foreach my $dist (make_list(exists $param{dist}?$param{dist}:[])) {
1389 # if some distributions are disallowed,
1390 # and this isn't an allowed
1391 # distribution, then we ignore this
1392 # distribution for the purposees of
1394 if ($some_distributions_disallowed and
1395 not exists $allowed_distributions{$dist}) {
1398 push @versions, get_versions(package => $package,
1400 ($source?(arch => 'source'):
1401 (defined $arch?(arch => $arch):())),
1404 next unless @versions;
1405 my @temp = make_source_versions(package => $package,
1407 versions => \@versions,
1409 @sourceversions{@temp} = (1) x @temp;
1412 # this should really be split out into a subroutine,
1413 # but it'd touch so many things currently, that we fake
1414 # it; it's needed to properly handle bugs which are
1415 # erroneously assigned to the binary package, and we'll
1416 # probably have it go away eventually.
1417 if (not keys %sourceversions and (not @archs or defined $archs[0])) {
1419 goto GET_SOURCE_VERSIONS;
1423 # TODO: This should probably be handled further out for efficiency and
1424 # for more ease of distinguishing between pkg= and src= queries.
1425 # DLA: src= queries should just pass arch=source, and they'll be happy.
1426 @sourceversions = keys %sourceversions;
1429 @sourceversions = @{$param{sourceversions}};
1431 my $maxbuggy = 'undef';
1432 if (@sourceversions) {
1433 $maxbuggy = max_buggy(bug => $param{bug},
1434 sourceversions => \@sourceversions,
1435 found => $status{found_versions},
1436 fixed => $status{fixed_versions},
1437 package => $status{package},
1438 version_cache => $version_cache,
1441 elsif (defined $param{dist} and
1442 not exists $pseudo_desc->{$status{package}}) {
1445 if (length($status{done}) and
1446 (not @sourceversions or not @{$status{fixed_versions}})) {
1461 =item bug -- scalar bug number
1463 =item sourceversion -- optional arrayref of source/version; overrides
1464 dist, arch, and version. [The entries in this array must be in the
1465 "source/version" format.] Eventually this can be used to for caching.
1469 Note: Currently the version information is cached; this needs to be
1470 changed before using this function in long lived programs.
1475 my %param = validate_with(params => \@_,
1476 spec => {bug => {type => SCALAR,
1479 sourceversions => {type => ARRAYREF,
1482 found => {type => ARRAYREF,
1485 fixed => {type => ARRAYREF,
1488 package => {type => SCALAR,
1490 version_cache => {type => HASHREF,
1495 # Resolve bugginess states (we might be looking at multiple
1496 # architectures, say). Found wins, then fixed, then absent.
1497 my $maxbuggy = 'absent';
1498 for my $package (split /\s*,\s*/, $param{package}) {
1499 for my $version (@{$param{sourceversions}}) {
1500 my $buggy = buggy(bug => $param{bug},
1501 version => $version,
1502 found => $param{found},
1503 fixed => $param{fixed},
1504 version_cache => $param{version_cache},
1505 package => $package,
1507 if ($buggy eq 'found') {
1509 } elsif ($buggy eq 'fixed') {
1510 $maxbuggy = 'fixed';
1527 Returns the output of Debbugs::Versions::buggy for a particular
1528 package, version and found/fixed set. Automatically turns found, fixed
1529 and version into source/version strings.
1531 Caching can be had by using the version_cache, but no attempt to check
1532 to see if the on disk information is more recent than the cache is
1533 made. [This will need to be fixed for long-lived processes.]
1538 my %param = validate_with(params => \@_,
1539 spec => {bug => {type => SCALAR,
1542 found => {type => ARRAYREF,
1545 fixed => {type => ARRAYREF,
1548 version_cache => {type => HASHREF,
1551 package => {type => SCALAR,
1553 version => {type => SCALAR,
1557 my @found = @{$param{found}};
1558 my @fixed = @{$param{fixed}};
1559 if (grep {$_ !~ m{/}} (@{$param{found}}, @{$param{fixed}})) {
1560 # We have non-source version versions
1561 @found = makesourceversions($param{package},undef,
1564 @fixed = makesourceversions($param{package},undef,
1568 if ($param{version} !~ m{/}) {
1569 my ($version) = makesourceversions($param{package},undef,
1572 $param{version} = $version if defined $version;
1574 # Figure out which source packages we need
1576 @sources{map {m{(.+)/}; $1} @found} = (1) x @found;
1577 @sources{map {m{(.+)/}; $1} @fixed} = (1) x @fixed;
1578 @sources{map {m{(.+)/}; $1} $param{version}} = 1 if
1579 $param{version} =~ m{/};
1581 if (not defined $param{version_cache} or
1582 not exists $param{version_cache}{join(',',sort keys %sources)}) {
1583 $version = Debbugs::Versions->new(\&Debbugs::Versions::Dpkg::vercmp);
1584 foreach my $source (keys %sources) {
1585 my $srchash = substr $source, 0, 1;
1586 my $version_fh = IO::File->new("$config{version_packages_dir}/$srchash/$source", 'r');
1587 if (not defined $version_fh) {
1588 # We only want to warn if it's a package which actually has a maintainer
1589 my $maints = getmaintainers();
1590 next if not exists $maints->{$source};
1591 warn "Bug $param{bug}: unable to open $config{version_packages_dir}/$srchash/$source: $!";
1594 $version->load($version_fh);
1596 if (defined $param{version_cache}) {
1597 $param{version_cache}{join(',',sort keys %sources)} = $version;
1601 $version = $param{version_cache}{join(',',sort keys %sources)};
1603 return $version->buggy($param{version},\@found,\@fixed);
1606 sub isstrongseverity {
1607 my $severity = shift;
1608 $severity = $config{default_severity} if
1609 not defined $severity or $severity eq '';
1610 return grep { $_ eq $severity } @{$config{strong_severities}};
1615 =head2 generate_index_db_line
1617 my $data = read_bug(bug => $bug,
1618 location => $initialdir);
1619 # generate_index_db_line hasn't been written yet at all.
1620 my $line = generate_index_db_line($data);
1622 Returns a line for a bug suitable to be written out to index.db.
1626 sub generate_index_db_line {
1627 my ($data,$bug) = @_;
1629 # just in case someone has given us a split out data
1630 $data = join_status_fields($data);
1632 my $whendone = "open";
1633 my $severity = $config{default_severity};
1634 (my $pkglist = $data->{package}) =~ s/[,\s]+/,/g;
1635 $pkglist =~ s/^,+//;
1636 $pkglist =~ s/,+$//;
1637 $whendone = "forwarded" if defined $data->{forwarded} and length $data->{forwarded};
1638 $whendone = "done" if defined $data->{done} and length $data->{done};
1639 $severity = $data->{severity} if length $data->{severity};
1640 return sprintf "%s %d %d %s [%s] %s %s\n",
1641 $pkglist, $data->{bug_num}//$bug, $data->{date}, $whendone,
1642 $data->{originator}, $severity, $data->{keywords};
1647 =head1 PRIVATE FUNCTIONS
1651 sub update_realtime {
1652 my ($file, %bugs) = @_;
1654 # update realtime index.db
1656 return () unless keys %bugs;
1657 my $idx_old = IO::File->new($file,'r')
1658 or die "Couldn't open ${file}: $!";
1659 my $idx_new = IO::File->new($file.'.new','w')
1660 or die "Couldn't open ${file}.new: $!";
1662 binmode($idx_old,':raw:utf8');
1663 binmode($idx_new,':raw:encoding(UTF-8)');
1664 my $min_bug = min(keys %bugs);
1668 while($line = <$idx_old>) {
1669 @line = split /\s/, $line;
1670 # Two cases; replacing existing line or adding new line
1671 if (exists $bugs{$line[1]}) {
1672 my $new = $bugs{$line[1]};
1673 delete $bugs{$line[1]};
1674 $min_bug = min(keys %bugs);
1675 if ($new eq "NOCHANGE") {
1676 print {$idx_new} $line;
1677 $changed_bugs{$line[1]} = $line;
1678 } elsif ($new eq "REMOVE") {
1679 $changed_bugs{$line[1]} = $line;
1681 print {$idx_new} $new;
1682 $changed_bugs{$line[1]} = $line;
1686 while ($line[1] > $min_bug) {
1687 print {$idx_new} $bugs{$min_bug};
1688 delete $bugs{$min_bug};
1689 last unless keys %bugs;
1690 $min_bug = min(keys %bugs);
1692 print {$idx_new} $line;
1694 last unless keys %bugs;
1696 print {$idx_new} map {$bugs{$_}} sort keys %bugs;
1698 print {$idx_new} <$idx_old>;
1703 rename("$file.new", $file);
1705 return %changed_bugs;
1708 sub bughook_archive {
1710 filelock("$config{spool_dir}/debbugs.trace.lock");
1711 appendfile("$config{spool_dir}/debbugs.trace","archive ".join(',',@refs)."\n");
1712 my %bugs = update_realtime("$config{spool_dir}/index.db.realtime",
1713 map{($_,'REMOVE')} @refs);
1714 update_realtime("$config{spool_dir}/index.archive.realtime",
1720 my ( $type, %bugs_temp ) = @_;
1721 filelock("$config{spool_dir}/debbugs.trace.lock");
1724 for my $bug (keys %bugs_temp) {
1725 my $data = $bugs_temp{$bug};
1726 appendfile("$config{spool_dir}/debbugs.trace","$type $bug\n",makestatus($data, 1));
1728 $bugs{$bug} = generate_index_db_line($data,$bug);
1730 update_realtime("$config{spool_dir}/index.db.realtime", %bugs);