2019-03-20 12:58:00 +08:00
|
|
|
package shell
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2019-12-13 16:22:37 +08:00
|
|
|
"io"
|
|
|
|
|
2019-03-20 12:58:00 +08:00
|
|
|
"github.com/chrislusf/seaweedfs/weed/filer2"
|
2020-03-04 16:39:47 +08:00
|
|
|
"github.com/chrislusf/seaweedfs/weed/pb"
|
2019-03-20 12:58:00 +08:00
|
|
|
"github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
2019-06-05 16:30:24 +08:00
|
|
|
Commands = append(Commands, &commandFsDu{})
|
2019-03-20 12:58:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
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 http://<filer_server>:<port>/dir
|
|
|
|
fs.du http://<filer_server>:<port>/dir/file_name
|
|
|
|
fs.du http://<filer_server>:<port>/dir/file_prefix
|
|
|
|
`
|
2019-03-20 12:58:00 +08:00
|
|
|
}
|
|
|
|
|
2019-06-05 16:30:24 +08:00
|
|
|
func (c *commandFsDu) Do(args []string, commandEnv *CommandEnv, writer io.Writer) (err error) {
|
2019-03-20 12:58:00 +08:00
|
|
|
|
2019-04-05 10:27:51 +08:00
|
|
|
filerServer, filerPort, path, err := commandEnv.parseUrl(findInputDirectory(args))
|
2019-03-20 12:58:00 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-02-26 13:50:12 +08:00
|
|
|
if commandEnv.isDirectory(filerServer, filerPort, path) {
|
2019-04-05 10:27:51 +08:00
|
|
|
path = path + "/"
|
2019-03-20 12:58:00 +08:00
|
|
|
}
|
|
|
|
|
2019-12-13 16:22:37 +08:00
|
|
|
var blockCount, byteCount uint64
|
|
|
|
dir, name := filer2.FullPath(path).DirAndName()
|
2020-02-26 13:50:12 +08:00
|
|
|
blockCount, byteCount, err = duTraverseDirectory(writer, commandEnv.getFilerClient(filerServer, filerPort), dir, name)
|
2019-03-20 12:58:00 +08:00
|
|
|
|
2019-12-13 16:22:37 +08:00
|
|
|
if name == "" && err == nil {
|
|
|
|
fmt.Fprintf(writer, "block:%4d\tbyte:%10d\t%s\n", blockCount, byteCount, dir)
|
|
|
|
}
|
2019-03-20 12:58:00 +08:00
|
|
|
|
2019-12-13 16:22:37 +08:00
|
|
|
return
|
2019-03-20 12:58:00 +08:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-02-26 13:50:12 +08:00
|
|
|
func duTraverseDirectory(writer io.Writer, filerClient filer2.FilerClient, dir, name string) (blockCount, byteCount uint64, err error) {
|
2019-03-20 12:58:00 +08:00
|
|
|
|
2020-02-26 13:50:12 +08:00
|
|
|
err = filer2.ReadDirAllEntries(filerClient, filer2.FullPath(dir), name, func(entry *filer_pb.Entry, isLast bool) {
|
2019-12-13 16:22:37 +08:00
|
|
|
if entry.IsDirectory {
|
|
|
|
subDir := fmt.Sprintf("%s/%s", dir, entry.Name)
|
|
|
|
if dir == "/" {
|
|
|
|
subDir = "/" + entry.Name
|
2019-03-20 12:58:00 +08:00
|
|
|
}
|
2020-02-26 13:50:12 +08:00
|
|
|
numBlock, numByte, err := duTraverseDirectory(writer, filerClient, subDir, "")
|
2019-12-13 16:22:37 +08:00
|
|
|
if err == nil {
|
|
|
|
blockCount += numBlock
|
|
|
|
byteCount += numByte
|
2019-03-20 12:58:00 +08:00
|
|
|
}
|
2019-12-13 16:22:37 +08:00
|
|
|
} else {
|
|
|
|
blockCount += uint64(len(entry.Chunks))
|
|
|
|
byteCount += filer2.TotalSize(entry.Chunks)
|
2019-03-20 12:58:00 +08:00
|
|
|
}
|
|
|
|
|
2019-12-13 16:22:37 +08:00
|
|
|
if name != "" && !entry.IsDirectory {
|
|
|
|
fmt.Fprintf(writer, "block:%4d\tbyte:%10d\t%s/%s\n", blockCount, byteCount, dir, name)
|
|
|
|
}
|
|
|
|
})
|
2019-03-20 12:58:00 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-02-26 13:50:12 +08:00
|
|
|
func (env *CommandEnv) withFilerClient(filerServer string, filerPort int64, fn func(filer_pb.SeaweedFilerClient) error) error {
|
2019-03-20 12:58:00 +08:00
|
|
|
|
|
|
|
filerGrpcAddress := fmt.Sprintf("%s:%d", filerServer, filerPort+10000)
|
2020-03-04 16:39:47 +08:00
|
|
|
return pb.WithGrpcFilerClient(filerGrpcAddress, env.option.GrpcDialOption, fn)
|
2019-03-20 12:58:00 +08:00
|
|
|
|
|
|
|
}
|
2019-12-13 16:22:37 +08:00
|
|
|
|
|
|
|
type commandFilerClient struct {
|
|
|
|
env *CommandEnv
|
|
|
|
filerServer string
|
|
|
|
filerPort int64
|
|
|
|
}
|
|
|
|
|
|
|
|
func (env *CommandEnv) getFilerClient(filerServer string, filerPort int64) *commandFilerClient {
|
|
|
|
return &commandFilerClient{
|
|
|
|
env: env,
|
|
|
|
filerServer: filerServer,
|
|
|
|
filerPort: filerPort,
|
|
|
|
}
|
|
|
|
}
|
2020-02-26 13:50:12 +08:00
|
|
|
func (c *commandFilerClient) WithFilerClient(fn func(filer_pb.SeaweedFilerClient) error) error {
|
|
|
|
return c.env.withFilerClient(c.filerServer, c.filerPort, fn)
|
2019-12-13 16:22:37 +08:00
|
|
|
}
|
2020-02-27 08:46:01 +08:00
|
|
|
func (c *commandFilerClient) AdjustedUrl(hostAndPort string) string {
|
|
|
|
return hostAndPort
|
|
|
|
}
|