Skip to content

Commit 880cfa9

Browse files
committed
cephfs: initial implementation of recycle bin functionality
1 parent 048f548 commit 880cfa9

File tree

5 files changed

+156
-11
lines changed

5 files changed

+156
-11
lines changed

changelog/unreleased/ceph-recycle.md

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Enhancement: recycle bin functionality for cephfs
2+
3+
This implementation is modeled after the CERN-deployed WinSpaces,
4+
where a folder within each space is designated as the recycle folder
5+
and organized by dates.
6+
7+
https://github.com/cs3org/reva/pull/4713

go.mod

+1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ require (
2525
github.com/go-playground/validator/v10 v10.23.0
2626
github.com/go-sql-driver/mysql v1.8.1
2727
github.com/gofrs/uuid v4.4.0+incompatible
28+
github.com/gogo/protobuf v1.3.2
2829
github.com/golang-jwt/jwt v3.2.2+incompatible
2930
github.com/golang/protobuf v1.5.4
3031
github.com/gomodule/redigo v1.9.2

go.sum

+1
Original file line numberDiff line numberDiff line change
@@ -1022,6 +1022,7 @@ github.com/gogo/googleapis v1.1.0/go.mod h1:gf4bu3Q80BeJ6H1S1vYPm8/ELATdvryBaNFG
10221022
github.com/gogo/protobuf v1.1.1/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7atdtwQ=
10231023
github.com/gogo/protobuf v1.2.0/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7atdtwQ=
10241024
github.com/gogo/protobuf v1.2.1/go.mod h1:hp+jE20tsWTFYpLwKvXlhS1hjn+gTNwPg2I6zVXpSg4=
1025+
github.com/gogo/protobuf v1.3.2 h1:Ov1cvc58UF3b5XjBnZv7+opcTcQFZebYjWzi34vdm4Q=
10251026
github.com/gogo/protobuf v1.3.2/go.mod h1:P1XiOD3dCwIKUDQYPy72D8LYyHL2YPYrpS2s69NZV8Q=
10261027
github.com/golang-jwt/jwt v3.2.2+incompatible h1:IfV12K8xAKAnZqdXVzCZ+TOjboZ2keLg81eXfW3O+oY=
10271028
github.com/golang-jwt/jwt v3.2.2+incompatible/go.mod h1:8pz2t5EyA70fFQQSrl6XZXzqecmYZeUEB8OUGHkxJ+I=

pkg/storage/fs/cephfs/cephfs.go

+123-10
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ import (
3838
goceph "github.com/ceph/go-ceph/cephfs"
3939
provider "github.com/cs3org/go-cs3apis/cs3/storage/provider/v1beta1"
4040
typepb "github.com/cs3org/go-cs3apis/cs3/types/v1beta1"
41+
typesv1beta1 "github.com/cs3org/go-cs3apis/cs3/types/v1beta1"
4142
"github.com/cs3org/reva/pkg/appctx"
4243
"github.com/cs3org/reva/pkg/errtypes"
4344
"github.com/cs3org/reva/pkg/storage"
@@ -151,6 +152,21 @@ func (fs *cephfs) CreateDir(ctx context.Context, ref *provider.Reference) error
151152
return getRevaError(ctx, err)
152153
}
153154

155+
func getRecycleTargetFromPath(path string, recyclePath string, recyclePathDepth int) (string, error) {
156+
// Tokenize the given (absolute) path
157+
components := strings.Split(filepath.Clean(string(filepath.Separator)+path), string(filepath.Separator))
158+
if recyclePathDepth > len(components)-1 {
159+
return "", errors.New("path is too short")
160+
}
161+
162+
// And construct the target by injecting the recyclePath at the required depth
163+
var target []string = []string{string(filepath.Separator)}
164+
target = append(target, components[:recyclePathDepth+1]...)
165+
target = append(target, recyclePath, time.Now().Format("2006/01/02"))
166+
target = append(target, components[recyclePathDepth+1:]...)
167+
return filepath.Join(target...), nil
168+
}
169+
154170
func (fs *cephfs) Delete(ctx context.Context, ref *provider.Reference) (err error) {
155171
var path string
156172
user := fs.makeUser(ctx)
@@ -161,8 +177,16 @@ func (fs *cephfs) Delete(ctx context.Context, ref *provider.Reference) (err erro
161177

162178
log := appctx.GetLogger(ctx)
163179
user.op(func(cv *cacheVal) {
164-
if err = cv.mount.Unlink(path); err != nil && err.Error() == errIsADirectory {
165-
err = cv.mount.RemoveDir(path)
180+
if fs.conf.RecyclePath != "" {
181+
// Recycle bin is configured, move to recycle as opposed to unlink
182+
targetPath, err := getRecycleTargetFromPath(path, fs.conf.RecyclePath, fs.conf.RecyclePathDepth)
183+
if err == nil {
184+
err = cv.mount.Rename(path, targetPath)
185+
}
186+
} else {
187+
if err = cv.mount.Unlink(path); err != nil && err.Error() == errIsADirectory {
188+
err = cv.mount.RemoveDir(path)
189+
}
166190
}
167191
})
168192

@@ -502,24 +526,113 @@ func (fs *cephfs) TouchFile(ctx context.Context, ref *provider.Reference) error
502526
return getRevaError(ctx, err)
503527
}
504528

505-
func (fs *cephfs) EmptyRecycle(ctx context.Context) error {
506-
return errtypes.NotSupported("unimplemented")
507-
}
529+
func (fs *cephfs) listDeletedEntries(ctx context.Context, maxentries int, basePath string, from, to time.Time) (res []*provider.RecycleItem, err error) {
530+
res = []*provider.RecycleItem{}
531+
user := fs.makeUser(ctx)
532+
count := 0
533+
rootRecyclePath := filepath.Join(basePath, fs.conf.RecyclePath)
534+
for d := to; !d.Before(from); d = d.AddDate(0, 0, -1) {
508535

509-
func (fs *cephfs) CreateStorageSpace(ctx context.Context, req *provider.CreateStorageSpaceRequest) (r *provider.CreateStorageSpaceResponse, err error) {
510-
return nil, errtypes.NotSupported("unimplemented")
536+
user.op(func(cv *cacheVal) {
537+
var dir *goceph.Directory
538+
if dir, err = cv.mount.OpenDir(filepath.Join(rootRecyclePath, d.Format("2006/01/02"))); err != nil {
539+
return
540+
}
541+
defer closeDir(dir)
542+
543+
var entry *goceph.DirEntryPlus
544+
for entry, err = dir.ReadDirPlus(goceph.StatxBasicStats, 0); entry != nil && err == nil; entry, err = dir.ReadDirPlus(goceph.StatxBasicStats, 0) {
545+
//TODO(lopresti) validate content of entry.Name() here.
546+
targetPath := filepath.Join(basePath, entry.Name())
547+
stat := entry.Statx()
548+
res = append(res, &provider.RecycleItem{
549+
Ref: &provider.Reference{Path: targetPath},
550+
Key: filepath.Join(rootRecyclePath, targetPath),
551+
Size: stat.Size,
552+
DeletionTime: &typesv1beta1.Timestamp{
553+
Seconds: uint64(stat.Mtime.Sec),
554+
Nanos: uint32(stat.Mtime.Nsec),
555+
},
556+
})
557+
558+
count += 1
559+
if count > maxentries {
560+
err = errtypes.BadRequest("list too long")
561+
return
562+
}
563+
}
564+
})
565+
}
566+
return res, err
511567
}
512568

513569
func (fs *cephfs) ListRecycle(ctx context.Context, basePath, key, relativePath string, from, to *typepb.Timestamp) ([]*provider.RecycleItem, error) {
514-
return nil, errtypes.NotSupported("unimplemented")
570+
md, err := fs.GetMD(ctx, &provider.Reference{Path: basePath}, nil)
571+
if err != nil {
572+
return nil, err
573+
}
574+
if !md.PermissionSet.ListRecycle {
575+
return nil, errtypes.PermissionDenied("cephfs: user doesn't have permissions to restore recycled items")
576+
}
577+
578+
var dateFrom, dateTo time.Time
579+
if from != nil && to != nil {
580+
dateFrom = time.Unix(int64(from.Seconds), 0)
581+
dateTo = time.Unix(int64(to.Seconds), 0)
582+
if dateFrom.AddDate(0, 0, fs.conf.MaxDaysInRecycleList).Before(dateTo) {
583+
return nil, errtypes.BadRequest("cephfs: too many days requested in listing the recycle bin")
584+
}
585+
} else {
586+
// if no date range was given, list up to two days ago
587+
dateTo = time.Now()
588+
dateFrom = dateTo.AddDate(0, 0, -2)
589+
}
590+
591+
sublog := appctx.GetLogger(ctx).With().Logger()
592+
sublog.Debug().Time("from", dateFrom).Time("to", dateTo).Msg("executing ListDeletedEntries")
593+
recycleEntries, err := fs.listDeletedEntries(ctx, fs.conf.MaxRecycleEntries, basePath, dateFrom, dateTo)
594+
if err != nil {
595+
switch err.(type) {
596+
case errtypes.IsBadRequest:
597+
return nil, errtypes.BadRequest("cephfs: too many entries found in listing the recycle bin")
598+
default:
599+
return nil, errors.Wrap(err, "cephfs: error listing deleted entries")
600+
}
601+
}
602+
return recycleEntries, nil
515603
}
516604

517605
func (fs *cephfs) RestoreRecycleItem(ctx context.Context, basePath, key, relativePath string, restoreRef *provider.Reference) error {
518-
return errtypes.NotSupported("unimplemented")
606+
user := fs.makeUser(ctx)
607+
md, err := fs.GetMD(ctx, &provider.Reference{Path: basePath}, nil)
608+
if err != nil {
609+
return err
610+
}
611+
if !md.PermissionSet.RestoreRecycleItem {
612+
return errtypes.PermissionDenied("cephfs: user doesn't have permissions to restore recycled items")
613+
}
614+
615+
user.op(func(cv *cacheVal) {
616+
//TODO(lopresti) validate content of basePath and relativePath. Key is expected to contain the recycled path
617+
if err = cv.mount.Rename(key, filepath.Join(basePath, relativePath)); err != nil {
618+
return
619+
}
620+
//TODO(tmourati): Add entry id logic, handle already moved file error
621+
})
622+
623+
return getRevaError(err)
519624
}
520625

521626
func (fs *cephfs) PurgeRecycleItem(ctx context.Context, basePath, key, relativePath string) error {
522-
return errtypes.NotSupported("unimplemented")
627+
return errtypes.NotSupported("cephfs: operation not supported")
628+
}
629+
630+
func (fs *cephfs) EmptyRecycle(ctx context.Context) error {
631+
return errtypes.NotSupported("cephfs: operation not supported")
632+
}
633+
634+
func (fs *cephfs) CreateStorageSpace(ctx context.Context, req *provider.CreateStorageSpaceRequest) (r *provider.CreateStorageSpaceResponse, err error) {
635+
return nil, errtypes.NotSupported("unimplemented")
523636
}
524637

525638
func (fs *cephfs) ListStorageSpaces(ctx context.Context, filter []*provider.ListStorageSpacesRequest_Filter) ([]*provider.StorageSpace, error) {

pkg/storage/fs/cephfs/options.go

+24-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,19 @@ type Options struct {
3838
DirPerms uint32 `mapstructure:"dir_perms"`
3939
FilePerms uint32 `mapstructure:"file_perms"`
4040
UserQuotaBytes uint64 `mapstructure:"user_quota_bytes"`
41-
HiddenDirs map[string]bool
41+
// Path of the recycle bin. If empty, recycling is disabled.
42+
RecyclePath string `mapstructure:"recycle_path"`
43+
// Depth of the Recycle bin location, that is after how many path components
44+
// the recycle path is located: this allows supporting recycles such as
45+
// /top-level/s/space/.recycle with a depth = 3. Defaults to 0.
46+
RecyclePathDepth int `mapstructure:"recycle_path_depth"`
47+
// Maximum entries count a ListRecycle call may return: if exceeded, ListRecycle
48+
// will return a BadRequest error
49+
MaxRecycleEntries int `mapstructure:"max_recycle_entries"`
50+
// Maximum time span in days a ListRecycle call may return: if exceeded, ListRecycle
51+
// will override the "to" date with "from" + this value
52+
MaxDaysInRecycleList int `mapstructure:"max_days_in_recycle_list"`
53+
HiddenDirs map[string]bool
4254
}
4355

4456
func (c *Options) ApplyDefaults() {
@@ -83,6 +95,9 @@ func (c *Options) ApplyDefaults() {
8395
"..": true,
8496
removeLeadingSlash(c.UploadFolder): true,
8597
}
98+
if c.RecyclePath != "" {
99+
c.HiddenDirs[c.RecyclePath] = true
100+
}
86101

87102
if c.DirPerms == 0 {
88103
c.DirPerms = dirPermDefault
@@ -95,4 +110,12 @@ func (c *Options) ApplyDefaults() {
95110
if c.UserQuotaBytes == 0 {
96111
c.UserQuotaBytes = 50000000000
97112
}
113+
114+
if c.MaxDaysInRecycleList == 0 {
115+
c.MaxDaysInRecycleList = 14
116+
}
117+
118+
if c.MaxRecycleEntries == 0 {
119+
c.MaxRecycleEntries = 2000
120+
}
98121
}

0 commit comments

Comments
 (0)