Merge branch 'attr'
[cloudstore.git] / lib / CloudStore / API.pm
index f41643a..ef39524 100644 (file)
@@ -3,17 +3,37 @@ use warnings;
 use strict;
 use autodie;
 
-use File::Path qw(make_path);
+use lib 'lib';
+use base qw(CloudStore::Gearman CloudStore::MD5sum);
+
+use File::Path qw(make_path remove_tree);
+use File::Find;
 use Data::Dump qw(dump);
+use Carp qw(confess);
 
 sub new {
-       my $class = shift;
-       my $self = {@_};
+       my ($class,$slice) = @_;
+
+       my ( undef, $dir, $port, undef ) = getgrnam($slice);
+       die "can't find group $slice: $!" unless $dir && $port;
+       my $self = {
+               passwd => '/var/lib/extrausers/passwd',
+               PORT => $port,
+               SLICE => $dir,
+       };
        bless $self, $class;
 
-       $self->{passwd} ||= '/var/lib/extrausers/passwd';
-       $self->{PORT}   ||= $ENV{PORT}  || die "no PORT in env";
-       $self->{SLICE}  ||= $ENV{SLICE} || die "no SLICE in env";
+       $self->{md5} = $self->user_info("md5") || die "can't find user md5";
+       $self->{md5}->{dir} = "$dir/md5";
+       if ( ! -e $self->{md5}->{dir} ) {
+               make_path $self->{md5}->{dir}, { uid => $self->{md5}->{uid}, gid => $self->{md5}->{gid} };
+               warn "## CREATED md5pool $self->{md5}->{dir}\n";
+       }
+
+       my $name = $self->{SLICE};
+       $name =~ s/\W+/_/g;
+       $name =~ s/^_+//;
+       $self->{quota} = $name . '_quota';
 
        return $self;
 }
@@ -21,9 +41,11 @@ sub new {
 sub user_info {
        my ($self,$login) = @_;
 
+       confess "need login" unless $login;
+
        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 $!;
+       die "user_info $login: $@" if $@;
        my $user;
        $user->{$_} = shift @p foreach @n;
        return $user;
@@ -54,76 +76,197 @@ sub create_user {
 
                mkdir $dir;
                chown $max_uid, $self->{PORT}, $dir;
+
+               my $path = "$dir/.meta/secrets";
+               $self->mkbasepath($path);
+               open($fh, '>', $path);
+               print $fh "u$max_uid:$new_passwd\n";
+               close $fh;
        }
 
+       # FIXME update quota only on create?
+       $self->gearman_do( "$self->{quota}_set" => "$found $new_quota" );
+
        return $found;
 }
 
 sub mkbasepath {
-       my ($path,$opts) = @_;
+       my ($self,$path,$opts) = @_;
        $path =~ s{/[^/]+$}{};
        make_path $path unless -d $path;
 }
 
-sub append {
-       my $self = shift @_;
-       my $user = shift @_;
-       my $path;
+sub user_dir {
+       my ( $self,$user, $dir ) = @_;
        $user = $self->user_info($user) unless ref $user eq 'HASH';
+       my $path;
        if ( exists $user->{dir} ) {
-               $path = $user->{dir} . '/.log';
+               $path = $user->{dir} . '/.meta/' . $dir;
        } else {
                die "no dir in ", dump $user;
        }
-       my $line = join('#',@_);
+       $path =~ s{//+}{/}g;
+
+       if ( ! -e $path ) {
+               $self->mkbasepath( $path, { uid => $user->{uid} } );
+               open(my $fh, '>', $path);
+               close $fh;
+               chown $user->{uid}, $user->{gid}, $path;
+               warn "# user_dir created $path\n";
+       }
+
+       #warn "### user_dir $path";
+       return $path;
+}
+
+sub append {
+       my $self = shift @_;
+       $self->append_meta( 'usage', @_ );
+}
+
+sub append_meta {
+       my $self = shift @_;
+       my $log  = shift @_;
+       my $user = shift @_;
+       my $path = $self->user_dir( $user => $log );
+       my $delimiter = '#';
+          $delimiter = '  ' if $log =~ m/md5sum$/;
+       my $line = join($delimiter,@_);
        open(my $fh, '>>', $path);
        print $fh "$line\n";
        close $fh;
        warn "## $path $line\n";
 }
 
+sub usage {
+       my ( $self, $user ) = @_;
+       $user = $self->user_info($user) unless ref $user eq 'HASH';
+       my $path = $self->user_dir( $user => 'usage');
+       my $sum;
+       open(my $fh, '<', $path);
+       while(<$fh>) {
+               chomp;
+               my @v = split(/#/,$_);
+               $sum->{ $v[0] } += $v[1];
+               $sum->{_usage}  += $v[1];
+       }
+       my ( $usage, $quota ) = split(/ /,
+               $self->gearman_do( "$self->{quota}_get" => $user->{uid} )
+       );
+       $sum->{_usage} += $usage;
+       $sum->{_quota} = $quota;
+       warn "## usage ",dump($user, $sum), $/;
+       return $sum;
+}
+
 sub send_file {
        my ( $self, $f_uid,$f_path, $t_uid,$t_path ) = @_;
 
        my $f = $self->user_info($f_uid);
+       die "FIXME not on current slice" if $f->{dir} !~ m/^$self->{SLICE}/;
        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} };
+       $self->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;
+               chown $self->{md5}->{uid}, $self->{md5}->{gid}, $f_full;
                chmod oct("0444"), $f_full;
-       } elsif ( $uid == $md5->{uid} ) {
+               $self->append( $f, 'sent', -s $f_full, $t->{uid}, $f_path );
+       } elsif ( $uid == $self->{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->delete( $t, $t_path ) if -e $t_full;
+
+       my $ok = link $f_full, $t_full; 
        $self->append( $t, 'recv', -s $t_full, $f->{uid}, $t_path );
+
+       $ok = -s $t_full if $ok; # replace true with file size
+
+       my $md5;
+       if ( $f->{uid} == $self->{md5}->{uid} ) {
+               $md5 = $f_path; # we don't have local md5sum db for md5 user!
+       } else {
+               $md5 = $self->md5_get($f_full);
+       }
+       if ( ! $md5 ) {
+               warn "ERROR: no md5 for $f_path";
+               return $ok;
+       }
+
+       $self->append_meta('md5sum', $t, $md5 => $t_path ); # md5sum for received files!
+
+       return $ok;
 }
 
 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 );
+       my $f_full = "$user->{dir}/$from";
+       my $t_full = "$user->{dir}/$to";
+
+       $self->mkbasepath( $t_full, { uid => $user->{uid}, gid => $user->{gid} } );
+       my $ok = rename $f_full, $t_full;
+
+       my $md5 = $self->md5_get($t_full);
+       if ( ! $md5 ) {
+               warn "ERROR: no md5sum for $from";
+               return $ok; # XXX our internal error
+       }
+
+       $self->append_meta('md5sum', $user, 'rename' => $from );
+       $self->append_meta('md5sum', $user, $md5 => $from );
+
+       return $ok;
 }
 
 
 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";
 
-       $self->append( $uid, 'delete', $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( $user, 'delete', -$deleted_size, $user->{uid}, $path );
+       $self->append_meta('md5sum', $user, 'delete', $path );
+
+       return $full_path;
 }
 
-sub usage {
-       my ( $uid ) = @_;
+sub file_size {
+       my ( $self, $user, $path ) = @_;
+       $user = $self->user_info($user) unless ref $user eq 'HASH';
 
+       my $full_path = "$user->{dir}/$path";
+       return -s $full_path;
 }
 
 1;