1 package CloudStore::API;
7 use base qw(CloudStore::Gearman CloudStore::MD5sum);
9 use File::Path qw(make_path remove_tree);
11 use Data::Dump qw(dump);
12 use Carp qw(confess cluck);
15 my ($class,$slice) = @_;
17 cluck "DEPRICIATED $slice specified" if $slice;
20 passwd => '/var/lib/extrausers/passwd',
24 $self->{md5} = $self->user_info("md5") || die "can't find user md5";
26 $self->{md5}->{dir} = "$dir/md5";
27 if ( ! -e $self->{md5}->{dir} ) {
28 make_path $self->{md5}->{dir}, { uid => $self->{md5}->{uid}, gid => $self->{md5}->{gid} };
29 warn "## CREATED md5pool $self->{md5}->{dir}\n";
37 my ($self,$slice) = @_;
38 my ( undef, $dir, $port, undef ) = getgrnam($slice);
39 warn "# slice_dir_port $slice = $dir $port\n";
40 return ( $dir, $port );
49 return join('_', $dir, @_);
53 my ($self,$login) = @_;
55 confess "need login" unless $login;
57 my @n = qw/ login passwd uid gid quota comment gecos dir shell expire /;
58 my @p = $login =~ m/^\d+$/ ? getpwuid $login : getpwnam $login;
59 die "user_info $login: $@" if $@;
61 $user->{$_} = shift @p foreach @n;
66 my ( $self, $new_email, $new_passwd, $new_quota ) = @_;
71 open(my $fh, '<', $self->{passwd});
73 my ( $login, $passwd, $uid, $gid, $email, $dir, $shell ) = split(/:/,$_);
74 $max_uid = $uid if $uid > $max_uid;
75 $found = $uid if $email eq $new_email;
79 my $slice = $ENV{SLICE} || 's1';
80 my ( $dir, $port ) = $self->slice_dir_port( $slice );
85 warn "# create_user $slice $new_email $new_quota = $max_uid $dir";
86 open(my $fh, '>>', $self->{passwd});
87 print $fh "u$max_uid:$new_passwd:$max_uid:$port:$new_email:$dir:/bin/true\n";
92 chown $max_uid, $port, $dir;
94 my $path = "$dir/.meta/secrets";
95 $self->mkbasepath($path);
96 open($fh, '>', $path);
97 print $fh "u$max_uid:$new_passwd\n";
101 # FIXME update quota only on create?
102 $self->gearman_do( $self->dir2gearman( $dir, 'quota', 'set' ) => "$found $new_quota" );
108 my ($self,$path,$opts) = @_;
109 $path =~ s{/[^/]+$}{};
110 make_path $path unless -d $path;
114 my ( $self,$user, $dir ) = @_;
115 $user = $self->user_info($user) unless ref $user eq 'HASH';
117 if ( exists $user->{dir} ) {
118 $path = $user->{dir} . '/.meta/' . $dir;
120 die "no dir in ", dump $user;
125 $self->mkbasepath( $path, { uid => $user->{uid} } );
126 open(my $fh, '>', $path);
128 chown $user->{uid}, $user->{gid}, $path;
129 warn "# user_dir created $path\n";
132 #warn "### user_dir $path";
138 $self->append_meta( 'usage', @_ );
145 my $path = $self->user_dir( $user => $log );
147 $delimiter = ' ' if $log =~ m/md5sum$/;
148 my $line = join($delimiter,@_);
149 open(my $fh, '>>', $path);
152 warn "## $path $line\n";
156 my ( $self, $user ) = @_;
157 $user = $self->user_info($user) unless ref $user eq 'HASH';
158 my $path = $self->user_dir( $user => 'usage');
160 open(my $fh, '<', $path);
163 my @v = split(/#/,$_);
164 $sum->{ $v[0] } += $v[1];
165 $sum->{_usage} += $v[1];
167 my ( $usage, $quota ) = split(/ /,
168 $self->gearman_do( $self->dir2gearman( $user->{dir}, 'quota', 'get' ) => $user->{uid} )
170 $sum->{_usage} += $usage;
171 $sum->{_quota} = $quota;
172 warn "## usage ",dump($user, $sum), $/;
177 my ( $self, $f_uid,$f_path, $t_uid,$t_path ) = @_;
179 my $f = $self->user_info($f_uid);
180 die "FIXME not on current slice" if $f->{dir} !~ m/^$ENV{SLICE}/;
181 my $t = $self->user_info($t_uid);
183 my $f_full = "$f->{dir}/$f_path";
184 my $t_full = "$t->{dir}/$t_path";
186 $self->mkbasepath( $t_full, { uid => $t->{uid} } );
188 my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size, $atime,$mtime,$ctime,$blksize,$blocks) = stat $f_full;
189 if ( $uid == $f->{uid} ) {
190 warn "# send_file - move $f_uid $f_path to pool\n";
191 chown $self->{md5}->{uid}, $self->{md5}->{gid}, $f_full;
192 chmod oct("0444"), $f_full;
193 $self->append( $f, 'sent', -s $f_full, $t->{uid}, $f_path );
194 } elsif ( $uid == $self->{md5}->{uid} ) {
195 warn "# send_file - shared $f_full\n";
198 $self->delete( $t, $t_path ) if -e $t_full;
200 my $size = -s $f_full;
206 $ok = link $f_full, $t_full
208 if ( ! $ok || $! =~ m/cross-link/ ) {
209 $ok = symlink $f_full, $t_full;
213 if ( $f->{uid} == $self->{md5}->{uid} ) {
214 $md5 = $f_path; # we don't have local md5sum db for md5 user!
216 $md5 = $self->md5_get($f_full);
222 $self->append( $t, 'recv', $size, $f->{uid}, $t_path );
223 $self->append_meta('md5sum', $t, $md5 => $t_path ) if $md5; # md5sum for received files! FIXME -- cross-slice md5
230 my ( $self, $user, $from, $to ) = @_;
231 $user = $self->user_info($user) unless ref $user eq 'HASH';
233 my $f_full = "$user->{dir}/$from";
234 my $t_full = "$user->{dir}/$to";
236 $self->mkbasepath( $t_full, { uid => $user->{uid}, gid => $user->{gid} } );
237 my $ok = rename $f_full, $t_full;
239 my $md5 = $self->md5_get($t_full);
241 warn "ERROR: no md5sum for $from";
242 return $ok; # XXX our internal error
245 $self->append_meta('md5sum', $user, 'rename' => $from );
246 $self->append_meta('md5sum', $user, $md5 => $from );
253 my ( $self, $user, $path ) = @_;
254 $user = $self->user_info($user) unless ref $user eq 'HASH';
256 my $deleted_size = 0;
257 my $full_path = "$user->{dir}/$path";
259 if ( -d $full_path ) {
265 my ($uid,$size) = (stat($_))[4,7];
266 warn "## find $uid $size $_\n";
267 if ( $uid == $self->{md5}->{uid} ) {
268 $deleted_size += $size;
272 remove_tree $full_path;
274 $deleted_size += -s $full_path;
278 warn "delete $deleted_size bytes shared\n";
280 $self->append( $user, 'delete', -$deleted_size, $user->{uid}, $path );
281 $self->append_meta('md5sum', $user, 'delete', $path );
287 my ( $self, $user, $path ) = @_;
288 $user = $self->user_info($user) unless ref $user eq 'HASH';
290 my $full_path = "$user->{dir}/$path";
291 return -s $full_path;