]> Sergey Matveev's repositories - btrtrc.git/commitdiff
fs: Move fileNode stuff into its own file
authorMatt Joiner <anacrolix@gmail.com>
Sat, 26 Aug 2017 03:25:04 +0000 (13:25 +1000)
committerMatt Joiner <anacrolix@gmail.com>
Sat, 26 Aug 2017 03:25:04 +0000 (13:25 +1000)
fs/filenode.go [new file with mode: 0644]
fs/torrentfs.go

diff --git a/fs/filenode.go b/fs/filenode.go
new file mode 100644 (file)
index 0000000..f59e5b0
--- /dev/null
@@ -0,0 +1,51 @@
+package torrentfs
+
+import (
+       "fmt"
+
+       "bazil.org/fuse"
+       fusefs "bazil.org/fuse/fs"
+       "golang.org/x/net/context"
+)
+
+type fileNode struct {
+       node
+       size          uint64
+       TorrentOffset int64
+}
+
+var _ fusefs.HandleReader = fileNode{}
+
+func (fn fileNode) Attr(ctx context.Context, attr *fuse.Attr) error {
+       attr.Size = fn.size
+       attr.Mode = defaultMode
+       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
+}
index 06e05e008fd48599af4ea19da1bd096c19777f19..0d9244b803c2a054bf6a8dc7178ff76d82c5267f 100644 (file)
@@ -2,7 +2,6 @@ package torrentfs
 
 import (
        "expvar"
-       "fmt"
        "io"
        "os"
        "path"
@@ -54,18 +53,6 @@ type node struct {
        t        *torrent.Torrent
 }
 
-type fileNode struct {
-       node
-       size          uint64
-       TorrentOffset int64
-}
-
-func (fn fileNode) Attr(ctx context.Context, attr *fuse.Attr) error {
-       attr.Size = fn.size
-       attr.Mode = defaultMode
-       return nil
-}
-
 func (n *node) fsPath() string {
        return "/" + n.metadata.Name + "/" + n.path
 }
@@ -120,41 +107,12 @@ func readFull(ctx context.Context, fs *TorrentFS, t *torrent.Torrent, off int64,
        return
 }
 
-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
-}
-
 type dirNode struct {
        node
 }
 
 var (
        _ fusefs.HandleReadDirAller = dirNode{}
-       _ fusefs.HandleReader       = fileNode{}
 )
 
 func isSubPath(parent, child string) bool {