implement and test mv from pending log
authorDobrica Pavlinusic <dpavlin@rot13.org>
Sun, 9 Oct 2011 12:09:26 +0000 (14:09 +0200)
committerDobrica Pavlinusic <dpavlin@rot13.org>
Sun, 9 Oct 2011 12:09:26 +0000 (14:09 +0200)
lib/CloudStore/Store.pm
test.sh

index 52c7ee7..8ba2ba4 100644 (file)
@@ -35,6 +35,12 @@ sub user_get {
        my ( $self,$data ) = @_;
 }
 
+sub mkbasedir {
+       my $dir = shift;
+       $dir =~ s{/[^/]+$}{}; # strip filename
+       mkdir $dir unless -e $dir;
+}
+
 sub modify_file {
        my ( $self,$data ) = @_;
 
@@ -44,8 +50,8 @@ sub modify_file {
                my $sent_files;
                open(my $send, '<', $self->blob_path($data) );
                while(<$send>) {
-warn $_;
-                       chomp;
+                       s/[\n\r]+$//;
+
                        my ( $to, $file ) = split(/\s+/,$_,2);
                        my ( undef, undef, $uid, $gid, undef, undef, $email, $dir, $shell ) =
                                getpwnam $to;
@@ -60,9 +66,7 @@ warn $_;
                        }
 
                        my $to_path = "$dir/received/$file";
-                       my $to_dir = $to_path;
-                       $to_dir =~ s{/[^/]+$}{};
-                       mkdir $to_dir unless -e $to_dir;
+                       mkbasedir $to_path;
 
                        warn "SEND $from_path -> $to_path";
                        $sent_files->{$to} += link $from_path, $to_path;
@@ -71,6 +75,32 @@ warn $_;
 
                warn "SENT ",dump $sent_files;
 
+               return 0; # skip dedup
+       } elsif ( $data->{file} =~ m{^(.*/).sync/pending/([^/]+)$} ) {
+               my $from_dir = $1;
+               warn "PENDIG $2 from $from_dir";
+               open(my $pend, '<', $self->blob_path($data) );
+               while(<$pend>) {
+                       s/[\n\r]+$//;
+warn $_;
+                       if ( ! /^MOVED\#/ ) {
+                               warn "skip $_\n";
+                               next;
+                       }
+
+                       my ( undef, $from, $to ) = split(/\#/,$_,3);
+
+                       my ( $from_path, $to_path ) = map {
+                               my $tmp = $data;
+                               $tmp->{file} = $from_dir . $_;
+                               $self->blob_path($tmp);
+                       } ( $from, $to );
+
+                       warn "MV $from_path -> $to_path";
+                       mkbasedir $to_path;
+                       rename $from_path, $to_path;
+               }
+
                return 0; # skip dedup
        }
 
diff --git a/test.sh b/test.sh
index 8677ce6..200af6b 100755 (executable)
--- a/test.sh
+++ b/test.sh
@@ -1,9 +1,6 @@
 #!/bin/sh -xe
 
-user_dir=/srv/cloudstore/users/test/
-rm -Rvf $user_dir/log/* #$user_dir/blob/*
-
-RSYNC_LOGIN=u2000
+RSYNC_LOGIN=fake
 
 push() {
        file=`shift`
@@ -80,8 +77,6 @@ push tmp -v
 rm tmp
 push . -r --delete -v
 
-fi # skip tests
-
 # test send
 
 ps ax >> send
@@ -100,3 +95,15 @@ __SEND__
 # order is important! .sync must be last!
 push send dir1 dir2 .sync -rv
 
+
+fi # skip tests
+
+# test file move
+
+pending=/tmp/pending.0610201109464117
+grep ^MOVED $pending | cut -d\# -f2 | xargs -i sh -c "ps ax > {}"
+push . -rv
+
+mkdir -p .sync/pending
+cp $pending .sync/pending/
+push .sync -rv