Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
18db2af
buffer: faster case for create buffer from empty string
JacksonTian Dec 18, 2015
6e4d9b3
build: enable compilation for linuxOne
mhdawson Mar 28, 2016
f56d6ad
build: add missing `openssl_fips%` to common.gypi
indutny Mar 27, 2016
09dc4cc
build: add script to create Android .mk files
robertchiras Mar 3, 2016
882fa25
build: add suport for x86 architecture
robertchiras Mar 3, 2016
459a7d6
child_process: refactor self=this in socket_list
benjamingr Mar 23, 2016
37f4df4
deps: backport 8d00c2c from v8 upstream
bnoordhuis Mar 6, 2016
648e0c3
dns: Use object without protoype for map
benjamingr Mar 22, 2016
4916fff
dns: Refactor forEach to map
benjamingr Mar 22, 2016
715ba18
doc: fix doc for Buffer.readInt32LE()
ghaiklor Mar 24, 2016
0da59ef
doc: add instructions to only sign a release
Fishrock123 Mar 23, 2016
b9682af
doc: fix order of end tags of list after heading
Mar 3, 2016
fc6513d
doc: use consistent event name parameter
benjamingr Mar 22, 2016
93638e1
doc: explain path.format expected properties
eversojk Mar 19, 2016
9e5fe2b
doc: typo: interal->internal.
kosak Mar 22, 2016
8df627e
etw: fix descriptors of events 9 and 23
joaocgreis Mar 16, 2016
1490a45
fs: add the fs.mkdtemp() function.
ralt Feb 24, 2016
f275176
http: speed up checkIsHttpToken
JacksonTian Jan 21, 2016
1d4c751
zlib: Fix handling of gzip magic bytes mid-file
addaleax Mar 23, 2016
5b5cb7e
win,build: build and test add-ons on test-ci
Mar 24, 2016
dddd365
tools: fix json doc generation
firedfox Mar 29, 2016
cebb8d7
timers: fixing API refs to use safe internal refs
getify Aug 22, 2015
cc85dd7
test: fix test-debugger-client.js
Trott Mar 22, 2016
ee8de3f
test: fix flaky test-http-set-timeout
Trott Mar 23, 2016
42903cc
test: move dns test to test/internet
bnoordhuis Mar 25, 2016
25244c1
test: fix flaky test-net-socket-timeout
mscdex Mar 24, 2016
f616adb
test: confirm globals not used internally
Trott Mar 24, 2016
5dc8df2
test: exclude new fs watch test for AIX
mhdawson Mar 28, 2016
5822f47
test: remove the use of curl in the test suite
santigimeno Mar 16, 2016
b8415ad
test: add test for piping large input from stdin
addaleax Mar 29, 2016
3f6fbaf
src: override v8 thread defaults using cli options
tomgco Dec 18, 2015
58de768
repl: support standalone blocks
princejwesley Mar 7, 2016
6ea6b64
querystring: don't stringify bad surrogate pair
mscdex Mar 23, 2016
d1483f0
net: emit host in lookup event
entertainyou Mar 8, 2016
8982d09
deps: upgrade npm to 3.8.3
othiym23 Mar 30, 2016
a7710b0
lib: refactor code with startsWith/endsWith
JacksonTian Mar 17, 2016
8bd81b2
src: Add missing `using v8::MaybeLocal`
addaleax Mar 31, 2016
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
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: #5333
Reviewed-By: Sakthipriyan Vairamani <thechargingvolcano@gmail.com>
Reviewed-By: Trevor Norris <trev.norris@gmail.com>
Reviewed-By: Saúl Ibarra Corretgé <saghul@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
  • Loading branch information
ralt authored and evanlucas committed Mar 30, 2016
commit 1490a4517fb4ad22b6c448e158b928d0cbdf698c
24 changes: 24 additions & 0 deletions doc/api/fs.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -532,6 +532,30 @@ to the completion callback. `mode` defaults to `0o777`.

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)

Asynchronous file open. See open(2). `flags` can be:
Expand Down
21 changes: 21 additions & 0 deletions lib/fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -2078,3 +2078,24 @@ SyncWriteStream.prototype.destroy = function() {
};

SyncWriteStream.prototype.destroySoon = SyncWriteStream.prototype.destroy;

fs.mkdtemp = function(prefix, callback) {
if (typeof callback !== 'function') {
throw new TypeError('"callback" argument must be a function');
}

if (!nullCheck(prefix, callback)) {
return;
}

var req = new FSReqWrap();
req.oncomplete = callback;

binding.mkdtemp(prefix + 'XXXXXX', req);
};

fs.mkdtempSync = function(prefix) {
nullCheck(prefix);

return binding.mkdtemp(prefix + 'XXXXXX');
};
26 changes: 26 additions & 0 deletions src/node_file.cc
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,11 @@ static void After(uv_fs_t *req) {
static_cast<const uv_stat_t*>(req->ptr));
break;

case UV_FS_MKDTEMP:
argv[1] = String::NewFromUtf8(env->isolate(),
static_cast<const char*>(req->path));
break;

case UV_FS_READLINK:
argv[1] = String::NewFromUtf8(env->isolate(),
static_cast<const char*>(req->ptr));
Expand Down Expand Up @@ -1291,6 +1296,25 @@ static void FUTimes(const FunctionCallbackInfo<Value>& args) {
}
}

static void Mkdtemp(const FunctionCallbackInfo<Value>& args) {
Environment* env = Environment::GetCurrent(args);

if (args.Length() < 1)
return TYPE_ERROR("template is required");
if (!args[0]->IsString())
return TYPE_ERROR("template must be a string");

node::Utf8Value tmpl(env->isolate(), args[0]);

if (args[1]->IsObject()) {
ASYNC_CALL(mkdtemp, args[1], *tmpl);
} else {
SYNC_CALL(mkdtemp, *tmpl, *tmpl);
args.GetReturnValue().Set(String::NewFromUtf8(env->isolate(),
SYNC_REQ.path));
}
}

void FSInitialize(const FunctionCallbackInfo<Value>& args) {
Local<Function> stats_constructor = args[0].As<Function>();
CHECK(stats_constructor->IsFunction());
Expand Down Expand Up @@ -1344,6 +1368,8 @@ void InitFs(Local<Object> target,
env->SetMethod(target, "utimes", UTimes);
env->SetMethod(target, "futimes", FUTimes);

env->SetMethod(target, "mkdtemp", Mkdtemp);

StatWatcher::Initialize(env, target);

// Create FunctionTemplate for FSReqWrap
Expand Down
27 changes: 27 additions & 0 deletions test/parallel/test-fs-mkdtemp.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
'use strict';

const common = require('../common');
const assert = require('assert');
const fs = require('fs');
const path = require('path');
const Buffer = require('buffer').Buffer;

common.refreshTmpDir();

const tmpFolder = fs.mkdtempSync(path.join(common.tmpDir, 'foo.'));

assert(path.basename(tmpFolder).length === 'foo.XXXXXX'.length);
assert(common.fileExists(tmpFolder));

const utf8 = fs.mkdtempSync(path.join(common.tmpDir, '\u0222abc.'));
assert.equal(Buffer.byteLength(path.basename(utf8)),
Buffer.byteLength('\u0222abc.XXXXXX'));
assert(common.fileExists(utf8));

fs.mkdtemp(
path.join(common.tmpDir, 'bar.'),
common.mustCall(function(err, folder) {
assert.ifError(err);
assert(common.fileExists(folder));
})
);