Implemeted readAt
This commit is contained in:
parent
74af0da4cd
commit
9e5d6d7fe8
|
@ -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)
|
||||
}
|
||||
|
|
|
@ -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 {
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue