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)
37 misc => [qw(make_list)],
39 lock => [qw(filelock unfilelock @cleanups)],
42 Exporter::export_ok_tags(qw(lock quit util misc));
43 $EXPORT_TAGS{all} = [@EXPORT_OK];
46 #use Debbugs::Config qw(:globals);
47 use Debbugs::Config qw(:config);
49 use Debbugs::MIME qw(decode_rfc1522);
55 our $DEBUG_FH = \*STDERR if not defined $DEBUG_FH;
59 The following functions are exported by the C<:util> tag
61 =head2 getbugcomponent
63 my $file = getbugcomponent($bug_number,$extension,$location)
65 Returns the path to the bug file in location C<$location>, bug number
66 C<$bugnumber> and extension C<$extension>
71 my ($bugnum, $ext, $location) = @_;
73 if (not defined $location) {
74 $location = getbuglocation($bugnum, $ext);
75 # Default to non-archived bugs only for now; CGI scripts want
76 # archived bugs but most of the backend scripts don't. For now,
77 # anything that is prepared to accept archived bugs should call
78 # getbuglocation() directly first.
79 return undef if defined $location and
80 ($location ne 'db' and $location ne 'db-h');
82 my $dir = getlocationpath($location);
83 return undef if not defined $dir;
84 if (defined $location and $location eq 'db') {
85 return "$dir/$bugnum.$ext";
87 my $hash = get_hashname($bugnum);
88 return "$dir/$hash/$bugnum.$ext";
94 getbuglocation($bug_number,$extension)
96 Returns the the location in which a particular bug exists; valid
97 locations returned currently are archive, db-h, or db. If the bug does
98 not exist, returns undef.
103 my ($bugnum, $ext) = @_;
104 my $archdir = get_hashname($bugnum);
105 return 'archive' if -r getlocationpath('archive')."/$archdir/$bugnum.$ext";
106 return 'db-h' if -r getlocationpath('db-h')."/$archdir/$bugnum.$ext";
107 return 'db' if -r getlocationpath('db')."/$bugnum.$ext";
112 =head2 getlocationpath
114 getlocationpath($location)
116 Returns the path to a specific location
120 sub getlocationpath {
122 if (defined $location and $location eq 'archive') {
123 return "$config{spool_dir}/archive";
124 } elsif (defined $location and $location eq 'db') {
125 return "$config{spool_dir}/db";
127 return "$config{spool_dir}/db-h";
136 Returns the hash of the bug which is the location within the archive
141 return "" if ( $_[ 0 ] < 0 );
142 return sprintf "%02d", $_[ 0 ] % 100;
149 Returns the path to the logfile corresponding to the bug.
155 my $location = getbuglocation($bugnum, 'log');
156 return getbugcomponent($bugnum, 'log', $location) if ($location);
157 $location = getbuglocation($bugnum, 'log.gz');
158 return getbugcomponent($bugnum, 'log.gz', $location);
164 appendfile($file,'data','to','append');
166 Opens a file for appending and writes data to it.
172 if (!open(AP,">>$file")) {
173 print $DEBUG_FH "failed open log<\n" if $DEBUG;
174 print $DEBUG_FH "failed open log err $!<\n" if $DEBUG;
175 &quit("opening $file (appendfile): $!");
177 print(AP @_) || &quit("writing $file (appendfile): $!");
178 close(AP) || &quit("closing $file (appendfile): $!");
181 =head2 getparsedaddrs
183 my $address = getparsedaddrs($address);
184 my @address = getpasredaddrs($address);
186 Returns the output from Mail::Address->parse, or the cached output if
187 this address has been parsed before. In SCALAR context returns the
188 first address parsed.
196 return () unless defined $addr;
197 return wantarray?@{$_parsedaddrs{$addr}}:$_parsedaddrs{$addr}[0]
198 if exists $_parsedaddrs{$addr};
199 @{$_parsedaddrs{$addr}} = Mail::Address->parse($addr);
200 return wantarray?@{$_parsedaddrs{$addr}}:$_parsedaddrs{$addr}[0];
206 return $_maintainer if $_maintainer;
209 for my $file (@config{qw(maintainer_file maintainer_file_override)}) {
210 next unless defined $file;
211 my $maintfile = new IO::File $file,'r' or
212 &quitcgi("Unable to open $file: $!");
213 while(<$maintfile>) {
214 next unless m/^(\S+)\s+(\S.*\S)\s*$/;
218 for my $maint (map {lc($_->address)} getparsedaddrs($b)) {
219 push @{$maintainer_rev{$maint}},$a;
224 $_maintainer = \%maintainer;
225 $_maintainer_rev = \%maintainer_rev;
228 sub getmaintainers_reverse{
229 return $_maintainer_rev if $_maintainer_rev;
231 return $_maintainer_rev;
237 These functions are exported with the :lock tag
243 FLOCKs the passed file. Use unfilelock to unlock it.
251 # NB - NOT COMPATIBLE WITH `with-lock'
253 if ($lockfile !~ m{^/}) {
254 $lockfile = cwd().'/'.$lockfile;
257 $count= 10; $errors= '';
260 my $fh2 = IO::File->new($lockfile,'w')
261 or die "Unable to open $lockfile for writing: $!";
262 flock($fh2,LOCK_EX|LOCK_NB)
263 or die "Unable to lock $lockfile $!";
270 push @filelocks, {fh => $fh, file => $lockfile};
275 &quit("failed to get lock on $lockfile -- $errors");
279 push(@cleanups,\&unfilelock);
287 Unlocks the file most recently locked.
289 Note that it is not currently possible to unlock a specific file
290 locked with filelock.
295 if (@filelocks == 0) {
296 warn "unfilelock called with no active filelocks!\n";
299 my %fl = %{pop(@filelocks)};
301 flock($fl{fh},LOCK_UN)
302 or warn "Unable to unlock lockfile $fl{file}: $!";
304 or warn "Unable to close lockfile $fl{file}: $!";
306 or warn "Unable to unlink lockfile $fl{file}: $!";
313 These functions are exported with the :quit tag.
319 Exits the program by calling die after running some cleanups.
321 This should be replaced with an END handler which runs the cleanups
322 instead. (Or possibly a die handler, if the cleanups are important)
327 print $DEBUG_FH "quitting >$_[0]<\n" if $DEBUG;
329 while ($u= $cleanups[$#cleanups]) { &$u; }
335 These functions are exported with the :misc tag
339 LIST = make_list(@_);
341 Turns a scalar or an arrayref into a list; expands a list of arrayrefs
344 That is, make_list([qw(a b c)]); returns qw(a b c); make_list([qw(a
345 b)],[qw(c d)] returns qw(a b c d);
350 return map {(ref($_) eq 'ARRAY')?@{$_}:$_} @_;