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.

115 lines
2.4 KiB

5 years ago
5 years ago
5 years ago
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), filer2.FullPath(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 filer2.FullPath, 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 := dir.Child(entry.Name)
  48. dirCount, fCount, terr := treeTraverseDirectory(ctx, writer, filerClient, subDir, "", prefix, level+1)
  49. directoryCount += dirCount
  50. fileCount += fCount
  51. err = terr
  52. } else {
  53. fileCount++
  54. }
  55. })
  56. return
  57. }
  58. type Prefix struct {
  59. markers map[int]bool
  60. }
  61. func newPrefix() *Prefix {
  62. return &Prefix{
  63. markers: make(map[int]bool),
  64. }
  65. }
  66. func (p *Prefix) addMarker(marker int) {
  67. p.markers[marker] = true
  68. }
  69. func (p *Prefix) removeMarker(marker int) {
  70. delete(p.markers, marker)
  71. }
  72. func (p *Prefix) getPrefix(level int, isLastChild bool) string {
  73. var sb strings.Builder
  74. if level < 0 {
  75. return ""
  76. }
  77. for i := 0; i < level; i++ {
  78. if _, ok := p.markers[i]; ok {
  79. sb.WriteString("│")
  80. } else {
  81. sb.WriteString(" ")
  82. }
  83. sb.WriteString(" ")
  84. }
  85. if isLastChild {
  86. sb.WriteString("└──")
  87. p.removeMarker(level)
  88. } else {
  89. sb.WriteString("├──")
  90. }
  91. return sb.String()
  92. }