fs: Move file Read behaviour onto a new handle type
This commit is contained in:
parent
afb83e792b
commit
58d66a1b62
43
fs/file_handle.go
Normal file
43
fs/file_handle.go
Normal file
@ -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
|
package torrentfs
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
|
|
||||||
"bazil.org/fuse"
|
"bazil.org/fuse"
|
||||||
fusefs "bazil.org/fuse/fs"
|
fusefs "bazil.org/fuse/fs"
|
||||||
"golang.org/x/net/context"
|
"golang.org/x/net/context"
|
||||||
@ -14,7 +12,9 @@ type fileNode struct {
|
|||||||
TorrentOffset int64
|
TorrentOffset int64
|
||||||
}
|
}
|
||||||
|
|
||||||
var _ fusefs.HandleReader = fileNode{}
|
var (
|
||||||
|
_ fusefs.NodeOpener = fileNode{}
|
||||||
|
)
|
||||||
|
|
||||||
func (fn fileNode) Attr(ctx context.Context, attr *fuse.Attr) error {
|
func (fn fileNode) Attr(ctx context.Context, attr *fuse.Attr) error {
|
||||||
attr.Size = fn.size
|
attr.Size = fn.size
|
||||||
@ -22,30 +22,6 @@ func (fn fileNode) Attr(ctx context.Context, attr *fuse.Attr) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (fn fileNode) Read(ctx context.Context, req *fuse.ReadRequest, resp *fuse.ReadResponse) error {
|
func (fn fileNode) Open(ctx context.Context, req *fuse.OpenRequest, resp *fuse.OpenResponse) (fusefs.Handle, error) {
|
||||||
torrentfsReadRequests.Add(1)
|
return fileHandle{fn}, nil
|
||||||
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
|
|
||||||
}
|
}
|
||||||
|
@ -210,7 +210,9 @@ func TestDownloadOnDemand(t *testing.T) {
|
|||||||
resp := &fuse.ReadResponse{
|
resp := &fuse.ReadResponse{
|
||||||
Data: make([]byte, size),
|
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),
|
Size: int(size),
|
||||||
}, resp)
|
}, resp)
|
||||||
assert.EqualValues(t, testutil.GreetingFileContents, resp.Data)
|
assert.EqualValues(t, testutil.GreetingFileContents, resp.Data)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user