From 4867aa03ab9b9bbeee5b326f520544c2977f74da Mon Sep 17 00:00:00 2001 From: wusong <75450248+wusongANKANG@users.noreply.github.com> Date: Thu, 27 Apr 2023 11:54:36 +0800 Subject: [PATCH] Fix: http rename move dir to subdir (#4432) Co-authored-by: wang wusong --- weed/filer/filer_rename.go | 11 +++++++++-- weed/server/filer_grpc_server_rename.go | 4 ++-- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/weed/filer/filer_rename.go b/weed/filer/filer_rename.go index fdd6051a8..cf4a2b79f 100644 --- a/weed/filer/filer_rename.go +++ b/weed/filer/filer_rename.go @@ -2,16 +2,23 @@ package filer import ( "fmt" - "github.com/seaweedfs/seaweedfs/weed/util" "strings" + + "github.com/seaweedfs/seaweedfs/weed/util" ) -func (f *Filer) CanRename(source, target util.FullPath) error { +func (f *Filer) CanRename(source, target util.FullPath, oldName string) error { + sourcePath := source.Child(oldName) + if strings.HasPrefix(string(target), string(sourcePath)) { + return fmt.Errorf("mv: can not move directory to a subdirectory of itself") + } + sourceBucket := f.DetectBucket(source) targetBucket := f.DetectBucket(target) if sourceBucket != targetBucket { return fmt.Errorf("can not move across collection %s => %s", sourceBucket, targetBucket) } + return nil } diff --git a/weed/server/filer_grpc_server_rename.go b/weed/server/filer_grpc_server_rename.go index ff682da43..016d37b60 100644 --- a/weed/server/filer_grpc_server_rename.go +++ b/weed/server/filer_grpc_server_rename.go @@ -19,7 +19,7 @@ func (fs *FilerServer) AtomicRenameEntry(ctx context.Context, req *filer_pb.Atom oldParent := util.FullPath(filepath.ToSlash(req.OldDirectory)) newParent := util.FullPath(filepath.ToSlash(req.NewDirectory)) - if err := fs.filer.CanRename(oldParent, newParent); err != nil { + if err := fs.filer.CanRename(oldParent, newParent, req.OldName); err != nil { return nil, err } @@ -55,7 +55,7 @@ func (fs *FilerServer) StreamRenameEntry(req *filer_pb.StreamRenameEntryRequest, oldParent := util.FullPath(filepath.ToSlash(req.OldDirectory)) newParent := util.FullPath(filepath.ToSlash(req.NewDirectory)) - if err := fs.filer.CanRename(oldParent, newParent); err != nil { + if err := fs.filer.CanRename(oldParent, newParent, req.OldName); err != nil { return err }