aboutsummaryrefslogtreecommitdiff
path: root/meta/info.go
blob: 57aa9374e3b6f69cb96ded6dc0fcf76139ac428c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package meta

import (
	"crypto/sha1"
	"errors"
	"fmt"
	"os"
	"path"
)

var ErrNotImplemented = errors.New("not implemented")

type Info struct {
	Files       []File `bencode:"files"`
	Length      int    `bencode:"length"`
	MD5Sum      []byte `bencode:"md5sum,optional"` // never seen in wildlife
	Name        string `bencode:"name"`
	PieceLength int    `bencode:"piece length"`
	Pieces      []byte `bencode:"pieces"`    // compact mode
	Private     bool   `bencode:"private"`   // BEP-0027
	RootHash    []byte `bencode:"root hash"` // BEP-0030
}

// Full returns count of full pieces
func (i Info) Full() int {
	return i.TotalLength() / i.PieceLength
}

// Last returns size of last truncated piece
func (i Info) Last() int {
	return i.TotalLength() % i.PieceLength
}

// TotalLength calculates and retuns the length of all files
func (i Info) TotalLength() int {
	if i.Length == 0 {
		for _, f := range i.Files {
			i.Length += f.Length
		}
	}
	return i.Length
}

func (i Info) GetPieces() []Piece {
	n, last := i.Full(), i.Last()
	if last > 0 {
		n++
	}
	p := make([]Piece, n)
	for k := 0; k < n; k++ {
		p[k].Length = i.PieceLength
		if k+1 == n {
			p[k].Length = last
		}
		off := k * sha1.Size
		copy(p[k].Sum[:], i.Pieces[off:off+sha1.Size])
		p[k].Offset = k * i.PieceLength
	}
	return p
}

// Open N-th file and allocate required path
func (i Info) Open(n int) (*os.File, error) {
	var p string
	if len(i.Files) == 0 {
		p = i.Name
	} else {
		p = path.Join(i.Name, i.Files[n].Name())
		if err := os.MkdirAll(p, 0755); err != nil {
			return nil, err
		}
	}
	return os.OpenFile(i.Name, os.O_RDWR|os.O_CREATE, 0644)
}

// Offset of piece in gross buffer
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
		}
		off -= l.Length
	}
	return 0, off
}

/*
 *  0    1    2    3    4    5    6    7   #piece
 * |----|----|----|----|----|----|----|--| pieces
 * |-|----|--|----|----------|-|----|----| files
 *  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
}

// FIXME ReadAt ...
func (i Info) ReadAt(b []byte, off int64) (n int, err error) {
	if len(i.Files) == 0 {
		fd, err := i.Open(0)
		if err != nil {
			return 0, 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
			}
		}
	}
	return 0, ErrNotImplemented
}

func (i Info) String() string {
	s := fmt.Sprintf("%s (%d) ", i.Name, i.TotalLength())
	s += fmt.Sprintf("%d × %d + %d\n", i.Full(), i.PieceLength, i.Last())
	for _, f := range i.Files {
		s += fmt.Sprintf("    %v\n", f)
	}
	return s
}