filer_rename.go 936 B

12345678910111213141516171819202122232425262728293031323334353637
  1. package filer
  2. import (
  3. "fmt"
  4. "strings"
  5. "github.com/seaweedfs/seaweedfs/weed/util"
  6. )
  7. func (f *Filer) CanRename(source, target util.FullPath, oldName string) error {
  8. sourcePath := source.Child(oldName)
  9. if strings.HasPrefix(string(target), string(sourcePath)) {
  10. return fmt.Errorf("mv: can not move directory to a subdirectory of itself")
  11. }
  12. sourceBucket := f.DetectBucket(source)
  13. targetBucket := f.DetectBucket(target)
  14. if sourceBucket != targetBucket {
  15. return fmt.Errorf("can not move across collection %s => %s", sourceBucket, targetBucket)
  16. }
  17. return nil
  18. }
  19. func (f *Filer) DetectBucket(source util.FullPath) (bucket string) {
  20. if strings.HasPrefix(string(source), f.DirBucketsPath+"/") {
  21. bucketAndObjectKey := string(source)[len(f.DirBucketsPath)+1:]
  22. t := strings.Index(bucketAndObjectKey, "/")
  23. if t < 0 {
  24. bucket = bucketAndObjectKey
  25. }
  26. if t > 0 {
  27. bucket = bucketAndObjectKey[:t]
  28. }
  29. }
  30. return bucket
  31. }