aboutsummaryrefslogtreecommitdiff
path: root/meta/torrent.go
diff options
context:
space:
mode:
Diffstat (limited to 'meta/torrent.go')
-rw-r--r--meta/torrent.go33
1 files changed, 30 insertions, 3 deletions
diff --git a/meta/torrent.go b/meta/torrent.go
index 140b992..63ac390 100644
--- a/meta/torrent.go
+++ b/meta/torrent.go
@@ -100,8 +100,11 @@ func (i Info) FullPath(n int) (string, error) {
// piece-to-file mapping:
// piece # -> [ { file #, off, len }, ... ]
-func (i Info) FileOffset(piece int) (int, int) {
- off := i.PieceLength * piece
+func (i Info) Offset(piece int) int {
+ return i.PieceLength * piece
+}
+
+func (i Info) FindFile(off int) (int, int) {
for n, l := range i.Files {
if l.Length > off {
return n, off
@@ -132,7 +135,31 @@ func (i Info) ReadAt(b []byte, off int64) (n int, err error) {
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]
+ p, err := i.FullPath(k)
+ if err != nil {
+ return 0, err
+ }
+ fd, err := os.OpenFile(p, os.O_RDWR|os.O_CREATE, 0644)
+ 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
+ }
+ }
}
return 0, ErrNotImplemented
}