fs/torrentfs.go | 32 +++++++++++++++++--------------- fs/torrentfs_test.go | 8 +++++--- diff --git a/fs/torrentfs.go b/fs/torrentfs.go index fd9cb0599a97cb2897bb84a969e11fef02590caa..0f448b381df20148dbdebd067ce9b2131a098be2 100644 --- a/fs/torrentfs.go +++ b/fs/torrentfs.go @@ -9,6 +9,8 @@ "strings" "sync" "time" + "golang.org/x/net/context" + "bazil.org/fuse" fusefs "bazil.org/fuse/fs" "bitbucket.org/anacrolix/go.torrent" @@ -37,7 +39,7 @@ _ fusefs.FSDestroyer = &TorrentFS{} _ fusefs.FSIniter = &TorrentFS{} ) -func (fs *TorrentFS) Init(req *fuse.InitRequest, resp *fuse.InitResponse, intr fusefs.Intr) fuse.Error { +func (fs *TorrentFS) Init(ctx context.Context, req *fuse.InitRequest, resp *fuse.InitResponse) error { log.Print(req) log.Print(resp) resp.MaxReadahead = req.MaxReadahead @@ -74,14 +76,14 @@ func (n *node) fsPath() string { return "/" + strings.Join(append([]string{n.metadata.Name}, n.path...), "/") } -func blockingRead(fs *TorrentFS, t torrent.Torrent, off int64, p []byte, intr fusefs.Intr) (n int, err fuse.Error) { +func blockingRead(ctx context.Context, fs *TorrentFS, t torrent.Torrent, off int64, p []byte) (n int, err error) { fs.mu.Lock() fs.blockedReads++ fs.event.Broadcast() fs.mu.Unlock() var ( _n int - _err fuse.Error + _err error ) readDone := make(chan struct{}) go func() { @@ -94,7 +96,7 @@ n = _n err = _err case <-fs.destroyed: err = fuse.EIO - case <-intr: + case <-ctx.Done(): err = fuse.EINTR } fs.mu.Lock() @@ -104,10 +106,10 @@ fs.mu.Unlock() return } -func readFull(fs *TorrentFS, t torrent.Torrent, off int64, p []byte, intr fusefs.Intr) (n int, err fuse.Error) { +func readFull(ctx context.Context, fs *TorrentFS, t torrent.Torrent, off int64, p []byte) (n int, err error) { for len(p) != 0 { var nn int - nn, err = blockingRead(fs, t, off, p, intr) + nn, err = blockingRead(ctx, fs, t, off, p) if err != nil { break } @@ -118,7 +120,7 @@ } return } -func (fn fileNode) Read(req *fuse.ReadRequest, resp *fuse.ReadResponse, intr fusefs.Intr) fuse.Error { +func (fn fileNode) Read(ctx context.Context, req *fuse.ReadRequest, resp *fuse.ReadResponse) error { torrentfsReadRequests.Add(1) started := time.Now() if req.Dir { @@ -144,7 +146,7 @@ if len(resp.Data) == 0 { return nil } torrentOff := fn.TorrentOffset + req.Offset - n, err := readFull(fn.FS, fn.t, torrentOff, resp.Data, intr) + n, err := readFull(ctx, fn.FS, fn.t, torrentOff, resp.Data) if err != nil { return err } @@ -159,8 +161,8 @@ node } var ( - _ fusefs.HandleReadDirer = dirNode{} - _ fusefs.HandleReader = fileNode{} + _ fusefs.HandleReadDirAller = dirNode{} + _ fusefs.HandleReader = fileNode{} ) func isSubPath(parent, child []string) bool { @@ -175,7 +177,7 @@ } return true } -func (dn dirNode) ReadDir(intr fusefs.Intr) (des []fuse.Dirent, err fuse.Error) { +func (dn dirNode) ReadDirAll(ctx context.Context) (des []fuse.Dirent, err error) { names := map[string]bool{} for _, fi := range dn.metadata.Files { if !isSubPath(dn.path, fi.Path) { @@ -199,7 +201,7 @@ } return } -func (dn dirNode) Lookup(name string, intr fusefs.Intr) (_node fusefs.Node, err fuse.Error) { +func (dn dirNode) Lookup(ctx context.Context, name string) (_node fusefs.Node, err error) { var torrentOffset int64 for _, fi := range dn.metadata.Files { if !isSubPath(dn.path, fi.Path) { @@ -234,7 +236,7 @@ attr.Mode = os.ModeDir | defaultMode return } -func (me rootNode) Lookup(name string, intr fusefs.Intr) (_node fusefs.Node, err fuse.Error) { +func (me rootNode) Lookup(ctx context.Context, name string) (_node fusefs.Node, err error) { for _, t := range me.fs.Client.Torrents() { if t.Name() != name || t.Info == nil { continue @@ -257,7 +259,7 @@ } return } -func (me rootNode) ReadDir(intr fusefs.Intr) (dirents []fuse.Dirent, err fuse.Error) { +func (me rootNode) ReadDir(ctx context.Context) (dirents []fuse.Dirent, err error) { for _, t := range me.fs.Client.Torrents() { if t.Info == nil { continue @@ -287,7 +289,7 @@ func (me rootNode) Forget() { me.fs.Destroy() } -func (tfs *TorrentFS) Root() (fusefs.Node, fuse.Error) { +func (tfs *TorrentFS) Root() (fusefs.Node, error) { return rootNode{tfs}, nil } diff --git a/fs/torrentfs_test.go b/fs/torrentfs_test.go index 369071e98c21355efe3bc5d03a082bdbfdd3424f..7eeb6a4d3671ac9bf706c72630d2cc046d8cf3f6 100644 --- a/fs/torrentfs_test.go +++ b/fs/torrentfs_test.go @@ -14,6 +14,8 @@ "strings" "testing" "time" + "golang.org/x/net/context" + "bitbucket.org/anacrolix/go.torrent" "bitbucket.org/anacrolix/go.torrent/testutil" "bitbucket.org/anacrolix/go.torrent/util" @@ -218,14 +220,14 @@ }()}) fs := New(leecher) defer fs.Destroy() root, _ := fs.Root() - node, _ := root.(fusefs.NodeStringLookuper).Lookup("greeting", nil) + node, _ := root.(fusefs.NodeStringLookuper).Lookup(context.Background(), "greeting") size := int(node.Attr().Size) resp := &fuse.ReadResponse{ Data: make([]byte, size), } - node.(fusefs.HandleReader).Read(&fuse.ReadRequest{ + node.(fusefs.HandleReader).Read(context.Background(), &fuse.ReadRequest{ Size: size, - }, resp, nil) + }, resp) content := resp.Data if string(content) != testutil.GreetingFileContents { t.FailNow()