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.

63 lines
1.6 KiB

5 years ago
5 years ago
5 years ago
5 years ago
  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(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. }