command_fs_tree.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. package shell
  2. import (
  3. "context"
  4. "fmt"
  5. "github.com/chrislusf/seaweedfs/weed/filer2"
  6. "github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
  7. "io"
  8. "strings"
  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. return commandEnv.withFilerClient(ctx, filerServer, filerPort, func(client filer_pb.SeaweedFilerClient) error {
  31. dirCount, fCount, terr := treeTraverseDirectory(ctx, writer, client, 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. }
  38. func treeTraverseDirectory(ctx context.Context, writer io.Writer, client filer_pb.SeaweedFilerClient, dir, name string, prefix *Prefix, level int) (directoryCount, fileCount int64, err error) {
  39. paginatedCount := -1
  40. startFromFileName := ""
  41. paginateSize := 1000
  42. for paginatedCount == -1 || paginatedCount == paginateSize {
  43. resp, listErr := client.ListEntries(ctx, &filer_pb.ListEntriesRequest{
  44. Directory: dir,
  45. Prefix: name,
  46. StartFromFileName: startFromFileName,
  47. InclusiveStartFrom: false,
  48. Limit: uint32(paginateSize),
  49. })
  50. if listErr != nil {
  51. err = listErr
  52. return
  53. }
  54. paginatedCount = len(resp.Entries)
  55. if paginatedCount > 0 {
  56. prefix.addMarker(level)
  57. }
  58. for i, entry := range resp.Entries {
  59. if level < 0 && name != "" {
  60. if entry.Name != name {
  61. break
  62. }
  63. }
  64. // 0.1% wrong prefix here, but fixing it would need to paginate to the next batch first
  65. isLast := paginatedCount < paginateSize && i == paginatedCount-1
  66. fmt.Fprintf(writer, "%s%s\n", prefix.getPrefix(level, isLast), entry.Name)
  67. if entry.IsDirectory {
  68. directoryCount++
  69. subDir := fmt.Sprintf("%s/%s", dir, entry.Name)
  70. if dir == "/" {
  71. subDir = "/" + entry.Name
  72. }
  73. dirCount, fCount, terr := treeTraverseDirectory(ctx, writer, client, subDir, "", prefix, level+1)
  74. directoryCount += dirCount
  75. fileCount += fCount
  76. err = terr
  77. } else {
  78. fileCount++
  79. }
  80. startFromFileName = entry.Name
  81. }
  82. }
  83. return
  84. }
  85. type Prefix struct {
  86. markers map[int]bool
  87. }
  88. func newPrefix() *Prefix {
  89. return &Prefix{
  90. markers: make(map[int]bool),
  91. }
  92. }
  93. func (p *Prefix) addMarker(marker int) {
  94. p.markers[marker] = true
  95. }
  96. func (p *Prefix) removeMarker(marker int) {
  97. delete(p.markers, marker)
  98. }
  99. func (p *Prefix) getPrefix(level int, isLastChild bool) string {
  100. var sb strings.Builder
  101. if level < 0 {
  102. return ""
  103. }
  104. for i := 0; i < level; i++ {
  105. if _, ok := p.markers[i]; ok {
  106. sb.WriteString("│")
  107. } else {
  108. sb.WriteString(" ")
  109. }
  110. sb.WriteString(" ")
  111. }
  112. if isLastChild {
  113. sb.WriteString("└──")
  114. p.removeMarker(level)
  115. } else {
  116. sb.WriteString("├──")
  117. }
  118. return sb.String()
  119. }