From 38425b1a06514265f0944fb129b4a47fb59789fd Mon Sep 17 00:00:00 2001 From: Juan Batiz-Benet Date: Mon, 2 Mar 2015 06:12:54 -0800 Subject: [PATCH] fuse: fix read problem in osx @whyrusleeping's fix in c88340b broke reading fuse in osx. i'm not sure why... anyway, i chose to revert back to io.ReadFull, but use the min of req.Size and r.Size(), which should not encounter the reading problem in linux that a77ea2f fixed in the first place. This commit also changes ipns, which had not been changed. --- fuse/ipns/ipns_unix.go | 11 ++++++++++- fuse/readonly/readonly_unix.go | 13 ++++++++++--- 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/fuse/ipns/ipns_unix.go b/fuse/ipns/ipns_unix.go index 2ced56339..edb126f6c 100644 --- a/fuse/ipns/ipns_unix.go +++ b/fuse/ipns/ipns_unix.go @@ -354,7 +354,9 @@ func (s *Node) Read(ctx context.Context, req *fuse.ReadRequest, resp *fuse.ReadR if err != nil { return err } - n, err := io.ReadFull(r, resp.Data[:req.Size]) + + buf := resp.Data[:min(req.Size, int(r.Size()))] + n, err := io.ReadFull(r, buf) resp.Data = resp.Data[:n] lm["res_size"] = n return err // may be non-nil / not succeeded @@ -652,3 +654,10 @@ type ipnsNode interface { } var _ ipnsNode = (*Node)(nil) + +func min(a, b int) int { + if a < b { + return a + } + return b +} diff --git a/fuse/readonly/readonly_unix.go b/fuse/readonly/readonly_unix.go index 543ec0d4a..ed4b80f5d 100644 --- a/fuse/readonly/readonly_unix.go +++ b/fuse/readonly/readonly_unix.go @@ -5,7 +5,6 @@ package readonly import ( - "bytes" "io" "os" @@ -169,8 +168,9 @@ func (s *Node) Read(ctx context.Context, req *fuse.ReadRequest, resp *fuse.ReadR if err != nil { return err } - buf := bytes.NewBuffer(resp.Data) - n, err := io.CopyN(buf, r, int64(req.Size)) + + buf := resp.Data[:min(req.Size, int(r.Size()))] + n, err := io.ReadFull(r, buf) if err != nil && err != io.EOF { return err } @@ -196,3 +196,10 @@ type roNode interface { } var _ roNode = (*Node)(nil) + +func min(a, b int) int { + if a < b { + return a + } + return b +}