2
0
mirror of synced 2025-02-24 06:38:14 +00:00

cmd/torrentfs: Switch to tagflag and rename -torrentPath->-metainfoDir

This commit is contained in:
Matt Joiner 2017-08-27 12:25:51 +10:00
parent 6268fa0b54
commit 4ffcd9f1ce
2 changed files with 34 additions and 46 deletions

View File

@ -53,10 +53,10 @@ Downloads torrents from the command-line. This first example does not use `godo`
### torrentfs ### torrentfs
torrentfs mounts a FUSE filesystem at `-mountDir`. The contents are the torrents described by the torrent files and magnet links at `-torrentPath`. Data for read requests is fetched only as required from the torrent network, and stored at `-downloadDir`. torrentfs mounts a FUSE filesystem at `-mountDir`. The contents are the torrents described by the torrent files and magnet links at `-metainfoDir`. Data for read requests is fetched only as required from the torrent network, and stored at `-downloadDir`.
$ mkdir mnt torrents $ mkdir mnt torrents
$ godo github.com/anacrolix/torrent/cmd/torrentfs -mountDir mnt -torrentPath torrents & $ godo github.com/anacrolix/torrent/cmd/torrentfs -mountDir=mnt -metainfoDir=torrents &
$ cd torrents $ cd torrents
$ wget http://releases.ubuntu.com/14.04.2/ubuntu-14.04.2-desktop-amd64.iso.torrent $ wget http://releases.ubuntu.com/14.04.2/ubuntu-14.04.2-desktop-amd64.iso.torrent
$ cd .. $ cd ..

View File

@ -2,7 +2,6 @@
package main package main
import ( import (
"flag"
"log" "log"
"net" "net"
"net/http" "net/http"
@ -18,6 +17,7 @@ import (
fusefs "bazil.org/fuse/fs" fusefs "bazil.org/fuse/fs"
"github.com/anacrolix/dht" "github.com/anacrolix/dht"
_ "github.com/anacrolix/envpprof" _ "github.com/anacrolix/envpprof"
"github.com/anacrolix/tagflag"
"github.com/anacrolix/torrent" "github.com/anacrolix/torrent"
"github.com/anacrolix/torrent/fs" "github.com/anacrolix/torrent/fs"
@ -25,42 +25,35 @@ import (
) )
var ( var (
torrentPath = flag.String("torrentPath", func() string { args = struct {
MetainfoDir string `help:"torrent files in this location describe the contents of the mounted filesystem"`
DownloadDir string `help:"location to save torrent data"`
MountDir string `help:"location the torrent contents are made available"`
DisableTrackers bool
TestPeer *net.TCPAddr
ReadaheadBytes tagflag.Bytes
ListenAddr *net.TCPAddr
}{
MetainfoDir: func() string {
_user, err := user.Current() _user, err := user.Current()
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
return filepath.Join(_user.HomeDir, ".config/transmission/torrents") return filepath.Join(_user.HomeDir, ".config/transmission/torrents")
}(), "torrent files in this location describe the contents of the mounted filesystem") }(),
downloadDir = flag.String("downloadDir", "", "location to save torrent data") ReadaheadBytes: 10 << 20,
mountDir = flag.String("mountDir", "", "location the torrent contents are made available") ListenAddr: &net.TCPAddr{},
}
disableTrackers = flag.Bool("disableTrackers", false, "disables trackers")
testPeer = flag.String("testPeer", "", "the address for a test peer")
readaheadBytes = flag.Int64("readaheadBytes", 10*1024*1024, "bytes to readahead in each torrent from the last read piece")
listenAddr = flag.String("listenAddr", ":6882", "incoming connection address")
testPeerAddr *net.TCPAddr
) )
func resolveTestPeerAddr() {
if *testPeer == "" {
return
}
var err error
testPeerAddr, err = net.ResolveTCPAddr("tcp4", *testPeer)
if err != nil {
log.Fatal(err)
}
}
func exitSignalHandlers(fs *torrentfs.TorrentFS) { func exitSignalHandlers(fs *torrentfs.TorrentFS) {
c := make(chan os.Signal) c := make(chan os.Signal)
signal.Notify(c, syscall.SIGINT, syscall.SIGTERM) signal.Notify(c, syscall.SIGINT, syscall.SIGTERM)
for { for {
<-c <-c
fs.Destroy() fs.Destroy()
err := fuse.Unmount(*mountDir) err := fuse.Unmount(args.MountDir)
if err != nil { if err != nil {
log.Print(err) log.Print(err)
} }
@ -70,8 +63,8 @@ func exitSignalHandlers(fs *torrentfs.TorrentFS) {
func addTestPeer(client *torrent.Client) { func addTestPeer(client *torrent.Client) {
for _, t := range client.Torrents() { for _, t := range client.Torrents() {
t.AddPeers([]torrent.Peer{{ t.AddPeers([]torrent.Peer{{
IP: testPeerAddr.IP, IP: args.TestPeer.IP,
Port: testPeerAddr.Port, Port: args.TestPeer.Port,
}}) }})
} }
} }
@ -81,27 +74,23 @@ func main() {
} }
func mainExitCode() int { func mainExitCode() int {
flag.Parse() tagflag.Parse(&args)
if flag.NArg() != 0 { if args.MountDir == "" {
os.Stderr.WriteString("one does not simply pass positional args\n")
return 2
}
if *mountDir == "" {
os.Stderr.WriteString("y u no specify mountpoint?\n") os.Stderr.WriteString("y u no specify mountpoint?\n")
return 2 return 2
} }
log.SetFlags(log.LstdFlags | log.Lshortfile) log.SetFlags(log.LstdFlags | log.Lshortfile)
conn, err := fuse.Mount(*mountDir) conn, err := fuse.Mount(args.MountDir)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
defer fuse.Unmount(*mountDir) defer fuse.Unmount(args.MountDir)
// TODO: Think about the ramifications of exiting not due to a signal. // TODO: Think about the ramifications of exiting not due to a signal.
defer conn.Close() defer conn.Close()
client, err := torrent.NewClient(&torrent.Config{ client, err := torrent.NewClient(&torrent.Config{
DataDir: *downloadDir, DataDir: args.DownloadDir,
DisableTrackers: *disableTrackers, DisableTrackers: args.DisableTrackers,
ListenAddr: *listenAddr, ListenAddr: args.ListenAddr.String(),
NoUpload: true, // Ensure that downloads are responsive. NoUpload: true, // Ensure that downloads are responsive.
DHTConfig: dht.ServerConfig{ DHTConfig: dht.ServerConfig{
StartingNodes: dht.GlobalBootstrapAddrs, StartingNodes: dht.GlobalBootstrapAddrs,
@ -115,7 +104,7 @@ func mainExitCode() int {
http.DefaultServeMux.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) { http.DefaultServeMux.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) {
client.WriteStatus(w) client.WriteStatus(w)
}) })
dw, err := dirwatch.New(*torrentPath) dw, err := dirwatch.New(args.MetainfoDir)
if err != nil { if err != nil {
log.Printf("error watching torrent dir: %s", err) log.Printf("error watching torrent dir: %s", err)
return 1 return 1
@ -144,11 +133,10 @@ func mainExitCode() int {
} }
} }
}() }()
resolveTestPeerAddr()
fs := torrentfs.New(client) fs := torrentfs.New(client)
go exitSignalHandlers(fs) go exitSignalHandlers(fs)
if testPeerAddr != nil { if args.TestPeer != nil {
go func() { go func() {
for { for {
addTestPeer(client) addTestPeer(client)