aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChris Lu <chris.lu@uber.com>2019-03-26 12:43:51 -0700
committerChris Lu <chris.lu@uber.com>2019-03-26 12:43:51 -0700
commit37ce4c5269a61ada46a99337d1d1f7657a83198b (patch)
treead587789ab17e9306c9ea80f54bb62d4f58cc721
parentd2bb351c8810f3a4589aef52436be7d41dcf12e4 (diff)
downloadseaweedfs-37ce4c5269a61ada46a99337d1d1f7657a83198b.tar.xz
seaweedfs-37ce4c5269a61ada46a99337d1d1f7657a83198b.zip
weed shell: add fs.ls
-rw-r--r--weed/shell/command_fs_ls.go141
1 files changed, 141 insertions, 0 deletions
diff --git a/weed/shell/command_fs_ls.go b/weed/shell/command_fs_ls.go
new file mode 100644
index 000000000..6ad96f5ac
--- /dev/null
+++ b/weed/shell/command_fs_ls.go
@@ -0,0 +1,141 @@
+package shell
+
+import (
+ "context"
+ "fmt"
+ "github.com/chrislusf/seaweedfs/weed/filer2"
+ "github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
+ "io"
+ "os"
+ "os/user"
+ "strconv"
+ "strings"
+)
+
+func init() {
+ commands = append(commands, &commandFsLs{})
+}
+
+type commandFsLs struct {
+}
+
+func (c *commandFsLs) Name() string {
+ return "fs.ls"
+}
+
+func (c *commandFsLs) Help() string {
+ return `[-l] [-a] list all files under a directory
+
+ fs.ls http://<filer_server>:<port>/dir/
+ fs.ls http://<filer_server>:<port>/dir/file_name
+ fs.ls http://<filer_server>:<port>/dir/file_prefix
+`
+}
+
+func (c *commandFsLs) Do(args []string, commandEnv *commandEnv, writer io.Writer) (err error) {
+
+ var isLongFormat, showHidden bool
+ for _, arg := range args {
+ switch arg {
+ case "-a":
+ showHidden = true
+ case "-l":
+ isLongFormat = true
+ }
+ }
+
+ filerServer, filerPort, path, err := parseFilerUrl(args[len(args)-1])
+ if err != nil {
+ return err
+ }
+
+ dir, name := filer2.FullPath(path).DirAndName()
+ if strings.HasSuffix(path, "/") {
+ if path == "/" {
+ dir, name = "/", ""
+ } else {
+ dir, name = path[0 : len(path)-1], ""
+ }
+ }
+
+ ctx := context.Background()
+
+ return commandEnv.withFilerClient(ctx, filerServer, filerPort, func(client filer_pb.SeaweedFilerClient) error {
+
+ return paginateOneDirectory(ctx, writer, client, dir, name, 1000, isLongFormat, showHidden)
+
+ })
+
+}
+
+func paginateOneDirectory(ctx context.Context, writer io.Writer, client filer_pb.SeaweedFilerClient, dir, name string, paginateSize int, isLongFormat, showHidden bool) (err error) {
+
+ entryCount := 0
+ paginatedCount := -1
+ startFromFileName := ""
+
+ for paginatedCount == -1 || paginatedCount == paginateSize {
+ resp, listErr := client.ListEntries(ctx, &filer_pb.ListEntriesRequest{
+ Directory: dir,
+ Prefix: name,
+ StartFromFileName: startFromFileName,
+ InclusiveStartFrom: false,
+ Limit: uint32(paginateSize),
+ })
+ if listErr != nil {
+ err = listErr
+ return
+ }
+
+ paginatedCount = len(resp.Entries)
+
+ for _, entry := range resp.Entries {
+
+ if !showHidden && strings.HasPrefix(entry.Name, ".") {
+ continue
+ }
+
+ entryCount++
+
+ if isLongFormat {
+ fileMode := os.FileMode(entry.Attributes.FileMode)
+ userName, groupNames := entry.Attributes.UserName, entry.Attributes.GroupName
+ if userName == "" {
+ if user, userErr := user.LookupId(strconv.Itoa(int(entry.Attributes.Uid))); userErr == nil {
+ userName = user.Username
+ }
+ }
+ groupName := ""
+ if len(groupNames) > 0 {
+ groupName = groupNames[0]
+ }
+ if groupName == "" {
+ if group, groupErr := user.LookupGroupId(strconv.Itoa(int(entry.Attributes.Gid))); groupErr == nil {
+ groupName = group.Name
+ }
+ }
+
+ if dir == "/" {
+ // just for printing
+ dir = ""
+ }
+ fmt.Fprintf(writer, "%s %3d %s %s %6d %s/%s\n",
+ fileMode, len(entry.Chunks),
+ userName, groupName,
+ filer2.TotalSize(entry.Chunks), dir, entry.Name)
+ } else {
+ fmt.Fprintf(writer, "%s\n", entry.Name)
+ }
+
+ startFromFileName = entry.Name
+
+ }
+ }
+
+ if isLongFormat {
+ fmt.Fprintf(writer, "total %d\n", entryCount)
+ }
+
+ return
+
+}