Fix some stuff Rob Clifford broke through stress testing

This commit is contained in:
Matt Joiner 2015-02-06 14:54:59 +11:00
parent 8e95c1a0ac
commit 4ca6b4e2c7
4 changed files with 55 additions and 12 deletions

View File

@ -1505,7 +1505,12 @@ func (cl *Client) saveTorrentFile(t *torrent) error {
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[:])
}
@ -1517,6 +1522,17 @@ func (cl *Client) setMetaData(t *torrent, md metainfo.Info, bytes []byte) (err e
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 {
@ -1533,9 +1549,6 @@ func (cl *Client) setMetaData(t *torrent, md metainfo.Info, bytes []byte) (err e
}
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
}
@ -1619,6 +1632,10 @@ type Torrent struct {
*torrent
}
func (t Torrent) Drop() {
t.cl.dropTorrent(t.InfoHash)
}
type File struct {
t Torrent
path string
@ -1721,7 +1738,10 @@ func (cl *Client) AddMagnet(uri string) (T Torrent, err error) {
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()
@ -1766,6 +1786,10 @@ func (cl *Client) connectionPruner(t *torrent) {
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")

View File

@ -25,6 +25,21 @@ func TestClientDefault(t *testing.T) {
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()
}

View File

@ -262,6 +262,9 @@ func (m Msg) Nodes() (nodes []NodeInfo) {
}()
return m["r"].(map[string]interface{})["nodes"].(string)
}()
if len(b)%26 != 0 {
return
}
for i := 0; i < len(b); i += 26 {
var n NodeInfo
err := n.UnmarshalCompact([]byte(b[i : i+26]))

View File

@ -218,15 +218,21 @@ func (t *torrent) SetMetadataSize(bytes int64) {
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 {
@ -446,11 +452,6 @@ func (t *torrent) NumPiecesCompleted() (num int) {
}
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
}