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.

114 lines
2.4 KiB

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