Skip to content

Commit

Permalink
fs: expose realpath(3) bindings
Browse files Browse the repository at this point in the history
Make the `uv_fs_realpath()` binding (which calls the libc `realpath()`
on UNIX and `GetFinalPathNameByHandle()` on Windows) available as the
`fs.realpath.native()` and `fs.realpathSync.native()` functions.

The binding was already available as `process.binding('fs').realpath`
but was not exposed or tested - and partly broken as a result.

Fixes: #8715
PR-URL: #15776
Refs: #7899
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Jeremiah Senkpiel <fishrock123@rocketmail.com>
Reviewed-By: Minwoo Jung <minwoo@nodesource.com>
Reviewed-By: Trevor Norris <trev.norris@gmail.com>
  • Loading branch information
bnoordhuis authored and evanlucas committed Nov 13, 2017
1 parent 2266caf commit 8611e3b
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 87 deletions.
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

0 comments on commit 8611e3b

Please sign in to comment.