Skip to content

Commit

Permalink
Move Request.handle to *Request
Browse files Browse the repository at this point in the history
Updates pkg#192

Move handle() onto *Request, this has flow on effects to the interface
types declared in request-interface.go, the examples and the tests.

The decision to address pkg#192 started with advice from gopl.io, but I
think has uncovered several bugs as many of the request methods operate
on copies of data stored in the RequestServer's cache. I think this is
unintentional, but may point to some correctness issues, see pkg#193.
  • Loading branch information
davecheney committed Aug 13, 2017
1 parent 1a91f31 commit f2782dd
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 31 deletions.
8 changes: 4 additions & 4 deletions request-example.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func InMemHandler() Handlers {
}

// Handlers
func (fs *root) Fileread(r Request) (io.ReaderAt, error) {
func (fs *root) Fileread(r *Request) (io.ReaderAt, error) {
fs.filesLock.Lock()
defer fs.filesLock.Unlock()
file, err := fs.fetch(r.Filepath)
Expand All @@ -41,7 +41,7 @@ func (fs *root) Fileread(r Request) (io.ReaderAt, error) {
return file.ReaderAt()
}

func (fs *root) Filewrite(r Request) (io.WriterAt, error) {
func (fs *root) Filewrite(r *Request) (io.WriterAt, error) {
fs.filesLock.Lock()
defer fs.filesLock.Unlock()
file, err := fs.fetch(r.Filepath)
Expand All @@ -59,7 +59,7 @@ func (fs *root) Filewrite(r Request) (io.WriterAt, error) {
return file.WriterAt()
}

func (fs *root) Filecmd(r Request) error {
func (fs *root) Filecmd(r *Request) error {
fs.filesLock.Lock()
defer fs.filesLock.Unlock()
switch r.Method {
Expand Down Expand Up @@ -115,7 +115,7 @@ func (f listerat) ListAt(ls []os.FileInfo, offset int64) (int, error) {
return n, nil
}

func (fs *root) Filelist(r Request) (ListerAt, error) {
func (fs *root) Filelist(r *Request) (ListerAt, error) {
fs.filesLock.Lock()
defer fs.filesLock.Unlock()

Expand Down
8 changes: 4 additions & 4 deletions request-interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,22 @@ import (

// FileReader should return an io.Reader for the filepath
type FileReader interface {
Fileread(Request) (io.ReaderAt, error)
Fileread(*Request) (io.ReaderAt, error)
}

// FileWriter should return an io.Writer for the filepath
type FileWriter interface {
Filewrite(Request) (io.WriterAt, error)
Filewrite(*Request) (io.WriterAt, error)
}

// FileCmder should return an error (rename, remove, setstate, etc.)
type FileCmder interface {
Filecmd(Request) error
Filecmd(*Request) error
}

// FileLister should return file info interface and errors (readdir, stat)
type FileLister interface {
Filelist(Request) (ListerAt, error)
Filelist(*Request) (ListerAt, error)
}

// ListerAt does for file lists what io.ReaderAt does for files.
Expand Down
29 changes: 14 additions & 15 deletions request.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,26 +152,23 @@ func (r *Request) popPacket() packet_data {
}

// called from worker to handle packet/request
func (r Request) handle(handlers Handlers) (responsePacket, error) {
var err error
var rpkt responsePacket
func (r *Request) handle(handlers Handlers) (responsePacket, error) {
switch r.Method {
case "Get":
rpkt, err = fileget(handlers.FileGet, r)
return fileget(handlers.FileGet, r)
case "Put": // add "Append" to this to handle append only file writes
rpkt, err = fileput(handlers.FilePut, r)
return fileput(handlers.FilePut, r)
case "Setstat", "Rename", "Rmdir", "Mkdir", "Symlink", "Remove":
rpkt, err = filecmd(handlers.FileCmd, r)
return filecmd(handlers.FileCmd, r)
case "List", "Stat", "Readlink":
rpkt, err = filelist(handlers.FileList, r)
return filelist(handlers.FileList, r)
default:
return rpkt, errors.Errorf("unexpected method: %s", r.Method)
return nil, errors.Errorf("unexpected method: %s", r.Method)
}
return rpkt, err
}

// wrap FileReader handler
func fileget(h FileReader, r Request) (responsePacket, error) {
func fileget(h FileReader, r *Request) (responsePacket, error) {
var err error
reader := r.getReader()
if reader == nil {
Expand All @@ -197,7 +194,7 @@ func fileget(h FileReader, r Request) (responsePacket, error) {
}

// wrap FileWriter handler
func fileput(h FileWriter, r Request) (responsePacket, error) {
func fileput(h FileWriter, r *Request) (responsePacket, error) {
var err error
writer := r.getWriter()
if writer == nil {
Expand All @@ -217,11 +214,12 @@ func fileput(h FileWriter, r Request) (responsePacket, error) {
ID: pd.id,
StatusError: StatusError{
Code: ssh_FX_OK,
}}, nil
},
}, nil
}

// wrap FileCmder handler
func filecmd(h FileCmder, r Request) (responsePacket, error) {
func filecmd(h FileCmder, r *Request) (responsePacket, error) {
err := h.Filecmd(r)
if err != nil {
return nil, err
Expand All @@ -230,11 +228,12 @@ func filecmd(h FileCmder, r Request) (responsePacket, error) {
ID: r.pkt_id,
StatusError: StatusError{
Code: ssh_FX_OK,
}}, nil
},
}, nil
}

// wrap FileLister handler
func filelist(h FileLister, r Request) (responsePacket, error) {
func filelist(h FileLister, r *Request) (responsePacket, error) {
var err error
lister := r.getLister()
if lister == nil {
Expand Down
13 changes: 5 additions & 8 deletions request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,28 +18,25 @@ type testHandler struct {
err error // dummy error, should be file related
}

func (t *testHandler) Fileread(r Request) (io.ReaderAt, error) {
func (t *testHandler) Fileread(r *Request) (io.ReaderAt, error) {
if t.err != nil {
return nil, t.err
}
return bytes.NewReader(t.filecontents), nil
}

func (t *testHandler) Filewrite(r Request) (io.WriterAt, error) {
func (t *testHandler) Filewrite(r *Request) (io.WriterAt, error) {
if t.err != nil {
return nil, t.err
}
return io.WriterAt(t.output), nil
}

func (t *testHandler) Filecmd(r Request) error {
if t.err != nil {
return t.err
}
return nil
func (t *testHandler) Filecmd(r *Request) error {
return t.err
}

func (t *testHandler) Filelist(r Request) (ListerAt, error) {
func (t *testHandler) Filelist(r *Request) (ListerAt, error) {
if t.err != nil {
return nil, t.err
}
Expand Down

0 comments on commit f2782dd

Please sign in to comment.