1 # This module is part of debbugs, and is released
2 # under the terms of the GPL version 2, or any later
3 # version at your option.
4 # See the file README and COPYING for more information.
6 # [Other people have contributed to this file; their copyrights should
8 # Copyright 2007 by Don Armstrong <don@donarmstrong.com>.
10 package Debbugs::Common;
14 Debbugs::Common -- Common routines for all of Debbugs
18 use Debbugs::Common qw(:url :html);
23 This module is a replacement for the general parts of errorlib.pl.
24 subroutines in errorlib.pl will be gradually phased out and replaced
25 with equivalent (or better) functionality here.
33 use vars qw($VERSION $DEBUG %EXPORT_TAGS @EXPORT_OK @EXPORT);
34 use base qw(Exporter);
38 $DEBUG = 0 unless defined $DEBUG;
41 %EXPORT_TAGS = (util => [qw(getbugcomponent getbuglocation getlocationpath get_hashname),
42 qw(appendfile buglog getparsedaddrs getmaintainers),
43 qw(getmaintainers_reverse)
45 misc => [qw(make_list)],
47 lock => [qw(filelock unfilelock @cleanups)],
50 Exporter::export_ok_tags(qw(lock quit util misc));
51 $EXPORT_TAGS{all} = [@EXPORT_OK];
54 #use Debbugs::Config qw(:globals);
55 use Debbugs::Config qw(:config);
57 use Debbugs::MIME qw(decode_rfc1522);
63 our $DEBUG_FH = \*STDERR if not defined $DEBUG_FH;
67 The following functions are exported by the C<:util> tag
69 =head2 getbugcomponent
71 my $file = getbugcomponent($bug_number,$extension,$location)
73 Returns the path to the bug file in location C<$location>, bug number
74 C<$bugnumber> and extension C<$extension>
79 my ($bugnum, $ext, $location) = @_;
81 if (not defined $location) {
82 $location = getbuglocation($bugnum, $ext);
83 # Default to non-archived bugs only for now; CGI scripts want
84 # archived bugs but most of the backend scripts don't. For now,
85 # anything that is prepared to accept archived bugs should call
86 # getbuglocation() directly first.
87 return undef if defined $location and
88 ($location ne 'db' and $location ne 'db-h');
90 my $dir = getlocationpath($location);
91 return undef if not defined $dir;
92 if (defined $location and $location eq 'db') {
93 return "$dir/$bugnum.$ext";
95 my $hash = get_hashname($bugnum);
96 return "$dir/$hash/$bugnum.$ext";
100 =head2 getbuglocation
102 getbuglocation($bug_number,$extension)
104 Returns the the location in which a particular bug exists; valid
105 locations returned currently are archive, db-h, or db. If the bug does
106 not exist, returns undef.
111 my ($bugnum, $ext) = @_;
112 my $archdir = get_hashname($bugnum);
113 return 'archive' if -r getlocationpath('archive')."/$archdir/$bugnum.$ext";
114 return 'db-h' if -r getlocationpath('db-h')."/$archdir/$bugnum.$ext";
115 return 'db' if -r getlocationpath('db')."/$bugnum.$ext";
120 =head2 getlocationpath
122 getlocationpath($location)
124 Returns the path to a specific location
128 sub getlocationpath {
130 if (defined $location and $location eq 'archive') {
131 return "$config{spool_dir}/archive";
132 } elsif (defined $location and $location eq 'db') {
133 return "$config{spool_dir}/db";
135 return "$config{spool_dir}/db-h";
144 Returns the hash of the bug which is the location within the archive
149 return "" if ( $_[ 0 ] < 0 );
150 return sprintf "%02d", $_[ 0 ] % 100;
157 Returns the path to the logfile corresponding to the bug.
163 my $location = getbuglocation($bugnum, 'log');
164 return getbugcomponent($bugnum, 'log', $location) if ($location);
165 $location = getbuglocation($bugnum, 'log.gz');
166 return getbugcomponent($bugnum, 'log.gz', $location);
172 appendfile($file,'data','to','append');
174 Opens a file for appending and writes data to it.
180 if (!open(AP,">>$file")) {
181 print $DEBUG_FH "failed open log<\n" if $DEBUG;
182 print $DEBUG_FH "failed open log err $!<\n" if $DEBUG;
183 &quit("opening $file (appendfile): $!");
185 print(AP @_) || &quit("writing $file (appendfile): $!");
186 close(AP) || &quit("closing $file (appendfile): $!");
189 =head2 getparsedaddrs
191 my $address = getparsedaddrs($address);
192 my @address = getparsedaddrs($address);
194 Returns the output from Mail::Address->parse, or the cached output if
195 this address has been parsed before. In SCALAR context returns the
196 first address parsed.
204 return () unless defined $addr;
205 return wantarray?@{$_parsedaddrs{$addr}}:$_parsedaddrs{$addr}[0]
206 if exists $_parsedaddrs{$addr};
207 @{$_parsedaddrs{$addr}} = Mail::Address->parse($addr);
208 return wantarray?@{$_parsedaddrs{$addr}}:$_parsedaddrs{$addr}[0];
212 our $_maintainer_rev;
214 return $_maintainer if $_maintainer;
217 for my $file (@config{qw(maintainer_file maintainer_file_override)}) {
218 next unless defined $file;
219 my $maintfile = new IO::File $file,'r' or
220 &quitcgi("Unable to open $file: $!");
221 while(<$maintfile>) {
222 next unless m/^(\S+)\s+(\S.*\S)\s*$/;
226 for my $maint (map {lc($_->address)} getparsedaddrs($b)) {
227 push @{$maintainer_rev{$maint}},$a;
232 $_maintainer = \%maintainer;
233 $_maintainer_rev = \%maintainer_rev;
236 sub getmaintainers_reverse{
237 return $_maintainer_rev if $_maintainer_rev;
239 return $_maintainer_rev;
245 These functions are exported with the :lock tag
251 FLOCKs the passed file. Use unfilelock to unlock it.
259 # NB - NOT COMPATIBLE WITH `with-lock'
261 if ($lockfile !~ m{^/}) {
262 $lockfile = cwd().'/'.$lockfile;
265 $count= 10; $errors= '';
268 my $fh2 = IO::File->new($lockfile,'w')
269 or die "Unable to open $lockfile for writing: $!";
270 flock($fh2,LOCK_EX|LOCK_NB)
271 or die "Unable to lock $lockfile $!";
278 push @filelocks, {fh => $fh, file => $lockfile};
283 &quit("failed to get lock on $lockfile -- $errors");
287 push(@cleanups,\&unfilelock);
295 Unlocks the file most recently locked.
297 Note that it is not currently possible to unlock a specific file
298 locked with filelock.
303 if (@filelocks == 0) {
304 warn "unfilelock called with no active filelocks!\n";
307 my %fl = %{pop(@filelocks)};
309 flock($fl{fh},LOCK_UN)
310 or warn "Unable to unlock lockfile $fl{file}: $!";
312 or warn "Unable to close lockfile $fl{file}: $!";
314 or warn "Unable to unlink lockfile $fl{file}: $!";
321 These functions are exported with the :quit tag.
327 Exits the program by calling die after running some cleanups.
329 This should be replaced with an END handler which runs the cleanups
330 instead. (Or possibly a die handler, if the cleanups are important)
335 print $DEBUG_FH "quitting >$_[0]<\n" if $DEBUG;
337 while ($u= $cleanups[$#cleanups]) { &$u; }
343 These functions are exported with the :misc tag
347 LIST = make_list(@_);
349 Turns a scalar or an arrayref into a list; expands a list of arrayrefs
352 That is, make_list([qw(a b c)]); returns qw(a b c); make_list([qw(a
353 b)],[qw(c d)] returns qw(a b c d);
358 return map {(ref($_) eq 'ARRAY')?@{$_}:$_} @_;