seaweedfs/weed/shell/command_fs_ls.go

112 lines
2.2 KiB
Go
Raw Permalink Normal View History

2019-03-26 19:43:51 +00:00
package shell
import (
"fmt"
"io"
"os"
"os/user"
"strconv"
"strings"
2019-12-13 08:22:37 +00:00
"github.com/seaweedfs/seaweedfs/weed/filer"
"github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
"github.com/seaweedfs/seaweedfs/weed/util"
2019-03-26 19:43:51 +00:00
)
func init() {
Commands = append(Commands, &commandFsLs{})
2019-03-26 19:43:51 +00:00
}
type commandFsLs struct {
}
func (c *commandFsLs) Name() string {
return "fs.ls"
}
func (c *commandFsLs) Help() string {
2019-03-28 07:05:04 +00:00
return `list all files under a directory
2019-03-26 19:43:51 +00:00
2019-04-21 22:43:43 +00:00
fs.ls [-l] [-a] /dir/
fs.ls [-l] [-a] /dir/file_name
fs.ls [-l] [-a] /dir/file_prefix
2019-03-26 19:43:51 +00:00
`
}
func (c *commandFsLs) Do(args []string, commandEnv *CommandEnv, writer io.Writer) (err error) {
2019-03-26 19:43:51 +00:00
var isLongFormat, showHidden bool
for _, arg := range args {
if !strings.HasPrefix(arg, "-") {
break
2019-03-26 19:43:51 +00:00
}
for _, t := range arg {
switch t {
case 'a':
showHidden = true
case 'l':
isLongFormat = true
}
}
}
2020-03-24 04:26:15 +00:00
path, err := commandEnv.parseUrl(findInputDirectory(args))
2019-03-26 19:43:51 +00:00
if err != nil {
return err
}
2020-03-24 04:26:15 +00:00
if commandEnv.isDirectory(path) {
path = path + "/"
}
2019-03-26 19:43:51 +00:00
2020-03-23 07:01:34 +00:00
dir, name := util.FullPath(path).DirAndName()
2019-12-13 08:22:37 +00:00
entryCount := 0
2019-03-26 19:43:51 +00:00
err = filer_pb.ReadDirAllEntries(commandEnv, util.FullPath(dir), name, func(entry *filer_pb.Entry, isLast bool) error {
2019-03-26 19:43:51 +00:00
2019-12-13 08:22:37 +00:00
if !showHidden && strings.HasPrefix(entry.Name, ".") {
return nil
2019-03-26 19:43:51 +00:00
}
2019-12-13 08:22:37 +00:00
entryCount++
2019-03-26 19:43:51 +00:00
2019-12-13 08:22:37 +00:00
if isLongFormat {
fileMode := os.FileMode(entry.Attributes.FileMode)
userName, groupNames := entry.Attributes.UserName, entry.Attributes.GroupName
if userName == "" {
if user, userErr := user.LookupId(strconv.Itoa(int(entry.Attributes.Uid))); userErr == nil {
userName = user.Username
2019-03-26 19:43:51 +00:00
}
2019-12-13 08:22:37 +00:00
}
groupName := ""
if len(groupNames) > 0 {
groupName = groupNames[0]
}
if groupName == "" {
if group, groupErr := user.LookupGroupId(strconv.Itoa(int(entry.Attributes.Gid))); groupErr == nil {
groupName = group.Name
2019-03-26 19:43:51 +00:00
}
}
2020-04-26 23:11:06 +00:00
if strings.HasSuffix(dir, "/") {
2019-12-13 08:22:37 +00:00
// just for printing
2020-04-26 23:11:06 +00:00
dir = dir[:len(dir)-1]
2019-12-13 08:22:37 +00:00
}
fmt.Fprintf(writer, "%s %3d %s %s %6d %s/%s\n",
fileMode, len(entry.Chunks),
userName, groupName,
2020-09-01 07:21:19 +00:00
filer.FileSize(entry), dir, entry.Name)
2019-12-13 08:22:37 +00:00
} else {
fmt.Fprintf(writer, "%s\n", entry.Name)
2019-03-26 19:43:51 +00:00
}
return nil
2019-12-13 08:22:37 +00:00
})
if isLongFormat && err == nil {
2019-03-26 19:43:51 +00:00
fmt.Fprintf(writer, "total %d\n", entryCount)
}
return
}