diff --git a/client/alloc_runner.go b/client/alloc_runner.go index 606ec8877..771f3fe87 100644 --- a/client/alloc_runner.go +++ b/client/alloc_runner.go @@ -428,3 +428,7 @@ func (r *AllocRunner) WaitCh() <-chan struct{} { func (r *AllocRunner) FSList(path string) ([]*allocdir.AllocFile, error) { return r.ctx.AllocDir.FSList(path) } + +func (r *AllocRunner) FSStat(path string) (*allocdir.AllocFile, error) { + return r.ctx.AllocDir.FSStat(path) +} diff --git a/client/allocdir/alloc_dir.go b/client/allocdir/alloc_dir.go index 4add2434d..19a26fd9c 100644 --- a/client/allocdir/alloc_dir.go +++ b/client/allocdir/alloc_dir.go @@ -41,6 +41,7 @@ type AllocDir struct { type AllocFile struct { Name string IsDir bool + Size int64 } func NewAllocDir(allocDir string) *AllocDir { @@ -233,11 +234,26 @@ func (d *AllocDir) FSList(path string) ([]*AllocFile, error) { files[idx] = &AllocFile{ Name: info.Name(), IsDir: info.IsDir(), + Size: info.Size(), } } return files, err } +func (d *AllocDir) FSStat(path string) (*AllocFile, error) { + info, err := os.Stat(path) + if err != nil { + return nil, err + } + + return &AllocFile{ + Size: info.Size(), + Name: info.Name(), + IsDir: info.IsDir(), + }, nil + +} + func fileCopy(src, dst string, perm os.FileMode) error { // Do a simple copy. srcFile, err := os.Open(src) diff --git a/client/client.go b/client/client.go index 20c7b1f7c..f7ae52682 100644 --- a/client/client.go +++ b/client/client.go @@ -363,6 +363,14 @@ func (c *Client) FSList(allocID string, path string) ([]*allocdir.AllocFile, err return ar.FSList(path) } +func (c *Client) FSStat(allocID string, path string) (*allocdir.AllocFile, error) { + ar, ok := c.allocs[allocID] + if !ok { + return nil, fmt.Errorf("alloc not found") + } + return ar.FSStat(path) +} + // restoreState is used to restore our state from the data dir func (c *Client) restoreState() error { if c.config.DevMode { diff --git a/command/agent/fs_endpoint.go b/command/agent/fs_endpoint.go index 46540997a..58dd1e5ab 100644 --- a/command/agent/fs_endpoint.go +++ b/command/agent/fs_endpoint.go @@ -24,7 +24,21 @@ func (s *HTTPServer) DirectoryListRequest(resp http.ResponseWriter, req *http.Re } func (s *HTTPServer) FileStatRequest(resp http.ResponseWriter, req *http.Request) (interface{}, error) { - return nil, nil + allocID := strings.TrimPrefix(req.URL.Path, "/v1/client/fs/ls/") + path := req.URL.Query().Get("path") + if path == "" { + resp.WriteHeader(http.StatusNotFound) + return nil, fmt.Errorf("must provide a file name") + } + if allocID == "" { + resp.WriteHeader(http.StatusNotFound) + return nil, fmt.Errorf("alloc id not found") + } + fileInfo, err := s.agent.client.FSStat(allocID, path) + if err != nil { + return nil, err + } + return fileInfo, nil } func (s *HTTPServer) FileReadAtRequest(resp http.ResponseWriter, req *http.Request) (interface{}, error) {