Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support trashbin sub paths in the recycle API #1827

Merged
merged 8 commits into from
Jul 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions changelog/unreleased/support-recycle-subpaths.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Enhancement: Support trashbin sub paths in the recycle API

The recycle API could only act on the root items of the trashbin. Meaning if you delete a deep tree, you couldn't restore just one file from that tree but you had to restore the whole tree. Now listing, restoring and purging work also for sub paths in the trashbin.

https://github.com/cs3org/reva/pull/1827

2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ require (
github.com/cheggaaa/pb v1.0.29
github.com/coreos/go-oidc v2.2.1+incompatible
github.com/cs3org/cato v0.0.0-20200828125504-e418fc54dd5e
github.com/cs3org/go-cs3apis v0.0.0-20210614143420-5ee2eb1e7887
github.com/cs3org/go-cs3apis v0.0.0-20210702091910-85a56bfd027f
github.com/dgrijalva/jwt-go v3.2.0+incompatible
github.com/eventials/go-tus v0.0.0-20200718001131-45c7ec8f5d59
github.com/gdexlab/go-render v1.0.1
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,8 @@ github.com/creack/pty v1.1.7/go.mod h1:lj5s0c3V2DBrqTV7llrYr5NG6My20zk30Fl46Y7Do
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
github.com/cs3org/cato v0.0.0-20200828125504-e418fc54dd5e h1:tqSPWQeueWTKnJVMJffz4pz0o1WuQxJ28+5x5JgaHD8=
github.com/cs3org/cato v0.0.0-20200828125504-e418fc54dd5e/go.mod h1:XJEZ3/EQuI3BXTp/6DUzFr850vlxq11I6satRtz0YQ4=
github.com/cs3org/go-cs3apis v0.0.0-20210614143420-5ee2eb1e7887 h1:X5Se3M/kbh9w6LZQvyLS7djAGKcWGzmaY6IOa7Talpk=
github.com/cs3org/go-cs3apis v0.0.0-20210614143420-5ee2eb1e7887/go.mod h1:UXha4TguuB52H14EMoSsCqDj7k8a/t7g4gVP+bgY5LY=
github.com/cs3org/go-cs3apis v0.0.0-20210702091910-85a56bfd027f h1:l09QSEPO8DI3V2hBnc6KhTsrNg/DTyBYjCTwSb/HR6Q=
github.com/cs3org/go-cs3apis v0.0.0-20210702091910-85a56bfd027f/go.mod h1:UXha4TguuB52H14EMoSsCqDj7k8a/t7g4gVP+bgY5LY=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
Expand Down
2 changes: 1 addition & 1 deletion internal/grpc/services/gateway/storageprovider.go
Original file line number Diff line number Diff line change
Expand Up @@ -1928,6 +1928,7 @@ func (s *svc) ListRecycle(ctx context.Context, req *gateway.ListRecycleRequest)
Opaque: req.Opaque,
FromTs: req.FromTs,
ToTs: req.ToTs,
Ref: req.Ref,
})
if err != nil {
return nil, errors.Wrap(err, "gateway: error calling ListRecycleRequest")
Expand All @@ -1953,7 +1954,6 @@ func (s *svc) RestoreRecycleItem(ctx context.Context, req *provider.RestoreRecyc
}

func (s *svc) PurgeRecycle(ctx context.Context, req *gateway.PurgeRecycleRequest) (*provider.PurgeRecycleResponse, error) {
// lookup storage by treating the key as a path. It has been prefixed with the storage path in ListRecycle
c, err := s.find(ctx, req.Ref)
if err != nil {
return &provider.PurgeRecycleResponse{
Expand Down
23 changes: 19 additions & 4 deletions internal/grpc/services/storageprovider/storageprovider.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import (
"github.com/cs3org/reva/pkg/mime"
"github.com/cs3org/reva/pkg/rgrpc"
"github.com/cs3org/reva/pkg/rgrpc/status"
"github.com/cs3org/reva/pkg/rhttp/router"
"github.com/cs3org/reva/pkg/storage"
"github.com/cs3org/reva/pkg/storage/fs/registry"
"github.com/mitchellh/mapstructure"
Expand Down Expand Up @@ -793,7 +794,12 @@ func (s *service) ListRecycleStream(req *provider.ListRecycleStreamRequest, ss p
ctx := ss.Context()
log := appctx.GetLogger(ctx)

items, err := s.storage.ListRecycle(ctx)
ref, err := s.unwrap(ctx, req.Ref)
if err != nil {
return err
}

items, err := s.storage.ListRecycle(ctx, ref.ResourceId.OpaqueId, ref.Path)
if err != nil {
var st *rpc.Status
switch err.(type) {
Expand Down Expand Up @@ -829,7 +835,12 @@ func (s *service) ListRecycleStream(req *provider.ListRecycleStreamRequest, ss p
}

func (s *service) ListRecycle(ctx context.Context, req *provider.ListRecycleRequest) (*provider.ListRecycleResponse, error) {
items, err := s.storage.ListRecycle(ctx)
ref, err := s.unwrap(ctx, req.Ref)
if err != nil {
return nil, err
}
key, itemPath := router.ShiftPath(ref.Path)
items, err := s.storage.ListRecycle(ctx, key, itemPath)
// TODO(labkode): CRITICAL: fill recycle info with storage provider.
if err != nil {
var st *rpc.Status
Expand All @@ -855,7 +866,11 @@ func (s *service) ListRecycle(ctx context.Context, req *provider.ListRecycleRequ

func (s *service) RestoreRecycleItem(ctx context.Context, req *provider.RestoreRecycleItemRequest) (*provider.RestoreRecycleItemResponse, error) {
// TODO(labkode): CRITICAL: fill recycle info with storage provider.
if err := s.storage.RestoreRecycleItem(ctx, req.Key, req.RestoreRef); err != nil {
ref, err := s.unwrap(ctx, req.Ref)
if err != nil {
return nil, err
}
if err := s.storage.RestoreRecycleItem(ctx, req.Key, ref.Path, req.RestoreRef); err != nil {
var st *rpc.Status
switch err.(type) {
case errtypes.IsNotFound:
Expand All @@ -879,7 +894,7 @@ func (s *service) RestoreRecycleItem(ctx context.Context, req *provider.RestoreR
func (s *service) PurgeRecycle(ctx context.Context, req *provider.PurgeRecycleRequest) (*provider.PurgeRecycleResponse, error) {
// if a key was sent as opaque id purge only that item
if req.GetRef().GetResourceId() != nil && req.GetRef().GetResourceId().OpaqueId != "" {
if err := s.storage.PurgeRecycleItem(ctx, req.GetRef().GetResourceId().OpaqueId); err != nil {
if err := s.storage.PurgeRecycleItem(ctx, req.GetRef().GetResourceId().OpaqueId, req.GetRef().Path); err != nil {
var st *rpc.Status
switch err.(type) {
case errtypes.IsNotFound:
Expand Down
81 changes: 66 additions & 15 deletions internal/http/services/owncloud/ocdav/trashbin.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import (
"fmt"
"net/http"
"path"
"path/filepath"
"strconv"
"strings"
"time"
Expand Down Expand Up @@ -107,8 +106,8 @@ func (h *TrashbinHandler) Handler(s *svc) http.Handler {
// return
//}

if key == "" && r.Method == "PROPFIND" {
h.listTrashbin(w, r, s, u)
if r.Method == "PROPFIND" {
h.listTrashbin(w, r, s, u, key, r.URL.Path)
return
}
if key != "" && r.Method == "MOVE" {
Expand All @@ -129,26 +128,38 @@ func (h *TrashbinHandler) Handler(s *svc) http.Handler {

log.Debug().Str("key", key).Str("dst", dst).Msg("restore")

h.restore(w, r, s, u, dst, key)
h.restore(w, r, s, u, dst, key, r.URL.Path)
return
}

if r.Method == "DELETE" {
h.delete(w, r, s, u, key)
h.delete(w, r, s, u, key, r.URL.Path)
return
}

http.Error(w, "501 Not implemented", http.StatusNotImplemented)
})
}

func (h *TrashbinHandler) listTrashbin(w http.ResponseWriter, r *http.Request, s *svc, u *userpb.User) {
func (h *TrashbinHandler) listTrashbin(w http.ResponseWriter, r *http.Request, s *svc, u *userpb.User, key, itemPath string) {
ctx := r.Context()
ctx, span := trace.StartSpan(ctx, "listTrashbin")
defer span.End()

depth := r.Header.Get("Depth")
if depth == "" {
depth = "1"
}

sublog := appctx.GetLogger(ctx).With().Logger()

// see https://tools.ietf.org/html/rfc4918#section-9.1
if depth != "0" && depth != "1" && depth != "infinity" {
sublog.Debug().Str("depth", depth).Msgf("invalid Depth header value")
w.WriteHeader(http.StatusBadRequest)
return
}

pf, status, err := readPropfind(r.Body)
if err != nil {
sublog.Debug().Err(err).Msg("error reading propfind request")
Expand Down Expand Up @@ -178,8 +189,7 @@ func (h *TrashbinHandler) listTrashbin(w http.ResponseWriter, r *http.Request, s
}

// ask gateway for recycle items
// TODO(labkode): add Reference to ListRecycleRequest
getRecycleRes, err := gc.ListRecycle(ctx, &gateway.ListRecycleRequest{Ref: &provider.Reference{Path: getHomeRes.Path}})
getRecycleRes, err := gc.ListRecycle(ctx, &gateway.ListRecycleRequest{Ref: &provider.Reference{Path: path.Join(getHomeRes.Path, key, itemPath)}})

if err != nil {
sublog.Error().Err(err).Msg("error calling ListRecycle")
Expand All @@ -192,7 +202,47 @@ func (h *TrashbinHandler) listTrashbin(w http.ResponseWriter, r *http.Request, s
return
}

propRes, err := h.formatTrashPropfind(ctx, s, u, &pf, getRecycleRes.RecycleItems)
items := getRecycleRes.RecycleItems

if depth == "infinity" {
var stack []string
// check sub-containers in reverse order and add them to the stack
// the reversed order here will produce a more logical sorting of results
for i := len(items) - 1; i >= 0; i-- {
// for i := range res.Infos {
if items[i].Type == provider.ResourceType_RESOURCE_TYPE_CONTAINER {
stack = append(stack, items[i].Key)
}
}

for len(stack) > 0 {
key := stack[len(stack)-1]
getRecycleRes, err := gc.ListRecycle(ctx, &gateway.ListRecycleRequest{Ref: &provider.Reference{Path: path.Join(getHomeRes.Path, key)}})
if err != nil {
sublog.Error().Err(err).Msg("error calling ListRecycle")
w.WriteHeader(http.StatusInternalServerError)
return
}

if getRecycleRes.Status.Code != rpc.Code_CODE_OK {
HandleErrorStatus(&sublog, w, getRecycleRes.Status)
return
}
items = append(items, getRecycleRes.RecycleItems...)

stack = stack[:len(stack)-1]
// check sub-containers in reverse order and add them to the stack
// the reversed order here will produce a more logical sorting of results
for i := len(getRecycleRes.RecycleItems) - 1; i >= 0; i-- {
// for i := range res.Infos {
if getRecycleRes.RecycleItems[i].Type == provider.ResourceType_RESOURCE_TYPE_CONTAINER {
stack = append(stack, getRecycleRes.RecycleItems[i].Key)
}
}
}
}

propRes, err := h.formatTrashPropfind(ctx, s, u, &pf, items)
if err != nil {
sublog.Error().Err(err).Msg("error formatting propfind")
w.WriteHeader(http.StatusInternalServerError)
Expand Down Expand Up @@ -279,7 +329,7 @@ func (h *TrashbinHandler) itemToPropResponse(ctx context.Context, s *svc, u *use
Prop: []*propertyXML{},
})
// yes this is redundant, can be derived from oc:trashbin-original-location which contains the full path, clients should not fetch it
response.Propstat[0].Prop = append(response.Propstat[0].Prop, s.newProp("oc:trashbin-original-filename", filepath.Base(item.Ref.Path)))
response.Propstat[0].Prop = append(response.Propstat[0].Prop, s.newProp("oc:trashbin-original-filename", path.Base(item.Ref.Path)))
response.Propstat[0].Prop = append(response.Propstat[0].Prop, s.newProp("oc:trashbin-original-location", strings.TrimPrefix(item.Ref.Path, "/")))
response.Propstat[0].Prop = append(response.Propstat[0].Prop, s.newProp("oc:trashbin-delete-timestamp", strconv.FormatUint(item.DeletionTime.Seconds, 10)))
response.Propstat[0].Prop = append(response.Propstat[0].Prop, s.newProp("oc:trashbin-delete-datetime", dTime))
Expand Down Expand Up @@ -316,7 +366,7 @@ func (h *TrashbinHandler) itemToPropResponse(ctx context.Context, s *svc, u *use
}
case "trashbin-original-filename":
// yes this is redundant, can be derived from oc:trashbin-original-location which contains the full path, clients should not fetch it
propstatOK.Prop = append(propstatOK.Prop, s.newProp("oc:trashbin-original-filename", filepath.Base(item.Ref.Path)))
propstatOK.Prop = append(propstatOK.Prop, s.newProp("oc:trashbin-original-filename", path.Base(item.Ref.Path)))
case "trashbin-original-location":
// TODO (jfd) double check and clarify the cs3 spec what the Key is about and if Path is only the folder that contains the file or if it includes the filename
propstatOK.Prop = append(propstatOK.Prop, s.newProp("oc:trashbin-original-location", strings.TrimPrefix(item.Ref.Path, "/")))
Expand Down Expand Up @@ -363,7 +413,7 @@ func (h *TrashbinHandler) itemToPropResponse(ctx context.Context, s *svc, u *use
}

// restore has a destination and a key
func (h *TrashbinHandler) restore(w http.ResponseWriter, r *http.Request, s *svc, u *userpb.User, dst string, key string) {
func (h *TrashbinHandler) restore(w http.ResponseWriter, r *http.Request, s *svc, u *userpb.User, dst, key, itemPath string) {
ctx := r.Context()
ctx, span := trace.StartSpan(ctx, "restore")
defer span.End()
Expand Down Expand Up @@ -401,7 +451,7 @@ func (h *TrashbinHandler) restore(w http.ResponseWriter, r *http.Request, s *svc
}

dstRef := &provider.Reference{
Path: filepath.Join(getHomeRes.Path, dst),
Path: path.Join(getHomeRes.Path, dst),
}

dstStatReq := &provider.StatRequest{
Expand Down Expand Up @@ -464,7 +514,7 @@ func (h *TrashbinHandler) restore(w http.ResponseWriter, r *http.Request, s *svc
// use the key which is prefixed with the StoragePath to lookup the correct storage ...
// TODO currently limited to the home storage
Ref: &provider.Reference{
Path: getHomeRes.Path,
Path: path.Join(getHomeRes.Path, itemPath),
},
Key: key,
RestoreRef: &provider.Reference{Path: dst},
Expand Down Expand Up @@ -503,7 +553,7 @@ func (h *TrashbinHandler) restore(w http.ResponseWriter, r *http.Request, s *svc
}

// delete has only a key
func (h *TrashbinHandler) delete(w http.ResponseWriter, r *http.Request, s *svc, u *userpb.User, key string) {
func (h *TrashbinHandler) delete(w http.ResponseWriter, r *http.Request, s *svc, u *userpb.User, key, itemPath string) {
ctx := r.Context()
ctx, span := trace.StartSpan(ctx, "erase")
defer span.End()
Expand Down Expand Up @@ -547,6 +597,7 @@ func (h *TrashbinHandler) delete(w http.ResponseWriter, r *http.Request, s *svc,
StorageId: sRes.Info.Id.StorageId,
OpaqueId: key,
},
Path: utils.MakeRelativePath(itemPath),
},
}

Expand Down
8 changes: 4 additions & 4 deletions pkg/storage/fs/owncloud/owncloud.go
Original file line number Diff line number Diff line change
Expand Up @@ -2060,7 +2060,7 @@ func (fs *ocfs) RestoreRevision(ctx context.Context, ref *provider.Reference, re
return fs.propagate(ctx, ip)
}

func (fs *ocfs) PurgeRecycleItem(ctx context.Context, key string) error {
func (fs *ocfs) PurgeRecycleItem(ctx context.Context, key, path string) error {
rp, err := fs.getRecyclePath(ctx)
if err != nil {
return errors.Wrap(err, "ocfs: error resolving recycle path")
Expand Down Expand Up @@ -2153,15 +2153,15 @@ func (fs *ocfs) convertToRecycleItem(ctx context.Context, rp string, md os.FileI
}
}

func (fs *ocfs) ListRecycle(ctx context.Context) ([]*provider.RecycleItem, error) {
func (fs *ocfs) ListRecycle(ctx context.Context, key, path string) ([]*provider.RecycleItem, error) {
// TODO check permission? on what? user must be the owner?
rp, err := fs.getRecyclePath(ctx)
if err != nil {
return nil, errors.Wrap(err, "ocfs: error resolving recycle path")
}

// list files folder
mds, err := ioutil.ReadDir(rp)
mds, err := ioutil.ReadDir(filepath.Join(rp, key))
if err != nil {
log := appctx.GetLogger(ctx)
log.Debug().Err(err).Str("path", rp).Msg("trash not readable")
Expand All @@ -2180,7 +2180,7 @@ func (fs *ocfs) ListRecycle(ctx context.Context) ([]*provider.RecycleItem, error
return items, nil
}

func (fs *ocfs) RestoreRecycleItem(ctx context.Context, key string, restoreRef *provider.Reference) error {
func (fs *ocfs) RestoreRecycleItem(ctx context.Context, key, path string, restoreRef *provider.Reference) error {
// TODO check permission? on what? user must be the owner?
log := appctx.GetLogger(ctx)
rp, err := fs.getRecyclePath(ctx)
Expand Down
6 changes: 3 additions & 3 deletions pkg/storage/fs/owncloudsql/owncloudsql.go
Original file line number Diff line number Diff line change
Expand Up @@ -1864,7 +1864,7 @@ func (fs *ocfs) RestoreRevision(ctx context.Context, ref *provider.Reference, re
return fs.propagate(ctx, ip)
}

func (fs *ocfs) PurgeRecycleItem(ctx context.Context, key string) error {
func (fs *ocfs) PurgeRecycleItem(ctx context.Context, key, path string) error {
rp, err := fs.getRecyclePath(ctx)
if err != nil {
return errors.Wrap(err, "owncloudsql: error resolving recycle path")
Expand Down Expand Up @@ -1972,7 +1972,7 @@ func (fs *ocfs) convertToRecycleItem(ctx context.Context, md os.FileInfo) *provi
}
}

func (fs *ocfs) ListRecycle(ctx context.Context) ([]*provider.RecycleItem, error) {
func (fs *ocfs) ListRecycle(ctx context.Context, key, path string) ([]*provider.RecycleItem, error) {
// TODO check permission? on what? user must be the owner?
rp, err := fs.getRecyclePath(ctx)
if err != nil {
Expand All @@ -1999,7 +1999,7 @@ func (fs *ocfs) ListRecycle(ctx context.Context) ([]*provider.RecycleItem, error
return items, nil
}

func (fs *ocfs) RestoreRecycleItem(ctx context.Context, key string, restoreRef *provider.Reference) error {
func (fs *ocfs) RestoreRecycleItem(ctx context.Context, key, path string, restoreRef *provider.Reference) error {
// TODO check permission? on what? user must be the owner?
log := appctx.GetLogger(ctx)
rp, err := fs.getRecyclePath(ctx)
Expand Down
6 changes: 3 additions & 3 deletions pkg/storage/fs/s3/s3.go
Original file line number Diff line number Diff line change
Expand Up @@ -646,19 +646,19 @@ func (fs *s3FS) RestoreRevision(ctx context.Context, ref *provider.Reference, re
return errtypes.NotSupported("restore revision")
}

func (fs *s3FS) PurgeRecycleItem(ctx context.Context, key string) error {
func (fs *s3FS) PurgeRecycleItem(ctx context.Context, key, path string) error {
return errtypes.NotSupported("purge recycle item")
}

func (fs *s3FS) EmptyRecycle(ctx context.Context) error {
return errtypes.NotSupported("empty recycle")
}

func (fs *s3FS) ListRecycle(ctx context.Context) ([]*provider.RecycleItem, error) {
func (fs *s3FS) ListRecycle(ctx context.Context, key, path string) ([]*provider.RecycleItem, error) {
return nil, errtypes.NotSupported("list recycle")
}

func (fs *s3FS) RestoreRecycleItem(ctx context.Context, key string, restoreRef *provider.Reference) error {
func (fs *s3FS) RestoreRecycleItem(ctx context.Context, key, path string, restoreRef *provider.Reference) error {
return errtypes.NotSupported("restore recycle")
}

Expand Down
6 changes: 3 additions & 3 deletions pkg/storage/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ type FS interface {
ListRevisions(ctx context.Context, ref *provider.Reference) ([]*provider.FileVersion, error)
DownloadRevision(ctx context.Context, ref *provider.Reference, key string) (io.ReadCloser, error)
RestoreRevision(ctx context.Context, ref *provider.Reference, key string) error
ListRecycle(ctx context.Context) ([]*provider.RecycleItem, error)
RestoreRecycleItem(ctx context.Context, key string, restoreRef *provider.Reference) error
PurgeRecycleItem(ctx context.Context, key string) error
ListRecycle(ctx context.Context, key, path string) ([]*provider.RecycleItem, error)
RestoreRecycleItem(ctx context.Context, key, path string, restoreRef *provider.Reference) error
PurgeRecycleItem(ctx context.Context, key, path string) error
EmptyRecycle(ctx context.Context) error
GetPathByID(ctx context.Context, id *provider.ResourceId) (string, error)
AddGrant(ctx context.Context, ref *provider.Reference, g *provider.Grant) error
Expand Down
4 changes: 2 additions & 2 deletions pkg/storage/utils/decomposedfs/decomposedfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ type Tree interface {
// CreateReference(ctx context.Context, node *node.Node, targetURI *url.URL) error
Move(ctx context.Context, oldNode *node.Node, newNode *node.Node) (err error)
Delete(ctx context.Context, node *node.Node) (err error)
RestoreRecycleItemFunc(ctx context.Context, key, restorePath string) (*node.Node, func() error, error) // FIXME REFERENCE use ref instead of path
PurgeRecycleItemFunc(ctx context.Context, key string) (*node.Node, func() error, error)
RestoreRecycleItemFunc(ctx context.Context, key, trashPath, restorePath string) (*node.Node, func() error, error) // FIXME REFERENCE use ref instead of path
PurgeRecycleItemFunc(ctx context.Context, key, purgePath string) (*node.Node, func() error, error)

WriteBlob(key string, reader io.Reader) error
ReadBlob(key string) (io.ReadCloser, error)
Expand Down
Loading