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.

118 lines
2.5 KiB

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