2013-10-22 07:10:25 +02:00
|
|
|
var fs = require('fs')
|
|
|
|
, Path = require('path')
|
2014-01-13 17:48:51 +01:00
|
|
|
, mkdirp = require('mkdirp')
|
2013-10-22 07:10:25 +02:00
|
|
|
, mystreams = require('./streams')
|
|
|
|
, FSError = require('./error').FSError
|
2013-06-01 00:57:28 +02:00
|
|
|
|
2014-03-31 06:32:11 +02:00
|
|
|
try {
|
|
|
|
var fsExt = require('fs-ext')
|
|
|
|
} catch(e) {
|
|
|
|
fsExt = {
|
|
|
|
flock: function() {
|
|
|
|
arguments[arguments.length-1]()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-18 20:14:55 +02:00
|
|
|
function write(dest, data, cb) {
|
2013-06-01 00:57:28 +02:00
|
|
|
var safe_write = function(cb) {
|
2013-10-26 14:18:36 +02:00
|
|
|
var tmpname = dest + '.tmp' + String(Math.random()).substr(2)
|
2013-06-22 02:19:46 +02:00
|
|
|
fs.writeFile(tmpname, data, function(err) {
|
2013-10-26 14:18:36 +02:00
|
|
|
if (err) return cb(err)
|
|
|
|
return fs.rename(tmpname, dest, cb)
|
|
|
|
})
|
2013-06-01 00:57:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
safe_write(function(err) {
|
|
|
|
if (err && err.code === 'ENOENT') {
|
2014-01-13 17:48:51 +01:00
|
|
|
mkdirp(Path.dirname(dest), function(err) {
|
|
|
|
if (err) return cb(err)
|
2013-10-26 14:18:36 +02:00
|
|
|
safe_write(cb)
|
2013-06-01 00:57:28 +02:00
|
|
|
})
|
|
|
|
} else {
|
2013-10-26 14:18:36 +02:00
|
|
|
cb(err)
|
2013-06-01 00:57:28 +02:00
|
|
|
}
|
2013-10-26 14:18:36 +02:00
|
|
|
})
|
2013-06-01 00:57:28 +02:00
|
|
|
}
|
|
|
|
|
2013-06-20 15:07:34 +02:00
|
|
|
function write_stream(name) {
|
2013-10-26 14:18:36 +02:00
|
|
|
var stream = new mystreams.UploadTarballStream()
|
2013-06-20 15:07:34 +02:00
|
|
|
|
2013-10-26 14:18:36 +02:00
|
|
|
var _ended = 0
|
2013-09-28 14:19:40 +02:00
|
|
|
stream.on('end', function() {
|
2013-10-26 14:18:36 +02:00
|
|
|
_ended = 1
|
|
|
|
})
|
2013-09-28 14:19:40 +02:00
|
|
|
|
2013-06-20 15:07:34 +02:00
|
|
|
fs.exists(name, function(exists) {
|
2013-10-26 14:18:36 +02:00
|
|
|
if (exists) return stream.emit('error', new FSError('EEXISTS'))
|
2013-06-20 15:07:34 +02:00
|
|
|
|
2013-10-26 14:18:36 +02:00
|
|
|
var tmpname = name + '.tmp-'+String(Math.random()).replace(/^0\./, '')
|
|
|
|
, file = fs.createWriteStream(tmpname)
|
|
|
|
, opened = false
|
|
|
|
stream.pipe(file)
|
2013-09-28 14:19:40 +02:00
|
|
|
|
2013-09-27 11:54:43 +02:00
|
|
|
stream.done = function() {
|
2013-09-28 14:19:40 +02:00
|
|
|
function onend() {
|
2013-09-27 13:31:28 +02:00
|
|
|
file.on('close', function() {
|
|
|
|
fs.rename(tmpname, name, function(err) {
|
2013-10-26 14:18:36 +02:00
|
|
|
if (err) stream.emit('error', err)
|
|
|
|
stream.emit('success')
|
|
|
|
})
|
|
|
|
})
|
|
|
|
file.destroySoon()
|
2013-09-28 14:19:40 +02:00
|
|
|
}
|
|
|
|
if (_ended) {
|
2013-10-26 14:18:36 +02:00
|
|
|
onend()
|
2013-09-28 14:19:40 +02:00
|
|
|
} else {
|
2013-10-26 14:18:36 +02:00
|
|
|
stream.on('end', onend)
|
2013-09-28 14:19:40 +02:00
|
|
|
}
|
2013-10-26 14:18:36 +02:00
|
|
|
}
|
2013-09-27 11:54:43 +02:00
|
|
|
stream.abort = function() {
|
2013-10-11 11:50:41 +02:00
|
|
|
if (opened) {
|
2013-10-26 14:18:36 +02:00
|
|
|
opened = false
|
2013-10-11 11:50:41 +02:00
|
|
|
file.on('close', function() {
|
2013-10-26 14:18:36 +02:00
|
|
|
fs.unlink(tmpname, function(){})
|
|
|
|
})
|
2013-10-11 11:50:41 +02:00
|
|
|
}
|
2013-10-26 14:18:36 +02:00
|
|
|
file.destroySoon()
|
|
|
|
}
|
2013-09-27 11:54:43 +02:00
|
|
|
file.on('open', function() {
|
2013-10-26 14:18:36 +02:00
|
|
|
opened = true
|
2013-09-27 11:54:43 +02:00
|
|
|
// re-emitting open because it's handled in storage.js
|
2013-10-26 14:18:36 +02:00
|
|
|
stream.emit('open')
|
|
|
|
})
|
2013-09-28 14:19:40 +02:00
|
|
|
file.on('error', function(err) {
|
2013-10-26 14:18:36 +02:00
|
|
|
stream.emit('error', err)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
return stream
|
2013-06-20 15:07:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function read_stream(name, stream, callback) {
|
2014-03-07 20:39:20 +01:00
|
|
|
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 = new mystreams.ReadTarballStream()
|
|
|
|
stream.abort = function() {
|
|
|
|
rstream.close()
|
|
|
|
}
|
|
|
|
return stream
|
2013-06-20 15:07:34 +02:00
|
|
|
}
|
|
|
|
|
2013-06-01 00:57:28 +02:00
|
|
|
function create(name, contents, callback) {
|
|
|
|
fs.exists(name, function(exists) {
|
2013-10-26 14:18:36 +02:00
|
|
|
if (exists) return callback(new FSError('EEXISTS'))
|
|
|
|
write(name, contents, callback)
|
|
|
|
})
|
2013-06-01 00:57:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function update(name, contents, callback) {
|
|
|
|
fs.exists(name, function(exists) {
|
2013-10-26 14:18:36 +02:00
|
|
|
if (!exists) return callback(new FSError('ENOENT'))
|
|
|
|
write(name, contents, callback)
|
|
|
|
})
|
2013-06-01 00:57:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function read(name, callback) {
|
2013-10-22 07:10:25 +02:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
2014-01-13 17:48:51 +01:00
|
|
|
}
|
2013-10-22 07:10:25 +02:00
|
|
|
|
|
|
|
// 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) {
|
|
|
|
if (err) return callback(err, fd)
|
|
|
|
|
|
|
|
fs.fstat(fd, function(err, st) {
|
|
|
|
if (err) return callback(err, fd)
|
|
|
|
|
|
|
|
var buffer = new Buffer(st.size)
|
|
|
|
fs.read(fd, buffer, 0, st.size, null, function(err, bytesRead, buffer) {
|
2014-04-14 02:44:17 +02:00
|
|
|
if (err) return callback(err)
|
2013-10-22 07:10:25 +02:00
|
|
|
if (bytesRead != st.size) return callback(new Error('st.size != bytesRead'), fd)
|
|
|
|
|
|
|
|
callback(null, fd, buffer)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
2013-06-01 00:57:28 +02:00
|
|
|
}
|
|
|
|
|
2014-01-13 17:48:51 +01:00
|
|
|
module.exports.read = read
|
2013-06-14 10:34:29 +02:00
|
|
|
|
2014-01-13 17:48:51 +01:00
|
|
|
module.exports.read_json = function(name, cb) {
|
|
|
|
read(name, function(err, res) {
|
2013-10-26 14:18:36 +02:00
|
|
|
if (err) return cb(err)
|
2013-10-18 22:46:13 +02:00
|
|
|
|
|
|
|
var args = []
|
|
|
|
try {
|
|
|
|
args = [null, JSON.parse(res.toString('utf8'))]
|
|
|
|
} catch(err) {
|
|
|
|
args = [err]
|
|
|
|
}
|
|
|
|
cb.apply(null, args)
|
|
|
|
})
|
2013-06-14 10:34:29 +02:00
|
|
|
}
|
|
|
|
|
2014-01-13 17:48:51 +01:00
|
|
|
module.exports.lock_and_read = lock_and_read
|
2013-10-22 07:10:25 +02:00
|
|
|
|
2014-01-13 17:48:51 +01:00
|
|
|
module.exports.lock_and_read_json = function(name, cb) {
|
|
|
|
lock_and_read(name, function(err, fd, res) {
|
2013-10-22 07:10:25 +02:00
|
|
|
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)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2014-01-13 17:48:51 +01:00
|
|
|
module.exports.create = create
|
2013-10-18 23:17:53 +02:00
|
|
|
|
2014-01-13 17:48:51 +01:00
|
|
|
module.exports.create_json = function(name, value, cb) {
|
|
|
|
create(name, JSON.stringify(value, null, '\t'), cb)
|
2013-06-14 10:34:29 +02:00
|
|
|
}
|
|
|
|
|
2014-01-13 17:48:51 +01:00
|
|
|
module.exports.update = update
|
2013-06-14 10:34:29 +02:00
|
|
|
|
2014-01-13 17:48:51 +01:00
|
|
|
module.exports.update_json = function(name, value, cb) {
|
|
|
|
update(name, JSON.stringify(value, null, '\t'), cb)
|
2013-06-14 10:34:29 +02:00
|
|
|
}
|
|
|
|
|
2014-01-13 17:48:51 +01:00
|
|
|
module.exports.write = write
|
2013-06-14 10:34:29 +02:00
|
|
|
|
2014-01-13 17:48:51 +01:00
|
|
|
module.exports.write_json = function(name, value, cb) {
|
|
|
|
write(name, JSON.stringify(value, null, '\t'), cb)
|
2013-06-18 20:14:55 +02:00
|
|
|
}
|
|
|
|
|
2014-01-13 17:48:51 +01:00
|
|
|
module.exports.write_stream = write_stream
|
2013-06-18 20:14:55 +02:00
|
|
|
|
2014-01-13 17:48:51 +01:00
|
|
|
module.exports.read_stream = read_stream
|
2013-06-20 15:07:34 +02:00
|
|
|
|
2014-01-13 17:48:51 +01:00
|
|
|
module.exports.unlink = fs.unlink
|
2013-10-06 10:27:50 +02:00
|
|
|
|
2014-01-13 17:48:51 +01:00
|
|
|
module.exports.rmdir = fs.rmdir
|
2013-06-01 00:57:28 +02:00
|
|
|
|