From 2269d7db0fed862f92352bb09c2221d5c0429a65 Mon Sep 17 00:00:00 2001 From: Florian MARGAINE Date: Wed, 24 Feb 2016 22:17:44 +0100 Subject: [PATCH] fs: add the fs.mkdtemp() function. This uses libuv's mkdtemp function to provide a way to create a temporary folder, using a prefix as the path. The prefix is appended six random characters. The callback function will receive the name of the folder that was created. Usage example: fs.mkdtemp('/tmp/foo-', function(err, folder) { console.log(folder); // Prints: /tmp/foo-Tedi42 }); The fs.mkdtempSync version is also provided. Usage example: console.log(fs.mkdtemp('/tmp/foo-')); // Prints: tmp/foo-Tedi42 This pull request also includes the relevant documentation changes and tests. PR-URL: https://github.com/nodejs/node/pull/5333 Reviewed-By: Sakthipriyan Vairamani Reviewed-By: Trevor Norris --- doc/api/fs.md | 24 ++++++++++++++++++++++++ lib/fs.js | 21 +++++++++++++++++++++ src/node_file.cc | 26 ++++++++++++++++++++++++++ test/parallel/test-fs-mkdtemp.js | 27 +++++++++++++++++++++++++++ 4 files changed, 98 insertions(+) create mode 100644 test/parallel/test-fs-mkdtemp.js diff --git a/doc/api/fs.md b/doc/api/fs.md index d20024419cb247..8d934e98340d41 100644 --- a/doc/api/fs.md +++ b/doc/api/fs.md @@ -851,6 +851,30 @@ added: v0.1.21 Synchronous mkdir(2). Returns `undefined`. +## fs.mkdtemp(prefix, callback) + +Creates a unique temporary directory. + +Generates six random characters to be appended behind a required +`prefix` to create a unique temporary directory. + +The created folder path is passed as a string to the callback's second +parameter. + +Example: + +```js +fs.mkdtemp('/tmp/foo-', (err, folder) => { + console.log(folder); + // Prints: /tmp/foo-itXde2 +}); +``` + +## fs.mkdtempSync(template) + +The synchronous version of [`fs.mkdtemp()`][]. Returns the created +folder path. + ## fs.open(path, flags[, mode], callback)