shell: add fs.cat

This commit is contained in:
Chris Lu 2019-04-21 15:43:43 -07:00
parent 59d532a8c3
commit 00b6f653fa
3 changed files with 82 additions and 2 deletions

View file

@ -14,7 +14,9 @@ import (
"github.com/chrislusf/seaweedfs/weed/filer2" "github.com/chrislusf/seaweedfs/weed/filer2"
"github.com/chrislusf/seaweedfs/weed/glog" "github.com/chrislusf/seaweedfs/weed/glog"
"github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
"github.com/chrislusf/seaweedfs/weed/util" "github.com/chrislusf/seaweedfs/weed/util"
"github.com/chrislusf/seaweedfs/weed/wdclient"
) )
func (fs *FilerServer) GetOrHeadHandler(w http.ResponseWriter, r *http.Request, isGetMethod bool) { func (fs *FilerServer) GetOrHeadHandler(w http.ResponseWriter, r *http.Request, isGetMethod bool) {
@ -231,13 +233,19 @@ func (fs *FilerServer) handleMultipleChunks(w http.ResponseWriter, r *http.Reque
func (fs *FilerServer) writeContent(w io.Writer, entry *filer2.Entry, offset int64, size int) error { func (fs *FilerServer) writeContent(w io.Writer, entry *filer2.Entry, offset int64, size int) error {
chunkViews := filer2.ViewFromChunks(entry.Chunks, offset, size) return StreamContent(fs.filer.MasterClient, w, entry.Chunks, offset, size)
}
func StreamContent(masterClient *wdclient.MasterClient, w io.Writer, chunks []*filer_pb.FileChunk, offset int64, size int) error {
chunkViews := filer2.ViewFromChunks(chunks, offset, size)
fileId2Url := make(map[string]string) fileId2Url := make(map[string]string)
for _, chunkView := range chunkViews { for _, chunkView := range chunkViews {
urlString, err := fs.filer.MasterClient.LookupFileId(chunkView.FileId) urlString, err := masterClient.LookupFileId(chunkView.FileId)
if err != nil { if err != nil {
glog.V(1).Infof("operation LookupFileId %s failed, err: %v", chunkView.FileId, err) glog.V(1).Infof("operation LookupFileId %s failed, err: %v", chunkView.FileId, err)
return err return err

View file

@ -0,0 +1,69 @@
package shell
import (
"context"
"fmt"
"io"
"math"
"github.com/chrislusf/seaweedfs/weed/filer2"
"github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
"github.com/chrislusf/seaweedfs/weed/server"
)
func init() {
commands = append(commands, &commandFsCat{})
}
type commandFsCat struct {
}
func (c *commandFsCat) Name() string {
return "fs.cat"
}
func (c *commandFsCat) Help() string {
return `stream the file content on to the screen
fs.cat /dir/
fs.cat /dir/file_name
fs.cat /dir/file_prefix
fs.cat http://<filer_server>:<port>/dir/
fs.cat http://<filer_server>:<port>/dir/file_name
fs.cat http://<filer_server>:<port>/dir/file_prefix
`
}
func (c *commandFsCat) Do(args []string, commandEnv *commandEnv, writer io.Writer) (err error) {
input := findInputDirectory(args)
filerServer, filerPort, path, err := commandEnv.parseUrl(input)
if err != nil {
return err
}
ctx := context.Background()
if commandEnv.isDirectory(ctx, filerServer, filerPort, path) {
return fmt.Errorf("%s is a directory", path)
}
dir, name := filer2.FullPath(path).DirAndName()
return commandEnv.withFilerClient(ctx, filerServer, filerPort, func(client filer_pb.SeaweedFilerClient) error {
request := &filer_pb.LookupDirectoryEntryRequest{
Name: name,
Directory: dir,
}
respLookupEntry, err := client.LookupDirectoryEntry(ctx, request)
if err != nil {
return err
}
return weed_server.StreamContent(commandEnv.masterClient, writer, respLookupEntry.Entry.Chunks, 0, math.MaxInt32)
})
}

View file

@ -26,6 +26,9 @@ func (c *commandFsLs) Name() string {
func (c *commandFsLs) Help() string { func (c *commandFsLs) Help() string {
return `list all files under a directory return `list all files under a directory
fs.ls [-l] [-a] /dir/
fs.ls [-l] [-a] /dir/file_name
fs.ls [-l] [-a] /dir/file_prefix
fs.ls [-l] [-a] http://<filer_server>:<port>/dir/ 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_name
fs.ls [-l] [-a] http://<filer_server>:<port>/dir/file_prefix fs.ls [-l] [-a] http://<filer_server>:<port>/dir/file_prefix