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),
46 misc => [qw(make_list)],
47 date => [qw(secs_to_english)],
49 lock => [qw(filelock unfilelock @cleanups lockpid)],
52 Exporter::export_ok_tags(qw(lock quit date util misc));
53 $EXPORT_TAGS{all} = [@EXPORT_OK];
56 #use Debbugs::Config qw(:globals);
57 use Debbugs::Config qw(:config);
59 use Debbugs::MIME qw(decode_rfc1522);
65 our $DEBUG_FH = \*STDERR if not defined $DEBUG_FH;
69 The following functions are exported by the C<:util> tag
71 =head2 getbugcomponent
73 my $file = getbugcomponent($bug_number,$extension,$location)
75 Returns the path to the bug file in location C<$location>, bug number
76 C<$bugnumber> and extension C<$extension>
81 my ($bugnum, $ext, $location) = @_;
83 if (not defined $location) {
84 $location = getbuglocation($bugnum, $ext);
85 # Default to non-archived bugs only for now; CGI scripts want
86 # archived bugs but most of the backend scripts don't. For now,
87 # anything that is prepared to accept archived bugs should call
88 # getbuglocation() directly first.
89 return undef if defined $location and
90 ($location ne 'db' and $location ne 'db-h');
92 my $dir = getlocationpath($location);
93 return undef if not defined $dir;
94 if (defined $location and $location eq 'db') {
95 return "$dir/$bugnum.$ext";
97 my $hash = get_hashname($bugnum);
98 return "$dir/$hash/$bugnum.$ext";
102 =head2 getbuglocation
104 getbuglocation($bug_number,$extension)
106 Returns the the location in which a particular bug exists; valid
107 locations returned currently are archive, db-h, or db. If the bug does
108 not exist, returns undef.
113 my ($bugnum, $ext) = @_;
114 my $archdir = get_hashname($bugnum);
115 return 'archive' if -r getlocationpath('archive')."/$archdir/$bugnum.$ext";
116 return 'db-h' if -r getlocationpath('db-h')."/$archdir/$bugnum.$ext";
117 return 'db' if -r getlocationpath('db')."/$bugnum.$ext";
122 =head2 getlocationpath
124 getlocationpath($location)
126 Returns the path to a specific location
130 sub getlocationpath {
132 if (defined $location and $location eq 'archive') {
133 return "$config{spool_dir}/archive";
134 } elsif (defined $location and $location eq 'db') {
135 return "$config{spool_dir}/db";
137 return "$config{spool_dir}/db-h";
146 Returns the hash of the bug which is the location within the archive
151 return "" if ( $_[ 0 ] < 0 );
152 return sprintf "%02d", $_[ 0 ] % 100;
159 Returns the path to the logfile corresponding to the bug.
165 my $location = getbuglocation($bugnum, 'log');
166 return getbugcomponent($bugnum, 'log', $location) if ($location);
167 $location = getbuglocation($bugnum, 'log.gz');
168 return getbugcomponent($bugnum, 'log.gz', $location);
174 appendfile($file,'data','to','append');
176 Opens a file for appending and writes data to it.
182 if (!open(AP,">>$file")) {
183 print $DEBUG_FH "failed open log<\n" if $DEBUG;
184 print $DEBUG_FH "failed open log err $!<\n" if $DEBUG;
185 &quit("opening $file (appendfile): $!");
187 print(AP @_) || &quit("writing $file (appendfile): $!");
188 close(AP) || &quit("closing $file (appendfile): $!");
191 =head2 getparsedaddrs
193 my $address = getparsedaddrs($address);
194 my @address = getparsedaddrs($address);
196 Returns the output from Mail::Address->parse, or the cached output if
197 this address has been parsed before. In SCALAR context returns the
198 first address parsed.
206 return () unless defined $addr;
207 return wantarray?@{$_parsedaddrs{$addr}}:$_parsedaddrs{$addr}[0]
208 if exists $_parsedaddrs{$addr};
210 # don't display the warnings from Mail::Address->parse
211 local $SIG{__WARN__} = sub { };
212 @{$_parsedaddrs{$addr}} = Mail::Address->parse($addr);
214 return wantarray?@{$_parsedaddrs{$addr}}:$_parsedaddrs{$addr}[0];
218 our $_maintainer_rev;
220 return $_maintainer if $_maintainer;
223 for my $file (@config{qw(maintainer_file maintainer_file_override pseduo_maint_file)}) {
224 next unless defined $file;
225 my $maintfile = new IO::File $file,'r' or
226 &quitcgi("Unable to open $file: $!");
227 while(<$maintfile>) {
228 next unless m/^(\S+)\s+(\S.*\S)\s*$/;
232 for my $maint (map {lc($_->address)} getparsedaddrs($b)) {
233 push @{$maintainer_rev{$maint}},$a;
238 $_maintainer = \%maintainer;
239 $_maintainer_rev = \%maintainer_rev;
242 sub getmaintainers_reverse{
243 return $_maintainer_rev if $_maintainer_rev;
245 return $_maintainer_rev;
250 my $pseudopkgdesc = getpseudodesc(...);
252 Returns the entry for a pseudo package from the
253 $config{pseudo_desc_file}. In cases where pseudo_desc_file is not
254 defined, returns an empty arrayref.
256 This function can be used to see if a particular package is a
257 pseudopackage or not.
263 return $_pseudodesc if $_pseudodesc;
266 if (not defined $config{pseudo_desc_file}) {
270 my $pseudo = IO::File->new($config{pseudo_desc_file},'r')
271 or die "Unable to open $config{pseudo_desc_file}: $!";
273 next unless m/^(\S+)\s+(\S.*\S)\s*$/;
274 $pseudodesc{lc $1} = $2;
277 $_pseudodesc = \%pseudodesc;
284 my $english = secs_to_english($seconds);
285 my ($days,$english) = secs_to_english($seconds);
287 XXX This should probably be changed to use Date::Calc
294 my $days = int($seconds / 86400);
295 my $years = int($days / 365);
299 push @age, "1 year" if ($years == 1);
300 push @age, "$years years" if ($years > 1);
301 push @age, "1 day" if ($days == 1);
302 push @age, "$days days" if ($days > 1);
303 $result .= join(" and ", @age);
305 return wantarray?(int($seconds/86400),$result):$result;
311 These functions are exported with the :lock tag
317 FLOCKs the passed file. Use unfilelock to unlock it.
325 # NB - NOT COMPATIBLE WITH `with-lock'
327 if ($lockfile !~ m{^/}) {
328 $lockfile = cwd().'/'.$lockfile;
331 $count= 10; $errors= '';
334 my $fh2 = IO::File->new($lockfile,'w')
335 or die "Unable to open $lockfile for writing: $!";
336 flock($fh2,LOCK_EX|LOCK_NB)
337 or die "Unable to lock $lockfile $!";
344 push @filelocks, {fh => $fh, file => $lockfile};
349 &quit("failed to get lock on $lockfile -- $errors");
353 push(@cleanups,\&unfilelock);
361 Unlocks the file most recently locked.
363 Note that it is not currently possible to unlock a specific file
364 locked with filelock.
369 if (@filelocks == 0) {
370 warn "unfilelock called with no active filelocks!\n";
373 my %fl = %{pop(@filelocks)};
375 flock($fl{fh},LOCK_UN)
376 or warn "Unable to unlock lockfile $fl{file}: $!";
378 or warn "Unable to close lockfile $fl{file}: $!";
380 or warn "Unable to unlink lockfile $fl{file}: $!";
385 lockpid('/path/to/pidfile');
387 Creates a pidfile '/path/to/pidfile' if one doesn't exist or if the
388 pid in the file does not respond to kill 0.
390 Returns 1 on success, false on failure; dies on unusual errors.
397 my $pidfh = IO::File->new($pidfile, 'r') or
398 die "Unable to open pidfile $pidfile: $!";
401 ($pid) = $pid =~ /(\d+)/;
402 if (defined $pid and kill(0,$pid)) {
407 die "Unable to unlink stale pidfile $pidfile $!";
409 my $pidfh = IO::File->new($pidfile,'w') or
410 die "Unable to open $pidfile for writing: $!";
411 print {$pidfh} $$ or die "Unable to write to $pidfile $!";
412 close $pidfh or die "Unable to close $pidfile $!";
419 These functions are exported with the :quit tag.
425 Exits the program by calling die after running some cleanups.
427 This should be replaced with an END handler which runs the cleanups
428 instead. (Or possibly a die handler, if the cleanups are important)
433 print $DEBUG_FH "quitting >$_[0]<\n" if $DEBUG;
435 while ($u= $cleanups[$#cleanups]) { &$u; }
441 These functions are exported with the :misc tag
445 LIST = make_list(@_);
447 Turns a scalar or an arrayref into a list; expands a list of arrayrefs
450 That is, make_list([qw(a b c)]); returns qw(a b c); make_list([qw(a
451 b)],[qw(c d)] returns qw(a b c d);
456 return map {(ref($_) eq 'ARRAY')?@{$_}:$_} @_;