You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

117 lines
2.4 KiB

3 months ago
5 years ago
5 years ago
5 years ago
6 years ago
  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. }