2 # $Id: errorlib.in,v 1.52 2005/10/06 03:46:13 ajt Exp $
5 use Debbugs::MIME qw(decode_rfc1522 encode_rfc1522);
8 sub F_SETLK { 6; } sub F_WRLCK{ 1; }
9 $flockstruct= 'sslll'; # And there ought to be something for this too.
12 return "" if ( $_[ 0 ] < 0 );
13 return sprintf "%02d", $_[ 0 ] % 100;
16 sub unlockreadbugmerge {
18 &unfilelock if $rv >= 2;
19 &unfilelock if $rv >= 1;
22 sub lockreadbugmerge {
23 local ($lref, $location) = @_;
25 if (!($data = &lockreadbug($lref, $location))) { return ( 0, undef ); }
26 if (!length($data->{mergedwith})) { return ( 1, $data ); }
28 &filelock('lock/merge');
29 if (!&lockreadbug($lref, $location)) { &unfilelock; return ( 0, undef ); }
34 my ( $bugnum, $ext ) = @_;
35 my $archdir = sprintf "%02d", $bugnum % 100;
36 return 'archive' if ( -r "$gSpoolDir/archive/$archdir/$bugnum.$ext" );
37 return 'db-h' if ( -r "$gSpoolDir/db-h/$archdir/$bugnum.$ext" );
38 return 'db' if ( -r "$gSpoolDir/db/$bugnum.$ext" );
44 if ($location eq 'archive') {
45 return "$gSpoolDir/archive";
46 } elsif ($location eq 'db') {
47 return "$gSpoolDir/db";
49 return "$gSpoolDir/db-h";
54 my ($bugnum, $ext, $location) = @_;
56 unless (defined $location) {
57 $location = getbuglocation($bugnum, $ext);
58 # Default to non-archived bugs only for now; CGI scripts want
59 # archived bugs but most of the backend scripts don't. For now,
60 # anything that is prepared to accept archived bugs should call
61 # getbuglocation() directly first.
62 return undef if defined $location and
63 ($location ne 'db' and $location ne 'db-h');
65 my $dir = getlocationpath($location);
66 return undef unless $dir;
67 if ($location eq 'db') {
68 return "$dir/$bugnum.$ext";
70 my $hash = get_hashname($bugnum);
71 return "$dir/$hash/$bugnum.$ext";
75 my @v1fieldorder = qw(originator date subject msgid package
76 keywords done forwarded mergedwith severity);
78 my %fields = (originator => 'submitter',
81 msgid => 'message-id',
82 'package' => 'package',
85 forwarded => 'forwarded-to',
86 mergedwith => 'merged-with',
87 severity => 'severity',
89 found_versions => 'found-in',
90 fixed_versions => 'fixed-in',
92 blockedby => 'blocked-by',
95 # Fields which need to be RFC1522-decoded in format versions earlier than 3.
96 my @rfc1522_fields = qw(originator subject done forwarded owner);
99 my ($lref, $location) = @_;
100 my $status = getbugcomponent($lref, 'summary', $location);
101 return undef unless defined $status;
102 if (!open(S,$status)) { return undef; }
112 $version = $1 if /^Format-Version: ([0-9]+)/i;
115 # Version 3 is the latest format version currently supported.
116 return undef if $version > 3;
118 my %namemap = reverse %fields;
119 for my $line (@lines) {
120 if ($line =~ /(\S+?): (.*)/) {
121 my ($name, $value) = (lc $1, $2);
122 $data{$namemap{$name}} = $value if exists $namemap{$name};
125 for my $field (keys %fields) {
126 $data{$field} = '' unless exists $data{$field};
131 $data{severity} = $gDefaultSeverity if $data{severity} eq '';
132 $data{found_versions} = [split ' ', $data{found_versions}];
133 $data{fixed_versions} = [split ' ', $data{fixed_versions}];
136 for my $field (@rfc1522_fields) {
137 $data{$field} = decode_rfc1522($data{$field});
145 local ($lref, $location) = @_;
146 &filelock("lock/$lref");
147 my $data = readbug($lref, $location);
148 &unfilelock unless defined $data;
155 $version = 2 unless defined $version;
157 local $data->{found_versions} = join ' ', @{$data->{found_versions}};
158 local $data->{fixed_versions} = join ' ', @{$data->{fixed_versions}};
162 my %newdata = %$data;
164 for my $field (@rfc1522_fields) {
165 $newdata{$field} = encode_rfc1522($newdata{$field});
170 for my $field (@v1fieldorder) {
171 if (exists $newdata{$field}) {
172 $contents .= "$newdata{$field}\n";
177 } elsif ($version == 2 or $version == 3) {
178 # Version 2 or 3. Add a file format version number for the sake of
179 # further extensibility in the future.
180 $contents .= "Format-Version: $version\n";
181 for my $field (keys %fields) {
182 if (exists $newdata{$field} and $newdata{$field} ne '') {
183 # Output field names in proper case, e.g. 'Merged-With'.
184 my $properfield = $fields{$field};
185 $properfield =~ s/(?:^|(?<=-))([a-z])/\u$1/g;
186 $contents .= "$properfield: $newdata{$field}\n";
195 my ($ref, $data, $location, $minversion, $disablebughook) = @_;
198 my %outputs = (1 => 'status', 2 => 'summary');
199 for my $version (keys %outputs) {
200 next if defined $minversion and $version < $minversion;
201 my $status = getbugcomponent($ref, $outputs{$version}, $location);
202 &quit("can't find location for $ref") unless defined $status;
203 open(S,"> $status.new") || &quit("opening $status.new: $!");
204 print(S makestatus($data, $version)) ||
205 &quit("writing $status.new: $!");
206 close(S) || &quit("closing $status.new: $!");
212 rename("$status.new",$status) || &quit("installing new $status: $!");
215 # $disablebughook is a bit of a hack to let format migration scripts use
216 # this function rather than having to duplicate it themselves.
217 &bughook($change,$ref,$data) unless $disablebughook;
226 # NB - NOT COMPATIBLE WITH `with-lock'
227 local ($lockfile,$flockpushno,$evalstring,$count,$errors,@s1,@s2) = @_;
228 $flockpushno= $#filelocks+1;
229 $count= 10; $errors= '';
232 open(FLOCK${flockpushno},\"> \$lockfile\") || die \"open: \$!\";
233 \$flockwant= pack(\$flockstruct,&F_WRLCK,0,0,1,0);".
235 fcntl(FLOCK$flockpushno,&F_SETLK,\$flockwant) || die \"setlk: \$!\";" : "
236 \$z= syscall(&SYS_fcntl,fileno(FLOCK$flockpushno),&F_SETLK,\$flockwant) < 0
237 && die \"syscall fcntl setlk: \$!\";") ."
238 (\@s1= lstat(\$lockfile)) || die \"lstat: \$!\";
239 (\@s2= stat(FLOCK$flockpushno)) || die \"fstat: \$!\";
240 join(',',\@s1) eq join(',',\@s2) || die \"file switched\";
243 last if eval $evalstring;
245 eval "close(FLOCK$flockpushno);";
248 &quit("failed to get lock on file $lockfile: $errors // $evalstring");
252 push(@cleanups,'unfilelock');
253 push(@filelocks,$lockfile);
257 if (@filelocks == 0) {
258 warn "unfilelock called with no active filelocks!\n";
261 local ($lockfile) = pop(@filelocks);
263 eval 'close(FLOCK'.($#filelocks+1).');' || warn "failed to close lock file $lockfile: $!";
264 unlink($lockfile) || warn "failed to remove lock file $lockfile: $!";
267 sub addfoundversions {
271 my $isbinary = shift;
272 return unless defined $version;
273 undef $package if $package =~ m[(?:\s|/)];
274 my $source = $package;
276 if (defined $package and $isbinary) {
277 my @srcinfo = binarytosource($package, $version, undef);
279 # We know the source package(s). Use a fully-qualified version.
280 addfoundversions($data, $_->[0], $_->[1], '') foreach @srcinfo;
283 # Otherwise, an unqualified version will have to do.
287 # Strip off various kinds of brain-damage.
289 $version =~ s/ *\(.*\)//;
290 $version =~ s/ +[A-Za-z].*//;
292 foreach my $ver (split /[,\s]+/, $version) {
293 my $sver = defined($source) ? "$source/$ver" : '';
294 unless (grep { $_ eq $ver or $_ eq $sver } @{$data->{found_versions}}) {
295 push @{$data->{found_versions}}, defined($source) ? $sver : $ver;
297 @{$data->{fixed_versions}} =
298 grep { $_ ne $ver and $_ ne $sver } @{$data->{fixed_versions}};
302 =head2 removefoundversions
304 removefoundversions($data,$package,$versiontoremove)
306 Removes found versions from $data
308 If a version is fully qualified (contains /) only versions matching
309 exactly are removed. Otherwise, all versions matching the version
312 Currently $package and $isbinary are entirely ignored, but accepted
313 for backwards compatibilty.
317 sub removefoundversions {
321 my $isbinary = shift;
322 return unless defined $version;
324 foreach my $ver (split /[,\s]+/, $version) {
326 # fully qualified version
327 @{$data->{found_versions}} =
329 @{$data->{found_versions}};
332 # non qualified version; delete all matchers
333 @{$data->{found_versions}} =
334 grep {$_ !~ m[(?:^|/)\Q$ver\E$]}
335 @{$data->{found_versions}};
340 sub addfixedversions {
344 my $isbinary = shift;
345 return unless defined $version;
346 undef $package if $package =~ m[(?:\s|/)];
347 my $source = $package;
349 if (defined $package and $isbinary) {
350 my @srcinfo = binarytosource($package, $version, undef);
352 # We know the source package(s). Use a fully-qualified version.
353 addfixedversions($data, $_->[0], $_->[1], '') foreach @srcinfo;
356 # Otherwise, an unqualified version will have to do.
360 # Strip off various kinds of brain-damage.
362 $version =~ s/ *\(.*\)//;
363 $version =~ s/ +[A-Za-z].*//;
365 foreach my $ver (split /[,\s]+/, $version) {
366 my $sver = defined($source) ? "$source/$ver" : '';
367 unless (grep { $_ eq $ver or $_ eq $sver } @{$data->{fixed_versions}}) {
368 push @{$data->{fixed_versions}}, defined($source) ? $sver : $ver;
370 @{$data->{found_versions}} =
371 grep { $_ ne $ver and $_ ne $sver } @{$data->{found_versions}};
375 sub removefixedversions {
379 my $isbinary = shift;
380 return unless defined $version;
381 undef $package if $package =~ m[(?:\s|/)];
382 my $source = $package;
384 if (defined $package and $isbinary) {
385 my @srcinfo = binarytosource($package, $version, undef);
387 # We know the source package(s). Use a fully-qualified version.
388 removefixedversions($data, $_->[0], $_->[1], '') foreach @srcinfo;
391 # Otherwise, an unqualified version will have to do.
395 foreach my $ver (split /[,\s]+/, $version) {
396 my $sver = defined($source) ? "$source/$ver" : '';
397 @{$data->{fixed_versions}} =
398 grep { $_ ne $ver and $_ ne $sver } @{$data->{fixed_versions}};
403 print DEBUG "quitting >$_[0]<\n";
405 while ($u= $cleanups[$#cleanups]) { &$u; }
409 %saniarray= ('<','lt', '>','gt', '&','amp', '"','quot');
414 while ($in =~ m/[<>&"]/) {
415 $out.= $`. '&'. $saniarray{$&}. ';';
422 sub update_realtime {
423 my ($file, $bug, $new) = @_;
425 # update realtime index.db
427 open(IDXDB, "<$file") or die "Couldn't open $file";
428 open(IDXNEW, ">$file.new");
432 while($line = <IDXDB>) {
433 @line = split /\s/, $line;
434 last if ($line[1] >= $bug);
439 if ($new eq "NOCHANGE") {
440 print IDXNEW $line if ($line ne "" && $line[1] == $ref);
441 } elsif ($new eq "REMOVE") {
446 if ($line ne "" && $line[1] > $bug) {
451 print IDXNEW while(<IDXDB>);
456 rename("$file.new", $file);
461 sub bughook_archive {
463 &filelock("debbugs.trace.lock");
464 &appendfile("debbugs.trace","archive $ref\n");
465 my $line = update_realtime(
466 "$gSpoolDir/index.db.realtime",
469 update_realtime("$gSpoolDir/index.archive.realtime",
475 my ( $type, $ref, $data ) = @_;
476 &filelock("debbugs.trace.lock");
478 &appendfile("debbugs.trace","$type $ref\n",makestatus($data, 1));
480 my $whendone = "open";
481 my $severity = $gDefaultSeverity;
482 (my $pkglist = $data->{package}) =~ s/[,\s]+/,/g;
485 $whendone = "forwarded" if length $data->{forwarded};
486 $whendone = "done" if length $data->{done};
487 $severity = $data->{severity} if length $data->{severity};
489 my $k = sprintf "%s %d %d %s [%s] %s %s\n",
490 $pkglist, $ref, $data->{date}, $whendone,
491 $data->{originator}, $severity, $data->{keywords};
493 update_realtime("$gSpoolDir/index.db.realtime", $ref, $k);
500 if (!open(AP,">>$file")) {
501 print DEBUG "failed open log<\n";
502 print DEBUG "failed open log err $!<\n";
503 &quit("opening $file (appendfile): $!");
505 print(AP @_) || &quit("writing $file (appendfile): $!");
506 close(AP) || &quit("closing $file (appendfile): $!");
511 my $type = $entity->effective_type;
512 if ($type =~ m#text/(?!html|enriched)# or
513 $type eq 'application/pgp') {
514 return $entity->bodyhandle;
515 } elsif ($type eq 'multipart/alternative') {
516 # RFC 2046 says we should use the last part we recognize.
517 for my $part (reverse $entity->parts) {
518 my $ret = getmailbody($part);
522 # For other multipart types, we just pretend they're
523 # multipart/mixed and run through in order.
524 for my $part ($entity->parts) {
525 my $ret = getmailbody($part);
534 map { $_->address() }
535 map { Mail::Address->parse($_) } @_;
540 map { s/^([\01-\07\030])/\030$1/gm } @log;
544 sub isstrongseverity {
545 my $severity = shift;
546 $severity = $gDefaultSeverity if $severity eq '';
547 return grep { $_ eq $severity } @gStrongSeverities;
551 @severities= grep { not exists $gObsoleteSeverities{$_} } @gSeverityList;
552 @showseverities= @severities;
553 grep ($_= $_ eq '' ? $gDefaultSeverity : $_, @showseverities);
554 %displayshowseverities= %gSeverityDisplay;
557 if (defined $gFowardList and not defined $gForwardList) {
558 $gForwardList = $gFowardList;