if err != nil {
return fmt.Errorf("error marshalling metainfo: %s", err)
}
- mi, _ := cl.torrentCacheMetaInfo(t.InfoHash)
+ 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 !bytes.Equal(mi.Info.Hash, t.InfoHash[:]) {
log.Fatalf("%x != %x", mi.Info.Hash, t.InfoHash[:])
}
if err != nil {
return
}
+
+ if err := cl.saveTorrentFile(t); err != nil {
+ log.Printf("error saving torrent file for %s: %s", t, err)
+ }
+
+ if strings.Contains(strings.ToLower(md.Name), "porn") {
+ cl.dropTorrent(t.InfoHash)
+ err = errors.New("no porn plx")
+ return
+ }
+
// If the client intends to upload, it needs to know what state pieces are
// in.
if !cl.noUpload {
}
cl.downloadStrategy.TorrentStarted(t)
- if err := cl.saveTorrentFile(t); err != nil {
- log.Printf("error saving torrent file for %s: %s", t, err)
- }
close(t.gotMetainfo)
return
}
*torrent
}
+func (t Torrent) Drop() {
+ t.cl.dropTorrent(t.InfoHash)
+}
+
type File struct {
t Torrent
path string
if err != nil {
log.Printf("error getting cached metainfo for %x: %s", m.InfoHash[:], err)
} else if mi != nil {
- cl.AddTorrent(mi)
+ _, err = cl.AddTorrent(mi)
+ if err != nil {
+ return
+ }
}
cl.mu.Lock()
defer cl.mu.Unlock()
func (me *Client) DropTorrent(infoHash InfoHash) (err error) {
me.mu.Lock()
defer me.mu.Unlock()
+ return me.dropTorrent(infoHash)
+}
+
+func (me *Client) dropTorrent(infoHash InfoHash) (err error) {
t, ok := me.torrents[infoHash]
if !ok {
err = fmt.Errorf("no such torrent")
cl.Stop()
}
+func TestAddDropTorrent(t *testing.T) {
+ cl, err := NewClient(nil)
+ if err != nil {
+ t.Fatal(err)
+ }
+ defer cl.Stop()
+ dir, mi := testutil.GreetingTestTorrent()
+ defer os.RemoveAll(dir)
+ tt, err := cl.AddTorrent(mi)
+ if err != nil {
+ t.Fatal(err)
+ }
+ tt.Drop()
+}
+
func TestAddTorrentNoSupportedTrackerSchemes(t *testing.T) {
t.SkipNow()
}
if t.MetaData != nil {
return
}
+ if bytes > 10000000 { // 10MB, pulled from my ass.
+ return
+ }
t.MetaData = make([]byte, bytes)
t.metadataHave = make([]bool, (bytes+(1<<14)-1)/(1<<14))
}
func (t *torrent) Name() string {
- if !t.haveInfo() {
+ if t.haveInfo() {
+ return t.Info.Name
+ }
+ if t.DisplayName != "" {
return t.DisplayName
}
- return t.Info.Name
+ return t.InfoHash.HexString()
}
func (t *torrent) pieceStatusChar(index int) byte {
}
func (t *torrent) Length() int64 {
- if t.Data == nil {
- // Possibly the length might be available before the data is mmapped,
- // I defer this decision to such a need arising.
- panic("torrent length not known?")
- }
return t.length
}