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 f4a1bbe
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 38 deletions.
95 changes: 57 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,30 @@ 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() {
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 +158,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 +224,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 +251,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 +371,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")
}
1 change: 1 addition & 0 deletions pkg/storage/utils/decomposedfs/testhelpers/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,7 @@ func (t *TestEnv) CreateTestFile(name, blobID, parentID, spaceID string, blobSiz
nil,
t.Lookup,
)
n.Type = providerv1beta1.ResourceType_RESOURCE_TYPE_FILE
nodePath := n.InternalPath()
if err := os.MkdirAll(filepath.Dir(nodePath), 0700); err != nil {
return nil, err
Expand Down
12 changes: 12 additions & 0 deletions pkg/storage/utils/decomposedfs/tree/tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -1022,6 +1022,18 @@ func (t *Tree) readRecycleItem(ctx context.Context, spaceID, key, path string) (
}

var attrStr string
// lookup blobID in extended attributes
if attrStr, err = xattrs.Get(deletedNodePath, xattrs.TypeAttr); err == nil {
var typeAttr int64
typeAttr, err = strconv.ParseInt(attrStr, 10, 64)
if err != nil {
return
}
recycleNode.Type = provider.ResourceType(typeAttr)
} else {
return
}

// lookup blobID in extended attributes
if attrStr, err = xattrs.Get(deletedNodePath, xattrs.BlobIDAttr); err == nil {
recycleNode.BlobID = attrStr
Expand Down

0 comments on commit f4a1bbe

Please sign in to comment.