From 7ac138c434e5d916bad4ff1f9fa03c9f2f74a897 Mon Sep 17 00:00:00 2001 From: Dimitri Sokolyuk Date: Sun, 3 Jul 2016 16:42:40 +0200 Subject: Less magic --- meta/torrent.go | 34 ++++++++++------------------------ meta/torrent_test.go | 11 ++++++----- 2 files changed, 16 insertions(+), 29 deletions(-) (limited to 'meta') diff --git a/meta/torrent.go b/meta/torrent.go index 886668f..002480c 100644 --- a/meta/torrent.go +++ b/meta/torrent.go @@ -5,12 +5,10 @@ import ( "crypto/sha1" "errors" "fmt" - "io/ioutil" + "log" "os" "path" "time" - - "dim13.org/btget/bencode" ) var ErrNotImplemented = errors.New("not implemented") @@ -31,32 +29,19 @@ type Info struct { Private bool `bencode:"private"` } -func New(fname string) (Torrent, error) { - var tor Torrent - body, err := ioutil.ReadFile(fname) - if err != nil { - return Torrent{}, err - } - err = bencode.Unmarshal(body, &tor) - if err != nil { - return Torrent{}, err - } - return tor, nil -} - func (i Info) NPieces() int { - return len(i.Pieces) / 20 + return len(i.Pieces) / sha1.Size } func (i Info) Check(b []byte, n int) bool { sum := sha1.Sum(b) - off := n * 20 - return bytes.Equal(i.Pieces[off:off+20], sum[:]) + off := n * sha1.Size + return bytes.Equal(i.Pieces[off:off+sha1.Size], sum[:]) } func (i Info) CheckSum(n int) []byte { - off := n * 20 - return i.Pieces[off : off+20] + off := n * sha1.Size + return i.Pieces[off : off+sha1.Size] } func (i Info) CheckSums() [][]byte { @@ -98,16 +83,17 @@ func (i Info) Offset(piece int) int { return i.PieceLength * piece } -func (i Info) FileOffset(piece int) (int, int, bool) { +func (i Info) FileOffset(piece int) (int, int) { off := i.PieceLength * piece for n, l := range i.Files { if l.Length < off { off -= l.Length } else { - return n, off, off+i.PieceLength > i.Length + log.Println(n, off, i.PieceLength-off) + return n, off } } - return 0, off, false + return 0, off } /* diff --git a/meta/torrent_test.go b/meta/torrent_test.go index 4965b87..5f304d5 100644 --- a/meta/torrent_test.go +++ b/meta/torrent_test.go @@ -1,20 +1,21 @@ package meta -import "testing" - var tors = map[string]string{ "bsd": "OpenBSD_5.9_amd64_install59.iso-2016-03-29-0449.torrent", "ogg": "OpenBSD_songs_ogg-2016-03-25-0127.torrent", "deb": "debian-8.5.0-amd64-netinst.iso.torrent", + "mul": "multi.torrent", } +/* func TestPieces(t *testing.T) { - tor, err := New("../examples/" + tors["ogg"]) + tor, err := Open("../examples/" + tors["mul"]) if err != nil { t.Error(err) } for i := 0; i < tor.Info.NPieces(); i++ { - n, off, span := tor.Info.FileOffset(i) - t.Log(n, off, span) + n, off := tor.Info.FileOffset(i) + t.Log(n, off) } } +*/ -- cgit v1.2.3