changed the API of the client

This commit is contained in:
Diptanu Choudhury
2016-01-14 13:35:42 -08:00
parent e7b1424bcb
commit 21847954e0
4 changed files with 36 additions and 38 deletions

View File

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

View File

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

View File

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

View File

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