command_fs_ls.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. package shell
  2. import (
  3. "context"
  4. "fmt"
  5. "io"
  6. "os"
  7. "os/user"
  8. "strconv"
  9. "strings"
  10. "github.com/chrislusf/seaweedfs/weed/filer2"
  11. "github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
  12. )
  13. func init() {
  14. Commands = append(Commands, &commandFsLs{})
  15. }
  16. type commandFsLs struct {
  17. }
  18. func (c *commandFsLs) Name() string {
  19. return "fs.ls"
  20. }
  21. func (c *commandFsLs) Help() string {
  22. return `list all files under a directory
  23. fs.ls [-l] [-a] /dir/
  24. fs.ls [-l] [-a] /dir/file_name
  25. fs.ls [-l] [-a] /dir/file_prefix
  26. fs.ls [-l] [-a] http://<filer_server>:<port>/dir/
  27. fs.ls [-l] [-a] http://<filer_server>:<port>/dir/file_name
  28. fs.ls [-l] [-a] http://<filer_server>:<port>/dir/file_prefix
  29. `
  30. }
  31. func (c *commandFsLs) Do(args []string, commandEnv *CommandEnv, writer io.Writer) (err error) {
  32. var isLongFormat, showHidden bool
  33. for _, arg := range args {
  34. if !strings.HasPrefix(arg, "-") {
  35. break
  36. }
  37. for _, t := range arg {
  38. switch t {
  39. case 'a':
  40. showHidden = true
  41. case 'l':
  42. isLongFormat = true
  43. }
  44. }
  45. }
  46. input := findInputDirectory(args)
  47. filerServer, filerPort, path, err := commandEnv.parseUrl(input)
  48. if err != nil {
  49. return err
  50. }
  51. ctx := context.Background()
  52. if commandEnv.isDirectory(ctx, filerServer, filerPort, path) {
  53. path = path + "/"
  54. }
  55. dir, name := filer2.FullPath(path).DirAndName()
  56. entryCount := 0
  57. err = filer2.ReadDirAllEntries(ctx, commandEnv.getFilerClient(filerServer, filerPort), filer2.FullPath(dir), name, func(entry *filer_pb.Entry, isLast bool) {
  58. if !showHidden && strings.HasPrefix(entry.Name, ".") {
  59. return
  60. }
  61. entryCount++
  62. if isLongFormat {
  63. fileMode := os.FileMode(entry.Attributes.FileMode)
  64. userName, groupNames := entry.Attributes.UserName, entry.Attributes.GroupName
  65. if userName == "" {
  66. if user, userErr := user.LookupId(strconv.Itoa(int(entry.Attributes.Uid))); userErr == nil {
  67. userName = user.Username
  68. }
  69. }
  70. groupName := ""
  71. if len(groupNames) > 0 {
  72. groupName = groupNames[0]
  73. }
  74. if groupName == "" {
  75. if group, groupErr := user.LookupGroupId(strconv.Itoa(int(entry.Attributes.Gid))); groupErr == nil {
  76. groupName = group.Name
  77. }
  78. }
  79. if dir == "/" {
  80. // just for printing
  81. dir = ""
  82. }
  83. fmt.Fprintf(writer, "%s %3d %s %s %6d %s/%s\n",
  84. fileMode, len(entry.Chunks),
  85. userName, groupName,
  86. filer2.TotalSize(entry.Chunks), dir, entry.Name)
  87. } else {
  88. fmt.Fprintf(writer, "%s\n", entry.Name)
  89. }
  90. })
  91. if isLongFormat && err == nil {
  92. fmt.Fprintf(writer, "total %d\n", entryCount)
  93. }
  94. return
  95. }