From b5812bb0b7efdef385efa1aae6a065420b0aca71 Mon Sep 17 00:00:00 2001 From: Matt Joiner Date: Thu, 5 May 2016 23:05:28 +1000 Subject: [PATCH] Remove the last of the "config dir" stuff This just conflates the Client. It should be done orthogonally. --- client.go | 91 +------------------------------------------- client_test.go | 41 ++------------------ config.go | 7 ---- fs/torrentfs_test.go | 3 -- 4 files changed, 6 insertions(+), 136 deletions(-) diff --git a/client.go b/client.go index 8bccd78f..20371325 100644 --- a/client.go +++ b/client.go @@ -13,8 +13,6 @@ import ( mathRand "math/rand" "net" "net/url" - "os" - "path/filepath" "sort" "strconv" "strings" @@ -180,19 +178,6 @@ func (cl *Client) WriteStatus(_w io.Writer) { } } -func (cl *Client) configDir() string { - if cl.config.ConfigDir == "" { - return filepath.Join(os.Getenv("HOME"), ".config/torrent") - } - return cl.config.ConfigDir -} - -// The directory where the Client expects to find and store configuration -// data. Defaults to $HOME/.config/torrent. -func (cl *Client) ConfigDir() string { - return cl.configDir() -} - // Creates a new client. func NewClient(cfg *Config) (cl *Client, err error) { if cfg == nil { @@ -1442,45 +1427,11 @@ func (cl *Client) addPeers(t *Torrent, peers []Peer) { } } -func (cl *Client) cachedMetaInfoFilename(ih metainfo.Hash) string { - return filepath.Join(cl.configDir(), "torrents", ih.HexString()+".torrent") -} - -func (cl *Client) saveTorrentFile(t *Torrent) error { - path := cl.cachedMetaInfoFilename(t.infoHash) - os.MkdirAll(filepath.Dir(path), 0777) - f, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0666) - if err != nil { - return fmt.Errorf("error opening file: %s", err) - } - defer f.Close() - e := bencode.NewEncoder(f) - err = e.Encode(t.metainfo()) - if err != nil { - return fmt.Errorf("error marshalling metainfo: %s", err) - } - mi, err := cl.torrentCacheMetaInfo(t.infoHash) - if err != nil { - // For example, a script kiddy makes us load too many files, and we're - // able to save the torrent, but not load it again to check it. - return nil - } - if mi.Info.Hash() != t.infoHash { - log.Fatalf("%x != %x", mi.Info.Hash, t.infoHash[:]) - } - return nil -} - func (cl *Client) setMetaData(t *Torrent, md *metainfo.Info, bytes []byte) (err error) { err = t.setMetadata(md, bytes) if err != nil { return } - if !cl.config.DisableMetainfoCache { - if err := cl.saveTorrentFile(t); err != nil { - log.Printf("error saving torrent file for %s: %s", t, err) - } - } cl.event.Broadcast() close(t.gotMetainfo) return @@ -1551,32 +1502,6 @@ type Handle interface { io.ReaderAt } -// Returns nil metainfo if it isn't in the cache. Checks that the retrieved -// metainfo has the correct infohash. -func (cl *Client) torrentCacheMetaInfo(ih metainfo.Hash) (mi *metainfo.MetaInfo, err error) { - if cl.config.DisableMetainfoCache { - return - } - f, err := os.Open(cl.cachedMetaInfoFilename(ih)) - if err != nil { - if os.IsNotExist(err) { - err = nil - } - return - } - defer f.Close() - dec := bencode.NewDecoder(f) - err = dec.Decode(&mi) - if err != nil { - return - } - if mi.Info.Hash() != ih { - err = fmt.Errorf("cached torrent has wrong infohash: %x != %x", mi.Info.Hash, ih[:]) - return - } - return -} - // Specifies a new torrent for adding to a client. There are helpers for // magnet URIs and torrent metainfo files. type TorrentSpec struct { @@ -1655,20 +1580,8 @@ func (cl *Client) AddTorrentSpec(spec *TorrentSpec) (t *Torrent, new bool, err e } // Try to merge in info we have on the torrent. Any err left will // terminate the function. - if t.info == nil { - if spec.Info != nil { - err = cl.setMetaData(t, &spec.Info.Info, spec.Info.Bytes) - } else { - var mi *metainfo.MetaInfo - mi, err = cl.torrentCacheMetaInfo(spec.InfoHash) - if err != nil { - log.Printf("error getting cached metainfo: %s", err) - err = nil - } else if mi != nil { - t.addTrackers(mi.AnnounceList) - err = cl.setMetaData(t, &mi.Info.Info, mi.Info.Bytes) - } - } + if t.info == nil && spec.Info != nil { + err = cl.setMetaData(t, &spec.Info.Info, spec.Info.Bytes) } if err != nil { return diff --git a/client_test.go b/client_test.go index 59c79f2e..f85f42c9 100644 --- a/client_test.go +++ b/client_test.go @@ -10,7 +10,6 @@ import ( "math/rand" "net" "os" - "path/filepath" "strings" "sync" "testing" @@ -37,11 +36,10 @@ func init() { } var TestingConfig = Config{ - ListenAddr: "localhost:0", - NoDHT: true, - DisableTrackers: true, - DisableMetainfoCache: true, - DataDir: "/dev/null", + ListenAddr: "localhost:0", + NoDHT: true, + DisableTrackers: true, + DataDir: "/dev/null", DHTConfig: dht.ServerConfig{ NoDefaultBootstrap: true, }, @@ -646,37 +644,6 @@ func TestAddTorrentSpecMerging(t *testing.T) { require.NotNil(t, tt.Info()) } -// Check that torrent Info is obtained from the metainfo file cache. -func TestAddTorrentMetainfoInCache(t *testing.T) { - cfg := TestingConfig - cfg.DisableMetainfoCache = false - cfg.ConfigDir, _ = ioutil.TempDir(os.TempDir(), "") - defer os.RemoveAll(cfg.ConfigDir) - cl, err := NewClient(&cfg) - require.NoError(t, err) - defer cl.Close() - dir, mi := testutil.GreetingTestTorrent() - defer os.RemoveAll(dir) - tt, new, err := cl.AddTorrentSpec(TorrentSpecFromMetaInfo(mi)) - require.NoError(t, err) - require.True(t, new) - require.NotNil(t, tt.Info()) - _, err = os.Stat(filepath.Join(cfg.ConfigDir, "torrents", fmt.Sprintf("%x.torrent", mi.Info.Hash()))) - require.NoError(t, err) - _, ok := cl.Torrent(mi.Info.Hash()) - require.True(t, ok) - tt.Drop() - _, ok = cl.Torrent(mi.Info.Hash()) - require.False(t, ok) - tt, new, err = cl.AddTorrentSpec(&TorrentSpec{ - InfoHash: mi.Info.Hash(), - }) - require.NoError(t, err) - require.True(t, new) - // Obtained from the metainfo cache. - require.NotNil(t, tt.Info()) -} - func TestTorrentDroppedBeforeGotInfo(t *testing.T) { dir, mi := testutil.GreetingTestTorrent() os.RemoveAll(dir) diff --git a/config.go b/config.go index 1f22df03..1a9ab8be 100644 --- a/config.go +++ b/config.go @@ -33,13 +33,6 @@ type Config struct { DisableUTP bool // For the bittorrent protocol. DisableTCP bool `long:"disable-tcp"` - // 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. - ConfigDir string - // Don't save or load to a cache of torrent files stored in - // "$ConfigDir/torrents". - DisableMetainfoCache bool // Called to instantiate storage for each added torrent. Provided backends // are in $REPO/data. If not set, the "file" implementation is used. DefaultStorage storage.I diff --git a/fs/torrentfs_test.go b/fs/torrentfs_test.go index 96c8bf2e..1799570d 100644 --- a/fs/torrentfs_test.go +++ b/fs/torrentfs_test.go @@ -166,9 +166,6 @@ func TestDownloadOnDemand(t *testing.T) { NoDHT: true, ListenAddr: "localhost:0", Seed: true, - // Ensure that the metainfo is obtained over the wire, since we added - // the torrent to the seeder by magnet. - DisableMetainfoCache: true, }) require.NoError(t, err) defer seeder.Close()