command_fs_du.go 2.0 KB

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