harden clearChannel implementation

pull/1/head
ansuz 7 years ago
parent 7be1e249e1
commit b295aaa6c7

@ -56,9 +56,9 @@ var closeChannel = function (env, channelName, cb) {
} }
}; };
var clearChannel = function (Env, channelId, cb) { var clearChannel = function (env, channelId, cb) {
var path = mkPath(Env, channelId); var path = mkPath(env, channelId);
getMetadataAtPath(Env, path, function (e, metadata) { getMetadataAtPath(env, path, function (e, metadata) {
if (e) { return cb(e); } if (e) { return cb(e); }
if (!metadata) { if (!metadata) {
return void Fs.truncate(path, 0, function (err) { return void Fs.truncate(path, 0, function (err) {
@ -70,9 +70,17 @@ var clearChannel = function (Env, channelId, cb) {
} }
var len = JSON.stringify(metadata).length + 1; var len = JSON.stringify(metadata).length + 1;
// as long as closeChannel is synchronous, this should not cause
// any race conditions. truncate ought to return faster than a channel
// can be opened and read by another user. if that turns out not to be
// the case, we'll need to implement locking.
closeChannel(env, channelId, function (err) {
if (err) { cb(err); }
Fs.truncate(path, len, function (err) { Fs.truncate(path, len, function (err) {
if (err) { return cb(err); } if (err) { return cb(err); }
closeChannel(Env, channelId, cb); cb();
});
}); });
}); });
}; };
@ -120,7 +128,7 @@ var checkPath = function (path, callback) {
}; };
var removeChannel = function (env, channelName, cb) { var removeChannel = function (env, channelName, cb) {
var filename = Path.join(env.root, channelName.slice(0, 2), channelName + '.ndjson'); var filename = mkPath(env, channelName);
Fs.unlink(filename, cb); Fs.unlink(filename, cb);
}; };

Loading…
Cancel
Save