Skip to content

Commit

Permalink
Fix trash bin item types
Browse files Browse the repository at this point in the history
  • Loading branch information
aduffeck committed Feb 13, 2023
1 parent 13b0ba8 commit b03f553
Showing 1 changed file with 58 additions and 38 deletions.
96 changes: 58 additions & 38 deletions pkg/storage/utils/decomposedfs/recycle.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
iofs "io/fs"
"os"
"path/filepath"
"strconv"
"strings"
"time"

Expand Down Expand Up @@ -117,16 +118,31 @@ func (fs *Decomposedfs) ListRecycle(ctx context.Context, ref *provider.Reference
}
defer f.Close()

if md, err := f.Stat(); err != nil {
return nil, err
} else if !md.IsDir() {
// this is the case when we want to directly list a file in the trashbin
attrs, err := xattrs.All(trashItemPath)
if err != nil {
return items, err
}

nodeType, err := strconv.ParseInt(attrs[xattrs.TypeAttr], 10, 64)
if err != nil {
return items, err
}
if provider.ResourceType(nodeType) != provider.ResourceType_RESOURCE_TYPE_CONTAINER {
// this is the case when we want to directly list a file in the trashbin
item, err := fs.createTrashItem(ctx, md, filepath.Join(key, relativePath), deletionTime)
md, err := f.Stat()
if err != nil {
return items, err
return nil, err
}
item.Ref = &provider.Reference{
Path: filepath.Join(origin, relativePath),

item := &provider.RecycleItem{
Type: provider.ResourceType(nodeType),
Size: uint64(md.Size()),
Key: filepath.Join(key, relativePath),
DeletionTime: deletionTime,
Ref: &provider.Reference{
Path: filepath.Join(origin, relativePath),
},
}
items = append(items, item)
return items, err
Expand All @@ -143,31 +159,30 @@ func (fs *Decomposedfs) ListRecycle(ctx context.Context, ref *provider.Reference
sublog.Error().Err(err).Str("name", name).Msg("could not stat, skipping")
continue
}
if item, err := fs.createTrashItem(ctx, md, filepath.Join(key, relativePath, name), deletionTime); err == nil {
item.Ref = &provider.Reference{
Path: filepath.Join(origin, relativePath, name),
}
items = append(items, item)
attrs, err := xattrs.All(filepath.Join(trashItemPath, name))
if err != nil {
sublog.Error().Err(err).Str("name", name).Msg("could not get extended attributes, skipping")
continue
}
}
return items, nil
}

func (fs *Decomposedfs) createTrashItem(ctx context.Context, md iofs.FileInfo, key string, deletionTime *types.Timestamp) (*provider.RecycleItem, error) {
nodeType, err := strconv.ParseInt(attrs[xattrs.TypeAttr], 10, 64)
if err != nil {
sublog.Error().Err(err).Str("name", name).Msg("invalid node type, skipping")
continue
}

item := &provider.RecycleItem{
Type: getResourceType(md.IsDir()),
Size: uint64(md.Size()),
Key: key,
DeletionTime: deletionTime,
item := &provider.RecycleItem{
Type: provider.ResourceType(nodeType),
Size: uint64(md.Size()),
Key: filepath.Join(key, relativePath, name),
DeletionTime: deletionTime,
Ref: &provider.Reference{
Path: filepath.Join(origin, relativePath, name),
},
}
items = append(items, item)
}

// TODO filter results by permission ... on the original parent? or the trashed node?
// if it were on the original parent it would be possible to see files that were trashed before the current user got access
// so -> check the trash node itself
// hmm listing trash currently lists the current users trash or the 'root' trash. from ocs only the home storage is queried for trash items.
// for now we can only really check if the current user is the owner
return item, nil
return items, nil
}

// readTrashLink returns nodeID and timestamp
Expand Down Expand Up @@ -210,8 +225,20 @@ func (fs *Decomposedfs) listTrashRoot(ctx context.Context, spaceID string) ([]*p
continue
}

attrs, err := xattrs.All(nodePath)
if err != nil {
log.Error().Err(err).Str("trashRoot", trashRoot).Str("item", itemPath).Str("node_path", nodePath).Msg("could not get extended attributes, skipping")
continue
}

nodeType, err := strconv.ParseInt(attrs[xattrs.TypeAttr], 10, 64)
if err != nil {
log.Error().Err(err).Str("trashRoot", trashRoot).Str("item", itemPath).Str("node_path", nodePath).Msg("invalid node type, skipping")
continue
}

item := &provider.RecycleItem{
Type: getResourceType(md.IsDir()),
Type: provider.ResourceType(nodeType),
Size: uint64(md.Size()),
Key: nodeID,
}
Expand All @@ -225,10 +252,10 @@ func (fs *Decomposedfs) listTrashRoot(ctx context.Context, spaceID string) ([]*p
}

// lookup origin path in extended attributes
if attr, err := xattrs.Get(nodePath, xattrs.TrashOriginAttr); err == nil {
if attr, ok := attrs[xattrs.TrashOriginAttr]; ok {
item.Ref = &provider.Reference{Path: attr}
} else {
log.Error().Err(err).Str("trashRoot", trashRoot).Str("item", itemPath).Str("node", nodeID).Str("dtime", timeSuffix).Msg("could not read origin path, skipping")
log.Error().Str("trashRoot", trashRoot).Str("item", itemPath).Str("node", nodeID).Str("dtime", timeSuffix).Msg("could not read origin path, skipping")
continue
}
// TODO filter results by permission ... on the original parent? or the trashed node?
Expand Down Expand Up @@ -345,13 +372,6 @@ func (fs *Decomposedfs) EmptyRecycle(ctx context.Context, ref *provider.Referenc
return os.RemoveAll(fs.getRecycleRoot(ctx, ref.ResourceId.SpaceId))
}

func getResourceType(isDir bool) provider.ResourceType {
if isDir {
return provider.ResourceType_RESOURCE_TYPE_CONTAINER
}
return provider.ResourceType_RESOURCE_TYPE_FILE
}

func (fs *Decomposedfs) getRecycleRoot(ctx context.Context, spaceID string) string {
return filepath.Join(fs.o.Root, "spaces", lookup.Pathify(spaceID, 1, 2), "trash")
}

0 comments on commit b03f553

Please sign in to comment.