Browse Source

Merge pull request #2208 from combineads/add_rm_options

pull/2210/head
Chris Lu 3 years ago
committed by GitHub
parent
commit
67537ff05b
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 67
      weed/shell/command_fs_rm.go

67
weed/shell/command_fs_rm.go

@ -4,6 +4,7 @@ import (
"context" "context"
"fmt" "fmt"
"io" "io"
"strings"
"github.com/chrislusf/seaweedfs/weed/pb/filer_pb" "github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
"github.com/chrislusf/seaweedfs/weed/util" "github.com/chrislusf/seaweedfs/weed/util"
@ -21,47 +22,79 @@ func (c *commandFsRm) Name() string {
} }
func (c *commandFsRm) Help() string { func (c *commandFsRm) Help() string {
return `remove a file or a folder, recursively delete all files and folders
return `remove file and directory entries
fs.rm <entry1>
fs.rm [-rf] <entry1> <entry2> ...
fs.rm /dir/file_name
fs.rm /dir/file_name1 dir/file_name2
fs.rm /dir fs.rm /dir
The option "-r" can be recursive.
The option "-f" can be ignored by recursive error.
` `
} }
func (c *commandFsRm) Do(args []string, commandEnv *CommandEnv, writer io.Writer) (err error) { func (c *commandFsRm) Do(args []string, commandEnv *CommandEnv, writer io.Writer) (err error) {
if len(args) != 1 {
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
}
}
}
if len(entiries) < 1 {
return fmt.Errorf("need to have arguments") return fmt.Errorf("need to have arguments")
} }
targetPath, err := commandEnv.parseUrl(args[0])
commandEnv.WithFilerClient(func(client filer_pb.SeaweedFilerClient) error {
for _, entry := range entiries {
targetPath, err := commandEnv.parseUrl(entry)
if err != nil { if err != nil {
return err
fmt.Fprintf(writer, "rm: %s: %v\n", targetPath, err)
continue
} }
targetDir, targetName := util.FullPath(targetPath).DirAndName() targetDir, targetName := util.FullPath(targetPath).DirAndName()
return commandEnv.WithFilerClient(func(client filer_pb.SeaweedFilerClient) error {
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{ request := &filer_pb.DeleteEntryRequest{
Directory: targetDir, Directory: targetDir,
Name: targetName, Name: targetName,
IgnoreRecursiveError: true,
IgnoreRecursiveError: ignoreRecursiveError,
IsDeleteData: true, IsDeleteData: true,
IsRecursive: true,
IsRecursive: isRecursive,
IsFromOtherCluster: false, IsFromOtherCluster: false,
Signatures: nil, Signatures: nil,
} }
_, err = client.DeleteEntry(context.Background(), request)
if err == nil {
fmt.Fprintf(writer, "remove: %s\n", targetPath)
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)
} }
return err
}
}
return nil
}) })
return
} }
Loading…
Cancel
Save