From 06fe111bb68698fa10363026c1641977ec0460af Mon Sep 17 00:00:00 2001 From: Nick Martin Date: Fri, 24 Feb 2012 18:13:47 -0800 Subject: [PATCH] Add a function to make a temporary directory we can write to. --- app/lib/files.js | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/app/lib/files.js b/app/lib/files.js index ed7ba3792e..9d5d5517c6 100644 --- a/app/lib/files.js +++ b/app/lib/files.js @@ -297,6 +297,32 @@ var files = module.exports = { } } }); + }, + + // Make a temporary directory. Returns the path to the newly created + // directory. Caller is responsible for deleting the directory later. + mkdtemp: function (prefix) { + prefix = prefix || 'meteor-temp-'; + // find /tmp + var tmp_dir = _.first(_.map(['TMPDIR', 'TMP', 'TEMP'], function (t) { + return process.env[t]; + }).filter(_.identity)) || '/tmp'; + tmp_dir = fs.realpathSync(tmp_dir); + + // make the directory. give it 3 tries in case of collisions from + // crappy random. + var tries = 3; + while (tries > 0) { + var dir_path = path.join( + tmp_dir, prefix + (Math.random() * 0x100000000 + 1).toString(36)); + try { + fs.mkdirSync(dir_path, 0755); + return dir_path; + } catch (err) { + tries--; + } + } + throw new Error("failed to make tempory directory in " + tmp_dir); } };