1 package CloudStore::API;
7 use base qw(CloudStore::Gearman);
9 use File::Path qw(make_path remove_tree);
11 use Data::Dump qw(dump);
14 my ($class,$group) = @_;
16 my ( undef, $dir, $port, undef ) = getgrnam($group) || die "can't find group $group: $!";
18 passwd => '/var/lib/extrausers/passwd',
24 $self->{md5} = $self->user_info('md5');
30 my ($self,$login) = @_;
32 my @n = qw/ login passwd uid gid quota comment gecos dir shell expire /;
33 my @p = $login =~ m/^\d+$/ ? getpwuid $login : getpwnam $login;
35 $user->{$_} = shift @p foreach @n;
40 my ( $self, $new_email, $new_passwd, $new_quota ) = @_;
45 open(my $fh, '<', $self->{passwd});
47 my ( $login, $passwd, $uid, $gid, $email, $dir, $shell ) = split(/:/,$_);
48 $max_uid = $uid if $uid > $max_uid;
49 $found = $uid if $email eq $new_email;
55 my $dir = "$self->{SLICE}/$max_uid";
56 warn "# create_user $new_email $new_quota = $max_uid $dir";
57 open(my $fh, '>>', $self->{passwd});
58 print $fh "u$max_uid:$new_passwd:$max_uid:$self->{PORT}:$new_email:$dir:/bin/true\n";
63 chown $max_uid, $self->{PORT}, $dir;
67 # FIXME update quota only on create?
68 $self->gearman_do( 'narada_s1_quota_set' => "$found $new_quota" );
74 my ($path,$opts) = @_;
75 $path =~ s{/[^/]+$}{};
76 make_path $path unless -d $path;
80 my ( $self,$user, $dir ) = @_;
81 $user = $self->user_info($user) unless ref $user eq 'HASH';
83 if ( exists $user->{dir} ) {
84 $path = $user->{dir} . '/' . $dir;
86 die "no dir in ", dump $user;
89 # warn "## user_dir $path";
96 my $path = $self->user_dir( $user => '.log');
97 my $line = join('#',@_);
98 open(my $fh, '>>', $path);
101 warn "## $path $line\n";
102 $user = $self->user_info($user) unless ref $user eq 'HASH';
106 my ( $self, $user ) = @_;
107 $user = $self->user_info($user) unless ref $user eq 'HASH';
108 my $path = $self->user_dir( $user => '.log');
110 open(my $fh, '<', $path);
113 my @v = split(/#/,$_);
114 $sum->{ $v[0] } += $v[1];
115 $sum->{_usage} += $v[1];
117 my ( $usage, $quota ) = split(/ /,
118 $self->gearman_do( 'narada_s1_quota_get' => $user->{uid} )
120 $sum->{_usage} += $usage;
121 $sum->{_quota} = $quota;
122 warn "## usage ",dump($user, $sum), $/;
127 my ( $self, $f_uid,$f_path, $t_uid,$t_path ) = @_;
129 my $f = $self->user_info($f_uid);
130 my $t = $self->user_info($t_uid);
132 my $f_full = "$f->{dir}/$f_path";
133 my $t_full = "$t->{dir}/$t_path";
135 mkbasepath $t_full, { uid => $t->{uid} };
137 my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size, $atime,$mtime,$ctime,$blksize,$blocks) = stat $f_full;
138 if ( $uid == $f->{uid} ) {
139 warn "# send_file - move $f_uid $f_path to pool\n";
140 chown $self->{md5}->{uid}, $self->{md5}->{gid}, $f_full;
141 chmod oct("0444"), $f_full;
142 $self->append( $f, 'sent', -s $f_full, $t->{uid}, $f_path );
143 } elsif ( $uid == $self->{md5}->{uid} ) {
144 warn "# send_file - shared $f_full\n";
147 $self->delete( $t, $t_path ) if -e $t_full;
149 link $f_full, $t_full;
150 $self->append( $t, 'recv', -s $t_full, $f->{uid}, $t_path );
154 my ( $self, $user, $from, $to ) = @_;
155 $user = $self->user_info($user) unless ref $user eq 'HASH';
157 $self->append( $user, 'rename', $from, $to );
162 my ( $self, $user, $path ) = @_;
163 $user = $self->user_info($user) unless ref $user eq 'HASH';
165 my $deleted_size = 0;
166 my $full_path = "$user->{dir}/$path";
168 if ( -d $full_path ) {
174 my ($uid,$size) = (stat($_))[4,7];
175 warn "## find $uid $size $_\n";
176 if ( $uid == $self->{md5}->{uid} ) {
177 $deleted_size += $size;
181 remove_tree $full_path;
183 $deleted_size += -s $full_path;
187 warn "delete $deleted_size bytes shared\n";
189 $self->append( $user, 'delete', -$deleted_size, $user->{uid}, $path );