Implemeted readAt

This commit is contained in:
Diptanu Choudhury 2016-01-12 22:06:42 -08:00
parent 74af0da4cd
commit 9e5d6d7fe8
4 changed files with 51 additions and 7 deletions

View file

@ -3,6 +3,7 @@ package client
import (
"encoding/json"
"fmt"
"io"
"log"
"os"
"path/filepath"
@ -432,3 +433,7 @@ func (r *AllocRunner) FSList(path string) ([]*allocdir.AllocFile, error) {
func (r *AllocRunner) FSStat(path string) (*allocdir.AllocFile, error) {
return r.ctx.AllocDir.FSStat(path)
}
func (r *AllocRunner) FSReadAt(allocID string, path string, offset int64, limit int64, w io.Writer) error {
return r.ctx.AllocDir.FSReadAt(allocID, path, offset, limit, w)
}

View file

@ -254,15 +254,19 @@ func (d *AllocDir) FSStat(path string) (*AllocFile, error) {
}, nil
}
func (d *AllocDir) FSReadAt(allocID string, path string, offset int64, limit int64) ([]byte, error) {
func (d *AllocDir) FSReadAt(allocID string, path string, offset int64, limit int64, w io.Writer) error {
buf := make([]byte, limit)
p := filepath.Join(d.AllocDir, path)
f, err := os.Open(p)
if err != nil {
return nil, err
return err
}
b := make([]byte, limit)
f.ReadAt(b, offset)
return b, nil
n, err := f.ReadAt(buf, offset)
if err != nil {
return err
}
w.Write(buf[:n])
return nil
}
func fileCopy(src, dst string, perm os.FileMode) error {

View file

@ -2,6 +2,7 @@ package client
import (
"fmt"
"io"
"io/ioutil"
"log"
"net"
@ -371,8 +372,12 @@ func (c *Client) FSStat(allocID string, path string) (*allocdir.AllocFile, error
return ar.FSStat(path)
}
func (c *Client) FSReadAt(allocID string, path string, offset int64, limit int64) ([]byte, error) {
return nil, 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

@ -3,6 +3,7 @@ package agent
import (
"fmt"
"net/http"
"strconv"
"strings"
)
@ -42,5 +43,34 @@ func (s *HTTPServer) FileStatRequest(resp http.ResponseWriter, req *http.Request
}
func (s *HTTPServer) FileReadAtRequest(resp http.ResponseWriter, req *http.Request) (interface{}, error) {
allocID := strings.TrimPrefix(req.URL.Path, "/v1/client/fs/readat/")
path := req.URL.Query().Get("path")
ofs := req.URL.Query().Get("offset")
if ofs == "" {
ofs = "0"
}
offset, err := strconv.ParseInt(ofs, 10, 64)
if err != nil {
return nil, fmt.Errorf("error parsing offset: %v", err)
}
lim := req.URL.Query().Get("limit")
limit, err := strconv.ParseInt(lim, 10, 64)
if err != nil {
return nil, fmt.Errorf("error parsing limit: %v", err)
}
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")
}
if err = s.agent.client.FSReadAt(allocID, path, offset, limit, resp); err != nil {
return nil, err
}
return nil, nil
}