Added the Stat API

This commit is contained in:
Diptanu Choudhury
2016-01-12 15:25:51 -08:00
parent 744c226c23
commit cbcdaec3fe
4 changed files with 43 additions and 1 deletions

View File

@@ -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)
}

View File

@@ -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)

View File

@@ -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 {

View File

@@ -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) {