Skip to content

Commit 8611e3b

Browse files
bnoordhuisevanlucas
authored andcommitted
fs: expose realpath(3) bindings
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 <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Jeremiah Senkpiel <[email protected]> Reviewed-By: Minwoo Jung <[email protected]> Reviewed-By: Trevor Norris <[email protected]>
1 parent 2266caf commit 8611e3b

File tree

4 files changed

+110
-87
lines changed

4 files changed

+110
-87
lines changed

lib/fs.js

+20
Original file line numberDiff line numberDiff line change
@@ -1722,6 +1722,14 @@ fs.realpathSync = function realpathSync(p, options) {
17221722
};
17231723

17241724

1725+
fs.realpathSync.native = function(path, options) {
1726+
options = getOptions(options, {});
1727+
handleError((path = getPathFromURL(path)));
1728+
nullCheck(path);
1729+
return binding.realpath(path, options.encoding);
1730+
};
1731+
1732+
17251733
fs.realpath = function realpath(p, options, callback) {
17261734
callback = maybeCallback(typeof options === 'function' ? options : callback);
17271735
if (!options)
@@ -1858,6 +1866,18 @@ fs.realpath = function realpath(p, options, callback) {
18581866
}
18591867
};
18601868

1869+
1870+
fs.realpath.native = function(path, options, callback) {
1871+
callback = maybeCallback(callback || options);
1872+
options = getOptions(options, {});
1873+
if (handleError((path = getPathFromURL(path)), callback)) return;
1874+
if (!nullCheck(path, callback)) return;
1875+
const req = new FSReqWrap();
1876+
req.oncomplete = callback;
1877+
return binding.realpath(path, options.encoding, req);
1878+
};
1879+
1880+
18611881
fs.mkdtemp = function(prefix, options, callback) {
18621882
callback = makeCallback(typeof options === 'function' ? options : callback);
18631883
options = getOptions(options, {});

src/node_file.cc

+3-12
Original file line numberDiff line numberDiff line change
@@ -827,24 +827,15 @@ static void MKDir(const FunctionCallbackInfo<Value>& args) {
827827
}
828828

829829
static void RealPath(const FunctionCallbackInfo<Value>& args) {
830+
CHECK_GE(args.Length(), 2);
830831
Environment* env = Environment::GetCurrent(args);
831-
832-
const int argc = args.Length();
833-
834-
if (argc < 1)
835-
return TYPE_ERROR("path required");
836-
837832
BufferValue path(env->isolate(), args[0]);
838833
ASSERT_PATH(path)
839834

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

842-
Local<Value> callback = Null(env->isolate());
843-
if (argc == 3)
844-
callback = args[2];
845-
846-
if (callback->IsObject()) {
847-
ASYNC_CALL(realpath, callback, encoding, *path);
837+
if (args[2]->IsObject()) {
838+
ASYNC_CALL(realpath, args[2], encoding, *path);
848839
} else {
849840
SYNC_CALL(realpath, *path, *path);
850841
const char* link_path = static_cast<const char*>(SYNC_REQ.ptr);
+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
'use strict';
2+
const common = require('../common');
3+
const assert = require('assert');
4+
const fs = require('fs');
5+
6+
if (!common.isOSX) common.skip('MacOS-only test.');
7+
8+
assert.strictEqual(fs.realpathSync.native('/users'), '/Users');
9+
fs.realpath.native('/users', common.mustCall((err, res) => {
10+
assert.ifError(err);
11+
assert.strictEqual(res, '/Users');
12+
}));

0 commit comments

Comments
 (0)