forked from antimatter15/summerTorrent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfilestore.js
384 lines (359 loc) · 11.6 KB
/
filestore.js
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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
var sys = require('sys'), bitfield = require('./bitfield'), fs = require('fs'), cryptolib = require('crypto'), path = require('path');
/*
* Filestore: { pieceLength, pieces, files: [{path offset length md5}...],
* left }
* offset is in increasing order, so that binary search can find a given absolute offset.
*/
function parseMultipleFiles(store, info, destDir){
var infoFiles = info.files, infoName = info.name, files = [], totalLength = 0, i, len, file;
for (i = 0, len = infoFiles.length; i < len; i += 1) {
file = infoFiles[i];
files.push({
path: path.join(destDir, infoName, file.path),
offset: totalLength,
length: file.length,
md5sum: file.md5sum
});
totalLength += file.length;
}
store.files = files;
store.totalLength = totalLength;
sys.log('files: ' + JSON.stringify(store.files));
}
function create(metaInfo, destDir){
var result, info, pieceLength;
result = {
pieceLength: metaInfo['piece length'],
pieces: metaInfo.pieces
};
if ('info' in metaInfo) {
info = metaInfo.info;
if (!('pieces' in info)) {
throw 'missing pieces';
}
result.pieces = info.pieces;
if (!('piece length' in info)) {
throw 'missing piece length';
}
pieceLength = info['piece length'];
result.pieceLength = pieceLength;
// Check if this is a single file torrent or a file list torrent
if ('length' in info) {
// single file
result.files = [{
path: path.join(destDir, info.name),
offset: 0,
length: info.length,
md5: info.md5
}];
result.totalLength = info.length;
}
else {
parseMultipleFiles(result, info, destDir);
}
}
else {
throw 'no info found in metaInfo';
}
result.pieceCount = Math.floor((result.totalLength + pieceLength - 1) / pieceLength);
result.lastPieceLength = result.pieceCount <= 0 ? 0 : result.totalLength - pieceLength * (result.pieceCount - 1);
return result;
}
// Find file that associates with offset
// returns index of file
function findFile(files, offset){
var a = -1, b = files.length, c, file;
while (a < b) {
c = (a + b) >> 1;
file = files[c];
if (file.offset <= offset && file.offset + file.length > offset) {
return c;
}
else
if (file.offset < offset) {
a = c;
}
else {
a = b;
}
}
}
// Returns an iterator object with two methods: hasNext() and next().
// Next will return {file, offset, length}
function createRangeIterator(store, offset, length){
var files = store.files, i = findFile(files, offset);
return {
hasNext: function(){
return length > 0;
},
next: function(){
var file, fileOffset, fileLength;
if (length <= 0) {
throw "StopIteraton";
}
file = files[i];
fileOffset = offset - file.offset;
fileLength = Math.min(file.length - fileOffset, length);
i += 1;
length -= fileLength;
offset += fileLength;
return {
file: file,
offset: fileOffset,
length: fileLength
};
}
};
}
// Makes sure all the directories exist for a given path.
// If they don't exist, tries to create them.
// Calls callback(err)
function ensureDirExists(fullPath, callback){
var mode = 7 * 64 + 7 * 8 + 7; // 0777 aka rwxrwxrwx-
var parts = fullPath.split('/'), root;
if (parts.length > 1) {
parts.pop();
if (fullPath.charAt(0) == '/') {
root = '';
}
else {
root = '.';
}
ensureDirExists2(root, parts, callback);
}
else {
callback(null);
}
function ensureDirExists2(base, dirList, callback){
var newPath = base + '/' + dirList.shift();
fs.stat(newPath, function(err, stats){
if (err) {
makeDir();
}
else
if (!stats.isDirectory()) {
fs.unlink(newPath, function(err){
if (err) {
callback(err);
}
else {
makeDir();
}
});
}
else {
checkKids();
}
});
function makeDir(){
fs.mkdir(newPath, mode, checkKids);
}
function checkKids(err){
if (err || dirList.length == 0) {
callback(err);
}
else {
ensureDirExists2(newPath, dirList, callback);
}
}
}
}
function ensureFile2(file, callback){
var mode = 6 * 64 + 4 * 8 + 4; // 0666 aka rw-rw-rw-
fs.stat(file.path, function(err, stats){
if (err) {
fs.open(file.path, 'w', mode, function(error, fd){
if (error) {
callback(error, file);
}
else {
fs.truncate(fd, file.length, function(error){
if (error) {
callback(error, file);
}
else {
// Need to close this descriptor and try again.
fs.close(fd, function(error){
if (error) {
callback(error, file);
}
ensureFile2(file, callback);
});
}
});
}
});
}
else
if (stats.isDirectory() || stats.size !== file.length) {
fs.unlink(file.path, function(error){
if (error) {
callback(error, file);
}
else {
ensureFile2(file, callback);
}
});
}
else {
// file exists, and is right length and type
fs.open(file.path, 'r+', mode, function(error, fd){
if (error) {
callback(error, file);
}
else {
file.fd = fd;
callback(error, file);
}
});
}
});
}
//Calls callback(err, file)
function ensureFile(file, callback){
if (file.fd) {
callback(null, file);
}
else {
ensureDirExists(file.path, function(err){
if (err) {
callback(err, file);
}
else {
ensureFile2(file, callback);
}
});
}
}
// begin and length are optional arguments.
function createPieceFragmentIterator(store, pieceIndex, begin, length){
var pieceLength = store.pieceLength, offset = pieceLength * pieceIndex + begin, length = Math.min(store.totalLength - offset, length);
return createRangeIterator(store, offset, length);
}
// Callback called repeatedly with args (error, data)
// data will be null after all fragments have been read.
function readPiecePart(store, pieceIndex, begin, length, callback){
var iterator = createPieceFragmentIterator(store, pieceIndex, begin, length);
function readPieceImp(){
var fragment;
if (iterator.hasNext()) {
fragment = iterator.next();
ensureFile(fragment.file, function(error, file){
if (error) {
callback(error);
}
else {
fs.read(file.fd, fragment.length, fragment.offset, 'binary', function(err, data, bytesRead){
var fragment;
callback(err, data);
if (!err) {
readPieceImp();
}
});
}
});
}
else {
callback(null, null);
}
}
readPieceImp();
}
function readPiece(store, pieceIndex, callback){
readPiecePart(store, pieceIndex, 0, store.pieceLength, callback);
}
//Callback (error)
function writePiecePart(store, pieceIndex, begin, data, callback){
var iterator = createPieceFragmentIterator(store, pieceIndex, begin, data.length);
function writePieceImp(){
var fragment;
if (iterator.hasNext()) {
fragment = iterator.next();
ensureFile(fragment.file, function(error, file){
var dataFrag;
if (error) {
callback(error);
}
else {
dataFrag = data.substring(0, fragment.length);
data = data.substring(fragment.length);
fs.write(file.fd, dataFrag, fragment.offset, 'binary', function(err, bytesWritten){
var fragment;
if (err) {
callback(err);
}
else {
writePieceImp();
}
});
}
});
}
else {
callback(null);
}
}
writePieceImp();
}
function pieceLength(store, pieceIndex){
if (pieceIndex < store.pieceCount - 1) {
return store.pieceLength;
}
else {
return store.lastPieceLength;
}
}
function inspectImp(store, pieceIndex, hash, callback){
readPiece(store, 0, function(error, data){
var digest, expected, goodPiece;
if (error) {
callback(error);
}
else {
if (data) {
hash.update(data);
}
else {
digest = hash.digest('binary');
expected = store.pieces.substring(pieceIndex * 20, (pieceIndex + 1) * 20);
goodPiece = expected === digest;
store.goodPieces.set(pieceIndex, goodPiece);
if (!goodPiece) {
store.left += pieceLength(store, pieceIndex);
}
pieceIndex += 1;
if (pieceIndex < store.pieceCount) {
hash = cryptolib.createHash('sha1');
inspectImp(store, pieceIndex, hash, callback);
}
else {
callback(null);
}
}
}
});
}
function inspectPiece(store, pieceIndex, callback){
var hash = cryptolib.createHash('sha1');
readPiece(store, pieceIndex, function(error, data){
var digest, expected, goodPiece;
if (error) callback(false, error);
hash.update(data);
digest = hash.digest('binary');
expected = store.pieces.substring(pieceIndex * 20, (pieceIndex + 1) * 20);
goodPiece = expected === digest;
callback(goodPiece)
});
}
// callback(err)
function inspect(store, callback){
var hash = cryptolib.createHash('sha1');
store.goodPieces = bitfield.create(store.pieceCount);
store.left = 0;
inspectImp(store, 0, hash, callback);
}
exports.create = create;
exports.inspect = inspect;
exports.inspectPiece = inspectPiece;
exports.readPiecePart = readPiecePart;
exports.writePiecePart = writePiecePart;