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