Skip to content

Commit

Permalink
Implemements Reader/Writer conformly with docker-archive/go-p9p#29
Browse files Browse the repository at this point in the history
Signed-off-by: Simon Ferquel <[email protected]>
  • Loading branch information
simonferquel committed Oct 6, 2016
1 parent 7f770a9 commit 0f3582d
Showing 1 changed file with 12 additions and 7 deletions.
19 changes: 12 additions & 7 deletions api/go-datakit/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -283,26 +283,31 @@ func (f *File) NewIOWriter(ctx context.Context, offset int64) io.Writer {
}

func (r *ioFileReaderWriter) Read(p []byte) (n int, err error) {
if len(p) > r.f.c.session.MaxReadSize() {
p = p[:r.f.c.session.MaxReadSize()]
}
r.f.m.Lock()
defer r.f.m.Unlock()
n, err = r.f.c.session.Read(r.ctx, r.f.fid, p, r.offset)
r.offset += int64(n)
// additional error handling for compliying with io.Reader
if n == 0 && (err == nil || err == io.EOF) {
err = io.ErrUnexpectedEOF
}
if n < len(p) && err == io.EOF {
err = io.ErrUnexpectedEOF
if n < len(p) && err == nil {
err = io.EOF
}
return
}

func minInt(a, b int) int {
if a < b {
return a
}
return b
}
func (w *ioFileReaderWriter) Write(p []byte) (n int, err error) {
w.f.m.Lock()
defer w.f.m.Unlock()
for err == nil {
var written int
written, err = w.f.c.session.Write(w.ctx, w.f.fid, p, w.offset)
written, err = w.f.c.session.Write(w.ctx, w.f.fid, p[:minInt(len(p), w.f.c.session.MaxWriteSize())], w.offset)
p = p[written:]
w.offset += int64(written)
n += written
Expand Down

0 comments on commit 0f3582d

Please sign in to comment.