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: refactor realpath with Map and Set #43569

Merged
merged 1 commit into from
Jun 30, 2022
Merged
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
44 changes: 22 additions & 22 deletions lib/fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,13 @@ const {
BigIntPrototypeToString,
MathMax,
Number,
ObjectCreate,
ObjectDefineProperties,
ObjectDefineProperty,
Promise,
ReflectApply,
RegExpPrototypeExec,
SafeMap,
SafeSet,
String,
StringPrototypeCharCodeAt,
StringPrototypeIndexOf,
Expand Down Expand Up @@ -2486,8 +2486,8 @@ function realpathSync(p, options) {
return maybeCachedResult;
}

const seenLinks = ObjectCreate(null);
const knownHard = ObjectCreate(null);
const seenLinks = new SafeMap();
const knownHard = new SafeSet();
const original = p;

// Current character position in p
Expand All @@ -2508,7 +2508,7 @@ function realpathSync(p, options) {
const ctx = { path: base };
binding.lstat(pathModule.toNamespacedPath(base), false, undefined, ctx);
handleErrorFromBinding(ctx);
knownHard[base] = true;
knownHard.add(base);
}

// Walk down the path, swapping out linked path parts for their real
Expand All @@ -2530,7 +2530,7 @@ function realpathSync(p, options) {
}

// Continue if not a symlink, break if a pipe/socket
if (knownHard[base] || cache?.get(base) === base) {
if (knownHard.has(base) || cache?.get(base) === base) {
if (isFileType(binding.statValues, S_IFIFO) ||
isFileType(binding.statValues, S_IFSOCK)) {
break;
Expand All @@ -2552,7 +2552,7 @@ function realpathSync(p, options) {
handleErrorFromBinding(ctx);

if (!isFileType(stats, S_IFLNK)) {
knownHard[base] = true;
knownHard.add(base);
cache?.set(base, base);
continue;
}
Expand All @@ -2565,8 +2565,8 @@ function realpathSync(p, options) {
const dev = BigIntPrototypeToString(stats[0], 32);
const ino = BigIntPrototypeToString(stats[7], 32);
id = `${dev}:${ino}`;
if (seenLinks[id]) {
linkTarget = seenLinks[id];
if (seenLinks.has(id)) {
linkTarget = seenLinks.get(id);
}
}
if (linkTarget === null) {
Expand All @@ -2579,7 +2579,7 @@ function realpathSync(p, options) {
resolvedLink = pathModule.resolve(previous, linkTarget);

cache?.set(base, resolvedLink);
if (!isWindows) seenLinks[id] = linkTarget;
if (!isWindows) seenLinks.set(id, linkTarget);
}

// Resolve the link, then start over
Expand All @@ -2590,11 +2590,11 @@ function realpathSync(p, options) {
pos = current.length;

// On windows, check that the root exists. On unix there is no need.
if (isWindows && !knownHard[base]) {
if (isWindows && !knownHard.has(base)) {
const ctx = { path: base };
binding.lstat(pathModule.toNamespacedPath(base), false, undefined, ctx);
handleErrorFromBinding(ctx);
knownHard[base] = true;
knownHard.add(base);
}
}

Expand Down Expand Up @@ -2639,8 +2639,8 @@ function realpath(p, options, callback) {
validatePath(p);
p = pathModule.resolve(p);

const seenLinks = ObjectCreate(null);
const knownHard = ObjectCreate(null);
const seenLinks = new SafeMap();
const knownHard = new SafeSet();

// Current character position in p
let pos;
Expand All @@ -2655,10 +2655,10 @@ function realpath(p, options, callback) {
pos = current.length;

// On windows, check that the root exists. On unix there is no need.
if (isWindows && !knownHard[base]) {
if (isWindows && !knownHard.has(base)) {
fs.lstat(base, (err, stats) => {
if (err) return callback(err);
knownHard[base] = true;
knownHard.add(base);
LOOP();
});
} else {
Expand Down Expand Up @@ -2688,7 +2688,7 @@ function realpath(p, options, callback) {
}

// Continue if not a symlink, break if a pipe/socket
if (knownHard[base]) {
if (knownHard.has(base)) {
if (isFileType(binding.statValues, S_IFIFO) ||
isFileType(binding.statValues, S_IFSOCK)) {
return callback(null, encodeRealpathResult(p, options));
Expand All @@ -2704,7 +2704,7 @@ function realpath(p, options, callback) {

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

Expand All @@ -2716,15 +2716,15 @@ function realpath(p, options, callback) {
const dev = BigIntPrototypeToString(stats.dev, 32);
const ino = BigIntPrototypeToString(stats.ino, 32);
id = `${dev}:${ino}`;
if (seenLinks[id]) {
return gotTarget(null, seenLinks[id]);
if (seenLinks.has(id)) {
return gotTarget(null, seenLinks.get(id));
}
}
fs.stat(base, (err) => {
if (err) return callback(err);

fs.readlink(base, (err, target) => {
if (!isWindows) seenLinks[id] = target;
if (!isWindows) seenLinks.set(id, target);
gotTarget(err, target);
});
});
Expand All @@ -2743,10 +2743,10 @@ function realpath(p, options, callback) {
pos = current.length;

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