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);
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;
65 open($fh, '>', "$dir/.meta/secrets");
66 print $fh "u$max_uid:$new_passwd\n";
70 # FIXME update quota only on create?
71 $self->gearman_do( 'narada_s1_quota_set' => "$found $new_quota" );
77 my ($self,$path,$opts) = @_;
78 $path =~ s{/[^/]+$}{};
79 make_path $path unless -d $path;
83 my ( $self,$user, $dir ) = @_;
84 $user = $self->user_info($user) unless ref $user eq 'HASH';
86 if ( exists $user->{dir} ) {
87 $path = $user->{dir} . '/.meta/' . $dir;
89 die "no dir in ", dump $user;
94 $self->mkbasepath( $path, { uid => $user->{uid} } );
95 open(my $fh, '>', $path);
97 chown $user->{uid}, $user->{gid}, $path;
98 warn "# user_dir created $path\n";
101 warn "## user_dir $path";
107 $self->append_meta( 'usage', @_ );
114 my $path = $self->user_dir( $user => $log );
115 my $line = join('#',@_);
116 open(my $fh, '>>', $path);
119 warn "## $path $line\n";
123 my ( $self, $user ) = @_;
124 $user = $self->user_info($user) unless ref $user eq 'HASH';
125 my $path = $self->user_dir( $user => 'usage');
127 open(my $fh, '<', $path);
130 my @v = split(/#/,$_);
131 $sum->{ $v[0] } += $v[1];
132 $sum->{_usage} += $v[1];
134 my ( $usage, $quota ) = split(/ /,
135 $self->gearman_do( 'narada_s1_quota_get' => $user->{uid} )
137 $sum->{_usage} += $usage;
138 $sum->{_quota} = $quota;
139 warn "## usage ",dump($user, $sum), $/;
144 my ( $self, $f_uid,$f_path, $t_uid,$t_path ) = @_;
146 my $f = $self->user_info($f_uid);
147 my $t = $self->user_info($t_uid);
149 my $f_full = "$f->{dir}/$f_path";
150 my $t_full = "$t->{dir}/$t_path";
152 $self->mkbasepath( $t_full, { uid => $t->{uid} } );
154 my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size, $atime,$mtime,$ctime,$blksize,$blocks) = stat $f_full;
155 if ( $uid == $f->{uid} ) {
156 warn "# send_file - move $f_uid $f_path to pool\n";
157 chown $self->{md5}->{uid}, $self->{md5}->{gid}, $f_full;
158 chmod oct("0444"), $f_full;
159 $self->append( $f, 'sent', -s $f_full, $t->{uid}, $f_path );
160 } elsif ( $uid == $self->{md5}->{uid} ) {
161 warn "# send_file - shared $f_full\n";
164 $self->delete( $t, $t_path ) if -e $t_full;
166 link $f_full, $t_full;
167 $self->append( $t, 'recv', -s $t_full, $f->{uid}, $t_path );
171 my ( $self, $user, $from, $to ) = @_;
172 $user = $self->user_info($user) unless ref $user eq 'HASH';
174 $self->append( $user, 'rename', $from, $to );
179 my ( $self, $user, $path ) = @_;
180 $user = $self->user_info($user) unless ref $user eq 'HASH';
182 my $deleted_size = 0;
183 my $full_path = "$user->{dir}/$path";
185 if ( -d $full_path ) {
191 my ($uid,$size) = (stat($_))[4,7];
192 warn "## find $uid $size $_\n";
193 if ( $uid == $self->{md5}->{uid} ) {
194 $deleted_size += $size;
198 remove_tree $full_path;
200 $deleted_size += -s $full_path;
204 warn "delete $deleted_size bytes shared\n";
206 $self->append( $user, 'delete', -$deleted_size, $user->{uid}, $path );
208 $self->md5sum($user)->out( $path );