added delete
authorDobrica Pavlinusic <dpavlin@rot13.org>
Sat, 10 Dec 2011 00:51:34 +0000 (01:51 +0100)
committerDobrica Pavlinusic <dpavlin@rot13.org>
Sat, 10 Dec 2011 00:51:34 +0000 (01:51 +0100)
lib/CloudStore/API.pm
t/API.t

index f41643a..d83792a 100644 (file)
@@ -3,7 +3,8 @@ use warnings;
 use strict;
 use autodie;
 
-use File::Path qw(make_path);
+use File::Path qw(make_path remove_tree);
+use File::Find;
 use Data::Dump qw(dump);
 
 sub new {
@@ -15,6 +16,8 @@ sub new {
        $self->{PORT}   ||= $ENV{PORT}  || die "no PORT in env";
        $self->{SLICE}  ||= $ENV{SLICE} || die "no SLICE in env";
 
+       $self->{md5} = $self->user_info('md5');
+
        return $self;
 }
 
@@ -23,7 +26,6 @@ sub user_info {
 
        my @n = qw/ login passwd uid gid quota comment gecos dir shell expire /;
        my @p = $login =~ m/^\d+$/ ? getpwuid $login : getpwnam $login;
-       die "$login: $!" if $!;
        my $user;
        $user->{$_} = shift @p foreach @n;
        return $user;
@@ -87,7 +89,6 @@ sub send_file {
 
        my $f = $self->user_info($f_uid);
        my $t = $self->user_info($t_uid);
-       my $md5 = $self->user_info('md5');
 
        my $f_full = "$f->{dir}/$f_path";
        my $t_full = "$t->{dir}/$t_path";
@@ -97,28 +98,56 @@ sub send_file {
        my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size, $atime,$mtime,$ctime,$blksize,$blocks) = stat $f_full;
        if ( $uid == $f->{uid} ) {
                warn "# send_file - move $f_uid $f_path to pool\n";
-               chown $md5->{uid}, $md5->{gid}, $f_full;
+               chown $self->{md5}->{uid}, $self->{md5}->{gid}, $f_full;
                chmod oct("0444"), $f_full;
-       } elsif ( $uid == $md5->{uid} ) {
+       } elsif ( $uid == $self->{md5}->{uid} ) {
                warn "# send_file - shared $f_full\n";
        }
 
+       $self->delete( $t, $t_path ) if -e $t_full;
+
        $self->append( $f, 'sent', -s $f_full, $t->{uid}, $f_path );
        link $f_full, $t_full; 
        $self->append( $t, 'recv', -s $t_full, $f->{uid}, $t_path );
 }
 
 sub rename_file {
-       my ( $self, $uid, $from, $to ) = @_;
+       my ( $self, $user, $from, $to ) = @_;
+       $user = $self->user_info($user) unless ref $user eq 'HASH';
 
-       $self->append( $uid, 'rename', $from, $to );
+       $self->append( $user, 'rename', $from, $to );
 }
 
 
 sub delete {
-       my ( $self, $uid, $path ) = @_;
+       my ( $self, $user, $path ) = @_;
+       $user = $self->user_info($user) unless ref $user eq 'HASH';
+
+       my $deleted_size = 0;
+       my $full_path = "$user->{dir}/$path";
+
+       if ( -d $full_path ) {
+
+               find({ 
+               no_chdir => 1,
+               wanted => sub {
+                       return if -d $_;
+                       my ($uid,$size) = (stat($_))[4,7];
+                       warn "## find $uid $size $_\n";
+                       if ( $uid == $self->{md5}->{uid} ) {
+                               $deleted_size += $size;
+                       }
+               }}, $full_path);
+
+               remove_tree $full_path;
+       } else {
+               $deleted_size += -s $full_path;
+               unlink $full_path;
+       }
+
+       warn "delete $deleted_size bytes shared\n";
 
-       $self->append( $uid, 'delete', $path );
+       $self->append( $user, 'delete', -$deleted_size, $path );
 }
 
 sub usage {
diff --git a/t/API.t b/t/API.t
index edbeac8..58eb691 100755 (executable)
--- a/t/API.t
+++ b/t/API.t
@@ -2,7 +2,7 @@
 use strict;
 use warnings;
 
-use Test::More tests => 7;
+use Test::More tests => 17;
 use Data::Dump qw(dump);
 
 use lib 'lib';
@@ -29,7 +29,17 @@ diag dump($test);
 ok open(my $fh, ">", $test->{dir} . '/foo.txt'), 'open';
 ok print($fh "test pid: $$\n"), 'print';
 ok close($fh) , 'close';
+ok chown $test->{uid}, $test->{gid}, "$test->{dir}/foo.txt", 'chown';
 
 ok my $uid2 = $o->create_user('test2@example.com','password',100_000_000), 'create_user test2';
 
 ok $o->send_file( $uid => '/foo.txt', $uid2 => 'dir1/dir2/bar.txt' ), 'send_file';
+
+ok $o->send_file( $uid2 => 'dir1/dir2/bar.txt', $uid => 'bar.txt' ), 'send_file back';
+
+ok $o->delete( $uid, 'foo.txt' );
+
+ok $o->delete( $uid2, 'dir1' );
+
+ok $o->delete( $uid, 'bar.txt' );
+