seaweedfs/weed/shell/command_fs_tree.go

148 lines
3.2 KiB
Go
Raw Permalink Normal View History

2019-03-28 07:05:04 +00:00
package shell
import (
"context"
"fmt"
"github.com/chrislusf/seaweedfs/weed/filer2"
"github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
"io"
"strings"
)
func init() {
Commands = append(Commands, &commandFsTree{})
2019-03-28 07:05:04 +00:00
}
type commandFsTree struct {
}
func (c *commandFsTree) Name() string {
return "fs.tree"
}
func (c *commandFsTree) Help() string {
return `recursively list all files under a directory
fs.tree http://<filer_server>:<port>/dir/
`
}
func (c *commandFsTree) Do(args []string, commandEnv *CommandEnv, writer io.Writer) (err error) {
2019-03-28 07:05:04 +00:00
filerServer, filerPort, path, err := commandEnv.parseUrl(findInputDirectory(args))
2019-03-28 07:05:04 +00:00
if err != nil {
return err
}
dir, name := filer2.FullPath(path).DirAndName()
ctx := context.Background()
return commandEnv.withFilerClient(ctx, filerServer, filerPort, func(client filer_pb.SeaweedFilerClient) error {
2019-04-05 18:40:54 +00:00
dirCount, fCount, terr := treeTraverseDirectory(ctx, writer, client, dir, name, newPrefix(), -1)
if terr == nil {
fmt.Fprintf(writer, "%d directories, %d files\n", dirCount, fCount)
}
return terr
2019-03-28 07:05:04 +00:00
})
}
2019-04-05 18:40:54 +00:00
func treeTraverseDirectory(ctx context.Context, writer io.Writer, client filer_pb.SeaweedFilerClient, dir, name string, prefix *Prefix, level int) (directoryCount, fileCount int64, err error) {
2019-03-28 07:05:04 +00:00
paginatedCount := -1
startFromFileName := ""
2019-04-05 18:40:54 +00:00
paginateSize := 1000
2019-03-28 07:05:04 +00:00
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)
if paginatedCount > 0 {
prefix.addMarker(level)
}
for i, entry := range resp.Entries {
2019-04-05 18:40:54 +00:00
if level < 0 && name != "" {
2019-04-05 18:40:54 +00:00
if entry.Name != name {
break
}
}
2019-03-28 07:38:30 +00:00
// 0.1% wrong prefix here, but fixing it would need to paginate to the next batch first
2019-03-28 07:05:04 +00:00
isLast := paginatedCount < paginateSize && i == paginatedCount-1
fmt.Fprintf(writer, "%s%s\n", prefix.getPrefix(level, isLast), entry.Name)
if entry.IsDirectory {
2019-04-05 18:40:54 +00:00
directoryCount++
2019-03-28 07:05:04 +00:00
subDir := fmt.Sprintf("%s/%s", dir, entry.Name)
if dir == "/" {
subDir = "/" + entry.Name
}
2019-04-05 18:40:54 +00:00
dirCount, fCount, terr := treeTraverseDirectory(ctx, writer, client, subDir, "", prefix, level+1)
directoryCount += dirCount
fileCount += fCount
err = terr
2019-03-28 07:05:04 +00:00
} else {
2019-04-05 18:40:54 +00:00
fileCount++
2019-03-28 07:05:04 +00:00
}
startFromFileName = entry.Name
}
}
return
}
type Prefix struct {
markers map[int]bool
}
func newPrefix() *Prefix {
return &Prefix{
markers: make(map[int]bool),
}
}
func (p *Prefix) addMarker(marker int) {
p.markers[marker] = true
}
func (p *Prefix) removeMarker(marker int) {
delete(p.markers, marker)
}
func (p *Prefix) getPrefix(level int, isLastChild bool) string {
var sb strings.Builder
2019-04-05 18:40:54 +00:00
if level < 0 {
return ""
}
2019-03-28 07:05:04 +00:00
for i := 0; i < level; i++ {
if _, ok := p.markers[i]; ok {
sb.WriteString("│")
} else {
sb.WriteString(" ")
}
sb.WriteString(" ")
}
if isLastChild {
sb.WriteString("└──")
2019-03-31 06:09:16 +00:00
p.removeMarker(level)
2019-03-28 07:05:04 +00:00
} else {
sb.WriteString("├──")
}
return sb.String()
}