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: restore javascript realpath implementation #7899

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from 6 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
46 changes: 46 additions & 0 deletions benchmark/fs/bench-realpath.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
'use strict';

const common = require('../common');
const fs = require('fs');
const path = require('path');
const resolved_path = path.resolve(__dirname, '../../lib/');
const relative_path = path.relative(__dirname, '../../lib/');

const bench = common.createBenchmark(main, {
n: [1e4],
type: ['relative', 'resolved'],
});


function main(conf) {
const n = conf.n >>> 0;
const type = conf.type;

bench.start();
if (type === 'relative')
relativePath(n);
else if (type === 'resolved')
resolvedPath(n);
else
throw new Error('unknown "type": ' + type);
}

function relativePath(n) {
(function r(cntr) {
if (--cntr <= 0)
return bench.end(n);
fs.realpath(relative_path, function() {
r(cntr);
});
}(n));
}

function resolvedPath(n) {
(function r(cntr) {
if (--cntr <= 0)
return bench.end(n);
fs.realpath(resolved_path, function() {
r(cntr);
});
}(n));
}
39 changes: 39 additions & 0 deletions benchmark/fs/bench-realpathSync.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
'use strict';

const common = require('../common');
const fs = require('fs');
const path = require('path');
const resolved_path = path.resolve(__dirname, '../../lib/');
const relative_path = path.relative(__dirname, '../../lib/');

const bench = common.createBenchmark(main, {
n: [1e4],
type: ['relative', 'resolved'],
});


function main(conf) {
const n = conf.n >>> 0;
const type = conf.type;

bench.start();
if (type === 'relative')
relativePath(n);
else if (type === 'resolved')
resolvedPath(n);
else
throw new Error('unknown "type": ' + type);
bench.end(n);
}

function relativePath(n) {
for (var i = 0; i < n; i++) {
fs.realpathSync(relative_path);
}
}

function resolvedPath(n) {
for (var i = 0; i < n; i++) {
fs.realpathSync(resolved_path);
}
}
8 changes: 6 additions & 2 deletions doc/api/fs.md
Original file line number Diff line number Diff line change
Expand Up @@ -1213,6 +1213,8 @@ added: v0.1.31
Asynchronous realpath(3). The `callback` gets two arguments `(err,
resolvedPath)`. May use `process.cwd` to resolve relative paths.

Only paths that can be converted to UTF8 strings are supported.

The optional `options` argument can be a string specifying an encoding, or an
object with an `encoding` property specifying the character encoding to use for
the path passed to the callback. If the `encoding` is set to `'buffer'`,
Expand All @@ -1229,10 +1231,12 @@ added: v0.1.31

Synchronous realpath(3). Returns the resolved path.

Only paths that can be converted to UTF8 strings are supported.

The optional `options` argument can be a string specifying an encoding, or an
object with an `encoding` property specifying the character encoding to use for
the path passed to the callback. If the `encoding` is set to `'buffer'`,
the path returned will be passed as a `Buffer` object.
the returned value. If the `encoding` is set to `'buffer'`, the path returned
will be passed as a `Buffer` object.

## fs.rename(oldPath, newPath, callback)
<!-- YAML
Expand Down
225 changes: 213 additions & 12 deletions lib/fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -1563,38 +1563,239 @@ fs.unwatchFile = function(filename, listener) {
};


fs.realpathSync = function realpathSync(path, options) {
// Regexp that finds the next partion of a (partial) path
Copy link
Member

Choose a reason for hiding this comment

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

typo: partion

Copy link
Contributor Author

Choose a reason for hiding this comment

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

fixed

// result is [base_with_slash, base], e.g. ['somedir/', 'somedir']
const nextPartRe = isWindows ?
/(.*?)(?:[\/\\]+|$)/g :
/(.*?)(?:[\/]+|$)/g;

// Regex to find the device root, including trailing slash. E.g. 'c:\\'.
const splitRootRe = isWindows ?
/^(?:[a-zA-Z]:|[\\\/]{2}[^\\\/]+[\\\/][^\\\/]+)?[\\\/]*/ :
/^[\/]*/;

function encodeRealpathResult(result, options, err) {
if (!options || !options.encoding || options.encoding === 'utf8' || err)
return result;
const asBuffer = Buffer.from(result);
if (options.encoding === 'buffer') {
return asBuffer;
} else {
return asBuffer.toString(options.encoding);
}
}

fs.realpathSync = function realpathSync(p, options) {
if (!options)
options = {};
else if (typeof options === 'string')
options = {encoding: options};
else if (typeof options !== 'object')
throw new TypeError('"options" must be a string or an object');
nullCheck(path);
return binding.realpath(pathModule._makeLong(path), options.encoding);
nullCheck(p);

p = p.toString('utf8');
p = pathModule.resolve(p);

const seenLinks = {};
const knownHard = {};

// current character position in p
var pos;
// the partial path so far, including a trailing slash if any
var current;
// the partial path without a trailing slash (except when pointing at a root)
var base;
// the partial path scanned in the previous round, with slash
var previous;

start();

function start() {
// Skip over roots
var m = splitRootRe.exec(p);
pos = m[0].length;
current = m[0];
base = m[0];
previous = '';

// On windows, check that the root exists. On unix there is no need.
if (isWindows && !knownHard[base]) {
fs.lstatSync(base);
knownHard[base] = true;
}
}

// walk down the path, swapping out linked pathparts for their real
// values
// NB: p.length changes.
while (pos < p.length) {
// find the next part
nextPartRe.lastIndex = pos;
var result = nextPartRe.exec(p);
previous = current;
current += result[0];
base = previous + result[1];
pos = nextPartRe.lastIndex;

// continue if not a symlink
if (knownHard[base]) {
continue;
}

var resolvedLink;
var stat = fs.lstatSync(base);
if (!stat.isSymbolicLink()) {
knownHard[base] = true;
continue;
}

// read the link if it wasn't read before
// dev/ino always return 0 on windows, so skip the check.
var linkTarget = null;
if (!isWindows) {
var id = stat.dev.toString(32) + ':' + stat.ino.toString(32);
if (seenLinks.hasOwnProperty(id)) {
linkTarget = seenLinks[id];
}
}
if (linkTarget === null) {
fs.statSync(base);
linkTarget = fs.readlinkSync(base);
}
resolvedLink = pathModule.resolve(previous, linkTarget);

if (!isWindows) seenLinks[id] = linkTarget;

// resolve the link, then start over
p = pathModule.resolve(resolvedLink, p.slice(pos));
start();
}

return encodeRealpathResult(p, options);
};


fs.realpath = function realpath(path, options, callback) {
fs.realpath = function realpath(p, options, callback) {
if (typeof callback !== 'function') {
callback = maybeCallback(options);
options = {};
}

if (!options) {
options = {};
} else if (typeof options === 'function') {
callback = options;
options = {};
} else if (typeof options === 'string') {
options = {encoding: options};
} else if (typeof options !== 'object') {
throw new TypeError('"options" must be a string or an object');
}
callback = makeCallback(callback);
if (!nullCheck(path, callback))
if (!nullCheck(p, callback))
return;
var req = new FSReqWrap();
req.oncomplete = callback;
binding.realpath(pathModule._makeLong(path), options.encoding, req);
return;
};

p = p.toString('utf8');
p = pathModule.resolve(p);

const seenLinks = {};
const knownHard = {};

// current character position in p
var pos;
// the partial path so far, including a trailing slash if any
var current;
// the partial path without a trailing slash (except when pointing at a root)
var base;
// the partial path scanned in the previous round, with slash
var previous;

start();

function start() {
// Skip over roots
var m = splitRootRe.exec(p);
pos = m[0].length;
current = m[0];
base = m[0];
previous = '';

// On windows, check that the root exists. On unix there is no need.
if (isWindows && !knownHard[base]) {
fs.lstat(base, function(err) {
if (err) return callback(err);
knownHard[base] = true;
LOOP();
});
} else {
process.nextTick(LOOP);
}
}

// walk down the path, swapping out linked pathparts for their real
// values
function LOOP() {
// stop if scanned past end of path
if (pos >= p.length) {
return callback(null, encodeRealpathResult(p, options));
}

// find the next part
nextPartRe.lastIndex = pos;
var result = nextPartRe.exec(p);
previous = current;
current += result[0];
base = previous + result[1];
pos = nextPartRe.lastIndex;

// continue if not a symlink
if (knownHard[base]) {
return process.nextTick(LOOP);
}

return fs.lstat(base, gotStat);
}

function gotStat(err, stat) {
if (err) return callback(err);

// if not a symlink, skip to the next path part
if (!stat.isSymbolicLink()) {
knownHard[base] = true;
return process.nextTick(LOOP);
}

// stat & read the link if not read before
// call gotTarget as soon as the link target is known
// dev/ino always return 0 on windows, so skip the check.
if (!isWindows) {
var id = stat.dev.toString(32) + ':' + stat.ino.toString(32);
if (seenLinks.hasOwnProperty(id)) {
return gotTarget(null, seenLinks[id], base);
}
}
fs.stat(base, function(err) {
if (err) return callback(err);

fs.readlink(base, function(err, target) {
if (!isWindows) seenLinks[id] = target;
gotTarget(err, target);
});
});
}

function gotTarget(err, target, base) {
if (err) return callback(err);

var resolvedLink = pathModule.resolve(previous, target);
gotResolvedLink(resolvedLink);
}

function gotResolvedLink(resolvedLink) {
// resolve the link, then start over
p = pathModule.resolve(resolvedLink, p.slice(pos));
start();
}
};

fs.mkdtemp = function(prefix, options, callback) {
if (!prefix || typeof prefix !== 'string')
Expand Down
Loading