2019-03-27 03:43:51 +08:00
|
|
|
package shell
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"os/user"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
2019-12-13 16:22:37 +08:00
|
|
|
|
|
|
|
"github.com/chrislusf/seaweedfs/weed/filer2"
|
|
|
|
"github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
|
2019-03-27 03:43:51 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
2019-06-05 16:30:24 +08:00
|
|
|
Commands = append(Commands, &commandFsLs{})
|
2019-03-27 03:43:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
type commandFsLs struct {
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *commandFsLs) Name() string {
|
|
|
|
return "fs.ls"
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *commandFsLs) Help() string {
|
2019-03-28 15:05:04 +08:00
|
|
|
return `list all files under a directory
|
2019-03-27 03:43:51 +08:00
|
|
|
|
2019-04-22 06:43:43 +08:00
|
|
|
fs.ls [-l] [-a] /dir/
|
|
|
|
fs.ls [-l] [-a] /dir/file_name
|
|
|
|
fs.ls [-l] [-a] /dir/file_prefix
|
2019-03-28 15:05:04 +08:00
|
|
|
fs.ls [-l] [-a] http://<filer_server>:<port>/dir/
|
|
|
|
fs.ls [-l] [-a] http://<filer_server>:<port>/dir/file_name
|
|
|
|
fs.ls [-l] [-a] http://<filer_server>:<port>/dir/file_prefix
|
2019-03-27 03:43:51 +08:00
|
|
|
`
|
|
|
|
}
|
|
|
|
|
2019-06-05 16:30:24 +08:00
|
|
|
func (c *commandFsLs) Do(args []string, commandEnv *CommandEnv, writer io.Writer) (err error) {
|
2019-03-27 03:43:51 +08:00
|
|
|
|
|
|
|
var isLongFormat, showHidden bool
|
|
|
|
for _, arg := range args {
|
2019-04-05 10:27:51 +08:00
|
|
|
if !strings.HasPrefix(arg, "-") {
|
|
|
|
break
|
2019-03-27 03:43:51 +08:00
|
|
|
}
|
2019-04-05 10:27:51 +08:00
|
|
|
for _, t := range arg {
|
|
|
|
switch t {
|
|
|
|
case 'a':
|
|
|
|
showHidden = true
|
|
|
|
case 'l':
|
|
|
|
isLongFormat = true
|
|
|
|
}
|
2019-04-03 15:20:00 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-05 10:27:51 +08:00
|
|
|
input := findInputDirectory(args)
|
|
|
|
|
2019-04-03 15:20:00 +08:00
|
|
|
filerServer, filerPort, path, err := commandEnv.parseUrl(input)
|
2019-03-27 03:43:51 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-04-05 10:27:51 +08:00
|
|
|
|
2020-02-26 13:50:12 +08:00
|
|
|
if commandEnv.isDirectory(filerServer, filerPort, path) {
|
2019-04-03 15:20:00 +08:00
|
|
|
path = path + "/"
|
|
|
|
}
|
2019-03-27 03:43:51 +08:00
|
|
|
|
|
|
|
dir, name := filer2.FullPath(path).DirAndName()
|
2019-12-13 16:22:37 +08:00
|
|
|
entryCount := 0
|
2019-03-27 03:43:51 +08:00
|
|
|
|
2020-02-26 13:50:12 +08:00
|
|
|
err = filer2.ReadDirAllEntries(commandEnv.getFilerClient(filerServer, filerPort), filer2.FullPath(dir), name, func(entry *filer_pb.Entry, isLast bool) {
|
2019-03-27 03:43:51 +08:00
|
|
|
|
2019-12-13 16:22:37 +08:00
|
|
|
if !showHidden && strings.HasPrefix(entry.Name, ".") {
|
2019-03-27 03:43:51 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-12-13 16:22:37 +08:00
|
|
|
entryCount++
|
2019-03-27 03:43:51 +08:00
|
|
|
|
2019-12-13 16:22:37 +08: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-27 03:43:51 +08:00
|
|
|
}
|
2019-12-13 16:22:37 +08: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-27 03:43:51 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-13 16:22:37 +08:00
|
|
|
if dir == "/" {
|
|
|
|
// just for printing
|
|
|
|
dir = ""
|
|
|
|
}
|
|
|
|
fmt.Fprintf(writer, "%s %3d %s %s %6d %s/%s\n",
|
|
|
|
fileMode, len(entry.Chunks),
|
|
|
|
userName, groupName,
|
|
|
|
filer2.TotalSize(entry.Chunks), dir, entry.Name)
|
|
|
|
} else {
|
|
|
|
fmt.Fprintf(writer, "%s\n", entry.Name)
|
2019-03-27 03:43:51 +08:00
|
|
|
}
|
|
|
|
|
2019-12-13 16:22:37 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
if isLongFormat && err == nil {
|
2019-03-27 03:43:51 +08:00
|
|
|
fmt.Fprintf(writer, "total %d\n", entryCount)
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|