From: Matt Joiner Date: Sat, 26 Aug 2017 03:25:04 +0000 (+1000) Subject: fs: Move fileNode stuff into its own file X-Git-Tag: v1.0.0~434 X-Git-Url: http://www.git.stargrave.org/?a=commitdiff_plain;h=625cca3b6f274c9c1f08347cd042eaf2057b2d1b;p=btrtrc.git fs: Move fileNode stuff into its own file --- diff --git a/fs/filenode.go b/fs/filenode.go new file mode 100644 index 00000000..f59e5b0d --- /dev/null +++ b/fs/filenode.go @@ -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 +} diff --git a/fs/torrentfs.go b/fs/torrentfs.go index 06e05e00..0d9244b8 100644 --- a/fs/torrentfs.go +++ b/fs/torrentfs.go @@ -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 {