17 fusefs "bazil.org/fuse/fs"
18 "bitbucket.org/anacrolix/go.torrent"
19 "bitbucket.org/anacrolix/go.torrent/fs"
20 metainfo "github.com/nsf/libtorgo/torrent"
27 disableTrackers = flag.Bool("disableTrackers", false, "disables trackers")
28 testPeer = flag.String("testPeer", "", "the address for a test peer")
29 pprofAddr = flag.String("pprofAddr", "", "pprof HTTP server bind address")
30 testPeerAddr *net.TCPAddr
34 flag.StringVar(&downloadDir, "downloadDir", "", "location to save torrent data")
35 flag.StringVar(&torrentPath, "torrentPath", func() string {
36 _user, err := user.Current()
40 return filepath.Join(_user.HomeDir, ".config/transmission/torrents")
41 }(), "torrent files in this location describe the contents of the mounted filesystem")
42 flag.StringVar(&mountDir, "mountDir", "", "location the torrent contents are made available")
45 func resolveTestPeerAddr() {
50 testPeerAddr, err = net.ResolveTCPAddr("tcp4", *testPeer)
56 func setSignalHandlers() {
57 c := make(chan os.Signal)
58 signal.Notify(c, syscall.SIGINT, syscall.SIGTERM)
62 err := fuse.Unmount(mountDir)
70 func addTorrent(c *torrent.Client, file string) {
71 metaInfo, err := metainfo.LoadFromFile(file)
76 err = c.AddTorrent(metaInfo)
83 func addTorrentDir(c *torrent.Client, _path string) {
84 torrentDir, err := os.Open(torrentPath)
85 defer torrentDir.Close()
89 names, err := torrentDir.Readdirnames(-1)
93 for _, name := range names {
94 go addTorrent(c, filepath.Join(_path, name))
98 func addTestPeer(client *torrent.Client) {
99 for _, t := range client.Torrents() {
100 if testPeerAddr != nil {
101 if err := client.AddPeers(t.InfoHash, []torrent.Peer{{
103 Port: testPeerAddr.Port,
113 if flag.NArg() != 0 {
114 os.Stderr.WriteString("one does not simply pass positional args\n")
118 os.Stderr.WriteString("y u no specify mountpoint?\n")
121 log.SetFlags(log.LstdFlags | log.Lshortfile)
122 if *pprofAddr != "" {
123 go http.ListenAndServe(*pprofAddr, nil)
125 conn, err := fuse.Mount(mountDir)
129 defer fuse.Unmount(mountDir)
130 // TODO: Think about the ramifications of exiting not due to a signal.
133 client := &torrent.Client{
134 DataDir: downloadDir,
135 DisableTrackers: *disableTrackers,
138 addTorrentDir(client, torrentPath)
139 resolveTestPeerAddr()
140 fs := torrentfs.New(client)
144 time.Sleep(10 * time.Second)
148 if err := fusefs.Serve(conn, fs); err != nil {
152 if err := conn.MountError; err != nil {