diff --git a/lib/database/filedown.js b/lib/database/filedown.js index 637d7e0f35..5c7f195a11 100644 --- a/lib/database/filedown.js +++ b/lib/database/filedown.js @@ -9,6 +9,19 @@ util.inherits(FileDown, AbstractLevelDOWN); function FileDown(location) { this.location = location; + const tmpDir = path.join(location, "_tmp"); + try { + // Fixes https://github.com/trufflesuite/ganache/issues/1617 + fs.mkdirSync(tmpDir, { recursive: true, mode: 0o1777 }); + } catch (e) { + // `recursive` doesn't throw if the file exists, but `recursive` doesn't + // exist in Node 8, so we catch the EEXISTS error in that case and ignore it. + // once we drop node 8 suport we can remove this try/catch + if (e.code !== "EEXIST") { + throw e; + } + } + this.tmpOptions = { keep: true, dir: tmpDir }; AbstractLevelDOWN.call(this, location); } @@ -79,7 +92,7 @@ FileDown.prototype._put = function(key, value, options, callback) { // leveldb implementation that doesn't use a separate file for every key Soon(TM). accessQueue.execute(lKey, () => { // get a tmp file to write the contents to... - tmp.file({ keep: true }, (err, path, fd, cleanupTmpFile) => { + tmp.file(this.tmpOptions, (err, path, fd, cleanupTmpFile) => { if (err) { callback(err); accessQueue.next(lKey);