mirror of
https://github.com/nodejs/node.git
synced 2026-11-30 11:55:44 -05:00
fs: refactor realpath with Map and Set
PR-URL: https://github.com/nodejs/node/pull/43569 Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de> Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com> Reviewed-By: Darshan Sen <raisinten@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
This commit is contained in:
1 parent
26e4dc83a8
commit
fcf74c53d8
1 file changed
+22
-22
@@ -33,13 +33,13 @@ const {
|
||||
BigIntPrototypeToString,
|
||||
MathMax,
|
||||
Number,
|
||||
ObjectCreate,
|
||||
ObjectDefineProperties,
|
||||
ObjectDefineProperty,
|
||||
Promise,
|
||||
ReflectApply,
|
||||
RegExpPrototypeExec,
|
||||
SafeMap,
|
||||
SafeSet,
|
||||
String,
|
||||
StringPrototypeCharCodeAt,
|
||||
StringPrototypeIndexOf,
|
||||
@@ -2472,8 +2472,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
|
||||
@@ -2494,7 +2494,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
|
||||
@@ -2516,7 +2516,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;
|
||||
@@ -2538,7 +2538,7 @@ function realpathSync(p, options) {
|
||||
handleErrorFromBinding(ctx);
|
||||
|
||||
if (!isFileType(stats, S_IFLNK)) {
|
||||
knownHard[base] = true;
|
||||
knownHard.add(base);
|
||||
cache?.set(base, base);
|
||||
continue;
|
||||
}
|
||||
@@ -2551,8 +2551,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) {
|
||||
@@ -2565,7 +2565,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
|
||||
@@ -2576,11 +2576,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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2625,8 +2625,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;
|
||||
@@ -2641,10 +2641,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 {
|
||||
@@ -2674,7 +2674,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));
|
||||
@@ -2690,7 +2690,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);
|
||||
}
|
||||
|
||||
@@ -2702,15 +2702,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);
|
||||
});
|
||||
});
|
||||
@@ -2729,10 +2729,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 {
|
||||
|
||||
Reference in new issue
Block a user