From 27844cd35849c739264e5c92f86311d657a17b3a Mon Sep 17 00:00:00 2001 From: Alex Kocharin Date: Sat, 22 Jun 2013 04:19:46 +0400 Subject: [PATCH] fixing some race conditions --- lib/fs-storage.js | 8 ++++++-- lib/st-local.js | 2 +- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/lib/fs-storage.js b/lib/fs-storage.js index b60b72dcd..455d745e8 100644 --- a/lib/fs-storage.js +++ b/lib/fs-storage.js @@ -19,7 +19,11 @@ function make_directories(dest, cb) { function write(dest, data, cb) { var safe_write = function(cb) { - fs.writeFile(dest, data, cb); + var tmpname = dest + '.tmp' + String(Math.random()).substr(2); + fs.writeFile(tmpname, data, function(err) { + if (err) return cb(err); + return fs.rename(tmpname, dest, cb); + }); } safe_write(function(err) { @@ -99,7 +103,7 @@ Storage.prototype.read = function(name, cb) { Storage.prototype.read_json = function(name, cb) { read(this.path + '/' + name, function(err, res) { if (err) return cb(err); - cb(null, JSON.parse(res)); + cb(null, JSON.parse(res.toString('utf8'))); }); } diff --git a/lib/st-local.js b/lib/st-local.js index 132d3fa94..175c92f66 100644 --- a/lib/st-local.js +++ b/lib/st-local.js @@ -92,7 +92,7 @@ Storage.prototype.update_versions = function(name, newdata, callback) { // if tag is updated to reference latter version, that's fine var need_change = (data['dist-tags'][tag] == null) || - (!semver.gt(newdata['dist-tags'][tag], data['dist-tags'][tag])); + (!semver.gte(newdata['dist-tags'][tag], data['dist-tags'][tag])); if (need_change) { change = true;