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.

90 lines
2.1 KiB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
  1. package shell
  2. import (
  3. "fmt"
  4. "io"
  5. "github.com/chrislusf/seaweedfs/weed/filer2"
  6. "github.com/chrislusf/seaweedfs/weed/pb"
  7. "github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
  8. "github.com/chrislusf/seaweedfs/weed/util"
  9. )
  10. func init() {
  11. Commands = append(Commands, &commandFsDu{})
  12. }
  13. type commandFsDu struct {
  14. }
  15. func (c *commandFsDu) Name() string {
  16. return "fs.du"
  17. }
  18. func (c *commandFsDu) Help() string {
  19. return `show disk usage
  20. fs.du /dir
  21. fs.du /dir/file_name
  22. fs.du /dir/file_prefix
  23. `
  24. }
  25. func (c *commandFsDu) Do(args []string, commandEnv *CommandEnv, writer io.Writer) (err error) {
  26. path, err := commandEnv.parseUrl(findInputDirectory(args))
  27. if err != nil {
  28. return err
  29. }
  30. if commandEnv.isDirectory(path) {
  31. path = path + "/"
  32. }
  33. var blockCount, byteCount uint64
  34. dir, name := util.FullPath(path).DirAndName()
  35. blockCount, byteCount, err = duTraverseDirectory(writer, commandEnv, dir, name)
  36. if name == "" && err == nil {
  37. fmt.Fprintf(writer, "block:%4d\tbyte:%10d\t%s\n", blockCount, byteCount, dir)
  38. }
  39. return
  40. }
  41. func duTraverseDirectory(writer io.Writer, filerClient filer_pb.FilerClient, dir, name string) (blockCount, byteCount uint64, err error) {
  42. err = filer_pb.ReadDirAllEntries(filerClient, util.FullPath(dir), name, func(entry *filer_pb.Entry, isLast bool) {
  43. if entry.IsDirectory {
  44. subDir := fmt.Sprintf("%s/%s", dir, entry.Name)
  45. if dir == "/" {
  46. subDir = "/" + entry.Name
  47. }
  48. numBlock, numByte, err := duTraverseDirectory(writer, filerClient, subDir, "")
  49. if err == nil {
  50. blockCount += numBlock
  51. byteCount += numByte
  52. }
  53. } else {
  54. blockCount += uint64(len(entry.Chunks))
  55. byteCount += filer2.TotalSize(entry.Chunks)
  56. }
  57. if name != "" && !entry.IsDirectory {
  58. fmt.Fprintf(writer, "block:%4d\tbyte:%10d\t%s/%s\n", blockCount, byteCount, dir, name)
  59. }
  60. })
  61. return
  62. }
  63. func (env *CommandEnv) WithFilerClient(fn func(filer_pb.SeaweedFilerClient) error) error {
  64. filerGrpcAddress := fmt.Sprintf("%s:%d", env.option.FilerHost, env.option.FilerPort+10000)
  65. return pb.WithGrpcFilerClient(filerGrpcAddress, env.option.GrpcDialOption, fn)
  66. }
  67. func (env *CommandEnv) AdjustedUrl(hostAndPort string) string {
  68. return hostAndPort
  69. }