1
0
Fork 0
mirror of https://github.com/chrislusf/seaweedfs synced 2024-06-16 07:32:12 +02:00
seaweedfs/weed/filer/filer_rename.go
wusong 4867aa03ab
Fix: http rename move dir to subdir (#4432)
Co-authored-by: wang wusong <wangwusong@virtaitech.com>
2023-04-26 20:54:36 -07:00

38 lines
936 B
Go

package filer
import (
"fmt"
"strings"
"github.com/seaweedfs/seaweedfs/weed/util"
)
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
}
func (f *Filer) DetectBucket(source util.FullPath) (bucket string) {
if strings.HasPrefix(string(source), f.DirBucketsPath+"/") {
bucketAndObjectKey := string(source)[len(f.DirBucketsPath)+1:]
t := strings.Index(bucketAndObjectKey, "/")
if t < 0 {
bucket = bucketAndObjectKey
}
if t > 0 {
bucket = bucketAndObjectKey[:t]
}
}
return bucket
}