From 82ca11faa9045023166b0a3781b694d9dc076f18 Mon Sep 17 00:00:00 2001 From: Matt Joiner Date: Mon, 1 Dec 2014 14:30:50 -0600 Subject: [PATCH] Destroy the torrent FS on exit signals so ongoing syscalls don't block the unmount --- cmd/torrentfs/main.go | 19 +++++++++---------- fs/torrentfs.go | 24 ++++++++++++------------ 2 files changed, 21 insertions(+), 22 deletions(-) diff --git a/cmd/torrentfs/main.go b/cmd/torrentfs/main.go index fa9389e4..ec31aa67 100644 --- a/cmd/torrentfs/main.go +++ b/cmd/torrentfs/main.go @@ -57,18 +57,17 @@ func resolveTestPeerAddr() { } } -func setSignalHandlers() { +func exitSignalHandlers(fs *torrentfs.TorrentFS) { c := make(chan os.Signal) signal.Notify(c, syscall.SIGINT, syscall.SIGTERM) - go func() { - for { - <-c - err := fuse.Unmount(mountDir) - if err != nil { - log.Print(err) - } + for { + <-c + fs.Destroy() + err := fuse.Unmount(*mountDir) + if err != nil { + log.Print(err) } - }() + } } func addTestPeer(client *torrent.Client) { @@ -104,7 +103,6 @@ func main() { } defer fuse.Unmount(mountDir) // TODO: Think about the ramifications of exiting not due to a signal. - setSignalHandlers() defer conn.Close() client, err := torrent.NewClient(&torrent.Config{ DataDir: downloadDir, @@ -145,6 +143,7 @@ func main() { }() resolveTestPeerAddr() fs := torrentfs.New(client) + go exitSignalHandlers(fs) go func() { for { addTestPeer(client) diff --git a/fs/torrentfs.go b/fs/torrentfs.go index 18012d89..835db462 100644 --- a/fs/torrentfs.go +++ b/fs/torrentfs.go @@ -24,18 +24,18 @@ var ( interruptedReads = expvar.NewInt("interruptedReads") ) -type torrentFS struct { +type TorrentFS struct { Client *torrent.Client destroyed chan struct{} mu sync.Mutex } var ( - _ fusefs.FSDestroyer = &torrentFS{} - _ fusefs.FSIniter = &torrentFS{} + _ 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(req *fuse.InitRequest, resp *fuse.InitResponse, intr fusefs.Intr) fuse.Error { log.Print(req) log.Print(resp) resp.MaxReadahead = req.MaxReadahead @@ -46,13 +46,13 @@ func (fs *torrentFS) Init(req *fuse.InitRequest, resp *fuse.InitResponse, intr f var _ fusefs.NodeForgetter = rootNode{} type rootNode struct { - fs *torrentFS + fs *TorrentFS } type node struct { path []string metadata *torrent.MetaInfo - FS *torrentFS + FS *TorrentFS InfoHash torrent.InfoHash } @@ -72,7 +72,7 @@ func (n *node) fsPath() string { return "/" + strings.Join(append([]string{n.metadata.Name}, n.path...), "/") } -func blockingRead(fs *torrentFS, ih torrent.InfoHash, off int64, p []byte, intr fusefs.Intr) (n int, err fuse.Error) { +func blockingRead(fs *TorrentFS, ih torrent.InfoHash, off int64, p []byte, intr fusefs.Intr) (n int, err fuse.Error) { dataWaiter := fs.Client.DataWaiter(ih, off) select { case <-dataWaiter: @@ -87,7 +87,7 @@ func blockingRead(fs *torrentFS, ih torrent.InfoHash, off int64, p []byte, intr return } -func readFull(fs *torrentFS, ih torrent.InfoHash, off int64, p []byte, intr fusefs.Intr) (n int, err fuse.Error) { +func readFull(fs *TorrentFS, ih torrent.InfoHash, off int64, p []byte, intr fusefs.Intr) (n int, err fuse.Error) { for len(p) != 0 { var nn int nn, err = blockingRead(fs, ih, off, p, intr) @@ -276,11 +276,11 @@ func (me rootNode) Forget() { me.fs.Destroy() } -func (tfs *torrentFS) Root() (fusefs.Node, fuse.Error) { +func (tfs *TorrentFS) Root() (fusefs.Node, fuse.Error) { return rootNode{tfs}, nil } -func (me *torrentFS) Destroy() { +func (me *TorrentFS) Destroy() { me.mu.Lock() select { case <-me.destroyed: @@ -290,8 +290,8 @@ func (me *torrentFS) Destroy() { me.mu.Unlock() } -func New(cl *torrent.Client) *torrentFS { - fs := &torrentFS{ +func New(cl *torrent.Client) *TorrentFS { + fs := &TorrentFS{ Client: cl, destroyed: make(chan struct{}), } -- 2.48.1