[Rt-commit] r5841 - in Mnemonic: . lib lib/Mnemonic lib/Mnemonic/Crypto

jesse at bestpractical.com jesse at bestpractical.com
Sun Sep 3 00:50:33 EDT 2006


Author: jesse
Date: Sun Sep  3 00:50:33 2006
New Revision: 5841

Modified:
   Mnemonic/   (props changed)
   Mnemonic/bin/mnemonic
   Mnemonic/lib/Mnemonic.pm
   Mnemonic/lib/Mnemonic/Crypto/OpenPGP.pm
   Mnemonic/lib/Mnemonic/FileSet.pm

Log:
 r26975 at pinglin:  jesse | 2006-09-02 16:24:02 -0400
 * Fixed the restore insanity


Modified: Mnemonic/bin/mnemonic
==============================================================================
--- Mnemonic/bin/mnemonic	(original)
+++ Mnemonic/bin/mnemonic	Sun Sep  3 00:50:33 2006
@@ -1,4 +1,4 @@
-#!/usr/bin/perl -w
+#!/usr/bin/env perl -w
 use warnings;
 use strict;
 use Mnemonic;

Modified: Mnemonic/lib/Mnemonic.pm
==============================================================================
--- Mnemonic/lib/Mnemonic.pm	(original)
+++ Mnemonic/lib/Mnemonic.pm	Sun Sep  3 00:50:33 2006
@@ -1,52 +1,54 @@
-#!/usr/bin/perl -w
 use warnings;
 use strict;
 
-
 package Mnemonic;
 
 use IO::All;
 use Digest::SHA qw(sha256);
 use Digest::MD5 qw(md5_hex);
-use YAML::Syck;
-use File::Path;
-use UNIVERSAL::require;
-use Mnemonic::FileSet;
-use Mnemonic::Crypto::OpenPGP;
+use YAML::Syck ();
+use File::Path ();
+use UNIVERSAL::require  ();
+use Mnemonic::FileSet ();
+use Mnemonic::Crypto::OpenPGP ();
+use Shell::Command ();
 
 use base qw/Class::Accessor/;
 
-BEGIN {__PACKAGE__->mk_accessors(qw/backend config_file config pgp/);}
-
+BEGIN { __PACKAGE__->mk_accessors(qw/backend config_file config pgp/); }
 
 sub init {
     my $self = shift;
     $self->load_config();
 
-    $self->pgp( Mnemonic::Crypto::OpenPGP->new({config => $self->config}));
+    $self->pgp(
+        Mnemonic::Crypto::OpenPGP->new( { config => $self->config } ) );
     my $backend = $self->config->{'backend'} || "Mnemonic::Backend::Tmp";
     $backend->require() || die $@;
-    $self->backend( $backend->new({ config => $self->config()}) );
+    $self->backend( $backend->new( { config => $self->config() } ) );
     $self->backend->init();
 }
 
 sub load_config {
     my $self = shift;
-    for ($self->config_file, $ENV{'MNEMONIC_CONFIG'}, $ENV{'HOME'}."/.mnemonic_config", "/etc/mnemonic_config") {
-        if ( $_ and -f $_) {
-            eval {
-                $self->config(YAML::Syck::LoadFile($_));
-            }; warn $@ if( $@);
+    for (
+        $self->config_file,                 $ENV{'MNEMONIC_CONFIG'},
+        $ENV{'HOME'} . "/.mnemonic_config", "/etc/mnemonic_config"
+        )
+    {
+        if ( $_ and -f $_ ) {
+            eval { $self->config( YAML::Syck::LoadFile($_) ); };
+            warn $@ if ($@);
         }
     }
-    unless ($self->config){ 
-        $self->config({});
-        }
+    unless ( $self->config ) {
+        $self->config( {} );
+    }
 
 }
 
 sub list_backups {
-    my $self    = shift;
+    my $self = shift;
     return ( $self->backend->get_keys(@_) );
 }
 
@@ -54,71 +56,78 @@
     my $self = shift;
     my %args = (
         stored_keys => undef,
-        path       => undef,
-        skip     => undef,
-        dry_run => undef,
+        path        => undef,
+        skip        => undef,
+        dry_run     => undef,
         @_
     );
 
     my %manifest;
 
-    my $search = Mnemonic::FileSet->new({ search_paths => $args{'path'}, skip_patterns => $args{'skip'}});
+    my $search = Mnemonic::FileSet->new(
+        { search_paths => $args{'path'}, skip_patterns => $args{'skip'} } );
     my @manifest = $search->search();
-    # store a key with a content-type and some optional metadata
 
+    # store a key with a content-type and some optional metadata
 
     my $seen_sha256 = {};
-    $seen_sha256->{$_} = "prestored" for (@{$args{'stored_keys'}});
-    
-    foreach my $filename ( @manifest ) {
-        warn "\t$filename\n";
-        next if ($args{dry_run});
-        my $sha256=   $self->store_file(manifest =>\%manifest, filename => $filename, stored_keys => $seen_sha256);
-        $seen_sha256->{$sha256} = $filename
+    $seen_sha256->{$_} = "prestored" for ( @{ $args{'stored_keys'} } );
+
+    foreach my $filename (@manifest) {
+        next if ( $args{dry_run} );
+        my $sha256 = $self->store_file(
+            manifest    => \%manifest,
+            filename    => $filename,
+            stored_keys => $seen_sha256
+        );
+        $seen_sha256->{$sha256} = $filename;
     }
-    return undef if ($args{'dry_run'});
+    return undef if ( $args{'dry_run'} );
 
     my $hostname = `hostname`;
     chomp $hostname;
     my $manifest_id = 'MANIFEST/' . $hostname . "/" . time() . '-' . $$;
     $self->backend->store( $manifest_id => YAML::Syck::Dump( \%manifest ) );
+    warn "Completed backup $manifest_id\n";
     return $manifest_id;
 }
 
-        sub store_file {
-            my $self = shift;
-            my %args = ( manifest => undef,
-                         filename => undef,
-                         @_);
+sub store_file {
+    my $self = shift;
+    my %args = (
+        manifest => undef,
+        filename => undef,
+        @_
+    );
 
-        my $filename = $args{'filename'};
-        my $manifest = $args{'manifest'};
+    my $filename = $args{'filename'};
+    my $manifest = $args{'manifest'};
 
-        my @path = File::Spec->splitdir($filename);
-        pop @path;
-        shift @path;
-        my $path = '';
-        while ( my $dir = shift @path ) {
-            $path = File::Spec->catdir( $path, $dir );
-            next unless -d $path;
-            next if ( exists $manifest->{$path} );
-
-            $manifest->{$path} = {
-                type     => 'directory',
-                statinfo => [ stat($path) ],
-                stored   => '0'
-                }
+    my @path = File::Spec->splitdir($filename);
+    pop @path;
+    shift @path;
+    my $path = '';
+    while ( my $dir = shift @path ) {
+        $path = File::Spec->catdir( $path, $dir );
+        next unless -d $path;
+        next if ( exists $manifest->{$path} );
+
+        $manifest->{$path} = {
+            type     => 'directory',
+            statinfo => [ stat($path) ],
+            stored   => '0'
+            }
 
-        } 
-        if (-d $filename) {
-            $manifest->{$filename} = {
-                type     => 'directory',
-                ttatinfo => [ stat($filename) ],
-                stored   => '0'
-                }
+    }
+    if ( -d $filename ) {
+        $manifest->{$filename} = {
+            type     => 'directory',
+            ttatinfo => [ stat($filename) ],
+            stored   => '0'
+            }
 
-        } elsif (-f $filename) {
-            if (-z $filename)  {
+    } elsif ( -f $filename ) {
+        if ( -z $filename ) {
             $manifest->{$filename} = {
                 type     => 'file',
                 size     => '0',
@@ -126,33 +135,37 @@
                 stored   => '0'
                 }
 
-
-            } else {
-            eval {
-        my $sha256_sum = _get_sha256($filename);
-        if ( $args{'stored_keys'}->{ $sha256_sum } ) {
-            warn "\t found $sha256_sum on the server. passing on it\n";
-
         } else {
-            warn "\t as $sha256_sum\n";
-        my $plaintext < io $filename;
-        my $cyphertext = $self->pgp->encrypt( $plaintext );
-            $self->backend->store( $sha256_sum => $cyphertext, );
-        }
-
-
-        my @stat       = stat($filename);
+            eval {
+                my $sha256_sum = _get_sha256($filename);
+                if ( $args{'stored_keys'}->{$sha256_sum} ) {
+                    warn "\t= $filename already stored\n";
+
+                } else {
+                    my $plaintext < io $filename;
+                    my $cyphertext = $self->pgp->encrypt($plaintext);
+                    eval {
+                        $self->backend->store( $sha256_sum => $cyphertext, );
+                        warn "\t+ $filename uploaded\n";
+                    };
+                    if ($@) {
+                        warn "\t!ERROR: $filename: $@\n";
+                    }
+                }
 
+                my @stat = stat($filename);
 
-        $manifest->{$filename} = {
-            key            => $sha256_sum,
-            statinfo       => \@stat,
-            stored         => 1
-        };
-    }; if ($@){
-        $manifest->{'!errors'}->{$filename} = $@;
-    }
-    } 
+                $manifest->{$filename} = {
+                    key      => $sha256_sum,
+                    statinfo => \@stat,
+                    stored   => 1,
+                    type    => 'file'
+                };
+            };
+            if ($@) {
+                $manifest->{'!errors'}->{$filename} = $@;
+            }
+        }
     }
 }
 
@@ -164,12 +177,12 @@
         @_
     );
 
-    $args{'restore_root'} = File::Spec->catdir( '/tmp', $args{'manifest_id'} );
-
+    $args{'restore_root'} ||= File::Spec->catdir( '/tmp', $args{'manifest_id'} );
     my $root = $args{'restore_root'};
     File::Path::mkpath( [$root] );
 
     my $result = $self->backend->fetch( $args{'manifest_id'} );
+
     #{ content_type, etag, value, @meta }
     my $manifest = YAML::Syck::Load( $result->{value} );
 
@@ -181,49 +194,72 @@
             File::Path::mkpath( [$dir] );
 
         }
+
         my $target = File::Spec->catfile( $root, $file );
-        warn "Fetching $file\n";
-        if  (($manifest->{$file}->{stored}||0) > 0 ) {
-            warn "...it's a file";
-            my $file_result = $self->backend->fetch( $manifest->{$file}->{key} );
-            my $pt = $self->pgp->decrypt( $file_result->{'value'} => $target);
-            unless ( $manifest->{$file}->{key} eq _get_sha256($target) ) {
+
+        if ($manifest->{$file}->{type} eq 'file') {
+            $self->restore_file($file => $target => $manifest->{$file});
+        } elsif($manifest->{$file}->{type} eq 'directory') {
+            $self->restore_directory($file => $target);
+
+        } else {
+            warn "WTF $file". YAML::Syck::Dump($manifest->{$file});;
+        }
+        $self->update_statinfo( $target => $manifest->{$file}->{statinfo} );
+        warn "\t+ Wrote $target\n";
+    }
+}
+
+
+sub restore_directory {
+        my $self =shift;
+        my $dir = shift;
+        my $target = shift;
+
+            File::Path::mkpath( [$target] );
+}
+
+sub restore_file {
+        my $self =shift;
+        my $file = shift;
+        my $target = shift;
+        my $manifest_entry = shift;
+
+        if ( ( $manifest_entry->{stored} || 0 ) > 0 ) {
+            my $file_result = $self->backend->fetch( $manifest_entry->{key} );
+            my $pt = $self->pgp->decrypt( $file_result->{'value'} => $target );
+            unless ( $manifest_entry->{key} eq _get_sha256($target) ) {
                 warn "$file has an invalid SHA256 sum after decryption";
             }
-        } else {
+        } elsif ($manifest_entry->{size} ==0  ) {
+            Shell::Command::touch $target;
+
+        } else{
             warn "Not fetching $file\n";
 
         }
-        if ((-f $target or -d $target) && $manifest->{$file}->{statinfo} ) {
-        my @stat = @{ $manifest->{$file}->{statinfo} };
-        my $mode = $stat[2] & 07777;
-        chmod $mode, $target || die $@;
-        utime $stat[8], $stat[9], $target;
-        chown $stat[4], $stat[5], $target;
-        print "Wrote $file to $target\n";
-    }
-    }
 }
 
 sub remove_key_from_store {
     my $self = shift;
-    my %args =  (key => undef,
-                 @_);
+    my %args = (
+        key => undef,
+        @_
+    );
 
-    $self->backend->delete($args{'key'});
+    $self->backend->delete( $args{'key'} );
 
 }
 
-
 sub list_backup_files {
     my $self = shift;
     my %args = (@_);
 
-    my $result = $self->backend->fetch( $args{'manifest_id'} );
+    my $result   = $self->backend->fetch( $args{'manifest_id'} );
     my $manifest = YAML::Syck::Load( $result->{value} );
     warn "This backup would restore:\n";
-    foreach my $file (sort keys %$manifest) {
-        print $file ."\n";
+    foreach my $file ( sort keys %$manifest ) {
+        print $file . "\n";
     }
 
 }
@@ -236,4 +272,27 @@
     return $sha->hexdigest;
 }
 
+
+
+
+        sub update_statinfo {
+            my $self = shift;
+            my $target = shift;
+            my $info = shift;
+
+
+
+if ( ( -f $target or -d $target ) && $info) {
+        {
+            my @stat = @{ $info};
+            my $mode = $stat[2] & 07777;
+            chmod $mode, $target || die $@;
+            utime $stat[8], $stat[9], $target;
+            chown $stat[4], $stat[5], $target;
+        }
+    }
+
+
+
+        }
 1;

Modified: Mnemonic/lib/Mnemonic/Crypto/OpenPGP.pm
==============================================================================
--- Mnemonic/lib/Mnemonic/Crypto/OpenPGP.pm	(original)
+++ Mnemonic/lib/Mnemonic/Crypto/OpenPGP.pm	Sun Sep  3 00:50:33 2006
@@ -1,9 +1,9 @@
-#!/usr/bin/perl -w
+package Mnemonic::Crypto::OpenPGP;
+
 use warnings;
 use strict;
 
 
-package Mnemonic::Crypto::OpenPGP;
 
     use Crypt::OpenPGP;
     use Crypt::OpenPGP::KeyRing;
@@ -34,7 +34,6 @@
 sub encrypt {
     my $self = shift;
     my $plaintext = shift;
-    warn "here";
     my $cyphertext;
     eval {
      $cyphertext = $self->pgp->encrypt(

Modified: Mnemonic/lib/Mnemonic/FileSet.pm
==============================================================================
--- Mnemonic/lib/Mnemonic/FileSet.pm	(original)
+++ Mnemonic/lib/Mnemonic/FileSet.pm	Sun Sep  3 00:50:33 2006
@@ -62,7 +62,6 @@
 
     
     , @{$self->search_paths});
-    print "\nDone";
     return @files;
 }
 


More information about the Rt-commit mailing list