seaweedfs/weed/shell/command_fs_du.go

85 lines
1.9 KiB
Go
Raw Normal View History

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