forked from rlidwka/sinopia
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlocal-fs.js
277 lines (234 loc) · 6.14 KB
/
local-fs.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
var fs = require('fs')
var Error = require('http-errors')
var mkdirp = require('mkdirp')
var Path = require('path')
var MyStreams = require('./streams')
function FSError(code) {
var err = Error(code)
err.code = code
return err
}
try {
var fsExt = require('fs-ext')
} catch (e) {
fsExt = {
flock: function() {
arguments[arguments.length-1]()
}
}
}
function tempFile(str) {
return str + '.tmp' + String(Math.random()).substr(2)
}
function renameTmp(src, dst, _cb) {
function cb(err) {
if (err) fs.unlink(src)
_cb(err)
}
if (process.platform !== 'win32') {
return fs.rename(src, dst, cb)
}
// windows can't remove opened file,
// but it seem to be able to rename it
var tmp = tempFile(dst)
fs.rename(dst, tmp, function(err) {
fs.rename(src, dst, cb)
if (!err) fs.unlink(tmp)
})
}
function write(dest, data, cb) {
var safe_write = function(cb) {
var tmpname = tempFile(dest)
fs.writeFile(tmpname, data, function(err) {
if (err) return cb(err)
renameTmp(tmpname, dest, cb)
})
}
safe_write(function(err) {
if (err && err.code === 'ENOENT') {
mkdirp(Path.dirname(dest), function(err) {
if (err) return cb(err)
safe_write(cb)
})
} else {
cb(err)
}
})
}
function write_stream(name) {
var stream = MyStreams.UploadTarballStream()
var _ended = 0
stream.on('end', function() {
_ended = 1
})
fs.exists(name, function(exists) {
if (exists) return stream.emit('error', FSError('EEXISTS'))
var tmpname = name + '.tmp-'+String(Math.random()).replace(/^0\./, '')
var file = fs.createWriteStream(tmpname)
var opened = false
stream.pipe(file)
stream.done = function() {
function onend() {
file.on('close', function() {
renameTmp(tmpname, name, function(err) {
if (err) {
stream.emit('error', err)
} else {
stream.emit('success')
}
})
})
file.destroySoon()
}
if (_ended) {
onend()
} else {
stream.on('end', onend)
}
}
stream.abort = function() {
if (opened) {
opened = false
file.on('close', function() {
fs.unlink(tmpname, function(){})
})
}
file.destroySoon()
}
file.on('open', function() {
opened = true
// re-emitting open because it's handled in storage.js
stream.emit('open')
})
file.on('error', function(err) {
stream.emit('error', err)
})
})
return stream
}
function read_stream(name, stream, callback) {
var rstream = fs.createReadStream(name)
rstream.on('error', function(err) {
stream.emit('error', err)
})
rstream.on('open', function(fd) {
fs.fstat(fd, function(err, stats) {
if (err) return stream.emit('error', err)
stream.emit('content-length', stats.size)
stream.emit('open')
rstream.pipe(stream)
})
})
var stream = MyStreams.ReadTarballStream()
stream.abort = function() {
rstream.close()
}
return stream
}
function create(name, contents, callback) {
fs.exists(name, function(exists) {
if (exists) return callback( FSError('EEXISTS') )
write(name, contents, callback)
})
}
function update(name, contents, callback) {
fs.exists(name, function(exists) {
if (!exists) return callback( FSError('ENOENT') )
write(name, contents, callback)
})
}
function read(name, callback) {
fs.readFile(name, callback)
}
// open and flock with exponential backoff
function open_flock(name, opmod, flmod, tries, backoff, cb) {
fs.open(name, opmod, function(err, fd) {
if (err) return cb(err, fd)
fsExt.flock(fd, flmod, function(err) {
if (err) {
if (!tries) {
fs.close(fd, function() {
cb(err)
})
} else {
fs.close(fd, function() {
setTimeout(function() {
open_flock(name, opmod, flmod, tries-1, backoff*2, cb)
}, backoff)
})
}
} else {
cb(null, fd)
}
})
})
}
// this function neither unlocks file nor closes it
// it'll have to be done manually later
function lock_and_read(name, _callback) {
open_flock(name, 'r', 'exnb', 4, 10, function(err, fd) {
function callback(err) {
if (err && fd) {
fs.close(fd, function(err2) {
_callback(err)
})
} else {
_callback.apply(null, arguments)
}
}
if (err) return callback(err, fd)
fs.fstat(fd, function(err, st) {
if (err) return callback(err, fd)
var buffer = Buffer(st.size)
if (st.size === 0) return onRead(null, 0, buffer)
fs.read(fd, buffer, 0, st.size, null, onRead)
function onRead(err, bytesRead, buffer) {
if (err) return callback(err, fd)
if (bytesRead != st.size) return callback(Error('st.size != bytesRead'), fd)
callback(null, fd, buffer)
}
})
})
}
module.exports.read = read
module.exports.read_json = function(name, cb) {
read(name, function(err, res) {
if (err) return cb(err)
var args = []
try {
args = [ null, JSON.parse(res.toString('utf8')) ]
} catch(err) {
args = [ err ]
}
cb.apply(null, args)
})
}
module.exports.lock_and_read = lock_and_read
module.exports.lock_and_read_json = function(name, cb) {
lock_and_read(name, function(err, fd, res) {
if (err) return cb(err, fd)
var args = []
try {
args = [ null, fd, JSON.parse(res.toString('utf8')) ]
} catch(err) {
args = [ err, fd ]
}
cb.apply(null, args)
})
}
module.exports.create = create
module.exports.create_json = function(name, value, cb) {
create(name, JSON.stringify(value, null, '\t'), cb)
}
module.exports.update = update
module.exports.update_json = function(name, value, cb) {
update(name, JSON.stringify(value, null, '\t'), cb)
}
module.exports.write = write
module.exports.write_json = function(name, value, cb) {
write(name, JSON.stringify(value, null, '\t'), cb)
}
module.exports.write_stream = write_stream
module.exports.read_stream = read_stream
module.exports.unlink = fs.unlink
module.exports.rmdir = fs.rmdir