From 58d66a1b622fffa3aa101cf2e85ed093f35b6e59 Mon Sep 17 00:00:00 2001 From: Matt Joiner Date: Sun, 27 Aug 2017 14:19:58 +1000 Subject: [PATCH] fs: Move file Read behaviour onto a new handle type --- fs/file_handle.go | 43 +++++++++++++++++++++++++++++++++++++++++++ fs/filenode.go | 34 +++++----------------------------- fs/torrentfs_test.go | 4 +++- 3 files changed, 51 insertions(+), 30 deletions(-) create mode 100644 fs/file_handle.go diff --git a/fs/file_handle.go b/fs/file_handle.go new file mode 100644 index 00000000..11a5d74d --- /dev/null +++ b/fs/file_handle.go @@ -0,0 +1,43 @@ +package torrentfs + +import ( + "context" + "fmt" + + "bazil.org/fuse" + "bazil.org/fuse/fs" +) + +type fileHandle struct { + fn fileNode +} + +var _ fs.HandleReader = fileHandle{} + +func (me fileHandle) Read(ctx context.Context, req *fuse.ReadRequest, resp *fuse.ReadResponse) error { + torrentfsReadRequests.Add(1) + if req.Dir { + panic("read on directory") + } + size := req.Size + fileLeft := int64(me.fn.size) - req.Offset + if fileLeft < 0 { + fileLeft = 0 + } + if fileLeft < int64(size) { + size = int(fileLeft) + } + resp.Data = resp.Data[:size] + if len(resp.Data) == 0 { + return nil + } + torrentOff := me.fn.TorrentOffset + req.Offset + n, err := readFull(ctx, me.fn.FS, me.fn.t, torrentOff, resp.Data) + if err != nil { + return err + } + if n != size { + panic(fmt.Sprintf("%d < %d", n, size)) + } + return nil +} diff --git a/fs/filenode.go b/fs/filenode.go index f59e5b0d..2c83fbf1 100644 --- a/fs/filenode.go +++ b/fs/filenode.go @@ -1,8 +1,6 @@ package torrentfs import ( - "fmt" - "bazil.org/fuse" fusefs "bazil.org/fuse/fs" "golang.org/x/net/context" @@ -14,7 +12,9 @@ type fileNode struct { TorrentOffset int64 } -var _ fusefs.HandleReader = fileNode{} +var ( + _ fusefs.NodeOpener = fileNode{} +) func (fn fileNode) Attr(ctx context.Context, attr *fuse.Attr) error { attr.Size = fn.size @@ -22,30 +22,6 @@ func (fn fileNode) Attr(ctx context.Context, attr *fuse.Attr) error { return nil } -func (fn fileNode) Read(ctx context.Context, req *fuse.ReadRequest, resp *fuse.ReadResponse) error { - torrentfsReadRequests.Add(1) - if req.Dir { - panic("read on directory") - } - size := req.Size - fileLeft := int64(fn.size) - req.Offset - if fileLeft < 0 { - fileLeft = 0 - } - if fileLeft < int64(size) { - size = int(fileLeft) - } - resp.Data = resp.Data[:size] - if len(resp.Data) == 0 { - return nil - } - torrentOff := fn.TorrentOffset + req.Offset - n, err := readFull(ctx, fn.FS, fn.t, torrentOff, resp.Data) - if err != nil { - return err - } - if n != size { - panic(fmt.Sprintf("%d < %d", n, size)) - } - return nil +func (fn fileNode) Open(ctx context.Context, req *fuse.OpenRequest, resp *fuse.OpenResponse) (fusefs.Handle, error) { + return fileHandle{fn}, nil } diff --git a/fs/torrentfs_test.go b/fs/torrentfs_test.go index 19d5b1f1..2625cc3c 100644 --- a/fs/torrentfs_test.go +++ b/fs/torrentfs_test.go @@ -210,7 +210,9 @@ func TestDownloadOnDemand(t *testing.T) { resp := &fuse.ReadResponse{ Data: make([]byte, size), } - node.(fusefs.HandleReader).Read(netContext.Background(), &fuse.ReadRequest{ + h, err := node.(fusefs.NodeOpener).Open(nil, nil, nil) + require.NoError(t, err) + h.(fusefs.HandleReader).Read(netContext.Background(), &fuse.ReadRequest{ Size: int(size), }, resp) assert.EqualValues(t, testutil.GreetingFileContents, resp.Data)