Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fs: expose realpath(3) bindings #15776

Merged
merged 1 commit into from
Nov 9, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
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
20 changes: 20 additions & 0 deletions lib/fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -1722,6 +1722,14 @@ fs.realpathSync = function realpathSync(p, options) {
};


fs.realpathSync.native = function(path, options) {
options = getOptions(options, {});
handleError((path = getPathFromURL(path)));
nullCheck(path);
return binding.realpath(path, options.encoding);
};


fs.realpath = function realpath(p, options, callback) {
callback = maybeCallback(typeof options === 'function' ? options : callback);
if (!options)
Expand Down Expand Up @@ -1858,6 +1866,18 @@ fs.realpath = function realpath(p, options, callback) {
}
};


fs.realpath.native = function(path, options, callback) {
callback = maybeCallback(callback || options);
options = getOptions(options, {});
if (handleError((path = getPathFromURL(path)), callback)) return;
if (!nullCheck(path, callback)) return;
const req = new FSReqWrap();
req.oncomplete = callback;
return binding.realpath(path, options.encoding, req);
};


fs.mkdtemp = function(prefix, options, callback) {
callback = makeCallback(typeof options === 'function' ? options : callback);
options = getOptions(options, {});
Expand Down
15 changes: 3 additions & 12 deletions src/node_file.cc
Original file line number Diff line number Diff line change
Expand Up @@ -827,24 +827,15 @@ static void MKDir(const FunctionCallbackInfo<Value>& args) {
}

static void RealPath(const FunctionCallbackInfo<Value>& args) {
CHECK_GE(args.Length(), 2);
Environment* env = Environment::GetCurrent(args);

const int argc = args.Length();

if (argc < 1)
return TYPE_ERROR("path required");

BufferValue path(env->isolate(), args[0]);
ASSERT_PATH(path)

const enum encoding encoding = ParseEncoding(env->isolate(), args[1], UTF8);

Local<Value> callback = Null(env->isolate());
if (argc == 3)
callback = args[2];

if (callback->IsObject()) {
ASYNC_CALL(realpath, callback, encoding, *path);
if (args[2]->IsObject()) {
ASYNC_CALL(realpath, args[2], encoding, *path);
} else {
SYNC_CALL(realpath, *path, *path);
const char* link_path = static_cast<const char*>(SYNC_REQ.ptr);
Expand Down
12 changes: 12 additions & 0 deletions test/parallel/test-fs-realpath-native.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
'use strict';
const common = require('../common');
const assert = require('assert');
const fs = require('fs');

if (!common.isOSX) common.skip('MacOS-only test.');

assert.strictEqual(fs.realpathSync.native('/users'), '/Users');
fs.realpath.native('/users', common.mustCall((err, res) => {
assert.ifError(err);
assert.strictEqual(res, '/Users');
}));
Loading