From 2aad1643b2aa39afb0721d21a241f6619a1ddc7e Mon Sep 17 00:00:00 2001 From: James M Snell Date: Mon, 16 May 2016 21:40:17 -0700 Subject: [PATCH 1/2] doc: clarify fs.mkdtemp prefix argument Per: https://github.com/nodejs/node/issues/6142 Clarify the prefix argument. Fixes: https://github.com/nodejs/node/issues/6142 --- doc/api/fs.md | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/doc/api/fs.md b/doc/api/fs.md index f3c74d388528ee..91fb96f61867b6 100644 --- a/doc/api/fs.md +++ b/doc/api/fs.md @@ -728,6 +728,37 @@ fs.mkdtemp('/tmp/foo-', (err, folder) => { }); ``` +*Note*: the `fs.mkdtemp()` method will append the six randomly selected +characters directly to the `prefix` string. For instance, given a directory +`/tmp`, if the intention is to create a temporary directory *within* `/tmp`, +the `prefix` *must* end with a trailing platform-specific path separator +(`require('path').sep`). + +```js +// The parent directory for the new temporary directory +const tmp_dir = '/tmp'; + +// This method is *INCORRECT*: +fs.mkdtemp(tmp_dir, (err, folder) => { + if (err) throw err; + console.log(folder); + // Will print something similar to `/tmp-abc123`. + // Note that a new temporary directory is created + // at the file system root rather than *within* + // the /tmp directory. +}); + +// This method is *CORRECT*: +const path = require('path'); +fs.mkdtemp(tmp_dir + path.sep, (err, folder) => { + if (err) throw err; + console.log(folder); + // Will print something similar to `/tmp/abc123`. + // A new temporary directory is created within + // the /tmp directory. +}); +``` + ## fs.mkdtempSync(template) The synchronous version of [`fs.mkdtemp()`][]. Returns the created From 37e5a8f76f3f476aef36f628780bd5f92b594df0 Mon Sep 17 00:00:00 2001 From: James M Snell Date: Tue, 17 May 2016 08:17:43 -0700 Subject: [PATCH 2/2] Fix nits --- doc/api/fs.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/doc/api/fs.md b/doc/api/fs.md index 91fb96f61867b6..34c26a29f91dc5 100644 --- a/doc/api/fs.md +++ b/doc/api/fs.md @@ -728,7 +728,7 @@ fs.mkdtemp('/tmp/foo-', (err, folder) => { }); ``` -*Note*: the `fs.mkdtemp()` method will append the six randomly selected +*Note*: The `fs.mkdtemp()` method will append the six randomly selected characters directly to the `prefix` string. For instance, given a directory `/tmp`, if the intention is to create a temporary directory *within* `/tmp`, the `prefix` *must* end with a trailing platform-specific path separator @@ -736,10 +736,10 @@ the `prefix` *must* end with a trailing platform-specific path separator ```js // The parent directory for the new temporary directory -const tmp_dir = '/tmp'; +const tmpDir = '/tmp'; // This method is *INCORRECT*: -fs.mkdtemp(tmp_dir, (err, folder) => { +fs.mkdtemp(tmpDir, (err, folder) => { if (err) throw err; console.log(folder); // Will print something similar to `/tmp-abc123`. @@ -750,7 +750,7 @@ fs.mkdtemp(tmp_dir, (err, folder) => { // This method is *CORRECT*: const path = require('path'); -fs.mkdtemp(tmp_dir + path.sep, (err, folder) => { +fs.mkdtemp(tmpDir + path.sep, (err, folder) => { if (err) throw err; console.log(folder); // Will print something similar to `/tmp/abc123`.