-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfilesStorage.go
298 lines (279 loc) · 6.7 KB
/
filesStorage.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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
package Torrent
import (
"bytes"
"github.com/JRonak/Torrent/MetaData"
"log"
"os"
)
type FileStorage struct {
files []*os.File
meta *MetaData.MetaInfo
MultipleFiles bool
writeChan chan localStruct
}
func (this *FileStorage) Init(meta *MetaData.MetaInfo) bool {
this.meta = meta
this.writeChan = make(chan localStruct, 10)
go this.write()
if meta.Info.Files != nil {
this.MultipleFiles = true
return this.multiInit()
} else {
this.MultipleFiles = false
return this.singleInit()
}
}
func (this *FileStorage) singleInit() bool {
if this.isFileExists(this.meta.Info.Name) {
file, err := os.OpenFile(this.meta.Info.Name, os.O_RDWR, 0766)
if err != nil {
log.Println(err)
return false
}
this.files = append(this.files, file)
return false
}
file := this.createFile(this.meta.Info.Name, this.meta.Size())
this.files = append(this.files, file)
return true
}
func (this *FileStorage) multiInit() bool {
if !this.isDirExists(this.meta.Info.Name) {
this.createDir(this.meta.Info.Name)
}
fileCreated := true
for _, f := range this.meta.Info.Files {
length := len(f.Path)
path := this.meta.Info.Name
for i := 0; i < length-1; i++ {
path += "/" + f.Path[i]
}
filePath := path + "/" + f.Path[length-1]
if !this.isDirExists(path) {
this.createDir(path)
}
if !this.isFileExists(filePath) {
file := this.createFile(filePath, f.Length)
this.files = append(this.files, file)
} else {
file, err := os.OpenFile(filePath, os.O_RDWR, 0777)
if err != nil {
log.Println(err)
continue
}
this.files = append(this.files, file)
fileCreated = false
}
}
return fileCreated
}
func (this *FileStorage) createFile(name string, size int) *os.File {
f, err := os.Create(name)
if err != nil {
log.Println(err)
return nil
}
buf := make([]byte, MB)
for size >= MB {
n, err := f.Write(buf)
if err != nil {
log.Println(err)
return nil
}
size -= n
}
if size > 0 {
_, err := f.Write(buf[:size])
if err != nil {
log.Println(err)
return nil
}
}
return f
}
func (this *FileStorage) createDir(name string) {
os.MkdirAll(name, 0766)
}
func (this *FileStorage) isDirExists(name string) bool {
f, err := os.Open(name)
if err != nil {
return false
}
defer f.Close()
stat, err := f.Stat()
if err != nil {
log.Println(err)
return false
}
if stat.IsDir() {
return true
}
return false
}
func (this *FileStorage) isFileExists(name string) bool {
f, err := os.Open(name)
if err != nil {
return false
}
defer f.Close()
stat, err := f.Stat()
if err != nil {
log.Println(err)
return false
}
if stat.IsDir() {
return false
}
return true
}
//Generic function
func writeFile(file *os.File, data *[]byte, offset int) {
if file != nil {
_, err := file.WriteAt(*data, int64(offset))
if err != nil {
log.Println(err)
}
}
}
func (this *FileStorage) writeMultiple(piece localStruct) {
pSize := len(*piece.data)
pStart := piece.offset * this.meta.Info.PieceLength
pEnd := pStart + pSize
pCurr := pStart
pOffset := 0
fOffset := 0
for i, file := range this.files {
fSize := this.meta.Info.Files[i].Length
fEnd := fOffset + fSize
if fOffset <= pStart && pStart <= fEnd {
if pEnd <= fEnd {
data := *piece.data
data = data[pOffset:]
writeFile(file, &data, (pCurr - fOffset))
break
} else {
data := *piece.data
data = data[pOffset:((fEnd - pCurr) + pOffset)]
writeFile(file, &data, (pCurr - fOffset))
pOffset = (fEnd - pCurr)
pCurr = fEnd
}
}
fOffset = fEnd
}
}
func (this *FileStorage) write() {
for i := range this.writeChan {
if !this.MultipleFiles {
if len(this.files) == 0 {
log.Fatal("file not added")
}
writeFile(this.files[0], i.data, this.meta.Info.PieceLength*i.offset)
} else {
this.tempWriteMulti(i)
}
}
}
func (this *FileStorage) PutBlock(data *[]byte, index int) {
this.writeChan <- localStruct{data: data, offset: index}
}
func (this *FileStorage) GetBlock(index, offset, size int) []byte {
if this.MultipleFiles {
return this.tempReadMultiple(index, offset, size)
} else {
return this.getBlockSingle(index, offset, size)
}
}
func (this *FileStorage) getBlockSingle(index, offset, size int) []byte {
file := this.files[0]
data := make([]byte, size)
n, err := file.ReadAt(data, int64((index*this.meta.Info.PieceLength)+offset))
if err != nil {
log.Println(err)
return nil
}
return data[:n]
}
func (this *FileStorage) getBlockMulti(index, offset, size int) []byte {
pStart := index * this.meta.Info.PieceLength
pEnd := pStart + size
pCurr := pStart
fCurr := 0
buff := bytes.NewBuffer(nil)
for i, file := range this.files {
fSize := this.meta.Info.Files[i].Length
fEnd := fCurr + fSize
if fCurr <= pStart && pStart < fEnd {
if pEnd <= fEnd {
data := make([]byte, (pEnd - pCurr))
file.ReadAt(data, int64(pCurr-fCurr))
buff.Write(data)
break
} else {
data := make([]byte, fEnd-pCurr)
file.ReadAt(data, int64(pCurr-fCurr))
buff.Write(data)
pCurr = fEnd
}
}
fCurr = fEnd
}
return buff.Bytes()
}
func (this *FileStorage) tempWriteMulti(piece localStruct) {
pieceSize := this.meta.Info.PieceLength
pieceStart := pieceSize * piece.offset
pieceEnd := pieceStart + pieceSize
pieceCurrent := pieceStart
fileStart := 0
for i, file := range this.files {
fileEnd := fileStart + this.meta.Info.Files[i].Length
if fileStart <= pieceCurrent && pieceCurrent < fileEnd {
if pieceEnd <= fileEnd {
data := *piece.data
data = data[pieceCurrent-pieceStart:]
_, err := file.WriteAt(data, int64(pieceCurrent-fileStart))
if err != nil {
log.Println(err)
}
break
} else {
data := *piece.data
dataStart := pieceCurrent - pieceStart
data = data[dataStart : dataStart+(fileEnd-pieceCurrent)]
_, err := file.WriteAt(data, int64(pieceCurrent-fileStart))
if err != nil {
log.Println(err)
}
pieceCurrent += (fileEnd - pieceCurrent)
}
}
fileStart = fileEnd
}
}
func (this *FileStorage) tempReadMultiple(index, offset, size int) []byte {
pieceSize := this.meta.Info.PieceLength
pieceStart := pieceSize*index + offset
pieceEnd := pieceStart + size
pieceCurrent := pieceStart
fileStart := 0
buffer := bytes.NewBuffer(nil)
for i, file := range this.files {
fileEnd := fileStart + this.meta.Info.Files[i].Length
if fileStart <= pieceCurrent && pieceCurrent < fileEnd {
if pieceEnd <= fileEnd {
data := make([]byte, pieceEnd-pieceCurrent)
file.ReadAt(data, int64(pieceCurrent-fileStart))
buffer.Write(data)
break
} else {
data := make([]byte, fileEnd-pieceCurrent)
file.ReadAt(data, int64(pieceCurrent-fileStart))
buffer.Write(data)
pieceCurrent += (fileEnd - pieceCurrent)
}
}
fileStart = fileEnd
}
return buffer.Bytes()
}