4 "bitbucket.org/anacrolix/go.torrent/util"
5 "bitbucket.org/anacrolix/go.torrent/util/dirwatch"
19 fusefs "bazil.org/fuse/fs"
20 "bitbucket.org/anacrolix/go.torrent"
21 "bitbucket.org/anacrolix/go.torrent/fs"
28 disableTrackers = flag.Bool("disableTrackers", false, "disables trackers")
29 testPeer = flag.String("testPeer", "", "the address for a test peer")
30 httpAddr = flag.String("httpAddr", "localhost:0", "HTTP server bind address")
31 readaheadBytes = flag.Int("readaheadBytes", 10*1024*1024, "bytes to readahead in each torrent from the last read piece")
32 testPeerAddr *net.TCPAddr
36 flag.StringVar(&downloadDir, "downloadDir", "", "location to save torrent data")
37 flag.StringVar(&torrentPath, "torrentPath", func() string {
38 _user, err := user.Current()
42 return filepath.Join(_user.HomeDir, ".config/transmission/torrents")
43 }(), "torrent files in this location describe the contents of the mounted filesystem")
44 flag.StringVar(&mountDir, "mountDir", "", "location the torrent contents are made available")
47 func resolveTestPeerAddr() {
52 testPeerAddr, err = net.ResolveTCPAddr("tcp4", *testPeer)
58 func setSignalHandlers() {
59 c := make(chan os.Signal)
60 signal.Notify(c, syscall.SIGINT, syscall.SIGTERM)
64 err := fuse.Unmount(mountDir)
72 func addTestPeer(client *torrent.Client) {
73 for _, t := range client.Torrents() {
74 if testPeerAddr != nil {
75 if err := client.AddPeers(t.InfoHash, []torrent.Peer{{
77 Port: testPeerAddr.Port,
88 os.Stderr.WriteString("one does not simply pass positional args\n")
92 os.Stderr.WriteString("y u no specify mountpoint?\n")
95 log.SetFlags(log.LstdFlags | log.Lshortfile)
97 util.LoggedHTTPServe(*httpAddr)
99 conn, err := fuse.Mount(mountDir)
103 defer fuse.Unmount(mountDir)
104 // TODO: Think about the ramifications of exiting not due to a signal.
107 client := &torrent.Client{
108 DataDir: downloadDir,
109 DisableTrackers: *disableTrackers,
110 DownloadStrategy: &torrent.ResponsiveDownloadStrategy{*readaheadBytes},
112 http.DefaultServeMux.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) {
113 client.WriteStatus(w)
116 dw, err := dirwatch.New(torrentPath)
121 for ev := range dw.Events {
124 if ev.TorrentFilePath != "" {
125 err := client.AddTorrentFromFile(ev.TorrentFilePath)
127 log.Printf("error adding torrent to client: %s", err)
129 } else if ev.Magnet != "" {
130 err := client.AddMagnet(ev.Magnet)
132 log.Printf("error adding magnet: %s", err)
135 case dirwatch.Removed:
136 err := client.DropTorrent(ev.InfoHash)
138 log.Printf("error dropping torrent: %s", err)
143 resolveTestPeerAddr()
144 fs := torrentfs.New(client)
148 time.Sleep(10 * time.Second)
152 if err := fusefs.Serve(conn, fs); err != nil {
156 if err := conn.MountError; err != nil {