command_fs_tree.go 2.4 KB

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