You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

37 lines
936 B

  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. }