From 181eca560ea6954295be2d3c4c7652fd904dc3d9 Mon Sep 17 00:00:00 2001 From: Antoine du Hamel Date: Sun, 3 Dec 2023 23:56:29 +0100 Subject: [PATCH] fixup! fs: introduce `dirent.parentPath` --- lib/fs.js | 2 +- lib/internal/fs/utils.js | 16 ++++++++-------- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/lib/fs.js b/lib/fs.js index 91ed441188f5f2..55e6373327c846 100644 --- a/lib/fs.js +++ b/lib/fs.js @@ -1431,7 +1431,7 @@ function readdirSyncRecursive(basePath, options) { const dirent = getDirent(path, readdirResult[0][i], readdirResult[1][i]); ArrayPrototypePush(readdirResults, dirent); if (dirent.isDirectory()) { - ArrayPrototypePush(pathsQueue, pathModule.join(dirent.parentPath, dirent.name)); + ArrayPrototypePush(pathsQueue, pathModule.join(dirent.path, dirent.name)); } } } else { diff --git a/lib/internal/fs/utils.js b/lib/internal/fs/utils.js index 4174beff00543d..061dd129a9f156 100644 --- a/lib/internal/fs/utils.js +++ b/lib/internal/fs/utils.js @@ -161,10 +161,10 @@ function assertEncoding(encoding) { } class Dirent { - constructor(name, type, path) { + constructor(name, type, path, filepath = path && join(path, name)) { this.name = name; this.parentPath = path; - this.path = pathModule.join(path, name); + this.path = filepath; this[kType] = type; } @@ -198,8 +198,8 @@ class Dirent { } class DirentFromStats extends Dirent { - constructor(name, stats, path) { - super(name, null, path); + constructor(name, stats, path, filepath) { + super(name, null, path, filepath); this[kStats] = stats; } } @@ -234,7 +234,7 @@ function join(path, name) { } if (typeof path === 'string' && typeof name === 'string') { - return pathModule.join(path, name); + return pathModule.basename(path) === name ? path : pathModule.join(path, name); } if (isUint8Array(path) && isUint8Array(name)) { @@ -269,7 +269,7 @@ function getDirents(path, { 0: names, 1: types }, callback) { callback(err); return; } - names[idx] = new DirentFromStats(name, stats, path); + names[idx] = new DirentFromStats(name, stats, path, filepath); if (--toFinish === 0) { callback(null, names); } @@ -305,7 +305,7 @@ function getDirent(path, name, type, callback) { callback(err); return; } - callback(null, new DirentFromStats(name, stats, filepath)); + callback(null, new DirentFromStats(name, stats, path, filepath)); }); } else { callback(null, new Dirent(name, type, path)); @@ -313,7 +313,7 @@ function getDirent(path, name, type, callback) { } else if (type === UV_DIRENT_UNKNOWN) { const filepath = join(path, name); const stats = lazyLoadFs().lstatSync(filepath); - return new DirentFromStats(name, stats, path); + return new DirentFromStats(name, stats, path, filepath); } else { return new Dirent(name, type, path); }