]> Sergey Matveev's repositories - btrtrc.git/commitdiff
Destroy the torrent FS on exit signals so ongoing syscalls don't block the unmount
authorMatt Joiner <anacrolix@gmail.com>
Mon, 1 Dec 2014 20:30:50 +0000 (14:30 -0600)
committerMatt Joiner <anacrolix@gmail.com>
Mon, 1 Dec 2014 20:30:50 +0000 (14:30 -0600)
cmd/torrentfs/main.go
fs/torrentfs.go

index fa9389e4e0f71bf099bfddc3dd7fde4375f95dd9..ec31aa6788083869ffa6ac9bf982fde476d0353d 100644 (file)
@@ -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)
index 18012d8960cc22e6124918fa71ad8e7b4ba1fa1c..835db4626cdc8d2b5f3ec157e8cd5580dd5d1fa7 100644 (file)
@@ -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{}),
        }