mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2025-09-19 05:27:58 +08:00
filer: streaming file listing
This commit is contained in:
@@ -3,13 +3,14 @@ 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"
|
||||
|
||||
"github.com/chrislusf/seaweedfs/weed/filer2"
|
||||
"github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
|
||||
)
|
||||
|
||||
func init() {
|
||||
@@ -66,83 +67,51 @@ func (c *commandFsLs) Do(args []string, commandEnv *CommandEnv, writer io.Writer
|
||||
}
|
||||
|
||||
dir, name := filer2.FullPath(path).DirAndName()
|
||||
|
||||
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
|
||||
err = filer2.ReadDirAllEntries(ctx, commandEnv.getFilerClient(filerServer, filerPort), dir, name, func(entry *filer_pb.Entry, isLast bool) {
|
||||
|
||||
if !showHidden && strings.HasPrefix(entry.Name, ".") {
|
||||
return
|
||||
}
|
||||
|
||||
paginatedCount = len(resp.Entries)
|
||||
entryCount++
|
||||
|
||||
for _, entry := range resp.Entries {
|
||||
|
||||
if !showHidden && strings.HasPrefix(entry.Name, ".") {
|
||||
continue
|
||||
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
|
||||
}
|
||||
}
|
||||
|
||||
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)
|
||||
if dir == "/" {
|
||||
// just for printing
|
||||
dir = ""
|
||||
}
|
||||
|
||||
startFromFileName = entry.Name
|
||||
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
if isLongFormat {
|
||||
})
|
||||
|
||||
if isLongFormat && err == nil {
|
||||
fmt.Fprintf(writer, "total %d\n", entryCount)
|
||||
}
|
||||
|
||||
return
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user