summaryrefslogtreecommitdiff
path: root/file-fetcher.pl
blob: 84b55b80c6b9455b427926f01d496dfd4fcdec9b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
#!/usr/bin/perl -w
#

use IPC::Run qw(run);

$commits_list = "pld-cvs-commit\@pld-linux.org";

$spool_dir = "./spool";
$copy_dir = "src"; # relative to ftp root
$no_url_dir = "./upload";
$df_server = "distfiles.pld-linux.org";
$df_scp = "plddist\@$df_server:ftp";

@md5 = ();
%url = ();
$problems = "";
$normal_out = "";
$requester = "";
$file = "";
$fetched_count = 0;
$force_reply = 0;
$req_login = "nobody";
@files = ();

# try lookup some file in spool, exit if it cannot be done
sub find_file_in_spool()
{
  opendir(DIR, $spool_dir) || die "can't opendir $spool_dir: $!";
  while (1) {
    my $f = readdir(DIR);
    defined $f or last;
    -f "$spool_dir/$f" or next;
    $file = "$spool_dir/$f";
    last;
  }
  closedir(DIR);

  exit 0 if ($file eq "");
}

# read file from spool, and try unlink it. if cannot unlink -- exit
# sets $requester (email), $problems, @md5 (arrays of md5's) 
# and %url (map from md5 to urls)
sub read_spool_file()
{
  open(F, "< $file") || exit 0;
  $requester = <F>;
  chomp $requester;
  $requester =~ /^[a-zA-Z_\-0-9\@\.]+$/ 
	or die "$file: evil requester: $requester";
  $req_login = "";
  $requester =~ /^([^@]+)\@/ and $req_login = $1;
  $req_login =~ /^[a-z0-9A-Z_]+$/ or die "$file: evil requester $requester";
  my $flags = <F>;
  $force_reply++ if ($flags =~ /force-reply/);
	
  while (<F>) {
    if (/^ERROR/) {
      s/^ERROR: //;
      $problems .= $_;
      next;
    }
    /^([a-f0-9]{32})\s+((ftp|http|https|no-url|no-url-copy):\/\/([=\@\?a-z0-9A-Z:\+\~\.\-\/_]|\%[0-9])+)\s*$/ 
  	or die "$file: corrupted";
    push @md5, $1;
    $url{$1} = $2;
    /\/$/ and die "$file: cannot fetch dir";
  }
  close(F);

  unlink($file) || exit 0;
}

sub basename($)
{
  my $f = shift;
  $f =~ s|.*/||;
  return $f;
}

sub by_md5($$)
{
  my ($md5, $url) = @_;
  $md5 =~ /^(.)(.)/;
  return "/by-md5/$1/$2/$md5/" . basename($url);
}

sub got_on_distfiles($$)
{
  my ($md5, $url) = @_;
  my $p = by_md5($md5, $url);
  my $l = `lftp -c 'debug 0; open $df_server; quote size $p'`;
  return $l =~ /^213 /;
}

sub copy_to_df($$)
{
  my ($from, $to) = @_;
  my $cmd = "scp -r -B -q $from $df_scp/$to";
  open(E, "$cmd 2>&1 |") or die;
  my $oops = "";
  while (<E>) {
    $oops .= $_;
  }
  $oops .= "\nThe command has exited with a non-zero status."
    unless (close (E));
  $problems .= "scp problems: $cmd:\n$oops\n"
    if ($oops ne "");
  return ($oops ne "");
}

sub move_file($$$)
{
  my ($md5, $url, $local_copy) = @_;

  my $bn = basename($url);
  
  if ($local_copy ne "$tmp_dir/$md5/$bn") {
    if (system("mv -f \"$local_copy\" \"$tmp_dir/$md5/$bn\"")) {
      $problems .= "FATAL: cannot move $local_copy to $tmp_dir\n";
      return;
    }
    $local_copy = "$tmp_dir/$md5/$bn";
  }
  
  if (open(D, "> $tmp_dir/$md5/$bn.desc")) {
    print D "URL: $url\n";
    print D "Login: $req_login\n";
    print D "MD5: $md5\n";
    print D 'Fetched-by: $Id$'."\n";
    print D "Time: " . time . "\n";
    close(D);
  } else {
    $problems .= "ERROR: cannot write $bn.desc\n";
  }

  my $dir = by_md5($md5, $url);
  $dir =~ s|/[^/]+/[^/]+$||;
  if (copy_to_df("$tmp_dir/$md5/", $dir) == 0) {
    $normal_out .= 
      "STORED: $url\n" .
      "\t$md5  " . basename($url) . "\n" .
      "\tSize: " .  (-s $local_copy) . " bytes\n";
    $fetched_count++;
  }
}

sub make_src_symlink($$)
{
  my ($md5, $url) = @_;
  
  return unless ($url =~ /^no-url/);
  
  my $b = basename($url);
  if (open(S, "> $tmp_dir/$b.link")) {
    if ($url =~ /^no-url-copy/) {
      print S (".." . by_md5($md5, $url));
    } else {
      print S "REMOVE";
    }
    close(S);
    copy_to_df("$tmp_dir/$b.link", "$copy_dir/$b.link");
  } else {
    $problems .= "ERROR: cannot write $tmp_dir/$b.link\n";
  }
}

sub md5($)
{
  my $file = shift;
  my $in = "";
  my $md5 = "";
  my $err = "";
  my @cmd = ("md5sum", $file);

  run \@cmd, \$in, \$md5, \$err;
  if ($err ne "") {
    chomp($err);
    $problems .= "FATAL: " . $err . "\n";
    return "error";
  }
  chomp $md5;
  $md5 =~ /^([a-f0-9]{32})/ and $md5 = $1;
  return $md5;
}

sub handle_no_url($$)
{
  my ($md5, $url) = @_;
  
  $url =~ m|://([^/]+)| or die "corrupted! (no-url)";
  my $basename = $1;
  my $file = "$no_url_dir/$req_login/$basename";

  if (-f $file) {
    my $computed_md5 = md5($file);
    if ($computed_md5 ne $md5) {
      $problems .= "FATAL: $file md5 mismatch, needed $md5, got $computed_md5\n";
    } else {
      move_file($md5, $url, $file);
      make_src_symlink($md5, $url);
    }
  } else {
    $problems .= "FATAL: $file was not uploaded\n";
  }
}

sub fetch_file($$)
{
  my ($md5, $url) = @_;
  my $out = "";
  my $all_out = "";
  my $bn = basename($url);
  my $local = "$tmp_dir/$md5/$bn";
  my $cmd = "wget -nv -O $local \"$url\"";
  my $cmd2 = "wget -nv --passive-ftp -O $local \"$url\"";

  push @files, $bn;

  if (got_on_distfiles($md5, $url)) {
    $normal_out .= 
    	"ALREADY GOT: $url\n" .
    	"\t$md5  " . basename($url) . "\n";
    make_src_symlink($md5, $url);
    return;
  }

  mkdir("$tmp_dir/$md5") or die;
  
  if ($url =~ /^no-url/) {
    handle_no_url($md5, $url);
    return;
  }
  
  open(W, "$cmd 2>&1 |");
  while (<W>) {
    $all_out .= $_;
    /URL:.*\s+\-\>\s+.*/ and next;
    $out .= $_;
  }
  close(W);
  if ($out ne "") {
    $problems .= "$cmd:\n$out\n\n";
  }
  if (-f $local && -s $local == 0 && $url =~ /^ftp:/) {
    $out = "";
    open(W, "$cmd2 2>&1 |");
    while (<W>) {
      $all_out .= "\n\t\t$_";
      /URL:.*\s+\-\>\s+.*/ and next;
      $out .= $_;
    }
    close(W);
    if ($out ne "") {
      $problems .= "$cmd:\n$out\n\n";
    }
  }
  if (-r $local && -s $local > 0) {
    my $computed_md5 = md5($local);
    if ($computed_md5 ne $md5) {
      $problems .= "FATAL: $url md5 mismatch, needed $md5, got $computed_md5\n";
    } else {
      my $testcmd = "file \"$local\" |";
      my $testres = "";
      if ($url =~ /^(http|https):/ && $local =~ /\.(tar\.(bz2|gz)|tgz|zip|jar|xpi)$/) {
        open(T, $testcmd) or die;
        $testres = <T>;
        close(T);
      }
      if ($testres =~ /empty|(ASCII|HTML|SGML).*text/) {
        $testres =~ s/.*://;
        $problems .= "FATAL: data returned from $url: $testres";
      } else {
        move_file($md5, $url, $local);
      }
    }
  } elsif (-f $local && -s $local > 0) {
    $problems .= "FATAL: $url ($md5) was not fetched ($cmd: $all_out): file is not readable\n";
  } else {
    $problems .= "FATAL: $url ($md5) was not fetched ($cmd: $all_out)\n";
  }
  # save space
  unlink($local);
}

sub fetch_files()
{
  $problems .= "\n\n" if ($problems ne "");
  foreach $md5 (@md5) {
    fetch_file($md5, $url{$md5});
  }
}

sub send_email()
{
  open(EMAIL, "| /usr/sbin/sendmail -t");
  #open(EMAIL, "| cat");
  my $marker = "";
  if ($problems ne "") {
    $marker = "ERRORS: ";
  }
  my $req_login;
  $requester =~ /^(.*)\@/ and $req_login = $1;

  splice(@files, 10, @files - 10, "...")
    if (@files > 10);
  
  print EMAIL 
"From: $req_login <$requester>
To: $commits_list
Cc: $requester
Subject: DISTFILES: ${marker}@{files}
Message-ID: <$$." . time . "\@distfiles.pld-linux.org>
X-distfiles-program: file-fetcher.pl
X-distfiles-version: " . '$Id$' . "

$problems
Files fetched: $fetched_count

$normal_out

-- 
Virtually Yours: distfiles.
";
  close(EMAIL) or die;
}

sub make_tmp_dir()
{
  my $id = `uuidgen 2>/dev/null`;
  chomp $id;
  $id = rand if (!defined $id or $id eq "");
  $tmp_dir = "./tmp/$id";
  mkdir($tmp_dir) or die;
}

sub clean_tmp_dir()
{
  system("rm -rf $tmp_dir")
    if ($tmp_dir ne "" && -d $tmp_dir);
}

umask(002);

find_file_in_spool();
read_spool_file();
make_tmp_dir();
fetch_files();
send_email() unless (!$force_reply and $problems eq "" and $fetched_count == 0);
clean_tmp_dir();