]> Sergey Matveev's repositories - btrtrc.git/blobdiff - fs/file_handle.go
fs: Use a new torrent file reader per handled request
[btrtrc.git] / fs / file_handle.go
index 592153c374667fdb3d95ca5a0d4d348f62cb9a93..2a8fe2ccad5f6a96cefa24b5db687bec26a520e1 100644 (file)
@@ -3,18 +3,17 @@ package torrentfs
 import (
        "context"
        "io"
-       "os"
 
-       "github.com/anacrolix/missinggo"
-       "github.com/anacrolix/torrent"
+       "github.com/anacrolix/fuse"
+       "github.com/anacrolix/fuse/fs"
+       "github.com/anacrolix/missinggo/v2"
 
-       "bazil.org/fuse"
-       "bazil.org/fuse/fs"
+       "github.com/anacrolix/torrent"
 )
 
 type fileHandle struct {
        fn fileNode
-       r  *torrent.Reader
+       tf *torrent.File
 }
 
 var _ interface {
@@ -27,11 +26,13 @@ func (me fileHandle) Read(ctx context.Context, req *fuse.ReadRequest, resp *fuse
        if req.Dir {
                panic("read on directory")
        }
-       pos, err := me.r.Seek(me.fn.TorrentOffset+req.Offset, os.SEEK_SET)
+       r := me.tf.NewReader()
+       defer r.Close()
+       pos, err := r.Seek(req.Offset, io.SeekStart)
        if err != nil {
                panic(err)
        }
-       if pos != me.fn.TorrentOffset+req.Offset {
+       if pos != req.Offset {
                panic("seek failed")
        }
        resp.Data = resp.Data[:req.Size]
@@ -45,10 +46,19 @@ func (me fileHandle) Read(ctx context.Context, req *fuse.ReadRequest, resp *fuse
                me.fn.FS.event.Broadcast()
                me.fn.FS.mu.Unlock()
                var n int
-               r := missinggo.ContextedReader{me.r, ctx}
-               n, readErr = r.Read(resp.Data)
-               if readErr == io.EOF {
-                       readErr = nil
+               r := missinggo.ContextedReader{r, ctx}
+               // log.Printf("reading %v bytes at %v", len(resp.Data), req.Offset)
+               if true {
+                       // 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.
+                       n, readErr = io.ReadFull(r, resp.Data)
+               } else {
+                       n, readErr = r.Read(resp.Data)
+                       if readErr == io.EOF {
+                               readErr = nil
+                       }
                }
                resp.Data = resp.Data[:n]
        }()
@@ -72,5 +82,5 @@ func (me fileHandle) Read(ctx context.Context, req *fuse.ReadRequest, resp *fuse
 }
 
 func (me fileHandle) Release(context.Context, *fuse.ReleaseRequest) error {
-       return me.r.Close()
+       return nil
 }