command_fs_ls.go 2.3 KB

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