- my $options = $ENV{WGET_OPTIONS};
- $options or $options = "";
-
- $mirror =~ s/\/$//;
- open WGET, "wget -t1 --timeout=20 $options -O- \"$mirror/$filename\" |" or die "Cannot launch wget.\n";
- open MD5SUM, "| $md5cmd > \"$target/$filename.md5sum\"" or die "Cannot launch md5sum.\n";
- open OUTPUT, "> $target/$filename.dl" or die "Cannot create file $target/$filename.dl: $!\n";
- my $buffer;
- while (read WGET, $buffer, 1048576) {
- print MD5SUM $buffer;
- print OUTPUT $buffer;
- }
- close MD5SUM;
- close WGET;
- close OUTPUT;
-
- if (($? >> 8) != 0 ) {
- print STDERR "Download failed.\n";
- cleanup();
- return;
- }
-
- my $sum = `cat "$target/$filename.md5sum"`;
- $sum =~ /^(\w+)\s*/ or die "Could not generate md5sum\n";
- $sum = $1;
-
- if (($md5sum =~ /\w{32}/) and ($sum ne $md5sum)) {
- print STDERR "MD5 sum of the downloaded file does not match - deleting download.\n";
- cleanup();
- return;
+ my $options = $ENV{WGET_OPTIONS} || "";
+
+ $mirror =~ s!/$!!;
+
+ if ($mirror =~ s!^file://!!) {
+ if (! -d "$mirror") {
+ print STDERR "Wrong local cache directory -$mirror-.\n";
+ cleanup();
+ return;
+ }
+
+ if (! -d "$target") {
+ system("mkdir", "-p", "$target/");
+ }
+
+ if (! open TMPDLS, "find $mirror -follow -name $filename 2>/dev/null |") {
+ print("Failed to search for $filename in $mirror\n");
+ return;
+ }
+
+ my $link;
+
+ while (defined(my $line = readline TMPDLS)) {
+ chomp ($link = $line);
+ if ($. > 1) {
+ print("$. or more instances of $filename in $mirror found . Only one instance allowed.\n");
+ return;
+ }
+ }
+
+ close TMPDLS;
+
+ if (! $link) {
+ print("No instances of $filename found in $mirror.\n");
+ return;
+ }
+
+ print("Copying $filename from $link\n");
+ copy($link, "$target/$filename.dl");
+
+ $hash_cmd and do {
+ if (system("cat '$target/$filename.dl' | $hash_cmd > '$target/$filename.hash'")) {
+ print("Failed to generate hash for $filename\n");
+ return;
+ }
+ };
+ } else {
+ open WGET, "wget -t5 --timeout=20 --no-check-certificate $options -O- '$mirror/$url_filename' |" or die "Cannot launch wget.\n";
+ $hash_cmd and do {
+ open MD5SUM, "| $hash_cmd > '$target/$filename.hash'" or die "Cannot launch $hash_cmd.\n";
+ };
+ open OUTPUT, "> $target/$filename.dl" or die "Cannot create file $target/$filename.dl: $!\n";
+ my $buffer;
+ while (read WGET, $buffer, 1048576) {
+ $hash_cmd and print MD5SUM $buffer;
+ print OUTPUT $buffer;
+ }
+ $hash_cmd and close MD5SUM;
+ close WGET;
+ close OUTPUT;
+
+ if ($? >> 8) {
+ print STDERR "Download failed.\n";
+ cleanup();
+ return;
+ }