fs: Move file Read behaviour onto a new handle type
This commit is contained in:
parent
afb83e792b
commit
58d66a1b62
|
@ -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
|
||||
}
|
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
|
|
Loading…
Reference in New Issue