From fe462468ddf6f3906c1c0644d7eb053375f5e008 Mon Sep 17 00:00:00 2001 From: Dobrica Pavlinusic Date: Sat, 10 Dec 2011 01:13:18 +0100 Subject: [PATCH] create sent/received logs --- lib/CloudStore/API.pm | 54 +++++++++++++++++++++++++++++++++++++++---- t/API.t | 7 ++++++ 2 files changed, 57 insertions(+), 4 deletions(-) diff --git a/lib/CloudStore/API.pm b/lib/CloudStore/API.pm index d504c65..f41643a 100644 --- a/lib/CloudStore/API.pm +++ b/lib/CloudStore/API.pm @@ -3,6 +3,7 @@ use warnings; use strict; use autodie; +use File::Path qw(make_path); use Data::Dump qw(dump); sub new { @@ -22,7 +23,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; -warn "## $login ",dump(@p); die "$login: $!" if $!; my $user; $user->{$_} = shift @p foreach @n; @@ -59,20 +59,66 @@ sub create_user { return $found; } +sub mkbasepath { + my ($path,$opts) = @_; + $path =~ s{/[^/]+$}{}; + make_path $path unless -d $path; +} + +sub append { + my $self = shift @_; + my $user = shift @_; + my $path; + $user = $self->user_info($user) unless ref $user eq 'HASH'; + if ( exists $user->{dir} ) { + $path = $user->{dir} . '/.log'; + } else { + die "no dir in ", dump $user; + } + my $line = join('#',@_); + open(my $fh, '>>', $path); + print $fh "$line\n"; + close $fh; + warn "## $path $line\n"; +} + sub send_file { my ( $self, $f_uid,$f_path, $t_uid,$t_path ) = @_; - + 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"; + + mkbasepath $t_full, { uid => $t->{uid} }; + + 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; + chmod oct("0444"), $f_full; + } elsif ( $uid == $md5->{uid} ) { + warn "# send_file - shared $f_full\n"; + } + + $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 ( $uid, $from, $to ) = @_; + my ( $self, $uid, $from, $to ) = @_; + + $self->append( $uid, 'rename', $from, $to ); } sub delete { - my ( $uid, $path ) = @_; + my ( $self, $uid, $path ) = @_; + $self->append( $uid, 'delete', $path ); } sub usage { diff --git a/t/API.t b/t/API.t index 8c326a7..edbeac8 100755 --- a/t/API.t +++ b/t/API.t @@ -26,3 +26,10 @@ diag "test: $uid"; ok my $test = $o->user_info( $uid ), "user_info $uid"; diag dump($test); +ok open(my $fh, ">", $test->{dir} . '/foo.txt'), 'open'; +ok print($fh "test pid: $$\n"), 'print'; +ok close($fh) , 'close'; + +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'; -- 2.20.1