cmd/torrentfs/main.go | 19 +++++++++---------- fs/torrentfs.go | 24 ++++++++++++------------ diff --git a/cmd/torrentfs/main.go b/cmd/torrentfs/main.go index fa9389e4e0f71bf099bfddc3dd7fde4375f95dd9..ec31aa6788083869ffa6ac9bf982fde476d0353d 100644 --- a/cmd/torrentfs/main.go +++ b/cmd/torrentfs/main.go @@ -57,18 +57,17 @@ log.Fatal(err) } } -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 @@ log.Fatal(err) } 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 @@ } }() resolveTestPeerAddr() fs := torrentfs.New(client) + go exitSignalHandlers(fs) go func() { for { addTestPeer(client) diff --git a/fs/torrentfs.go b/fs/torrentfs.go index 18012d8960cc22e6124918fa71ad8e7b4ba1fa1c..835db4626cdc8d2b5f3ec157e8cd5580dd5d1fa7 100644 --- a/fs/torrentfs.go +++ b/fs/torrentfs.go @@ -24,18 +24,18 @@ torrentfsDelayedReadRequests = expvar.NewInt("torrentfsDelayedReadRequests") 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 @@ 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 @@ n, err = fs.Client.TorrentReadAt(ih, off, p) 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 @@ } 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{}), }