2019-03-28 07:05:04 +00:00
|
|
|
package shell
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"strings"
|
2019-12-13 08:22:37 +00:00
|
|
|
|
|
|
|
"github.com/chrislusf/seaweedfs/weed/filer2"
|
|
|
|
"github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
|
2019-03-28 07:05:04 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
2019-06-05 08:30:24 +00:00
|
|
|
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/
|
|
|
|
`
|
|
|
|
}
|
|
|
|
|
2019-06-05 08:30:24 +00:00
|
|
|
func (c *commandFsTree) Do(args []string, commandEnv *CommandEnv, writer io.Writer) (err error) {
|
2019-03-28 07:05:04 +00:00
|
|
|
|
2019-04-05 02:27:51 +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()
|
|
|
|
|
2020-02-26 06:23:59 +00:00
|
|
|
dirCount, fCount, terr := treeTraverseDirectory(writer, commandEnv.getFilerClient(filerServer, filerPort), filer2.FullPath(dir), name, newPrefix(), -1)
|
2019-03-28 07:05:04 +00:00
|
|
|
|
2019-12-13 08:22:37 +00:00
|
|
|
if terr == nil {
|
|
|
|
fmt.Fprintf(writer, "%d directories, %d files\n", dirCount, fCount)
|
|
|
|
}
|
2019-03-28 07:05:04 +00:00
|
|
|
|
2019-12-13 08:22:37 +00:00
|
|
|
return terr
|
2019-03-28 07:05:04 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-02-26 06:23:59 +00:00
|
|
|
func treeTraverseDirectory(writer io.Writer, filerClient filer2.FilerClient, dir filer2.FullPath, name string, prefix *Prefix, level int) (directoryCount, fileCount int64, err error) {
|
2019-03-28 07:05:04 +00:00
|
|
|
|
2019-12-13 08:22:37 +00:00
|
|
|
prefix.addMarker(level)
|
2019-04-05 18:40:54 +00:00
|
|
|
|
2020-02-26 05:50:12 +00:00
|
|
|
err = filer2.ReadDirAllEntries(filerClient, dir, name, func(entry *filer_pb.Entry, isLast bool) {
|
2019-12-13 08:22:37 +00:00
|
|
|
if level < 0 && name != "" {
|
|
|
|
if entry.Name != name {
|
|
|
|
return
|
2019-04-05 18:40:54 +00:00
|
|
|
}
|
2019-12-13 08:22:37 +00:00
|
|
|
}
|
2019-04-05 18:40:54 +00:00
|
|
|
|
2019-12-13 08:22:37 +00:00
|
|
|
fmt.Fprintf(writer, "%s%s\n", prefix.getPrefix(level, isLast), entry.Name)
|
2019-03-28 07:05:04 +00:00
|
|
|
|
2019-12-13 08:22:37 +00:00
|
|
|
if entry.IsDirectory {
|
|
|
|
directoryCount++
|
2020-01-20 07:59:46 +00:00
|
|
|
subDir := dir.Child(entry.Name)
|
2020-02-26 06:23:59 +00:00
|
|
|
dirCount, fCount, terr := treeTraverseDirectory(writer, filerClient, subDir, "", prefix, level+1)
|
2019-12-13 08:22:37 +00:00
|
|
|
directoryCount += dirCount
|
|
|
|
fileCount += fCount
|
|
|
|
err = terr
|
|
|
|
} else {
|
|
|
|
fileCount++
|
2019-03-28 07:05:04 +00:00
|
|
|
}
|
|
|
|
|
2019-12-13 08:22:37 +00:00
|
|
|
})
|
2019-03-28 07:05:04 +00:00
|
|
|
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()
|
|
|
|
}
|