-
Notifications
You must be signed in to change notification settings - Fork 24
/
index.js
255 lines (194 loc) · 5.95 KB
/
index.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
const RandomAccessStorage = require('random-access-storage')
const fs = require('fs')
const path = require('path')
const constants = fs.constants
let fsext = null
try {
fsext = require('fs-native-extensions')
} catch {}
const RDWR = constants.O_RDWR
const RDONLY = constants.O_RDONLY
const WRONLY = constants.O_WRONLY
const CREAT = constants.O_CREAT
class Pool {
constructor (maxSize) {
this.maxSize = maxSize
this.active = []
}
_onactive (file) {
// suspend a random one when the pool
if (this.active.length >= this.maxSize) {
const r = Math.floor(Math.random() * this.active.length)
this.active[r].suspend()
}
file._pi = this.active.push(file) - 1
}
_oninactive (file) {
const head = this.active.pop()
if (head !== file) {
head._pi = file._pi
this.active[head._pi] = head
}
}
}
module.exports = class RandomAccessFile extends RandomAccessStorage {
constructor (filename, opts = {}) {
const size = opts.size || (opts.truncate ? 0 : -1)
super()
if (opts.directory) filename = path.join(opts.directory, path.resolve('/', filename).replace(/^\w+:\\/, ''))
this.directory = opts.directory || null
this.filename = filename
this.fd = 0
const {
readable = true,
writable = true
} = opts
this.mode = readable && writable ? RDWR : (readable ? RDONLY : WRONLY)
this._pi = 0 // pool index
this._pool = opts.pool || null
this._size = size
this._rmdir = !!opts.rmdir
this._lock = opts.lock === true
this._sparse = opts.sparse === true
this._alloc = opts.alloc || Buffer.allocUnsafe
this._alwaysCreate = size >= 0
}
static createPool (maxSize) {
return new Pool(maxSize)
}
_open (req) {
const create = this._alwaysCreate || this.writing // .writing comes from RAS
const self = this
const mode = this.mode | (create ? CREAT : 0)
if (create) fs.mkdir(path.dirname(this.filename), { recursive: true }, ondir)
else ondir(null)
function ondir (err) {
if (err) return req.callback(err)
fs.open(self.filename, mode, onopen)
}
function onopen (err, fd) {
if (err) return onerror(err)
self.fd = fd
if (!self._lock || !fsext) return onlock(null)
// Should we aquire a read lock?
const shared = self.mode === RDONLY
if (fsext.tryLock(self.fd, { shared })) onlock(null)
else onlock(createLockError(self.filename))
}
function onlock (err) {
if (err) return onerrorafteropen(err)
if (!self._sparse || !fsext || self.mode === RDONLY) return onsparse(null)
fsext.sparse(self.fd).then(onsparse, onsparse)
}
function onsparse (err) {
if (err) return onerrorafteropen(err)
if (self._size < 0) return ontruncate(null)
fs.ftruncate(self.fd, self._size, ontruncate)
}
function ontruncate (err) {
if (err) return onerrorafteropen(err)
if (self._pool !== null) self._pool._onactive(self)
req.callback(null)
}
function onerror (err) {
req.callback(err)
}
function onerrorafteropen (err) {
fs.close(self.fd, function () {
self.fd = 0
onerror(err)
})
}
}
_write (req) {
const data = req.data
const fd = this.fd
fs.write(fd, data, 0, req.size, req.offset, onwrite)
function onwrite (err, wrote) {
if (err) return req.callback(err)
req.size -= wrote
req.offset += wrote
if (!req.size) return req.callback(null)
fs.write(fd, data, data.length - req.size, req.size, req.offset, onwrite)
}
}
_read (req) {
const self = this
const data = req.data || this._alloc(req.size)
const fd = this.fd
if (!req.size) return process.nextTick(readEmpty, req)
fs.read(fd, data, 0, req.size, req.offset, onread)
function onread (err, read) {
if (err) return req.callback(err)
if (!read) return req.callback(createReadError(self.filename, req.offset, req.size))
req.size -= read
req.offset += read
if (!req.size) return req.callback(null, data)
fs.read(fd, data, data.length - req.size, req.size, req.offset, onread)
}
}
_del (req) {
if (req.size === Infinity) return this._truncate(req) // TODO: remove this when all callsites use truncate
if (!fsext) return req.callback(null)
fsext.trim(this.fd, req.offset, req.size).then(ontrim, ontrim)
function ontrim (err) {
req.callback(err)
}
}
_truncate (req) {
fs.ftruncate(this.fd, req.offset, ontruncate)
function ontruncate (err) {
req.callback(err)
}
}
_stat (req) {
fs.fstat(this.fd, onstat)
function onstat (err, st) {
req.callback(err, st)
}
}
_close (req) {
const self = this
fs.close(this.fd, onclose)
function onclose (err) {
if (err) return req.callback(err)
if (self._pool !== null) self._pool._oninactive(self)
self.fd = 0
req.callback(null)
}
}
_unlink (req) {
const self = this
const root = this.directory && path.resolve(path.join(this.directory, '.'))
let dir = path.resolve(path.dirname(this.filename))
fs.unlink(this.filename, onunlink)
function onunlink (err) {
// if the file isn't there, its already unlinked, ignore
if (err && err.code === 'ENOENT') err = null
if (err || !self._rmdir || !root || dir === root) return req.callback(err)
fs.rmdir(dir, onrmdir)
}
function onrmdir (err) {
dir = path.join(dir, '..')
if (err || dir === root) return req.callback(null)
fs.rmdir(dir, onrmdir)
}
}
}
function readEmpty (req) {
req.callback(null, Buffer.alloc(0))
}
function createLockError (path) {
const err = new Error('ELOCKED: File is locked')
err.code = 'ELOCKED'
err.path = path
return err
}
function createReadError (path, offset, size) {
const err = new Error('EPARTIALREAD: Could not satisfy length')
err.code = 'EPARTIALREAD'
err.path = path
err.offset = offset
err.size = size
return err
}