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.

98 lines
2.1 KiB

3 months ago
4 years ago
5 years ago
5 years ago
5 years ago
5 years ago
  1. package shell
  2. import (
  3. "context"
  4. "fmt"
  5. "io"
  6. "github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
  7. "github.com/seaweedfs/seaweedfs/weed/util"
  8. )
  9. func init() {
  10. Commands = append(Commands, &commandFsMv{})
  11. }
  12. type commandFsMv struct {
  13. }
  14. func (c *commandFsMv) Name() string {
  15. return "fs.mv"
  16. }
  17. func (c *commandFsMv) Help() string {
  18. return `move or rename a file or a folder
  19. fs.mv <source entry> <destination entry>
  20. fs.mv /dir/file_name /dir2/filename2
  21. fs.mv /dir/file_name /dir2
  22. fs.mv /dir/dir2 /dir3/dir4/
  23. fs.mv /dir/dir2 /dir3/new_dir
  24. `
  25. }
  26. func (c *commandFsMv) HasTag(CommandTag) bool {
  27. return false
  28. }
  29. func (c *commandFsMv) Do(args []string, commandEnv *CommandEnv, writer io.Writer) (err error) {
  30. if len(args) != 2 {
  31. return fmt.Errorf("need to have 2 arguments")
  32. }
  33. sourcePath, err := commandEnv.parseUrl(args[0])
  34. if err != nil {
  35. return err
  36. }
  37. destinationPath, err := commandEnv.parseUrl(args[1])
  38. if err != nil {
  39. return err
  40. }
  41. sourceDir, sourceName := util.FullPath(sourcePath).DirAndName()
  42. destinationDir, destinationName := util.FullPath(destinationPath).DirAndName()
  43. return commandEnv.WithFilerClient(false, func(client filer_pb.SeaweedFilerClient) error {
  44. // collect destination entry info
  45. destinationRequest := &filer_pb.LookupDirectoryEntryRequest{
  46. Name: destinationDir,
  47. Directory: destinationName,
  48. }
  49. respDestinationLookupEntry, err := filer_pb.LookupEntry(client, destinationRequest)
  50. var targetDir, targetName string
  51. // moving a file or folder
  52. if err == nil && respDestinationLookupEntry.Entry.IsDirectory {
  53. // to a directory
  54. targetDir = util.Join(destinationDir, destinationName)
  55. targetName = sourceName
  56. } else {
  57. // to a file or folder
  58. targetDir = destinationDir
  59. targetName = destinationName
  60. }
  61. request := &filer_pb.AtomicRenameEntryRequest{
  62. OldDirectory: sourceDir,
  63. OldName: sourceName,
  64. NewDirectory: targetDir,
  65. NewName: targetName,
  66. }
  67. _, err = client.AtomicRenameEntry(context.Background(), request)
  68. fmt.Fprintf(writer, "move: %s => %s\n", sourcePath, util.NewFullPath(targetDir, targetName))
  69. return err
  70. })
  71. }