1 |
package upstream; |
2 |
|
3 |
use warnings; |
4 |
use strict; |
5 |
use autodie; |
6 |
|
7 |
use LWP::Simple qw/mirror RC_NOT_MODIFIED/; |
8 |
use server; |
9 |
|
10 |
sub mirror_file { |
11 |
my ( $url, $file ) = @_; |
12 |
print STDERR "mirror $url"; |
13 |
mirror( $url, $file ) |
14 |
== RC_NOT_MODIFIED |
15 |
&& warn(" not modified\n") |
16 |
|| warn(" done ", -s $file, " bytes\n") |
17 |
; |
18 |
} |
19 |
|
20 |
our $just_once; |
21 |
|
22 |
sub iso { |
23 |
my $url = shift; |
24 |
|
25 |
return if $just_once->{$url}++; |
26 |
|
27 |
my $name = (caller(1))[3]; |
28 |
$name =~ s{config::}{} || die "caller isn't package config !"; |
29 |
|
30 |
warn "$name $url"; |
31 |
|
32 |
my $dir = "$server::base_dir/iso"; |
33 |
mkdir $dir unless -e $dir; |
34 |
|
35 |
my $file = $1 if $url =~ m{/([^/]+\.iso$)}i; |
36 |
die "can't find iso file in $url" unless $file; |
37 |
|
38 |
my $iso = "$dir/$file"; |
39 |
|
40 |
mirror_file( $url, $iso ); |
41 |
|
42 |
$file =~ s{\.iso$}{}i; |
43 |
my $mnt = "$server::base_dir/tftp/$name"; |
44 |
mkdir $mnt unless -d $mnt; |
45 |
$mnt .= '/iso'; |
46 |
mkdir $mnt unless -d $mnt; |
47 |
|
48 |
system("mount -t iso9660 | grep $name/iso || sudo mount $iso $mnt -o loop -t iso9660 -v") == 0; |
49 |
} |
50 |
|
51 |
sub file { |
52 |
my ( $url, $file ) = @_; |
53 |
my $path = "$server::base_dir/$file"; |
54 |
my @file_parts = split m{/}, $file; |
55 |
foreach ( 1 .. $#file_parts - 1 ) { |
56 |
my $path = splice @file_parts, 0, $_; |
57 |
warn "? $path\n"; |
58 |
mkdir $path unless -e $path; |
59 |
} |
60 |
mirror_file( $url, "$server::base_dir/$file" ); |
61 |
} |
62 |
|
63 |
1; |