mkbasepath now correctly creates paths with uid
[cloudstore.git] / lib / CloudStore / API.pm
index 692a42a..1b2625d 100644 (file)
@@ -111,8 +111,15 @@ sub create_user {
 
 sub mkbasepath {
        my ($self,$path,$opts) = @_;
+       cluck "ERROR: mkbasepath called without opts, so user is root!" unless $opts;
+       if ( $ENV{DEBUG} ) {
+               warn "# mkbasepath $path ",dump($opts);
+               $opts->{verbose} ||= 1;
+       }
        $path =~ s{/[^/]+$}{};
-       make_path $path unless -d $path;
+       if ( ! -d $path ) {
+               make_path $path, $opts;
+       }
 }
 
 sub user_dir {
@@ -162,7 +169,8 @@ sub usage {
        $user = $self->user_info($user) unless ref $user eq 'HASH';
 
        my $usage_path = $user->{dir} . '/.meta/files.usage';
-       if ( -e $usage_path ) {
+       $self->mkbasepath( $usage_path, { uid => $user->{uid} } );
+       if ( ! -e $usage_path ) {
                warn "# usage $usage_path missing";
                $self->list_files($user);
        }
@@ -171,7 +179,7 @@ sub usage {
        my $size = <$fh>;
        chomp $size;
 
-       warn "# usage $user->{login} usage: $size\n";
+       warn "# usage $user->{login} $size bytes\n";
        return $size;
 
 =for slow and broken
@@ -319,7 +327,9 @@ sub file_size {
        $user = $self->user_info($user) unless ref $user eq 'HASH';
 
        my $full_path = "$user->{dir}/$path";
-       return -s $full_path;
+       my $size = -s $full_path;
+       warn "# file_size $full_path = $size bytes\n";
+       return $size;
 }
 
 sub list_files {
@@ -332,12 +342,13 @@ sub list_files {
        die "no dir for ",dump($user) unless exists $user->{dir};
 
        my $files = $user->{dir} . '/.meta/files';
-       if ( -e $files && -s $files > 0) {
+       $self->mkbasepath( $files, { uid => $user->{uid} } );
+       if ( -e $files && -s $files > 0 && -e "$files.usage") {
                local $/ = undef;
                open(my $fh, '<', $files);
                my $list = <$fh>;
                close($fh);
-               warn "# list_files $user->{login} cached ", length($list), " bytes";
+               warn "# list_files $user->{login} from cache ", length($list), " bytes\n";
                return $list;
        }
 
@@ -348,7 +359,7 @@ sub list_files {
        my $list_txt;
        while(<$pipe>) {
                chomp;
-               my ( $type, $size, $name ) = split(/\s/, $_);
+               my ( $type, $size, $name ) = split(/\s/, $_, 3);
                $name =~ s{$dir}{./};
                $name =~ s{//+}{/}g;
                my $line = "$type $size $name\n";