2017-08-27 12:19:58 +08:00
|
|
|
package torrentfs
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2017-08-27 23:42:02 +08:00
|
|
|
"io"
|
|
|
|
|
2021-11-16 15:20:02 +08:00
|
|
|
"github.com/anacrolix/fuse"
|
|
|
|
"github.com/anacrolix/fuse/fs"
|
2021-06-23 15:24:50 +08:00
|
|
|
"github.com/anacrolix/missinggo/v2"
|
2017-12-01 15:12:29 +08:00
|
|
|
"github.com/anacrolix/torrent"
|
2017-08-27 12:19:58 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
type fileHandle struct {
|
|
|
|
fn fileNode
|
2018-01-06 13:37:13 +08:00
|
|
|
r torrent.Reader
|
2017-08-27 12:19:58 +08:00
|
|
|
}
|
|
|
|
|
2017-08-27 23:42:02 +08:00
|
|
|
var _ interface {
|
|
|
|
fs.HandleReader
|
|
|
|
fs.HandleReleaser
|
|
|
|
} = fileHandle{}
|
2017-08-27 12:19:58 +08:00
|
|
|
|
|
|
|
func (me fileHandle) Read(ctx context.Context, req *fuse.ReadRequest, resp *fuse.ReadResponse) error {
|
|
|
|
torrentfsReadRequests.Add(1)
|
|
|
|
if req.Dir {
|
|
|
|
panic("read on directory")
|
|
|
|
}
|
2021-02-15 12:53:32 +08:00
|
|
|
r := me.r
|
|
|
|
pos, err := r.Seek(req.Offset, io.SeekStart)
|
2017-08-27 12:19:58 +08:00
|
|
|
if err != nil {
|
2017-08-27 23:42:02 +08:00
|
|
|
panic(err)
|
2017-08-27 12:19:58 +08:00
|
|
|
}
|
2018-01-06 13:37:13 +08:00
|
|
|
if pos != req.Offset {
|
2017-08-27 23:42:02 +08:00
|
|
|
panic("seek failed")
|
2017-08-27 12:19:58 +08:00
|
|
|
}
|
2017-08-27 23:42:02 +08:00
|
|
|
resp.Data = resp.Data[:req.Size]
|
|
|
|
readDone := make(chan struct{})
|
|
|
|
ctx, cancel := context.WithCancel(ctx)
|
|
|
|
var readErr error
|
|
|
|
go func() {
|
|
|
|
defer close(readDone)
|
|
|
|
me.fn.FS.mu.Lock()
|
|
|
|
me.fn.FS.blockedReads++
|
|
|
|
me.fn.FS.event.Broadcast()
|
|
|
|
me.fn.FS.mu.Unlock()
|
|
|
|
var n int
|
2021-02-15 12:53:32 +08:00
|
|
|
r := missinggo.ContextedReader{r, ctx}
|
2021-11-08 11:47:01 +08:00
|
|
|
// log.Printf("reading %v bytes at %v", len(resp.Data), req.Offset)
|
2021-02-15 12:53:32 +08:00
|
|
|
if true {
|
2021-02-22 07:10:43 +08:00
|
|
|
// A user reported on that on freebsd 12.2, the system requires that reads are
|
|
|
|
// completely filled. Their system only asks for 64KiB at a time. I've seen systems that
|
|
|
|
// can demand up to 16MiB at a time, so this gets tricky. For now, I'll restore the old
|
|
|
|
// behaviour from before 2a7352a, which nobody reported problems with.
|
2021-02-15 12:53:32 +08:00
|
|
|
n, readErr = io.ReadFull(r, resp.Data)
|
|
|
|
} else {
|
|
|
|
n, readErr = r.Read(resp.Data)
|
|
|
|
if readErr == io.EOF {
|
|
|
|
readErr = nil
|
|
|
|
}
|
2017-08-27 23:42:02 +08:00
|
|
|
}
|
|
|
|
resp.Data = resp.Data[:n]
|
|
|
|
}()
|
|
|
|
defer func() {
|
|
|
|
<-readDone
|
|
|
|
me.fn.FS.mu.Lock()
|
|
|
|
me.fn.FS.blockedReads--
|
|
|
|
me.fn.FS.event.Broadcast()
|
|
|
|
me.fn.FS.mu.Unlock()
|
|
|
|
}()
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-readDone:
|
|
|
|
return readErr
|
|
|
|
case <-me.fn.FS.destroyed:
|
|
|
|
return fuse.EIO
|
|
|
|
case <-ctx.Done():
|
|
|
|
return fuse.EINTR
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (me fileHandle) Release(context.Context, *fuse.ReleaseRequest) error {
|
|
|
|
return me.r.Close()
|
2017-08-27 12:19:58 +08:00
|
|
|
}
|