seaweedfs/weed/shell/command_fs_rm.go

101 lines
2.2 KiB
Go
Raw Normal View History

2021-07-20 07:49:28 +08:00
package shell
import (
"context"
"fmt"
"io"
2021-07-21 20:10:36 +08:00
"strings"
2021-07-20 07:49:28 +08:00
"github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
"github.com/chrislusf/seaweedfs/weed/util"
)
func init() {
Commands = append(Commands, &commandFsRm{})
}
type commandFsRm struct {
}
func (c *commandFsRm) Name() string {
return "fs.rm"
}
func (c *commandFsRm) Help() string {
2021-07-21 20:10:36 +08:00
return `remove file and directory entries
2021-07-20 07:49:28 +08:00
2021-07-21 20:10:36 +08:00
fs.rm [-rf] <entry1> <entry2> ...
2021-07-20 07:49:28 +08:00
2021-07-21 20:10:36 +08:00
fs.rm /dir/file_name1 dir/file_name2
2021-07-20 07:49:28 +08:00
fs.rm /dir
2021-07-21 20:10:36 +08:00
The option "-r" can be recursive.
The option "-f" can be ignored by recursive error.
2021-07-20 07:49:28 +08:00
`
}
func (c *commandFsRm) Do(args []string, commandEnv *CommandEnv, writer io.Writer) (err error) {
2021-07-21 20:10:36 +08:00
isRecursive := false
ignoreRecursiveError := false
var entiries []string
for _, arg := range args {
if !strings.HasPrefix(arg, "-") {
entiries = append(entiries, arg)
continue
}
for _, t := range arg {
switch t {
case 'r':
isRecursive = true
case 'f':
ignoreRecursiveError = true
}
}
2021-07-20 07:49:28 +08:00
}
2021-07-21 20:10:36 +08:00
if len(entiries) < 1 {
return fmt.Errorf("need to have arguments")
2021-07-20 07:49:28 +08:00
}
2021-07-21 20:10:36 +08:00
commandEnv.WithFilerClient(func(client filer_pb.SeaweedFilerClient) error {
for _, entry := range entiries {
targetPath, err := commandEnv.parseUrl(entry)
if err != nil {
fmt.Fprintf(writer, "rm: %s: %v\n", targetPath, err)
continue
}
targetDir, targetName := util.FullPath(targetPath).DirAndName()
lookupRequest := &filer_pb.LookupDirectoryEntryRequest{
Directory: targetDir,
Name: targetName,
}
_, err = filer_pb.LookupEntry(client, lookupRequest)
if err != nil {
fmt.Fprintf(writer, "rm: %s: %v\n", targetPath, err)
continue
}
request := &filer_pb.DeleteEntryRequest{
Directory: targetDir,
Name: targetName,
IgnoreRecursiveError: ignoreRecursiveError,
IsDeleteData: true,
IsRecursive: isRecursive,
IsFromOtherCluster: false,
Signatures: nil,
}
if resp, err := client.DeleteEntry(context.Background(), request); err != nil {
fmt.Fprintf(writer, "rm: %s: %v\n", targetPath, err)
} else {
if resp.Error != "" {
fmt.Fprintf(writer, "rm: %s: %v\n", targetPath, resp.Error)
}
}
2021-07-20 07:49:28 +08:00
}
2021-07-21 20:10:36 +08:00
return nil
2021-07-20 07:49:28 +08:00
})
2021-07-21 20:10:36 +08:00
return
2021-07-20 07:49:28 +08:00
}