Index: /trunk/Nebulous/Build.PL
===================================================================
--- /trunk/Nebulous/Build.PL	(revision 17609)
+++ /trunk/Nebulous/Build.PL	(revision 17610)
@@ -80,4 +80,5 @@
     create_makefile_pl  => 'passthrough',
     requires            => {
+        'Digest::MD5'           => 0,
         'File::Copy'            => 0,
         'Time::HiRes'           => 0,
Index: /trunk/Nebulous/Changes
===================================================================
--- /trunk/Nebulous/Changes	(revision 17609)
+++ /trunk/Nebulous/Changes	(revision 17610)
@@ -2,4 +2,7 @@
 
 0.09
+    - change Nebulous::Client->replicate() to check the md5sum of the
+      replicated file and to not leave empty instances laying around if there
+      is a failure
     - add a virtual "summary" volume to neb-df
     - fix neb-df coding issues
Index: /trunk/Nebulous/lib/Nebulous/Client.pm
===================================================================
--- /trunk/Nebulous/lib/Nebulous/Client.pm	(revision 17609)
+++ /trunk/Nebulous/lib/Nebulous/Client.pm	(revision 17610)
@@ -1,5 +1,5 @@
 # Copyright (c) 2004-2008  Joshua Hoblitt
 #
-# $Id: Client.pm,v 1.45 2008-05-06 03:03:50 jhoblitt Exp $
+# $Id: Client.pm,v 1.46 2008-05-09 21:15:07 jhoblitt Exp $
 
 package Nebulous::Client;
@@ -11,4 +11,5 @@
 our $VERSION = '0.08';
 
+use Digest::MD5;
 use File::Copy qw();
 use Log::Log4perl qw(get_logger :levels);
@@ -217,9 +218,25 @@
     my $new_fh;
     eval {
-        $new_fh = _open_uri( $uri, '>' );
+        # must open read/write so we can check the md5sum
+        $new_fh = _open_uri( $uri, '+>' );
     };
     $log->logdie( $@ ) if $@;
 
-    File::Copy::copy( $fh, $new_fh ) or $log->logdie( "can not copy instance $uri" );
+    my $success = File::Copy::copy( $fh, $new_fh );
+    unless ($success) {
+        $self->delete_instance("$uri");
+        $log->logdie( "can not copy instance $uri" );
+    } 
+
+    # check md5sum
+    my $src_md5 = Digest::MD5->new->addfile($fh)->hexdigest;
+    $log->debug( "md5sum src instance: $src_md5" );
+    my $dst_md5 = Digest::MD5->new->addfile($new_fh)->hexdigest;
+    $log->debug( "md5sum dst instance: $dst_md5" );
+
+    unless ($src_md5 eq $dst_md5) {
+        $self->delete_instance("$uri");
+        $log->logdie( "md5sum mismatch" );
+    }
 
     $log->debug( "copied instance" );
@@ -232,4 +249,5 @@
     return $uri;
 }
+
 
 sub cull {
