2 package Debbugs::Common;
6 Debbugs::Common -- Common routines for all of Debbugs
10 use Debbugs::Common qw(:url :html);
15 This module is a replacement for the general parts of errorlib.pl.
16 subroutines in errorlib.pl will be gradually phased out and replaced
17 with equivalent (or better) functionality here.
25 use vars qw($VERSION $DEBUG %EXPORT_TAGS @EXPORT_OK @EXPORT);
26 use base qw(Exporter);
30 $DEBUG = 0 unless defined $DEBUG;
33 %EXPORT_TAGS = (util => [qw(getbugcomponent getbuglocation getlocationpath get_hashname),
34 qw(appendfile buglog getparsedaddrs getmaintainers),
35 qw(getmaintainers_reverse)
38 lock => [qw(filelock unfilelock @cleanups)],
41 Exporter::export_ok_tags(qw(lock quit util));
42 $EXPORT_TAGS{all} = [@EXPORT_OK];
45 #use Debbugs::Config qw(:globals);
46 use Debbugs::Config qw(:config);
48 use Debbugs::MIME qw(decode_rfc1522);
54 our $DEBUG_FH = \*STDERR if not defined $DEBUG_FH;
58 The following functions are exported by the C<:util> tag
60 =head2 getbugcomponent
62 my $file = getbugcomponent($bug_number,$extension,$location)
64 Returns the path to the bug file in location C<$location>, bug number
65 C<$bugnumber> and extension C<$extension>
70 my ($bugnum, $ext, $location) = @_;
72 if (not defined $location) {
73 $location = getbuglocation($bugnum, $ext);
74 # Default to non-archived bugs only for now; CGI scripts want
75 # archived bugs but most of the backend scripts don't. For now,
76 # anything that is prepared to accept archived bugs should call
77 # getbuglocation() directly first.
78 return undef if defined $location and
79 ($location ne 'db' and $location ne 'db-h');
81 my $dir = getlocationpath($location);
82 return undef if not defined $dir;
83 if (defined $location and $location eq 'db') {
84 return "$dir/$bugnum.$ext";
86 my $hash = get_hashname($bugnum);
87 return "$dir/$hash/$bugnum.$ext";
93 getbuglocation($bug_number,$extension)
95 Returns the the location in which a particular bug exists; valid
96 locations returned currently are archive, db-h, or db. If the bug does
97 not exist, returns undef.
102 my ($bugnum, $ext) = @_;
103 my $archdir = get_hashname($bugnum);
104 return 'archive' if -r getlocationpath('archive')."/$archdir/$bugnum.$ext";
105 return 'db-h' if -r getlocationpath('db-h')."/$archdir/$bugnum.$ext";
106 return 'db' if -r getlocationpath('db')."/$bugnum.$ext";
111 =head2 getlocationpath
113 getlocationpath($location)
115 Returns the path to a specific location
119 sub getlocationpath {
121 if (defined $location and $location eq 'archive') {
122 return "$config{spool_dir}/archive";
123 } elsif (defined $location and $location eq 'db') {
124 return "$config{spool_dir}/db";
126 return "$config{spool_dir}/db-h";
135 Returns the hash of the bug which is the location within the archive
140 return "" if ( $_[ 0 ] < 0 );
141 return sprintf "%02d", $_[ 0 ] % 100;
148 Returns the path to the logfile corresponding to the bug.
154 my $location = getbuglocation($bugnum, 'log');
155 return getbugcomponent($bugnum, 'log', $location) if ($location);
156 $location = getbuglocation($bugnum, 'log.gz');
157 return getbugcomponent($bugnum, 'log.gz', $location);
163 appendfile($file,'data','to','append');
165 Opens a file for appending and writes data to it.
171 if (!open(AP,">>$file")) {
172 print $DEBUG_FH "failed open log<\n" if $DEBUG;
173 print $DEBUG_FH "failed open log err $!<\n" if $DEBUG;
174 &quit("opening $file (appendfile): $!");
176 print(AP @_) || &quit("writing $file (appendfile): $!");
177 close(AP) || &quit("closing $file (appendfile): $!");
180 =head2 getparsedaddrs
182 my $address = getparsedaddrs($address);
183 my @address = getpasredaddrs($address);
185 Returns the output from Mail::Address->parse, or the cached output if
186 this address has been parsed before. In SCALAR context returns the
187 first address parsed.
195 return () unless defined $addr;
196 return wantarray?@{$_parsedaddrs{$addr}}:$_parsedaddrs{$addr}[0]
197 if exists $_parsedaddrs{$addr};
198 @{$_parsedaddrs{$addr}} = Mail::Address->parse($addr);
199 return wantarray?@{$_parsedaddrs{$addr}}:$_parsedaddrs{$addr}[0];
205 return $_maintainer if $_maintainer;
208 for my $file (@config{qw(maintainer_file maintainer_file_override)}) {
209 next unless defined $file;
210 my $maintfile = new IO::File $file,'r' or
211 &quitcgi("Unable to open $file: $!");
212 while(<$maintfile>) {
213 next unless m/^(\S+)\s+(\S.*\S)\s*$/;
217 for my $maint (map {lc($_->address)} getparsedaddrs($b)) {
218 push @{$maintainer_rev{$maint}},$a;
223 $_maintainer = \%maintainer;
224 $_maintainer_rev = \%maintainer_rev;
227 sub getmaintainers_reverse{
228 return $_maintainer_rev if $_maintainer_rev;
230 return $_maintainer_rev;
236 These functions are exported with the :lock tag
242 FLOCKs the passed file. Use unfilelock to unlock it.
250 # NB - NOT COMPATIBLE WITH `with-lock'
252 if ($lockfile !~ m{^/}) {
253 $lockfile = cwd().'/'.$lockfile;
256 $count= 10; $errors= '';
259 my $fh2 = IO::File->new($lockfile,'w')
260 or die "Unable to open $lockfile for writing: $!";
261 flock($fh2,LOCK_EX|LOCK_NB)
262 or die "Unable to lock $lockfile $!";
269 push @filelocks, {fh => $fh, file => $lockfile};
274 &quit("failed to get lock on $lockfile -- $errors");
278 push(@cleanups,\&unfilelock);
286 Unlocks the file most recently locked.
288 Note that it is not currently possible to unlock a specific file
289 locked with filelock.
294 if (@filelocks == 0) {
295 warn "unfilelock called with no active filelocks!\n";
298 my %fl = %{pop(@filelocks)};
300 flock($fl{fh},LOCK_UN)
301 or warn "Unable to unlock lockfile $fl{file}: $!";
303 or warn "Unable to close lockfile $fl{file}: $!";
305 or warn "Unable to unlink lockfile $fl{file}: $!";
312 These functions are exported with the :quit tag.
318 Exits the program by calling die after running some cleanups.
320 This should be replaced with an END handler which runs the cleanups
321 instead. (Or possibly a die handler, if the cleanups are important)
326 print $DEBUG_FH "quitting >$_[0]<\n" if $DEBUG;
328 while ($u= $cleanups[$#cleanups]) { &$u; }