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.

64 lines
1.7 KiB

5 years ago
5 years ago
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/glog"
  5. "github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
  6. "github.com/chrislusf/seaweedfs/weed/util"
  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(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(context.Background(), 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 := util.NewFullPath(newDir.Path, req.NewName)
  29. oldPath := util.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(util.FullPath(newDir.Path), func() fs.Node {
  36. return nil
  37. })
  38. // fmt.Printf("new path: %v dir: %v node:%+v\n", newPath, newDir.Path, newDirNode)
  39. dir.wfs.forgetNode(newPath)
  40. dir.wfs.forgetNode(oldPath)
  41. if oldFileNode != nil && newDirNode != nil {
  42. oldFile := oldFileNode.(*File)
  43. oldFile.Name = req.NewName
  44. oldFile.dir = newDirNode.(*Dir)
  45. dir.wfs.getNode(newPath, func() fs.Node {
  46. return oldFile
  47. })
  48. }
  49. }
  50. return err
  51. }