command_fs_tree.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. package shell
  2. import (
  3. "fmt"
  4. "io"
  5. "strings"
  6. "github.com/chrislusf/seaweedfs/weed/filer2"
  7. "github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
  8. )
  9. func init() {
  10. Commands = append(Commands, &commandFsTree{})
  11. }
  12. type commandFsTree struct {
  13. }
  14. func (c *commandFsTree) Name() string {
  15. return "fs.tree"
  16. }
  17. func (c *commandFsTree) Help() string {
  18. return `recursively list all files under a directory
  19. fs.tree http://<filer_server>:<port>/dir/
  20. `
  21. }
  22. func (c *commandFsTree) Do(args []string, commandEnv *CommandEnv, writer io.Writer) (err error) {
  23. filerServer, filerPort, path, err := commandEnv.parseUrl(findInputDirectory(args))
  24. if err != nil {
  25. return err
  26. }
  27. dir, name := filer2.FullPath(path).DirAndName()
  28. dirCount, fCount, terr := treeTraverseDirectory(writer, commandEnv.getFilerClient(filerServer, filerPort), filer2.FullPath(dir), name, newPrefix(), -1)
  29. if terr == nil {
  30. fmt.Fprintf(writer, "%d directories, %d files\n", dirCount, fCount)
  31. }
  32. return terr
  33. }
  34. func treeTraverseDirectory(writer io.Writer, filerClient filer2.FilerClient, dir filer2.FullPath, name string, prefix *Prefix, level int) (directoryCount, fileCount int64, err error) {
  35. prefix.addMarker(level)
  36. err = filer2.ReadDirAllEntries(filerClient, dir, name, func(entry *filer_pb.Entry, isLast bool) {
  37. if level < 0 && name != "" {
  38. if entry.Name != name {
  39. return
  40. }
  41. }
  42. fmt.Fprintf(writer, "%s%s\n", prefix.getPrefix(level, isLast), entry.Name)
  43. if entry.IsDirectory {
  44. directoryCount++
  45. subDir := dir.Child(entry.Name)
  46. dirCount, fCount, terr := treeTraverseDirectory(writer, filerClient, subDir, "", prefix, level+1)
  47. directoryCount += dirCount
  48. fileCount += fCount
  49. err = terr
  50. } else {
  51. fileCount++
  52. }
  53. })
  54. return
  55. }
  56. type Prefix struct {
  57. markers map[int]bool
  58. }
  59. func newPrefix() *Prefix {
  60. return &Prefix{
  61. markers: make(map[int]bool),
  62. }
  63. }
  64. func (p *Prefix) addMarker(marker int) {
  65. p.markers[marker] = true
  66. }
  67. func (p *Prefix) removeMarker(marker int) {
  68. delete(p.markers, marker)
  69. }
  70. func (p *Prefix) getPrefix(level int, isLastChild bool) string {
  71. var sb strings.Builder
  72. if level < 0 {
  73. return ""
  74. }
  75. for i := 0; i < level; i++ {
  76. if _, ok := p.markers[i]; ok {
  77. sb.WriteString("│")
  78. } else {
  79. sb.WriteString(" ")
  80. }
  81. sb.WriteString(" ")
  82. }
  83. if isLastChild {
  84. sb.WriteString("└──")
  85. p.removeMarker(level)
  86. } else {
  87. sb.WriteString("├──")
  88. }
  89. return sb.String()
  90. }