diff options
author | Florian MARGAINE <florian@margaine.com> | 2016-02-24 22:17:44 +0100 |
---|---|---|
committer | Benjamin Gruenbaum <inglor@gmail.com> | 2016-03-20 11:49:02 +0200 |
commit | e5f8a6a2fa10a1d30fa0bf56f2f67bc20e24f195 (patch) | |
tree | dc2454a30be48a365ef896f7b7bdc995384b8d1d /src/node_file.cc | |
parent | e6bfe044ff78d85541e4b85a3f1890e11d2e25c9 (diff) | |
download | android-node-v8-e5f8a6a2fa10a1d30fa0bf56f2f67bc20e24f195.tar.gz android-node-v8-e5f8a6a2fa10a1d30fa0bf56f2f67bc20e24f195.tar.bz2 android-node-v8-e5f8a6a2fa10a1d30fa0bf56f2f67bc20e24f195.zip |
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 <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>
Diffstat (limited to 'src/node_file.cc')
-rw-r--r-- | src/node_file.cc | 26 |
1 files changed, 26 insertions, 0 deletions
diff --git a/src/node_file.cc b/src/node_file.cc index 1a318ce075..9c2bea4897 100644 --- a/src/node_file.cc +++ b/src/node_file.cc @@ -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)); @@ -1294,6 +1299,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()); @@ -1347,6 +1371,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 |