]> git.donarmstrong.com Git - debbugs.git/blob - scripts/expire.in
[project @ 2003-08-06 10:57:23 by cjwatson]
[debbugs.git] / scripts / expire.in
1 #!/usr/bin/perl
2 # $Id: expire.in,v 1.17 2003/08/06 10:57:23 cjwatson Exp $
3
4 # Load modules and set envirnment
5 use File::Copy;
6 $config_path = '/etc/debbugs';
7 $lib_path = '/usr/lib/debbugs';
8
9 require("$config_path/config");
10 require("$config_path/text");
11 require("$lib_path/errorlib");
12 $ENV{'PATH'} = $lib_path.':'.$ENV{'PATH'};
13
14 # No $gRemoveAge means "never expire".
15 exit 0 unless $gRemoveAge;
16
17 chdir("$gSpoolDir") || die "chdir spool: $!\n";
18
19 #global variables
20 $debug = 0;
21 defined($startdate= time) || &quit("failed to get time: $!");
22
23 #get list of bugs (ie, status files)
24 opendir(DIR,"db-h") || &quit("opendir db: $!\n");
25 @dirs = sort { $a <=> $b } grep(s,^,db-h/,, grep(m/^\d+$/,readdir(DIR)));
26 close(DIR);
27 foreach my $dir (@dirs) {
28         opendir(DIR,$dir);
29         push @list, sort { $a <=> $b } grep(s/\.status$//,grep(m/^\d+\.status$/,readdir(DIR)));
30         close(DIR);
31 }
32
33 #process each bug (ie, status file)
34 while (length($ref=shift(@list))) {
35         print STDERR "$ref considering\n" if $debug;
36     ($bfound, $data)= &lockreadbugmerge($ref);
37         print STDERR "$ref read $bfound\n" if $debug;
38     $bfound || next;
39         print "$ref read ok (done $data->{done})\n" if $debug;
40     (&unlockreadbugmerge($bfound), next) unless length($data->{done});
41         print "$ref read done\n" if $debug;
42     @aref= ($ref);
43     if (length($data->{mergedwith})) { push(@aref,split / /,$data->{mergedwith}); }
44         print "$ref aref @aref\n" if $debug;
45     $oktoremove= 1;
46     for $mref (@aref) {
47         print "$ref $mref check\n" if $debug;
48         if ($mref != $ref) {
49                 print "$ref $mref reading\n" if $debug;
50             $newdata = &lockreadbug($mref) || die "huh $mref ?";
51                 print "$ref $mref read ok\n" if $debug;
52             $bfound++;
53         } else {
54             $newdata = $data;
55         }
56         print "$ref $mref read/not\n" if $debug;
57         $expectmerge= join(' ',grep($_ != $mref, sort { $a <=> $b } @aref));
58         $newdata->{mergedwith} eq $expectmerge ||
59             die "$ref -> $mref: ($newdata->{mergedwith}) vs. ($expectmerge) (@aref)";
60                 print "$ref $mref merge-ok\n" if $debug;
61         length($newdata->{done}) || die "$ref -> $mref";
62                 print "$ref $mref done-ok\n" if $debug;
63         $days= -M "db-h/".get_hashname($mref)."/$mref.log";
64                 print "ref $mref days $days\n" if $debug;
65         if ($days <= $gRemoveAge) 
66                 { print "$ref $mref saved\n" if $debug; $oktoremove= 0; }
67     }
68     if ($oktoremove) {
69         print "$ref removing\n" if $debug;
70         for $mref (@aref) {
71                 print "$ref removing $mref\n" if $debug;
72                 my $dir = get_hashname($mref);
73                 if ( $gSaveOldBugs ) {
74                         `mkdir -p "archive/$dir"`;
75                         link( "db-h/$dir/$mref.log", "archive/$dir/$mref.log" ) || copy( "db-h/$dir/$mref.log", "archive/$dir/$mref.log" );
76                         link( "db-h/$dir/$mref.status", "archive/$dir/$mref.status" ) || copy( "db-h/$dir/$mref.status", "archive/$dir/$mref.status" );
77                         link( "db-h/$dir/$mref.report", "archive/$dir/$mref.report" ) || copy( "db-h/$dir/$mref.report", "archive/$dir/$mref.report" );
78                         print("archived $mref to archive/$dir (from $ref)\n") || &quit("output old: $!");
79                 }
80                 unlink("db-h/$dir/$mref.log", "db-h/$dir/$mref.status", "db-h/$dir/$mref.report");
81                 print("deleted $mref (from $ref)\n") || &quit("output old: $!");
82                 bughook_archive($mref);
83         }
84     }
85         print "$ref unlocking $bfound\n" if $debug;
86     for ($i=0; $i<$bfound; $i++) { &unfilelock; }
87         print "$ref unlocking done\n" if $debug;
88 }
89
90 close(STDOUT) || &quit("close stdout: $!");