From 21847954e061cdd2ece595f4ff783148fe8dc157 Mon Sep 17 00:00:00 2001 From: Diptanu Choudhury Date: Thu, 14 Jan 2016 13:35:42 -0800 Subject: [PATCH] changed the API of the client --- client/alloc_runner.go | 13 ++----------- client/allocdir/alloc_dir.go | 19 ++++++++++++++----- client/client.go | 21 ++------------------- command/agent/fs_endpoint.go | 21 ++++++++++++++++++--- 4 files changed, 36 insertions(+), 38 deletions(-) diff --git a/client/alloc_runner.go b/client/alloc_runner.go index 65a73b15f..1a6fd639b 100644 --- a/client/alloc_runner.go +++ b/client/alloc_runner.go @@ -3,7 +3,6 @@ package client import ( "encoding/json" "fmt" - "io" "log" "os" "path/filepath" @@ -426,14 +425,6 @@ func (r *AllocRunner) WaitCh() <-chan struct{} { return r.waitCh } -func (r *AllocRunner) FSList(path string) ([]*allocdir.AllocFileInfo, error) { - return r.ctx.AllocDir.List(path) -} - -func (r *AllocRunner) FSStat(path string) (*allocdir.AllocFileInfo, error) { - return r.ctx.AllocDir.Stat(path) -} - -func (r *AllocRunner) FSReadAt(allocID string, path string, offset int64, limit int64, w io.Writer) error { - return r.ctx.AllocDir.ReadAt(allocID, path, offset, limit, w) +func (r *AllocRunner) GetAllocFS(allocID string) allocdir.AllocDirFS { + return r.ctx.AllocDir } diff --git a/client/allocdir/alloc_dir.go b/client/allocdir/alloc_dir.go index eafca6d4e..ddefdde0f 100644 --- a/client/allocdir/alloc_dir.go +++ b/client/allocdir/alloc_dir.go @@ -44,6 +44,12 @@ type AllocFileInfo struct { Size int64 } +type AllocDirFS interface { + List(path string) ([]*AllocFileInfo, error) + Stat(path string) (*AllocFileInfo, error) + ReadAt(path string, offset int64, limit int64) (io.ReadCloser, error) +} + func NewAllocDir(allocDir string) *AllocDir { d := &AllocDir{AllocDir: allocDir, TaskDirs: make(map[string]string)} d.SharedDir = filepath.Join(d.AllocDir, SharedAllocName) @@ -254,15 +260,18 @@ func (d *AllocDir) Stat(path string) (*AllocFileInfo, error) { }, nil } -func (d *AllocDir) ReadAt(allocID string, path string, offset int64, limit int64, w io.Writer) error { +func (d *AllocDir) ReadAt(path string, offset int64, limit int64) (io.ReadCloser, error) { p := filepath.Join(d.AllocDir, path) f, err := os.Open(p) if err != nil { - return err + return nil, err } - defer f.Close() - io.Copy(w, io.LimitReader(f, limit)) - return nil + return &LimitReadCloser{Reader: io.LimitReader(f, limit), Closer: f}, nil +} + +type LimitReadCloser struct { + io.Reader + io.Closer } func fileCopy(src, dst string, perm os.FileMode) error { diff --git a/client/client.go b/client/client.go index 737ab9b38..5e2d22066 100644 --- a/client/client.go +++ b/client/client.go @@ -2,7 +2,6 @@ package client import ( "fmt" - "io" "io/ioutil" "log" "net" @@ -355,29 +354,13 @@ func (c *Client) Node() *structs.Node { return c.config.Node } -func (c *Client) FSList(allocID string, path string) ([]*allocdir.AllocFileInfo, error) { - ar, ok := c.allocs[allocID] - if !ok { - return nil, fmt.Errorf("alloc not present") - } - - return ar.FSList(path) -} - -func (c *Client) FSStat(allocID string, path string) (*allocdir.AllocFileInfo, error) { +func (c *Client) GetAllocFS(allocID string) (allocdir.AllocDirFS, error) { ar, ok := c.allocs[allocID] if !ok { return nil, fmt.Errorf("alloc not found") } - return ar.FSStat(path) -} + return ar.GetAllocFS(allocID), nil -func (c *Client) FSReadAt(allocID string, path string, offset int64, limit int64, w io.Writer) error { - ar, ok := c.allocs[allocID] - if !ok { - return fmt.Errorf("alloc not found") - } - return ar.FSReadAt(allocID, path, offset, limit, w) } // restoreState is used to restore our state from the data dir diff --git a/command/agent/fs_endpoint.go b/command/agent/fs_endpoint.go index aaadad386..4a8de5698 100644 --- a/command/agent/fs_endpoint.go +++ b/command/agent/fs_endpoint.go @@ -2,6 +2,7 @@ package agent import ( "fmt" + "io" "net/http" "strconv" "strings" @@ -21,7 +22,11 @@ func (s *HTTPServer) DirectoryListRequest(resp http.ResponseWriter, req *http.Re if path = req.URL.Query().Get("path"); path == "" { path = "/" } - return s.agent.client.FSList(allocID, path) + fs, err := s.agent.client.GetAllocFS(allocID) + if err != nil { + return nil, err + } + return fs.List(path) } func (s *HTTPServer) FileStatRequest(resp http.ResponseWriter, req *http.Request) (interface{}, error) { @@ -32,7 +37,11 @@ func (s *HTTPServer) FileStatRequest(resp http.ResponseWriter, req *http.Request if path = req.URL.Query().Get("path"); path == "" { return nil, fileNameNotPresentErr } - return s.agent.client.FSStat(allocID, path) + fs, err := s.agent.client.GetAllocFS(allocID) + if err != nil { + return nil, err + } + return fs.Stat(path) } func (s *HTTPServer) FileReadAtRequest(resp http.ResponseWriter, req *http.Request) (interface{}, error) { @@ -55,8 +64,14 @@ func (s *HTTPServer) FileReadAtRequest(resp http.ResponseWriter, req *http.Reque if limit, err = strconv.ParseInt(q.Get("limit"), 10, 64); err != nil { return nil, fmt.Errorf("error parsing limit: %v", err) } - if err = s.agent.client.FSReadAt(allocID, path, offset, limit, resp); err != nil { + fs, err := s.agent.client.GetAllocFS(allocID) + if err != nil { return nil, err } + r, err := fs.ReadAt(path, offset, limit) + if err != nil { + return nil, err + } + io.Copy(resp, r) return nil, nil }