remove unneeded user_info call
[cloudstore.git] / lib / CloudStore / API.pm
1 package CloudStore::API;
2 use warnings;
3 use strict;
4 use autodie;
5
6 use lib 'lib';
7 use base qw(CloudStore::Gearman CloudStore::MD5sum);
8
9 use File::Path qw(make_path remove_tree);
10 use File::Find;
11 use Data::Dump qw(dump);
12
13 sub new {
14         my ($class,$group) = @_;
15
16         my ( undef, $dir, $port, undef ) = getgrnam($group) || die "can't find group $group: $!";
17         my $self = {
18                 passwd => '/var/lib/extrausers/passwd',
19                 PORT => $port,
20                 SLICE => $dir,
21         };
22         bless $self, $class;
23
24         $self->{md5} = $self->user_info('md5');
25
26         return $self;
27 }
28
29 sub user_info {
30         my ($self,$login) = @_;
31
32         my @n = qw/ login passwd uid gid quota comment gecos dir shell expire /;
33         my @p = $login =~ m/^\d+$/ ? getpwuid $login : getpwnam $login;
34         my $user;
35         $user->{$_} = shift @p foreach @n;
36         return $user;
37 }
38
39 sub create_user {
40         my ( $self, $new_email, $new_passwd, $new_quota ) = @_;
41
42         my $max_uid = 0;
43         my $found = 0;
44
45         open(my $fh, '<', $self->{passwd});
46         while(<$fh>) {
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;
50         }
51         close($fh);
52
53         if ( ! $found ) {
54                 $max_uid++;
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";
59                 close($fh);
60                 $found = $max_uid;
61
62                 mkdir $dir;
63                 chown $max_uid, $self->{PORT}, $dir;
64
65                 open($fh, '>', "$dir/.secrets");
66                 print $fh "u$max_uid:$new_passwd\n";
67                 close $fh;
68         }
69
70         # FIXME update quota only on create?
71         $self->gearman_do( 'narada_s1_quota_set' => "$found $new_quota" );
72
73         return $found;
74 }
75
76 sub mkbasepath {
77         my ($self,$path,$opts) = @_;
78         $path =~ s{/[^/]+$}{};
79         make_path $path unless -d $path;
80 }
81
82 sub user_dir {
83         my ( $self,$user, $dir ) = @_;
84         $user = $self->user_info($user) unless ref $user eq 'HASH';
85         my $path;
86         if ( exists $user->{dir} ) {
87                 $path = $user->{dir} . '/' . $dir;
88         } else {
89                 die "no dir in ", dump $user;
90         }
91         $path =~ s{//+}{/}g;
92 #       warn "## user_dir $path";
93         return $path;
94 }
95
96 sub append {
97         my $self = shift @_;
98         my $user = shift @_;
99         my $path = $self->user_dir( $user => '.log');
100         my $line = join('#',@_);
101         open(my $fh, '>>', $path);
102         print $fh "$line\n";
103         close $fh;
104         warn "## $path $line\n";
105 }
106
107 sub usage {
108         my ( $self, $user ) = @_;
109         $user = $self->user_info($user) unless ref $user eq 'HASH';
110         my $path = $self->user_dir( $user => '.log');
111         my $sum;
112         open(my $fh, '<', $path);
113         while(<$fh>) {
114                 chomp;
115                 my @v = split(/#/,$_);
116                 $sum->{ $v[0] } += $v[1];
117                 $sum->{_usage}  += $v[1];
118         }
119         my ( $usage, $quota ) = split(/ /,
120                 $self->gearman_do( 'narada_s1_quota_get' => $user->{uid} )
121         );
122         $sum->{_usage} += $usage;
123         $sum->{_quota} = $quota;
124         warn "## usage ",dump($user, $sum), $/;
125         return $sum;
126 }
127
128 sub send_file {
129         my ( $self, $f_uid,$f_path, $t_uid,$t_path ) = @_;
130
131         my $f = $self->user_info($f_uid);
132         my $t = $self->user_info($t_uid);
133
134         my $f_full = "$f->{dir}/$f_path";
135         my $t_full = "$t->{dir}/$t_path";
136
137         $self->mkbasepath( $t_full, { uid => $t->{uid} } );
138
139         my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size, $atime,$mtime,$ctime,$blksize,$blocks) = stat $f_full;
140         if ( $uid == $f->{uid} ) {
141                 warn "# send_file - move $f_uid $f_path to pool\n";
142                 chown $self->{md5}->{uid}, $self->{md5}->{gid}, $f_full;
143                 chmod oct("0444"), $f_full;
144                 $self->append( $f, 'sent', -s $f_full, $t->{uid}, $f_path );
145         } elsif ( $uid == $self->{md5}->{uid} ) {
146                 warn "# send_file - shared $f_full\n";
147         }
148
149         $self->delete( $t, $t_path ) if -e $t_full;
150
151         link $f_full, $t_full; 
152         $self->append( $t, 'recv', -s $t_full, $f->{uid}, $t_path );
153 }
154
155 sub rename_file {
156         my ( $self, $user, $from, $to ) = @_;
157         $user = $self->user_info($user) unless ref $user eq 'HASH';
158
159         $self->append( $user, 'rename', $from, $to );
160 }
161
162
163 sub delete {
164         my ( $self, $user, $path ) = @_;
165         $user = $self->user_info($user) unless ref $user eq 'HASH';
166
167         my $deleted_size = 0;
168         my $full_path = "$user->{dir}/$path";
169
170         if ( -d $full_path ) {
171
172                 find({ 
173                 no_chdir => 1,
174                 wanted => sub {
175                         return if -d $_;
176                         my ($uid,$size) = (stat($_))[4,7];
177                         warn "## find $uid $size $_\n";
178                         if ( $uid == $self->{md5}->{uid} ) {
179                                 $deleted_size += $size;
180                         }
181                 }}, $full_path);
182
183                 remove_tree $full_path;
184         } else {
185                 $deleted_size += -s $full_path;
186                 unlink $full_path;
187         }
188
189         warn "delete $deleted_size bytes shared\n";
190
191         $self->append( $user, 'delete', -$deleted_size, $user->{uid}, $path );
192
193         $self->md5sum($user)->out( $path );
194         
195         return $full_path;
196 }
197
198 1;