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 sub removefoundversions {
306 my $isbinary = shift;
307 return unless defined $version;
308 undef $package if $package =~ m[(?:\s|/)];
309 my $source = $package;
311 if (defined $package and $isbinary) {
312 my @srcinfo = binarytosource($package, $version, undef);
314 # We know the source package(s). Use a fully-qualified version.
315 removefoundversions($data, $_->[0], $_->[1], '') foreach @srcinfo;
318 # Otherwise, an unqualified version will have to do.
322 foreach my $ver (split /[,\s]+/, $version) {
323 my $sver = defined($source) ? "$source/$ver" : '';
324 @{$data->{found_versions}} =
325 grep { $_ ne $ver and $_ ne $sver } @{$data->{found_versions}};
329 sub addfixedversions {
333 my $isbinary = shift;
334 return unless defined $version;
335 undef $package if $package =~ m[(?:\s|/)];
336 my $source = $package;
338 if (defined $package and $isbinary) {
339 my @srcinfo = binarytosource($package, $version, undef);
341 # We know the source package(s). Use a fully-qualified version.
342 addfixedversions($data, $_->[0], $_->[1], '') foreach @srcinfo;
345 # Otherwise, an unqualified version will have to do.
349 # Strip off various kinds of brain-damage.
351 $version =~ s/ *\(.*\)//;
352 $version =~ s/ +[A-Za-z].*//;
354 foreach my $ver (split /[,\s]+/, $version) {
355 my $sver = defined($source) ? "$source/$ver" : '';
356 unless (grep { $_ eq $ver or $_ eq $sver } @{$data->{fixed_versions}}) {
357 push @{$data->{fixed_versions}}, defined($source) ? $sver : $ver;
359 @{$data->{found_versions}} =
360 grep { $_ ne $ver and $_ ne $sver } @{$data->{found_versions}};
364 sub removefixedversions {
368 my $isbinary = shift;
369 return unless defined $version;
370 undef $package if $package =~ m[(?:\s|/)];
371 my $source = $package;
373 if (defined $package and $isbinary) {
374 my @srcinfo = binarytosource($package, $version, undef);
376 # We know the source package(s). Use a fully-qualified version.
377 removefixedversions($data, $_->[0], $_->[1], '') foreach @srcinfo;
380 # Otherwise, an unqualified version will have to do.
384 foreach my $ver (split /[,\s]+/, $version) {
385 my $sver = defined($source) ? "$source/$ver" : '';
386 @{$data->{fixed_versions}} =
387 grep { $_ ne $ver and $_ ne $sver } @{$data->{fixed_versions}};
392 print DEBUG "quitting >$_[0]<\n";
394 while ($u= $cleanups[$#cleanups]) { &$u; }
398 %saniarray= ('<','lt', '>','gt', '&','amp', '"','quot');
403 while ($in =~ m/[<>&"]/) {
404 $out.= $`. '&'. $saniarray{$&}. ';';
411 sub update_realtime {
412 my ($file, $bug, $new) = @_;
414 # update realtime index.db
416 open(IDXDB, "<$file") or die "Couldn't open $file";
417 open(IDXNEW, ">$file.new");
421 while($line = <IDXDB>) {
422 @line = split /\s/, $line;
423 last if ($line[1] >= $bug);
428 if ($new eq "NOCHANGE") {
429 print IDXNEW $line if ($line ne "" && $line[1] == $ref);
430 } elsif ($new eq "REMOVE") {
435 if ($line ne "" && $line[1] > $bug) {
440 print IDXNEW while(<IDXDB>);
445 rename("$file.new", $file);
450 sub bughook_archive {
452 &filelock("debbugs.trace.lock");
453 &appendfile("debbugs.trace","archive $ref\n");
454 my $line = update_realtime(
455 "$gSpoolDir/index.db.realtime",
458 update_realtime("$gSpoolDir/index.archive.realtime",
464 my ( $type, $ref, $data ) = @_;
465 &filelock("debbugs.trace.lock");
467 &appendfile("debbugs.trace","$type $ref\n",makestatus($data, 1));
469 my $whendone = "open";
470 my $severity = $gDefaultSeverity;
471 (my $pkglist = $data->{package}) =~ s/[,\s]+/,/g;
474 $whendone = "forwarded" if length $data->{forwarded};
475 $whendone = "done" if length $data->{done};
476 $severity = $data->{severity} if length $data->{severity};
478 my $k = sprintf "%s %d %d %s [%s] %s %s\n",
479 $pkglist, $ref, $data->{date}, $whendone,
480 $data->{originator}, $severity, $data->{keywords};
482 update_realtime("$gSpoolDir/index.db.realtime", $ref, $k);
489 if (!open(AP,">>$file")) {
490 print DEBUG "failed open log<\n";
491 print DEBUG "failed open log err $!<\n";
492 &quit("opening $file (appendfile): $!");
494 print(AP @_) || &quit("writing $file (appendfile): $!");
495 close(AP) || &quit("closing $file (appendfile): $!");
500 my $type = $entity->effective_type;
501 if ($type =~ m#text/(?!html|enriched)# or
502 $type eq 'application/pgp') {
503 return $entity->bodyhandle;
504 } elsif ($type eq 'multipart/alternative') {
505 # RFC 2046 says we should use the last part we recognize.
506 for my $part (reverse $entity->parts) {
507 my $ret = getmailbody($part);
511 # For other multipart types, we just pretend they're
512 # multipart/mixed and run through in order.
513 for my $part ($entity->parts) {
514 my $ret = getmailbody($part);
523 map { $_->address() }
524 map { Mail::Address->parse($_) } @_;
529 map { s/^([\01-\07\030])/\030$1/gm } @log;
533 sub isstrongseverity {
534 my $severity = shift;
535 $severity = $gDefaultSeverity if $severity eq '';
536 return grep { $_ eq $severity } @gStrongSeverities;
540 @severities= grep { not exists $gObsoleteSeverities{$_} } @gSeverityList;
541 @showseverities= @severities;
542 grep ($_= $_ eq '' ? $gDefaultSeverity : $_, @showseverities);
543 %displayshowseverities= %gSeverityDisplay;
546 if (defined $gFowardList and not defined $gForwardList) {
547 $gForwardList = $gFowardList;