}
}
-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) {
}
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,
}()
resolveTestPeerAddr()
fs := torrentfs.New(client)
+ go exitSignalHandlers(fs)
go func() {
for {
addTestPeer(client)
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
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
}
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:
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)
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:
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{}),
}