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.

110 lines
2.9 KiB

  1. package shell
  2. import (
  3. "fmt"
  4. "io"
  5. "google.golang.org/grpc"
  6. "github.com/chrislusf/seaweedfs/weed/filer2"
  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 http://<filer_server>:<port>/dir
  21. fs.du http://<filer_server>:<port>/dir/file_name
  22. fs.du http://<filer_server>:<port>/dir/file_prefix
  23. `
  24. }
  25. func (c *commandFsDu) Do(args []string, commandEnv *CommandEnv, writer io.Writer) (err error) {
  26. filerServer, filerPort, path, err := commandEnv.parseUrl(findInputDirectory(args))
  27. if err != nil {
  28. return err
  29. }
  30. if commandEnv.isDirectory(filerServer, filerPort, path) {
  31. path = path + "/"
  32. }
  33. var blockCount, byteCount uint64
  34. dir, name := filer2.FullPath(path).DirAndName()
  35. blockCount, byteCount, err = duTraverseDirectory(writer, commandEnv.getFilerClient(filerServer, filerPort), 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 filer2.FilerClient, dir, name string) (blockCount, byteCount uint64, err error) {
  42. err = filer2.ReadDirAllEntries(filerClient, filer2.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(filerServer string, filerPort int64, fn func(filer_pb.SeaweedFilerClient) error) error {
  64. filerGrpcAddress := fmt.Sprintf("%s:%d", filerServer, filerPort+10000)
  65. return util.WithCachedGrpcClient(func(grpcConnection *grpc.ClientConn) error {
  66. client := filer_pb.NewSeaweedFilerClient(grpcConnection)
  67. return fn(client)
  68. }, filerGrpcAddress, env.option.GrpcDialOption)
  69. }
  70. type commandFilerClient struct {
  71. env *CommandEnv
  72. filerServer string
  73. filerPort int64
  74. }
  75. func (env *CommandEnv) getFilerClient(filerServer string, filerPort int64) *commandFilerClient {
  76. return &commandFilerClient{
  77. env: env,
  78. filerServer: filerServer,
  79. filerPort: filerPort,
  80. }
  81. }
  82. func (c *commandFilerClient) WithFilerClient(fn func(filer_pb.SeaweedFilerClient) error) error {
  83. return c.env.withFilerClient(c.filerServer, c.filerPort, fn)
  84. }
  85. func (c *commandFilerClient) AdjustedUrl(hostAndPort string) string {
  86. return hostAndPort
  87. }