-
Notifications
You must be signed in to change notification settings - Fork 0
/
file.go
227 lines (206 loc) · 4.82 KB
/
file.go
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
package p2p
import (
"errors"
"io"
log "github.com/cihub/seelog"
)
// File Interface for a file.
// Multiple goroutines may access a File at the same time.
type File interface {
io.ReaderAt
io.WriterAt
io.Closer
}
// FsProvider Interface for a provider of filesystems.
type FsProvider interface {
NewFS() (FileSystem, error)
}
// FileSystem Interface for a file system. A file system contains files.
type FileSystem interface {
Open(name []string, length int64) (file File, err error)
io.Closer
}
// FileStore a file store.
type FileStore interface {
io.ReaderAt
io.WriterAt
io.Closer
SetCache(FileCache)
Commit(int, []byte, int64)
}
type fileStore struct {
fileSystem FileSystem
offsets []int64
files []fileEntry // Stored in increasing globalOffset order
cache FileCache
}
type fileEntry struct {
length int64
file File
}
// NewFileStore 根据元数据信息打开所有文件
func NewFileStore(info *MetaInfo, fileSystem FileSystem) (f FileStore, totalSize int64, err error) {
fs := &fileStore{}
fs.fileSystem = fileSystem
numFiles := len(info.Files)
fs.files = make([]fileEntry, numFiles)
fs.offsets = make([]int64, numFiles)
for i, src := range info.Files {
var file File
file, err = fs.fileSystem.Open([]string{src.Path, src.Name}, src.Length)
if err != nil {
log.Errorf("Open file failed, file=%v/%v, error=%v", src.Path, src.Name, err)
// Close all files opened up to now.
for i2 := 0; i2 < i; i2++ {
fs.files[i2].file.Close()
}
return
}
fs.files[i].file = file
fs.files[i].length = src.Length
fs.offsets[i] = totalSize
totalSize += src.Length
}
f = fs
return
}
// SetCache ...
func (f *fileStore) SetCache(cache FileCache) {
f.cache = cache
}
func (f *fileStore) find(offset int64) int {
// Binary search
offsets := f.offsets
low := 0
high := len(offsets)
for low < high-1 {
probe := (low + high) / 2
entry := offsets[probe]
if offset < entry {
high = probe
} else {
low = probe
}
}
return low
}
// ReadAt ...
func (f *fileStore) ReadAt(p []byte, off int64) (int, error) {
if f.cache == nil {
return f.RawReadAt(p, off)
}
unfullfilled := f.cache.readAt(p, off)
var retErr error
for _, unf := range unfullfilled {
_, err := f.RawReadAt(unf.data, unf.i)
if err != nil {
log.Error("Got an error on read (off=", unf.i, "len=", len(unf.data), ") from filestore:", err)
retErr = err
}
}
return len(p), retErr
}
// RawReadAt ...
func (f *fileStore) RawReadAt(p []byte, off int64) (n int, err error) {
index := f.find(off)
for len(p) > 0 && index < len(f.offsets) {
chunk := int64(len(p))
entry := &f.files[index]
itemOffset := off - f.offsets[index]
if itemOffset < entry.length {
space := entry.length - itemOffset
if space < chunk {
chunk = space
}
var nThisTime int
nThisTime, err = entry.file.ReadAt(p[0:chunk], itemOffset)
n = n + nThisTime
if err != nil {
return
}
p = p[nThisTime:]
off += int64(nThisTime)
}
index++
}
// At this point if there's anything left to read it means we've run off the
// end of the file store. Read zeros. This is defined by the bittorrent protocol.
for i := range p {
p[i] = 0
}
return
}
// WriteAt ...
func (f *fileStore) WriteAt(p []byte, off int64) (int, error) {
if f.cache != nil {
needRawWrite := f.cache.writeAt(p, off)
if needRawWrite != nil {
for _, nc := range needRawWrite {
f.RawWriteAt(nc.data, nc.i)
}
}
return len(p), nil
}
return f.RawWriteAt(p, off)
}
// Commit ...
func (f *fileStore) Commit(pieceNum int, piece []byte, off int64) {
if f.cache != nil {
_, err := f.RawWriteAt(piece, off)
if err != nil {
log.Error("Error committing to storage:", err)
return
}
f.cache.MarkCommitted(pieceNum)
}
}
// RawWriteAt ...
func (f *fileStore) RawWriteAt(p []byte, off int64) (n int, err error) {
index := f.find(off)
for len(p) > 0 && index < len(f.offsets) {
chunk := int64(len(p))
entry := &f.files[index]
itemOffset := off - f.offsets[index]
if itemOffset < entry.length {
space := entry.length - itemOffset
if space < chunk {
chunk = space
}
var nThisTime int
nThisTime, err = entry.file.WriteAt(p[0:chunk], itemOffset)
n += nThisTime
if err != nil {
return
}
p = p[nThisTime:]
off += int64(nThisTime)
}
index++
}
// At this point if there's anything left to write it means we've run off the
// end of the file store. Check that the data is zeros.
// This is defined by the bittorrent protocol.
for i := range p {
if p[i] != 0 {
err = errors.New("Unexpected non-zero data at end of store.")
n = n + i
return
}
}
n = n + len(p)
return
}
// Close ...
func (f *fileStore) Close() (err error) {
for i := range f.files {
f.files[i].file.Close()
}
if f.cache != nil {
f.cache.Close()
f.cache = nil
}
if f.fileSystem != nil {
err = f.fileSystem.Close()
}
return
}