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.

90 lines
2.0 KiB

5 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/chrislusf/seaweedfs/weed/pb/filer_pb"
  7. "github.com/chrislusf/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) Do(args []string, commandEnv *CommandEnv, writer io.Writer) (err error) {
  27. sourcePath, err := commandEnv.parseUrl(args[0])
  28. if err != nil {
  29. return err
  30. }
  31. destinationPath, err := commandEnv.parseUrl(args[1])
  32. if err != nil {
  33. return err
  34. }
  35. sourceDir, sourceName := util.FullPath(sourcePath).DirAndName()
  36. destinationDir, destinationName := util.FullPath(destinationPath).DirAndName()
  37. return commandEnv.WithFilerClient(func(client filer_pb.SeaweedFilerClient) error {
  38. // collect destination entry info
  39. destinationRequest := &filer_pb.LookupDirectoryEntryRequest{
  40. Name: destinationDir,
  41. Directory: destinationName,
  42. }
  43. respDestinationLookupEntry, err := filer_pb.LookupEntry(client, destinationRequest)
  44. var targetDir, targetName string
  45. // moving a file or folder
  46. if err == nil && respDestinationLookupEntry.Entry.IsDirectory {
  47. // to a directory
  48. targetDir = util.Join(destinationDir, destinationName)
  49. targetName = sourceName
  50. } else {
  51. // to a file or folder
  52. targetDir = destinationDir
  53. targetName = destinationName
  54. }
  55. request := &filer_pb.AtomicRenameEntryRequest{
  56. OldDirectory: sourceDir,
  57. OldName: sourceName,
  58. NewDirectory: targetDir,
  59. NewName: targetName,
  60. }
  61. _, err = client.AtomicRenameEntry(context.Background(), request)
  62. fmt.Fprintf(writer, "move: %s => %s\n", sourcePath, util.NewFullPath(targetDir, targetName))
  63. return err
  64. })
  65. }