From 13b1d4b9a06147cfd1140d42dc6b9ed2ba7a8a90 Mon Sep 17 00:00:00 2001 From: Dimitri Sokolyuk Date: Sat, 9 Jul 2016 15:17:37 +0200 Subject: int64 --- bencode/bencode.go | 4 ++-- bencode/bencode_test.go | 14 ++++++++++- meta/file.go | 2 +- meta/info.go | 63 +++++++++++++++++++------------------------------ meta/piece.go | 4 ++-- 5 files changed, 42 insertions(+), 45 deletions(-) diff --git a/bencode/bencode.go b/bencode/bencode.go index 14d8953..934e1b9 100644 --- a/bencode/bencode.go +++ b/bencode/bencode.go @@ -33,7 +33,7 @@ func marshalField(out io.Writer, v reflect.Value) error { switch v.Kind() { case reflect.String: marshalString(out, v.String()) - case reflect.Int: + case reflect.Int, reflect.Int64: marshalInt(out, v.Int()) case reflect.Slice: marshalList(out, v) @@ -51,7 +51,7 @@ func marshalField(out io.Writer, v reflect.Value) error { func isZero(v reflect.Value) bool { switch v.Kind() { - case reflect.Int: + case reflect.Int, reflect.Int64: return v.Int() == 0 case reflect.String, reflect.Slice: return v.Len() == 0 diff --git a/bencode/bencode_test.go b/bencode/bencode_test.go index 5965afa..329baad 100644 --- a/bencode/bencode_test.go +++ b/bencode/bencode_test.go @@ -101,7 +101,7 @@ func TestUnmarshalPartial(t *testing.T) { } */ -func TestBoolUnmarshal(t *testing.T) { +func TestUnmarshalBool(t *testing.T) { var b struct{ A bool } in := `d1:Ai1ee` err := Unmarshal([]byte(in), &b) @@ -112,3 +112,15 @@ func TestBoolUnmarshal(t *testing.T) { t.Error("expected true, got", b.A) } } + +func TestUnmarshalInt64(t *testing.T) { + var b struct{ A int64 } + in := `d1:Ai42ee` + err := Unmarshal([]byte(in), &b) + if err != nil { + t.Error(err) + } + if b.A != 42 { + t.Error("expected true, got", b.A) + } +} diff --git a/meta/file.go b/meta/file.go index 8a5a382..3560197 100644 --- a/meta/file.go +++ b/meta/file.go @@ -6,7 +6,7 @@ import ( ) type File struct { - Length int `bencode:"length"` + Length int64 `bencode:"length"` MD5Sum []byte `bencode:"md5sum,optional"` // never seen in wildlife Path []string `bencode:"path"` } diff --git a/meta/info.go b/meta/info.go index b99fdd7..c498f9e 100644 --- a/meta/info.go +++ b/meta/info.go @@ -12,10 +12,10 @@ var ErrNotImplemented = errors.New("not implemented") type Info struct { Files []File `bencode:"files"` - Length int `bencode:"length"` + Length int64 `bencode:"length"` MD5Sum []byte `bencode:"md5sum,optional"` // never seen in wildlife Name string `bencode:"name"` - PieceLength int `bencode:"piece length"` + PieceLength int64 `bencode:"piece length"` Pieces []byte `bencode:"pieces"` // compact mode Private bool `bencode:"private"` // BEP-0027 RootHash []byte `bencode:"root hash"` // BEP-0030 @@ -23,16 +23,16 @@ type Info struct { // Full returns count of full pieces func (i Info) Full() int { - return i.TotalLength() / i.PieceLength + return int(i.TotalLength() / i.PieceLength) } // Last returns size of last truncated piece -func (i Info) Last() int { +func (i Info) Last() int64 { return i.TotalLength() % i.PieceLength } // TotalLength calculates and retuns the length of all files -func (i Info) TotalLength() int { +func (i Info) TotalLength() int64 { if i.Length == 0 { for _, f := range i.Files { i.Length += f.Length @@ -54,7 +54,7 @@ func (i Info) GetPieces() []Piece { } off := k * sha1.Size copy(p[k].Sum[:], i.Pieces[off:off+sha1.Size]) - p[k].Offset = k * i.PieceLength + p[k].Offset = int64(k) * i.PieceLength } return p } @@ -78,14 +78,14 @@ func (i Info) Open(n int) (*os.File, error) { } // Offset of piece in gross buffer -func (i Info) Offset(piece int) int { - return i.PieceLength * piece +func (i Info) Offset(piece int) int64 { + return i.PieceLength * int64(piece) } -// FindFile looks up first file # corresponding to offset in gross buffer -// If piece is bigger the next file(s) with offset 0 shall be used to -// fit a piece into -func (i Info) FindFile(off int) (int, int) { +// FindFile looks up first file # corresponding to offset into it +// If piece is bigger then file, next file(s) with offset 0 shall be used +// to fit a piece into +func (i Info) FindFile(off int64) (int, int64) { for n, l := range i.Files { if l.Length > off { return n, off @@ -114,37 +114,22 @@ func (i Info) WriteAt(b []byte, off int64) (n int, err error) { // FIXME ReadAt ... func (i Info) ReadAt(b []byte, off int64) (n int, err error) { - if len(i.Files) == 0 { - fd, err := i.Open(0) + n, offset := i.FindFile(off) + var done int + for k := n; done < len(b); k++ { + fd, err := i.Open(k) if err != nil { - return 0, err + return done, err } - defer fd.Close() - return fd.ReadAt(b, int64(i.PieceLength)*off) - } else { - n, off := i.FindFile(int(off)) - start := off - end := i.PieceLength - for k := n; k < len(i.Files); k++ { - f := i.Files[n] - fd, err := i.Open(k) - if err != nil { - return 0, err - } - defer fd.Close() - if end > f.Length { - end = f.Length - } - fd.ReadAt(b[start:end], int64(off)) - start = end - end = i.PieceLength - off += f.Length - if start == end { - return cap(b), nil - } + nn, err := fd.ReadAt(b[done:], offset) + if err != nil { + return done, err } + done += nn + fd.Close() + off = 0 } - return 0, ErrNotImplemented + return done, nil } func (i Info) String() string { diff --git a/meta/piece.go b/meta/piece.go index 35588a6..f06c371 100644 --- a/meta/piece.go +++ b/meta/piece.go @@ -3,8 +3,8 @@ package meta import "crypto/sha1" type Piece struct { - Offset int - Length int + Offset int64 + Length int64 Sum [sha1.Size]byte Ok bool } -- cgit v1.2.3