aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDimitri Sokolyuk <demon@dim13.org>2016-07-06 03:16:54 +0200
committerDimitri Sokolyuk <demon@dim13.org>2016-07-06 03:16:54 +0200
commite7daf8c15a1b0e3afa06fc8ccc5f1fc9d4c8cbaa (patch)
treeb3024820f8191610f622efa6eda75c4e77f86a8a
parent910a5ae2c7ac25184afed9334f271581deb31fd1 (diff)
wip
-rw-r--r--meta/piece.go17
-rw-r--r--meta/torrent.go35
-rw-r--r--meta/torrent_test.go7
3 files changed, 24 insertions, 35 deletions
diff --git a/meta/piece.go b/meta/piece.go
new file mode 100644
index 0000000..58b87af
--- /dev/null
+++ b/meta/piece.go
@@ -0,0 +1,17 @@
+package meta
+
+import "crypto/sha1"
+
+type Piece struct {
+ Offset int
+ Length int
+ Sum [sha1.Size]byte
+}
+
+func (p Piece) Check(b []byte) bool {
+ return sha1.Sum(b) == p.Sum
+}
+
+func (p Piece) Size() int {
+ return p.Length
+}
diff --git a/meta/torrent.go b/meta/torrent.go
index 7ab4bec..20496be 100644
--- a/meta/torrent.go
+++ b/meta/torrent.go
@@ -1,7 +1,6 @@
package meta
import (
- "bytes"
"crypto/sha1"
"errors"
"fmt"
@@ -22,11 +21,6 @@ func (f File) Name() string {
return path.Join(f.Path...)
}
-type Piece struct {
- Length int
- CheckSum [sha1.Size]byte
-}
-
type Info struct {
Files []File `bencode:"files"`
Length int `bencode:"length"`
@@ -62,34 +56,12 @@ func (i Info) GetPieces() []Piece {
p[k].Length = length
}
off := k * sha1.Size
- copy(p[k].CheckSum[:], i.Pieces[off:off+sha1.Size])
+ copy(p[k].Sum[:], i.Pieces[off:off+sha1.Size])
+ p[k].Offset = k * i.PieceLength
}
return p
}
-func (i Info) NPieces() int {
- return len(i.Pieces) / sha1.Size
-}
-
-func (i Info) Check(b []byte, n int) bool {
- sum := sha1.Sum(b)
- off := n * sha1.Size
- return bytes.Equal(i.Pieces[off:off+sha1.Size], sum[:])
-}
-
-func (i Info) CheckSum(n int) []byte {
- off := n * sha1.Size
- return i.Pieces[off : off+sha1.Size]
-}
-
-func (i Info) CheckSums() [][]byte {
- cs := make([][]byte, i.NPieces())
- for n := 0; n < i.NPieces(); n++ {
- cs[n] = i.CheckSum(n)
- }
- return cs
-}
-
func (i Info) FullPath(n int) (string, error) {
if i.isSingleFile() {
return i.Name, nil
@@ -121,11 +93,12 @@ func (i Info) FindFile(off int) (int, int) {
* 0 1 2 3 4 5 6 7 #file
*/
+// FIXME WriteAt ...
func (i Info) WriteAt(b []byte, off int64) (n int, err error) {
return 0, ErrNotImplemented
}
-// ReadAt ...
+// FIXME ReadAt ...
func (i Info) ReadAt(b []byte, off int64) (n int, err error) {
if i.isSingleFile() {
fd, err := os.OpenFile(i.Name, os.O_RDWR|os.O_CREATE, 0644)
diff --git a/meta/torrent_test.go b/meta/torrent_test.go
index c3e960c..d393e45 100644
--- a/meta/torrent_test.go
+++ b/meta/torrent_test.go
@@ -32,10 +32,9 @@ func TestPieces(t *testing.T) {
if err != nil {
t.Error(err)
}
- for i := 0; i < tor.Info.NPieces(); i++ {
- off := i * tor.Info.PieceLength
- n, foff := tor.Info.FindFile(off)
- t.Log(off, n, foff)
+ for i, p := range tor.Info.GetPieces() {
+ n, off := tor.Info.FindFile(p.Offset)
+ t.Log(i, n, off)
}
}