OSDN Git Service

Imported Classpath 0.18.
[pf3gnuchains/gcc-fork.git] / libjava / classpath / scripts / patches.pl
1 #!/usr/bin/perl -w
2 # Purpose is to move patches from upload directory to 
3 # public patches directory.  Any file not matching the correct 
4 # pattern is deleted.  Any patch file without a README and the
5 # file was last modified more than 120 minutes ago is deleted.
6 # Any README file without a patch file which was last modified
7 # more than 120 minutes ago is deleted.
8 #
9 # notes to self: as long as this runs as root do not worry
10 # about quota problems or disk space
11
12 use strict;
13
14 my ($upload_dir) = "/home/ftp/classpath/incoming";
15 my ($public_dir) = "/home/ftp/classpath/pub/patches";
16 my ($user) = "classpath";
17 my ($group) = "classpath";
18 my ($mode_dir) = "775";
19 my ($mode_file) = "664";
20 my (@patches) = ();
21
22 use vars qw($upload_dir $public_dir @patches $user $group
23             $mode_dir $mode_file);
24
25 # main
26 {
27     @patches = &getPatches();
28     &movePatches(@patches);
29 }
30
31 #---------------------------------------------------------------
32 # Purpose: To remove files not matching the correct pattern.
33 #   To remove README files without patches (last modified greater
34 #   than 2 hours).  To remove patches without README files (last
35 #   modified greater than 2 hours).
36 #---------------------------------------------------------------
37 sub getPatches
38 {
39     my (@patches) = ();
40     my (@entries) = ();
41     my (%maybe) = ();
42     my ($entry, $debug, $prefix, $junk, $file, $patch, $readme) = "";
43     my ($dev, $ino, $mode, $nlink, $uid, $gid, $rdev, $size, $atime,
44         $mtime, $ctime, $blksize, $blocks) = "";
45
46     $debug = 1;
47
48     opendir(INCOMING, "$upload_dir") || die "could not open $upload_dir\n";
49     @entries = grep( !/^\.\S+/, readdir(INCOMING)); # no .*
50     closedir(INCOMING);
51     foreach $entry (sort @entries)
52     {
53         if (($entry eq ".") || ($entry eq "..")) { next; }
54         if (-d "$upload_dir/$entry")
55         {
56             print "Directory: $upload_dir/$entry/\n";
57         }
58         elsif (-e "$upload_dir/$entry")
59         {
60             if ($entry eq ".message") { next; }
61             if ($entry eq "README") { next; }
62             if ($entry !~ /^\w+-\d\d\d\d\d\d-\d+\.patch\.(gz|README)$/)
63             {
64                 print "REGEX FAILED: $entry\n";
65                 unlink("$upload_dir/$entry");
66             }
67             else
68             {
69                 ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,
70                  $ctime,$blksize,$blocks) = stat("$upload_dir/$entry");
71                 if ($size > 512000)
72                 {
73                     print "LARGE PATCH: $entry\n";
74                     unlink("$upload_dir/$entry");
75                 }
76                 else 
77                 {
78                     ($prefix,$junk) = split(/(\.gz|\.README)/, $entry, 2);
79                     $maybe{$prefix} += 1;
80                 }
81             }
82         }
83     }
84
85     foreach $entry (keys(%maybe))
86     {
87         if ($maybe{$entry} == 2)
88         { 
89             $patch = "$entry.gz"; 
90             $readme = "$entry.README"; 
91
92             ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,
93              $ctime,$blksize,$blocks) = stat($patch);
94             if (time-$mtime > 900)
95             {
96                 ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,
97                  $ctime,$blksize,$blocks) = stat($readme);
98                 if (time-$mtime > 900)
99                 {
100                     $patches[$#patches+1] = $entry;
101                 }
102             }
103         }
104         else
105         {
106             if (-e "$upload_dir/$entry.gz")
107             { 
108                 unlink("$upload_dir/$entry.gz");
109                 print "STALE PATCH: $entry.gz\n";
110             }
111             elsif (-e "$upload_dir/$entry.README") 
112             {
113                 unlink("$upload_dir/$entry.README");
114                 print "STALE README: $entry.README\n";
115             }
116         }
117     }
118     return (@patches);
119 }
120
121 #---------------------------------------------------------------
122 # Purpose: To move the patches to the proper directory and set
123 #   the permissions correctly.
124 #---------------------------------------------------------------
125 sub movePatches
126 {
127     my (@patches) = @_;
128     my ($patch) = "";
129     my ($fail) = 0;
130
131     if (!(-d "$public_dir"))
132     {
133         system("mkdir -p $public_dir");
134         system("chown $user.$group $public_dir");
135         system("chmod $mode_dir $public_dir");
136     }
137     foreach $patch (@patches)
138     {
139         if (-e "$public_dir/$patch.gz")
140         {
141             print "Patch exists: $public_dir/$patch.gz\n";
142             $fail = 1;
143         }
144         if (-e "$public_dir/$patch.README")
145         {
146             print "README exists: $public_dir/$patch.README\n";
147             $fail = 1;
148         }
149         if ($fail == 0)
150         {
151             system("mv $upload_dir/$patch.gz $public_dir/$patch.gz");
152             system("mv $upload_dir/$patch.README $public_dir/$patch.README");
153             system("chown $user.$group $public_dir/*");
154             system("chmod $mode_file $public_dir/*");
155             open(MAIL, "|mail -s \"Classpath: $patch uploaded\" core\@classpath.org") || die "could not open mail\n";
156             print MAIL "GNU Classpath FTP Maintenance\n";
157             print MAIL "\n";
158             print MAIL "Added Files:\n";
159             print MAIL "ftp://ftp.classpath.org/pub/patches/$patch.gz\n";
160             print MAIL "ftp://ftp.classpath.org/pub/patches/$patch.README\n\n";
161             close(MAIL);
162         }
163     }
164 }