first working /.send/ implementation for all cases
[cloudstore.git] / lib / CloudStore / Couchbase.pm
index d2a0c39..c59a1ac 100644 (file)
@@ -188,18 +188,23 @@ sub new_link {
        if ( $data->{file} =~ m{^(.*/?)\.send/([^/]+)/(.+)$} ) {
                my ( $dir, $to, $name ) = ( $1, $2, $3 );
                my $path = "users/$data->{login}/blob/" . $data->{file};
-               my $file = readlink $path;
-               warn "## $path -> $file";
-               if ( $file =~ s{^\Q/rsyncd-munged/../../\E}{$dir} ) {
-
-                       while ( $file =~ s{/[^/]+/../}{/} ) {
-                               warn "cleanup parent dir /../ $file";
+               my $link_to = readlink $path;
+               warn "$link_to";
+               if ( $link_to =~ s{^\Q/rsyncd-munged/\E}{/} ) {
+
+                       my $s = $path;
+                       $s =~ s{/[^/]+$}{}; # strip filename
+                       while ( $link_to =~ s{/../}{/} ) {
+                               $s =~ s{/[^/]+$}{} || die "can't strip $s";
+                               warn "## simplify $s $link_to\n";
                        }
+                       $s .= $link_to;
 
-                       warn "SEND To:$to Name:$name File:$file\n";
-                       my $s = "users/$data->{login}/blob/$file";
                        my $d = "users/$to/blob";
-                       die "no user $to" unless -e $d;
+                       if ( ! -e $d ) {
+                               warn "ERROR: no to user $to in $d";
+                               return;
+                       }
                        $d .= "/$name";
 
                        # $name can contain directories so we must create them
@@ -207,18 +212,26 @@ sub new_link {
                        $to_dir =~ s{/[^/]+$}{};
                        make_path $to_dir if ! -e $to_dir;
 
-                       warn "link $s -> $d\n";
-                       link $s, $d;
+                       if ( ! -e $s ) {
+                               warn "ERROR: can't find source $s";
+                       } else {
+
+                               warn "link $s -> $d\n";
+                               link $s, $d;
+
+                               my ($l,$f) = ($1,$2) if $s =~ m{users/([^/]+)/blob/(.+)};
+
+                               my $origin = $self->file_get({
+                                       login => $l,
+                                       file  => $f,
+                               });
+                               $self->new_file($origin);
+                               warn "INFO: sent file ",dump($origin);
+                       }
 
-                       my $origin = $self->file_get({
-                               login => $data->{login},
-                               file  => $file,
-                       });
-                       $self->new_file($origin);
-                       warn "INFO: sent file ",dump($origin);
 
                } else {
-                       warn "ERROR: can't SEND To:$to Name:$name File:$file";
+                       warn "ERROR: can't SEND To:$to Name:$name Link:$link_to";
                }
        }
 }