implement first_time($identifier) with Storable
authorDobrica Pavlinusic <dpavlin@rot13.org>
Sun, 16 Aug 2009 23:31:30 +0000 (23:31 +0000)
committerDobrica Pavlinusic <dpavlin@rot13.org>
Sun, 16 Aug 2009 23:31:30 +0000 (23:31 +0000)
lib/PXElator/upstream.pm

index 3b6b431..e1f4ecb 100644 (file)
@@ -5,6 +5,7 @@ use strict;
 use autodie;
 
 use LWP::Simple qw/mirror RC_NOT_MODIFIED/;
+use Storable;
 use server;
 
 sub mirror_file {
@@ -17,12 +18,19 @@ sub mirror_file {
                ;
 }
 
-our $just_once;
+my $once_path = '/tmp/pxelator.once';
+our $just_once = retrieve $once_path;
+sub first_time {
+       my $what = shift;
+       return if $just_once->{$what}++;
+       store $just_once, $once_path;
+       return 1;
+}
 
 sub iso {
        my $url = shift;
 
-       return if $just_once->{$url}++;
+       return if ! first_time($url);
 
        my $name = (caller(1))[3];
        $name =~ s{config::}{} || die "caller isn't package config !";
@@ -48,16 +56,29 @@ sub iso {
        system("mount -t iso9660 | grep $name/iso || sudo mount $iso $mnt -o loop -t iso9660 -v") == 0;
 }
 
-sub file {
-       my ( $url, $file ) = @_;
-       my $path = "$server::base_dir/$file";
+sub files {
+       my $url = shift;
+
+       my $name = (caller(1))[3];
+       $name =~ s{config::}{} || die "caller isn't package config !";
+
+       my $path = "$server::base_dir/tftp/$name";
+
+       foreach my $file ( @_ ) {
+               mirror_file "$url/$file", "$path/$file"
+                       if first_time( "$url/$file" );
+       }
+}
+
+sub mkpath {
+       my $file = shift;
+
        my @file_parts = split m{/}, $file;
        foreach ( 1 .. $#file_parts - 1 ) {
                my $path = splice @file_parts, 0, $_;
                warn "? $path\n";
                mkdir $path unless -e $path;
        }
-       mirror_file( $url, "$server::base_dir/$file" );
 }
 
 1;