diff options
Diffstat (limited to 'node_modules/write-file-atomic/index.js')
-rw-r--r-- | node_modules/write-file-atomic/index.js | 24 |
1 files changed, 21 insertions, 3 deletions
diff --git a/node_modules/write-file-atomic/index.js b/node_modules/write-file-atomic/index.js index 7939828e5..438c8e473 100644 --- a/node_modules/write-file-atomic/index.js +++ b/node_modules/write-file-atomic/index.js @@ -51,15 +51,30 @@ function _writeFile (filename, data, options, callback) { function thenWriteFile () { chain([ - [fs, fs.writeFile, tmpfile, data, options.encoding || 'utf8'], - options.mode && [fs, fs.chmod, tmpfile, options.mode], + [writeFileAsync, tmpfile, data, options.mode, options.encoding || 'utf8'], options.chown && [fs, fs.chown, tmpfile, options.chown.uid, options.chown.gid], + options.mode && [fs, fs.chmod, tmpfile, options.mode], [fs, fs.rename, tmpfile, filename] ], function (err) { err ? fs.unlink(tmpfile, function () { callback(err) }) : callback() }) } + + // doing this instead of `fs.writeFile` in order to get the ability to + // call `fsync`. + function writeFileAsync (file, data, mode, encoding, cb) { + fs.open(file, 'w', options.mode, function (err, fd) { + if (err) return cb(err) + fs.write(fd, data, encoding, function (err) { + if (err) return cb(err) + fs.fsync(fd, function (err) { + if (err) return cb(err) + fs.close(fd, cb) + }) + }) + }) + } } function writeFileSync (filename, data, options) { @@ -89,7 +104,10 @@ function writeFileSync (filename, data, options) { } } - fs.writeFileSync(tmpfile, data, options.encoding || 'utf8') + var fd = fs.openSync(tmpfile, 'w', options.mode) + fs.writeSync(fd, data, 0, options.encoding || 'utf8') + fs.fsyncSync(fd) + fs.closeSync(fd) if (options.chown) fs.chownSync(tmpfile, options.chown.uid, options.chown.gid) if (options.mode) fs.chmodSync(tmpfile, options.mode) fs.renameSync(tmpfile, filename) |