2019-03-20 04:58:00 +00:00
|
|
|
package shell
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2019-12-13 08:22:37 +00:00
|
|
|
"io"
|
|
|
|
|
2019-03-20 04:58:00 +00:00
|
|
|
"github.com/chrislusf/seaweedfs/weed/filer2"
|
|
|
|
"github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
|
2020-03-23 07:01:34 +00:00
|
|
|
"github.com/chrislusf/seaweedfs/weed/util"
|
2019-03-20 04:58:00 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
2019-06-05 08:30:24 +00:00
|
|
|
Commands = append(Commands, &commandFsDu{})
|
2019-03-20 04:58:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type commandFsDu struct {
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *commandFsDu) Name() string {
|
|
|
|
return "fs.du"
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *commandFsDu) Help() string {
|
2019-03-23 18:54:26 +00:00
|
|
|
return `show disk usage
|
|
|
|
|
2020-03-24 03:46:17 +00:00
|
|
|
fs.du /dir
|
|
|
|
fs.du /dir/file_name
|
|
|
|
fs.du /dir/file_prefix
|
2019-03-23 18:54:26 +00:00
|
|
|
`
|
2019-03-20 04:58:00 +00:00
|
|
|
}
|
|
|
|
|
2019-06-05 08:30:24 +00:00
|
|
|
func (c *commandFsDu) Do(args []string, commandEnv *CommandEnv, writer io.Writer) (err error) {
|
2019-03-20 04:58:00 +00:00
|
|
|
|
2020-03-24 04:26:15 +00:00
|
|
|
path, err := commandEnv.parseUrl(findInputDirectory(args))
|
2019-03-20 04:58:00 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-03-24 04:26:15 +00:00
|
|
|
if commandEnv.isDirectory(path) {
|
2019-04-05 02:27:51 +00:00
|
|
|
path = path + "/"
|
2019-03-20 04:58:00 +00:00
|
|
|
}
|
|
|
|
|
2019-12-13 08:22:37 +00:00
|
|
|
var blockCount, byteCount uint64
|
2020-03-23 07:01:34 +00:00
|
|
|
dir, name := util.FullPath(path).DirAndName()
|
2020-03-24 04:26:15 +00:00
|
|
|
blockCount, byteCount, err = duTraverseDirectory(writer, commandEnv, dir, name)
|
2019-03-20 04:58:00 +00:00
|
|
|
|
2019-12-13 08:22:37 +00:00
|
|
|
if name == "" && err == nil {
|
|
|
|
fmt.Fprintf(writer, "block:%4d\tbyte:%10d\t%s\n", blockCount, byteCount, dir)
|
|
|
|
}
|
2019-03-20 04:58:00 +00:00
|
|
|
|
2019-12-13 08:22:37 +00:00
|
|
|
return
|
2019-03-20 04:58:00 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-03-23 07:01:34 +00:00
|
|
|
func duTraverseDirectory(writer io.Writer, filerClient filer_pb.FilerClient, dir, name string) (blockCount, byteCount uint64, err error) {
|
2019-03-20 04:58:00 +00:00
|
|
|
|
2020-04-30 00:40:08 +00:00
|
|
|
err = filer_pb.ReadDirAllEntries(filerClient, util.FullPath(dir), name, func(entry *filer_pb.Entry, isLast bool) error {
|
2020-03-24 04:37:04 +00:00
|
|
|
|
|
|
|
var fileBlockCount, fileByteCount uint64
|
|
|
|
|
2019-12-13 08:22:37 +00:00
|
|
|
if entry.IsDirectory {
|
|
|
|
subDir := fmt.Sprintf("%s/%s", dir, entry.Name)
|
|
|
|
if dir == "/" {
|
|
|
|
subDir = "/" + entry.Name
|
2019-03-20 04:58:00 +00:00
|
|
|
}
|
2020-02-26 05:50:12 +00:00
|
|
|
numBlock, numByte, err := duTraverseDirectory(writer, filerClient, subDir, "")
|
2019-12-13 08:22:37 +00:00
|
|
|
if err == nil {
|
|
|
|
blockCount += numBlock
|
|
|
|
byteCount += numByte
|
2019-03-20 04:58:00 +00:00
|
|
|
}
|
2019-12-13 08:22:37 +00:00
|
|
|
} else {
|
2020-03-24 04:37:04 +00:00
|
|
|
fileBlockCount = uint64(len(entry.Chunks))
|
|
|
|
fileByteCount = filer2.TotalSize(entry.Chunks)
|
2019-12-13 08:22:37 +00:00
|
|
|
blockCount += uint64(len(entry.Chunks))
|
|
|
|
byteCount += filer2.TotalSize(entry.Chunks)
|
2019-03-20 04:58:00 +00:00
|
|
|
}
|
|
|
|
|
2019-12-13 08:22:37 +00:00
|
|
|
if name != "" && !entry.IsDirectory {
|
2020-03-24 04:37:04 +00:00
|
|
|
fmt.Fprintf(writer, "block:%4d\tbyte:%10d\t%s/%s\n", fileBlockCount, fileByteCount, dir, entry.Name)
|
2019-12-13 08:22:37 +00:00
|
|
|
}
|
2020-04-30 00:40:08 +00:00
|
|
|
return nil
|
2019-12-13 08:22:37 +00:00
|
|
|
})
|
2019-03-20 04:58:00 +00:00
|
|
|
return
|
|
|
|
}
|