6 $commits_list = "pld-cvs-commit\@pld-linux.org";
8 $spool_dir = "./spool";
9 $copy_dir = "src"; # relative to ftp root
10 $no_url_dir = "./upload";
11 $df_server = "distfiles.pld-linux.org";
12 $df_scp = "plddist\@$df_server:ftp";
22 $req_login = "nobody";
25 # try lookup some file in spool, exit if it cannot be done
26 sub find_file_in_spool()
28 opendir(DIR, $spool_dir) || die "can't opendir $spool_dir: $!";
32 -f "$spool_dir/$f" or next;
33 $file = "$spool_dir/$f";
38 exit 0 if ($file eq "");
41 # read file from spool, and try unlink it. if cannot unlink -- exit
42 # sets $requester (email), $problems, @md5 (arrays of md5's)
43 # and %url (map from md5 to urls)
46 open(F, "< $file") || exit 0;
49 $requester =~ /^[a-zA-Z_\-0-9\@\.]+$/
50 or die "$file: evil requester: $requester";
52 $requester =~ /^([^@]+)\@/ and $req_login = $1;
53 $req_login =~ /^[a-z0-9A-Z_]+$/ or die "$file: evil requester $requester";
55 $force_reply++ if ($flags =~ /force-reply/);
63 /^([a-f0-9]{32})\s+((ftp|http|no-url|no-url-copy):\/\/([=\@\?a-z0-9A-Z:\+\~\.\-\/_]|\%[0-9])+)\s*$/
64 or die "$file: corrupted";
67 /\/$/ and die "$file: cannot fetch dir";
71 unlink($file) || exit 0;
85 return "/by-md5/$1/$2/$md5/" . basename($url);
88 sub got_on_distfiles($$)
91 my $p = by_md5($md5, $url);
92 my $l = `lftp -c 'debug 0; open $df_server; quote size $p'`;
99 my $cmd = "scp -r -B -q $from $df_scp/$to";
100 open(E, "$cmd 2>&1 |") or die;
106 $problems .= "scp problems: $cmd:\n$oops\n"
108 return ($oops ne "");
113 my ($md5, $url, $local_copy) = @_;
115 my $bn = basename($url);
117 if ($local_copy ne "$tmp_dir/$md5/$bn") {
118 if (system("mv -f \"$local_copy\" \"$tmp_dir/$md5/$bn\"")) {
119 $problems .= "FATAL: cannot move $local_copy to $tmp_dir\n";
122 $local_copy = "$tmp_dir/$md5/$bn";
125 if (open(D, "> $tmp_dir/$md5/$bn.desc")) {
126 print D "URL: $url\n";
127 print D "Login: $req_login\n";
128 print D "MD5: $md5\n";
129 print D 'Fetched-by: $Id$'."\n";
130 print D "Time: " . time . "\n";
133 $problems .= "ERROR: cannot write $bn.desc\n";
136 my $dir = by_md5($md5, $url);
137 $dir =~ s|/[^/]+/[^/]+$||;
138 if (copy_to_df("$tmp_dir/$md5/", $dir) == 0) {
141 "\t$md5 " . basename($url) . "\n" .
142 "\tSize: " . (-s $local_copy) . " bytes\n";
147 sub make_src_symlink($$)
149 my ($md5, $url) = @_;
151 return unless ($url =~ /^no-url/);
153 my $b = basename($url);
154 if (open(S, "> $tmp_dir/$b.link")) {
155 if ($url =~ /^no-url-copy/) {
156 print S (".." . by_md5($md5, $url));
161 copy_to_df("$tmp_dir/$b.link", "$copy_dir/$b.link");
163 $problems .= "ERROR: cannot write $tmp_dir/$b.link\n";
173 my @cmd = ("md5sum", $file);
175 run \@cmd, \$in, \$md5, \$err;
178 $problems .= "FATAL: " . $err . "\n";
182 $md5 =~ /^([a-f0-9]{32})/ and $md5 = $1;
186 sub handle_no_url($$)
188 my ($md5, $url) = @_;
190 $url =~ m|://([^/]+)| or die "corrupted! (no-url)";
192 my $file = "$no_url_dir/$req_login/$basename";
195 my $computed_md5 = md5($file);
196 if ($computed_md5 ne $md5) {
197 $problems .= "FATAL: $file md5 mismatch, needed $md5, got $computed_md5\n";
199 move_file($md5, $url, $file);
200 make_src_symlink($md5, $url);
203 $problems .= "FATAL: $file was not uploaded\n";
209 my ($md5, $url) = @_;
212 my $bn = basename($url);
213 my $local = "$tmp_dir/$md5/$bn";
214 my $cmd = "wget -nv -O $local \"$url\"";
215 my $cmd2 = "wget -nv --passive-ftp -O $local \"$url\"";
219 if (got_on_distfiles($md5, $url)) {
221 "ALREADY GOT: $url\n" .
222 "\t$md5 " . basename($url) . "\n";
223 make_src_symlink($md5, $url);
227 mkdir("$tmp_dir/$md5") or die;
229 if ($url =~ /^no-url/) {
230 handle_no_url($md5, $url);
234 open(W, "$cmd 2>&1 |");
237 /URL:.*\s+\-\>\s+.*/ and next;
242 $problems .= "$cmd:\n$out\n\n";
244 if (-f $local && -s $local == 0 && $url =~ /^ftp:/) {
246 open(W, "$cmd2 2>&1 |");
248 $all_out .= "\n\t\t$_";
249 /URL:.*\s+\-\>\s+.*/ and next;
254 $problems .= "$cmd:\n$out\n\n";
257 if (-r $local && -s $local > 0) {
258 my $computed_md5 = md5($local);
259 if ($computed_md5 ne $md5) {
260 $problems .= "FATAL: $url md5 mismatch, needed $md5, got $computed_md5\n";
262 my $testcmd = "file \"$local\" |";
264 if ($url =~ /^http:/ && $local =~ /\.(tar\.(bz2|gz)|tgz|zip|jar|xpi)$/) {
265 open(T, $testcmd) or die;
269 if ($testres =~ /empty|(ASCII|HTML|SGML).*text/) {
271 $problems .= "FATAL: data returned from $url: $testres";
273 move_file($md5, $url, $local);
276 } elsif (-f $local && -s $local > 0) {
277 $problems .= "FATAL: $url ($md5) was not fetched ($cmd: $all_out): file is not readable\n";
279 $problems .= "FATAL: $url ($md5) was not fetched ($cmd: $all_out)\n";
287 $problems .= "\n\n" if ($problems ne "");
288 foreach $md5 (@md5) {
289 fetch_file($md5, $url{$md5});
295 open(EMAIL, "| /usr/sbin/sendmail -t");
296 #open(EMAIL, "| cat");
298 if ($problems ne "") {
299 $marker = "ERRORS: ";
302 $requester =~ /^(.*)\@/ and $req_login = $1;
304 splice(@files, 10, @files - 10, "...")
308 "From: $req_login <$requester>
311 Subject: DISTFILES: ${marker}@{files}
312 Message-ID: <$$." . time . "\@distfiles.pld-linux.org>
313 X-distfiles-program: file-fetcher.pl
314 X-distfiles-version: " . '$Id$' . "
317 Files fetched: $fetched_count
322 Virtually Yours: distfiles.
329 my $id = `uuidgen 2>/dev/null`;
331 $id = rand if (!defined $id or $id eq "");
332 $tmp_dir = "./tmp/$id";
333 mkdir($tmp_dir) or die;
338 system("rm -rf $tmp_dir")
339 if ($tmp_dir ne "" && -d $tmp_dir);
344 find_file_in_spool();
348 send_email() unless (!$force_reply and $problems eq "" and $fetched_count == 0);