dir_rename.go 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. package filesys
  2. import (
  3. "context"
  4. "github.com/chrislusf/seaweedfs/weed/filer2"
  5. "github.com/chrislusf/seaweedfs/weed/glog"
  6. "github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
  7. "github.com/seaweedfs/fuse"
  8. "github.com/seaweedfs/fuse/fs"
  9. )
  10. func (dir *Dir) Rename(ctx context.Context, req *fuse.RenameRequest, newDirectory fs.Node) error {
  11. newDir := newDirectory.(*Dir)
  12. glog.V(4).Infof("dir Rename %s/%s => %s/%s", dir.Path, req.OldName, newDir.Path, req.NewName)
  13. err := dir.wfs.WithFilerClient(ctx, func(ctx context.Context, client filer_pb.SeaweedFilerClient) error {
  14. request := &filer_pb.AtomicRenameEntryRequest{
  15. OldDirectory: dir.Path,
  16. OldName: req.OldName,
  17. NewDirectory: newDir.Path,
  18. NewName: req.NewName,
  19. }
  20. _, err := client.AtomicRenameEntry(ctx, request)
  21. if err != nil {
  22. glog.V(0).Infof("dir Rename %s/%s => %s/%s : %v", dir.Path, req.OldName, newDir.Path, req.NewName, err)
  23. return fuse.EIO
  24. }
  25. return nil
  26. })
  27. if err == nil {
  28. newPath := filer2.NewFullPath(newDir.Path, req.NewName)
  29. oldPath := filer2.NewFullPath(dir.Path, req.OldName)
  30. dir.wfs.cacheDelete(newPath)
  31. dir.wfs.cacheDelete(oldPath)
  32. oldFileNode := dir.wfs.getNode(oldPath, func() fs.Node {
  33. return nil
  34. })
  35. newDirNode := dir.wfs.getNode(filer2.FullPath(dir.Path), func() fs.Node {
  36. return nil
  37. })
  38. dir.wfs.forgetNode(newPath)
  39. dir.wfs.forgetNode(oldPath)
  40. if oldFileNode != nil && newDirNode != nil {
  41. oldFile := oldFileNode.(*File)
  42. oldFile.Name = req.NewName
  43. oldFile.dir = newDirNode.(*Dir)
  44. dir.wfs.getNode(newPath, func() fs.Node {
  45. return oldFile
  46. })
  47. }
  48. }
  49. return err
  50. }