Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
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
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
  • Loading branch information
ralt committed Mar 1, 2016
commit 5f405498380105544d867f832288a89a6f219110
21 changes: 21 additions & 0 deletions lib/fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -2138,3 +2138,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));
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jasnell Are we standardizing fs module to return utf8 encoded or one-byte encoded?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jasnell or would this run under your PR to allow an option to be passed to return a buffer instead of a string? guess either way it would return utf8 by default.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes and yes. If this lands first, I'll update #5616 to make this work that way. If #5616 lands first, this would need to be modified to work consistently.

}
}

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