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 {
$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;
}
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;
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";
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 {
use strict;
use warnings;
-use Test::More tests => 7;
+use Test::More tests => 17;
use Data::Dump qw(dump);
use lib 'lib';
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' );
+