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
|
module.exports = FSStorage
var Storage = require('bittorrent-client').Storage
var inherits = require('inherits')
var extend = require('extend.js')
var os = require('os')
var fs = require('fs')
var path = require('path')
var raf = require('random-access-file')
var mkdirp = require('mkdirp')
var rimraf = require('rimraf')
var thunky = require('thunky')
var TMP = fs.existsSync('/tmp') ? '/tmp' : os.tmpDir()
function noop () {}
inherits(FSStorage, Storage)
/**
* fs-backed Storage for a torrent download.
*
* @param {Object} parsedTorrent
* @param {Object} opts
*/
function FSStorage (parsedTorrent, opts) {
var self = this
opts = extend({
nobuffer: true,
tmp: TMP,
name: 'webtorrent'
}, opts || {})
Storage.call(self, parsedTorrent, opts)
if (!opts.path) {
opts.path = path.join(opts.tmp, opts.name, parsedTorrent.infoHash)
}
self.path = opts.path
self.piecesMap = []
self.nonExistentError = new Error('Cannot read from non-existent file')
self.files.forEach(function (file) {
var fileStart = file.offset
var fileEnd = fileStart + file.length
var pieceLength = file.pieceLength
var filePath = path.join(self.path, file.path)
var openWrite = thunky(function (cb) {
var fileDir = path.dirname(filePath)
mkdirp(fileDir, function (err) {
if (err) return cb(err)
if (self.closed) return cb(new Error('Storage closed'))
var fd = raf(filePath)
file.fd = fd
cb(null, fd)
})
})
var openRead = thunky(function (cb) {
// TODO: no need for fs.exists call, just try opening and handle error.
// fs.exists then open creates opportunity for race condition.
fs.exists(filePath, function (exists) {
if (exists) return openWrite(cb)
cb(self.nonExistentError)
})
})
file.pieces.forEach(function (piece) {
var index = piece.index
var pieceStart = index * pieceLength
var pieceEnd = pieceStart + piece.length
var from = (fileStart < pieceStart) ? 0 : fileStart - pieceStart
var to = (fileEnd > pieceEnd) ? pieceLength : fileEnd - pieceStart
var offset = (fileStart > pieceStart) ? 0 : pieceStart - fileStart
if (!self.piecesMap[index]) self.piecesMap[index] = []
self.piecesMap[index].push({
from: from,
to: to,
offset: offset,
openWrite: openWrite,
openRead: openRead
})
})
})
}
FSStorage.prototype.readBlock = function (index, offset, length, cb) {
var self = this
if (!cb) return console.error('FSStorage.readBlock requires a callback')
var piece = self.pieces[index]
if (!piece) return cb(new Error('invalid piece index ' + index))
if (piece.verified && piece.buffer) {
// piece is verified and cached in memory, so read directly from its buffer
// instead of reading from the filesystem.
return piece.readBlock(offset, length, cb)
}
var rangeFrom = offset
var rangeTo = rangeFrom + length
var targets = self.piecesMap[index].filter(function (target) {
return (target.to > rangeFrom && target.from < rangeTo)
})
if (!targets.length) return cb(new Error('no file matching the requested range?'))
var buffers = []
var end = targets.length
var i = 0
function readFromNextFile (err, buffer) {
if (err) return cb(err)
if (buffer) buffers.push(buffer)
if (i >= end) return cb(null, Buffer.concat(buffers))
var target = targets[i++]
var from = target.from
var to = target.to
var offset = target.offset
if (to > rangeTo) to = rangeTo
if (from < rangeFrom) {
offset += rangeFrom - from
from = rangeFrom
}
target.openRead(function (err, file) {
if (err) return (err === self.nonExistentError ? readFromNextFile(null, new Buffer(0)) : cb(err))
file.read(offset, to - from, readFromNextFile)
})
}
readFromNextFile()
}
// flush pieces to file once they're done and verified
FSStorage.prototype._onPieceDone = function (piece) {
var self = this
var targets = self.piecesMap[piece.index]
var end = targets.length
var i = 0
function cb () {
Storage.prototype._onPieceDone.call(self, piece)
}
if (!piece.buffer || self.readonly) return cb()
var writeToNextFile = function (err) {
if (err) return self.emit('error', err)
if (i >= end) {
return cb()
}
var target = targets[i++]
target.openWrite(function (err, file) {
if (err) return self.emit('error', err)
file.write(target.offset, piece.buffer.slice(target.from, target.to), writeToNextFile)
})
}
writeToNextFile()
}
/**
* Removes and cleans up any backing store for this storage.
*/
FSStorage.prototype.remove = function (cb) {
var self = this
if (!cb) cb = noop
self.close(function (err) {
if (err) return cb(err)
var root = self.files[0].path.split(path.sep)[0]
rimraf(path.join(self.path, root), cb)
})
}
/**
* Closes the backing store for this storage.
*/
FSStorage.prototype.close = function (cb) {
var self = this
if (!cb) cb = noop
if (self.closed) return cb()
Storage.prototype.close.call(self, function (err) {
if (err) return cb(err)
var i = 0
function loop (err) {
if (i >= self.files.length) return cb()
if (err) return cb(err)
var next = self.files[i++]
if (!next || !next.fd) return process.nextTick(loop)
next.fd.close(loop)
}
process.nextTick(loop)
})
}
|