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

Remove some of the magic ConfigDir stuff

This might become a helper. Torrent file cache still remains.
This commit is contained in:
Matt Joiner 2016-04-04 14:04:48 +10:00
parent fa511154e4
commit 8f164ae956
6 changed files with 1 additions and 109 deletions

View File

@ -28,7 +28,6 @@ import (
"github.com/anacrolix/missinggo/pubsub"
"github.com/anacrolix/sync"
"github.com/anacrolix/utp"
"github.com/edsrzf/mmap-go"
"github.com/anacrolix/torrent/bencode"
"github.com/anacrolix/torrent/dht"
@ -284,85 +283,6 @@ func (cl *Client) ConfigDir() string {
return cl.configDir()
}
func loadPackedBlocklist(filename string) (ret iplist.Ranger, err error) {
f, err := os.Open(filename)
if os.IsNotExist(err) {
err = nil
return
}
if err != nil {
return
}
defer f.Close()
mm, err := mmap.Map(f, mmap.RDONLY, 0)
if err != nil {
return
}
ret = iplist.NewFromPacked(mm)
return
}
func (cl *Client) setEnvBlocklist() (err error) {
filename := os.Getenv("TORRENT_BLOCKLIST_FILE")
defaultBlocklist := filename == ""
if defaultBlocklist {
cl.ipBlockList, err = loadPackedBlocklist(filepath.Join(cl.configDir(), "packed-blocklist"))
if err != nil {
return
}
if cl.ipBlockList != nil {
return
}
filename = filepath.Join(cl.configDir(), "blocklist")
}
f, err := os.Open(filename)
if err != nil {
if defaultBlocklist {
err = nil
}
return
}
defer f.Close()
cl.ipBlockList, err = iplist.NewFromReader(f)
return
}
func (cl *Client) initBannedTorrents() error {
f, err := os.Open(filepath.Join(cl.configDir(), "banned_infohashes"))
if err != nil {
if os.IsNotExist(err) {
return nil
}
return fmt.Errorf("error opening banned infohashes file: %s", err)
}
defer f.Close()
scanner := bufio.NewScanner(f)
cl.bannedTorrents = make(map[metainfo.Hash]struct{})
for scanner.Scan() {
if strings.HasPrefix(strings.TrimSpace(scanner.Text()), "#") {
continue
}
var ihs string
n, err := fmt.Sscanf(scanner.Text(), "%x", &ihs)
if err != nil {
return fmt.Errorf("error reading infohash: %s", err)
}
if n != 1 {
continue
}
if len(ihs) != 20 {
return errors.New("bad infohash")
}
var ih metainfo.Hash
missinggo.CopyExact(&ih, ihs)
cl.bannedTorrents[ih] = struct{}{}
}
if err := scanner.Err(); err != nil {
return fmt.Errorf("error scanning file: %s", err)
}
return nil
}
// Creates a new client.
func NewClient(cfg *Config) (cl *Client, err error) {
if cfg == nil {
@ -388,16 +308,6 @@ func NewClient(cfg *Config) (cl *Client, err error) {
}
if cfg.IPBlocklist != nil {
cl.ipBlockList = cfg.IPBlocklist
} else if !cfg.NoDefaultBlocklist {
err = cl.setEnvBlocklist()
if err != nil {
return
}
}
if err = cl.initBannedTorrents(); err != nil {
err = fmt.Errorf("error initing banned torrents: %s", err)
return
}
if cfg.PeerID != "" {

View File

@ -40,7 +40,6 @@ var TestingConfig = Config{
ListenAddr: "localhost:0",
NoDHT: true,
DisableTrackers: true,
NoDefaultBlocklist: true,
DisableMetainfoCache: true,
DataDir: "/dev/null",
DHTConfig: dht.ServerConfig{

View File

@ -33,8 +33,6 @@ type Config struct {
DisableUTP bool
// For the bittorrent protocol.
DisableTCP bool `long:"disable-tcp"`
// Don't automatically load "$ConfigDir/blocklist".
NoDefaultBlocklist bool
// Defaults to "$HOME/.config/torrent". This is where "blocklist",
// "torrents" and other operational files are stored. TODO: Dump this
// stuff, this is specific to the default cmd/torrent client only.

5
doc.go
View File

@ -18,11 +18,6 @@ ConfigDir
A Client has a configurable ConfigDir that defaults to $HOME/.config/torrent.
Torrent metainfo files are cached at $CONFIGDIR/torrents/$infohash.torrent.
Infohashes in $CONFIGDIR/banned_infohashes cannot be added to the Client. A
P2P Plaintext Format blocklist is loaded from a file at the location specified
by the environment variable TORRENT_BLOCKLIST_FILE if set. otherwise from
$CONFIGDIR/blocklist. If $CONFIGDIR/packed-blocklist exists, this is memory-
mapped as a packed IP blocklist, saving considerable memory.
*/
package torrent

View File

@ -98,8 +98,6 @@ func TestUnmountWedged(t *testing.T) {
ListenAddr: "redonk",
DisableTCP: true,
DisableUTP: true,
NoDefaultBlocklist: true,
})
require.NoError(t, err)
defer client.Close()
@ -171,8 +169,6 @@ func TestDownloadOnDemand(t *testing.T) {
NoDHT: true,
ListenAddr: "localhost:0",
Seed: true,
NoDefaultBlocklist: true,
// Ensure that the metainfo is obtained over the wire, since we added
// the torrent to the seeder by magnet.
DisableMetainfoCache: true,
@ -188,14 +184,9 @@ func TestDownloadOnDemand(t *testing.T) {
NoDHT: true,
ListenAddr: "localhost:0",
DisableTCP: true,
NoDefaultBlocklist: true,
DefaultStorage: storage.NewMMap(filepath.Join(layout.BaseDir, "download")),
// This can be used to check if clients can connect to other clients
// with the same ID.
// PeerID: seeder.PeerID(),
})
leecher.SetIPBlockList(nil)

View File

@ -35,7 +35,6 @@ func issue35TestingConfig() *Config {
ListenAddr: "localhost:0",
NoDHT: false,
DisableTrackers: true,
NoDefaultBlocklist: true,
DisableUTP: false,
DisableMetainfoCache: true,
DisableIPv6: true,