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)],
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);
55 The following functions are exported by the C<:util> tag
57 =head2 getbugcomponent
59 my $file = getbugcomponent($bug_number,$extension,$location)
61 Returns the path to the bug file in location C<$location>, bug number
62 C<$bugnumber> and extension C<$extension>
67 my ($bugnum, $ext, $location) = @_;
69 if (not defined $location) {
70 $location = getbuglocation($bugnum, $ext);
71 # Default to non-archived bugs only for now; CGI scripts want
72 # archived bugs but most of the backend scripts don't. For now,
73 # anything that is prepared to accept archived bugs should call
74 # getbuglocation() directly first.
75 return undef if defined $location and
76 ($location ne 'db' and $location ne 'db-h');
78 return undef if not defined $location;
79 my $dir = getlocationpath($location);
80 return undef if not defined $dir;
81 if ($location eq 'db') {
82 return "$dir/$bugnum.$ext";
84 my $hash = get_hashname($bugnum);
85 return "$dir/$hash/$bugnum.$ext";
91 getbuglocation($bug_number,$extension)
93 Returns the the location in which a particular bug exists; valid
94 locations returned currently are archive, db-h, or db. If the bug does
95 not exist, returns undef.
100 my ($bugnum, $ext) = @_;
101 my $archdir = get_hashname($bugnum);
102 return 'archive' if -r getlocationpath('archive')."/$archdir/$bugnum.$ext";
103 return 'db-h' if -r getlocationpath('db-h')."/$archdir/$bugnum.$ext";
104 return 'db' if -r getlocationpath('db')."/$bugnum.$ext";
109 =head2 getlocationpath
111 getlocationpath($location)
113 Returns the path to a specific location
117 sub getlocationpath {
119 if (defined $location and $location eq 'archive') {
120 return "$config{spool_dir}/archive";
121 } elsif (defined $location and $location eq 'db') {
122 return "$config{spool_dir}/db";
124 return "$config{spool_dir}/db-h";
133 Returns the hash of the bug which is the location within the archive
138 return "" if ( $_[ 0 ] < 0 );
139 return sprintf "%02d", $_[ 0 ] % 100;
146 Returns the path to the logfile corresponding to the bug.
152 my $location = getbuglocation($bugnum, 'log');
153 return getbugcomponent($bugnum, 'log', $location) if ($location);
154 $location = getbuglocation($bugnum, 'log.gz');
155 return getbugcomponent($bugnum, 'log.gz', $location);
161 appendfile($file,'data','to','append');
163 Opens a file for appending and writes data to it.
169 if (!open(AP,">>$file")) {
170 print DEBUG "failed open log<\n";
171 print DEBUG "failed open log err $!<\n";
172 &quit("opening $file (appendfile): $!");
174 print(AP @_) || &quit("writing $file (appendfile): $!");
175 close(AP) || &quit("closing $file (appendfile): $!");
178 =head2 getparsedaddrs
180 my $address = getparsedaddrs($address);
181 my @address = getpasredaddrs($address);
183 Returns the output from Mail::Address->parse, or the cached output if
184 this address has been parsed before. In SCALAR context returns the
185 first address parsed.
193 return () unless defined $addr;
194 return wantarray?@{$_parsedaddrs{$addr}}:$_parsedaddrs{$addr}[0]
195 if exists $_parsedaddrs{$addr};
196 @{$_parsedaddrs{$addr}} = Mail::Address->parse($addr);
197 return wantarray?@{$_parsedaddrs{$addr}}:$_parsedaddrs{$addr}[0];
203 return $_maintainer if $_maintainer;
206 for my $file (@config{qw(maintainer_file maintainer_file_override)}) {
207 next unless defined $file;
208 my $maintfile = new IO::File $file,'r' or
209 &quitcgi("Unable to open $file: $!");
210 while(<$maintfile>) {
211 next unless m/^(\S+)\s+(\S.*\S)\s*$/;
215 for my $maint (map {lc($_->address)} getparsedaddrs($b)) {
216 push @{$maintainer_rev{$maint}},$a;
221 $_maintainer = \%maintainer;
222 $_maintainer_rev = \%maintainer_rev;
225 sub getmaintainers_reverse{
226 return $_maintainer_rev if $_maintainer_rev;
228 return $_maintainer_rev;
234 These functions are exported with the :lock tag
240 FLOCKs the passed file. Use unfilelock to unlock it.
248 # NB - NOT COMPATIBLE WITH `with-lock'
250 my ($count,$errors) = @_;
251 $count= 10; $errors= '';
254 my $fh = new IO::File $lockfile,'w'
255 or die "Unable to open $lockfile for writing: $!";
256 flock($fh,LOCK_EX|LOCK_NB)
257 or die "Unable to lock $lockfile $!";
264 push @filelocks, {fh => $fh, file => $lockfile};
269 &quit("failed to get lock on $lockfile -- $errors");
273 push(@cleanups,\&unfilelock);
281 Unlocks the file most recently locked.
283 Note that it is not currently possible to unlock a specific file
284 locked with filelock.
289 if (@filelocks == 0) {
290 warn "unfilelock called with no active filelocks!\n";
293 my %fl = %{pop(@filelocks)};
295 flock($fl{fh},LOCK_UN)
296 or warn "Unable to unlock lockfile $fl{file}: $!";
298 or warn "Unable to close lockfile $fl{file}: $!";
300 or warn "Unable to unlink locfile $fl{file}: $!";
307 These functions are exported with the :quit tag.
313 Exits the program by calling die after running some cleanups.
315 This should be replaced with an END handler which runs the cleanups
316 instead. (Or possibly a die handler, if the cleanups are important)
321 print DEBUG "quitting >$_[0]<\n";
323 while ($u= $cleanups[$#cleanups]) { &$u; }