From 00b6f653fa593a0793aa278d732e9f229eb6a5ad Mon Sep 17 00:00:00 2001 From: Chris Lu Date: Sun, 21 Apr 2019 15:43:43 -0700 Subject: [PATCH] shell: add fs.cat --- weed/server/filer_server_handlers_read.go | 12 +++- weed/shell/command_fs_cat.go | 69 +++++++++++++++++++++++ weed/shell/command_fs_ls.go | 3 + 3 files changed, 82 insertions(+), 2 deletions(-) create mode 100644 weed/shell/command_fs_cat.go diff --git a/weed/server/filer_server_handlers_read.go b/weed/server/filer_server_handlers_read.go index 9ddf4dae5..ad057f1d3 100644 --- a/weed/server/filer_server_handlers_read.go +++ b/weed/server/filer_server_handlers_read.go @@ -14,7 +14,9 @@ import ( "github.com/chrislusf/seaweedfs/weed/filer2" "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/wdclient" ) 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 { - 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) for _, chunkView := range chunkViews { - urlString, err := fs.filer.MasterClient.LookupFileId(chunkView.FileId) + urlString, err := masterClient.LookupFileId(chunkView.FileId) if err != nil { glog.V(1).Infof("operation LookupFileId %s failed, err: %v", chunkView.FileId, err) return err diff --git a/weed/shell/command_fs_cat.go b/weed/shell/command_fs_cat.go new file mode 100644 index 000000000..09e86d657 --- /dev/null +++ b/weed/shell/command_fs_cat.go @@ -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://:/dir/ + fs.cat http://:/dir/file_name + fs.cat http://:/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) + + }) + +} diff --git a/weed/shell/command_fs_ls.go b/weed/shell/command_fs_ls.go index 93b86fa9f..5c5130c53 100644 --- a/weed/shell/command_fs_ls.go +++ b/weed/shell/command_fs_ls.go @@ -26,6 +26,9 @@ func (c *commandFsLs) Name() string { func (c *commandFsLs) Help() string { 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://:/dir/ fs.ls [-l] [-a] http://:/dir/file_name fs.ls [-l] [-a] http://:/dir/file_prefix