2019-07-12 00:29:04 +08:00
|
|
|
package shell
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
|
2022-07-29 15:17:28 +08:00
|
|
|
"github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/util"
|
2019-07-12 00:29:04 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
Commands = append(Commands, &commandFsMv{})
|
|
|
|
}
|
|
|
|
|
|
|
|
type commandFsMv struct {
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *commandFsMv) Name() string {
|
|
|
|
return "fs.mv"
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *commandFsMv) Help() string {
|
|
|
|
return `move or rename a file or a folder
|
|
|
|
|
|
|
|
fs.mv <source entry> <destination entry>
|
|
|
|
|
|
|
|
fs.mv /dir/file_name /dir2/filename2
|
|
|
|
fs.mv /dir/file_name /dir2
|
|
|
|
|
|
|
|
fs.mv /dir/dir2 /dir3/dir4/
|
|
|
|
fs.mv /dir/dir2 /dir3/new_dir
|
|
|
|
|
|
|
|
`
|
|
|
|
}
|
|
|
|
|
2024-09-30 01:38:22 +08:00
|
|
|
func (c *commandFsMv) HasTag(CommandTag) bool {
|
2024-09-29 11:22:57 +08:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2019-07-12 00:29:04 +08:00
|
|
|
func (c *commandFsMv) Do(args []string, commandEnv *CommandEnv, writer io.Writer) (err error) {
|
|
|
|
|
2021-03-20 21:34:09 +08:00
|
|
|
if len(args) != 2 {
|
2021-03-20 21:37:03 +08:00
|
|
|
return fmt.Errorf("need to have 2 arguments")
|
2021-03-20 21:34:09 +08:00
|
|
|
}
|
|
|
|
|
2020-03-24 12:26:15 +08:00
|
|
|
sourcePath, err := commandEnv.parseUrl(args[0])
|
2019-07-12 00:29:04 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-03-24 12:26:15 +08:00
|
|
|
destinationPath, err := commandEnv.parseUrl(args[1])
|
2019-07-12 00:29:04 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-03-23 15:01:34 +08:00
|
|
|
sourceDir, sourceName := util.FullPath(sourcePath).DirAndName()
|
2019-07-12 00:29:04 +08:00
|
|
|
|
2020-03-23 15:01:34 +08:00
|
|
|
destinationDir, destinationName := util.FullPath(destinationPath).DirAndName()
|
2019-07-12 00:29:04 +08:00
|
|
|
|
2021-12-26 16:15:03 +08:00
|
|
|
return commandEnv.WithFilerClient(false, func(client filer_pb.SeaweedFilerClient) error {
|
2019-07-12 00:29:04 +08:00
|
|
|
|
|
|
|
// collect destination entry info
|
|
|
|
destinationRequest := &filer_pb.LookupDirectoryEntryRequest{
|
|
|
|
Name: destinationDir,
|
|
|
|
Directory: destinationName,
|
|
|
|
}
|
2020-03-08 08:51:46 +08:00
|
|
|
respDestinationLookupEntry, err := filer_pb.LookupEntry(client, destinationRequest)
|
2019-07-12 00:29:04 +08:00
|
|
|
|
|
|
|
var targetDir, targetName string
|
|
|
|
|
|
|
|
// moving a file or folder
|
2020-03-08 08:51:46 +08:00
|
|
|
if err == nil && respDestinationLookupEntry.Entry.IsDirectory {
|
2019-07-12 00:29:04 +08:00
|
|
|
// to a directory
|
2020-04-06 04:11:43 +08:00
|
|
|
targetDir = util.Join(destinationDir, destinationName)
|
2019-07-12 00:29:04 +08:00
|
|
|
targetName = sourceName
|
|
|
|
} else {
|
|
|
|
// to a file or folder
|
|
|
|
targetDir = destinationDir
|
|
|
|
targetName = destinationName
|
|
|
|
}
|
|
|
|
|
|
|
|
request := &filer_pb.AtomicRenameEntryRequest{
|
|
|
|
OldDirectory: sourceDir,
|
|
|
|
OldName: sourceName,
|
|
|
|
NewDirectory: targetDir,
|
|
|
|
NewName: targetName,
|
|
|
|
}
|
|
|
|
|
2020-02-26 13:50:12 +08:00
|
|
|
_, err = client.AtomicRenameEntry(context.Background(), request)
|
2019-07-12 00:29:04 +08:00
|
|
|
|
2020-03-23 15:01:34 +08:00
|
|
|
fmt.Fprintf(writer, "move: %s => %s\n", sourcePath, util.NewFullPath(targetDir, targetName))
|
2019-07-12 00:29:04 +08:00
|
|
|
|
|
|
|
return err
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
}
|