Skip to content

Commit a611c98

Browse files
committed
fs: move ToNamespacedPath to c++
1 parent 0b67673 commit a611c98

File tree

8 files changed

+259
-195
lines changed

8 files changed

+259
-195
lines changed

lib/fs.js

+99-132
Large diffs are not rendered by default.

lib/internal/fs/promises.js

+50-58
Original file line numberDiff line numberDiff line change
@@ -597,30 +597,28 @@ async function readFileHandle(filehandle, options) {
597597
// All of the functions are defined as async in order to ensure that errors
598598
// thrown cause promise rejections rather than being thrown synchronously.
599599
async function access(path, mode = F_OK) {
600-
path = getValidatedPath(path);
601-
602600
return await PromisePrototypeThen(
603-
binding.access(pathModule.toNamespacedPath(path), mode, kUsePromises),
601+
binding.access(getValidatedPath(path), mode, kUsePromises),
604602
undefined,
605603
handleErrorFromBinding,
606604
);
607605
}
608606

609607
async function cp(src, dest, options) {
610608
options = validateCpOptions(options);
611-
src = pathModule.toNamespacedPath(getValidatedPath(src, 'src'));
612-
dest = pathModule.toNamespacedPath(getValidatedPath(dest, 'dest'));
609+
src = getValidatedPath(src, 'src');
610+
dest = getValidatedPath(dest, 'dest');
613611
return lazyLoadCpPromises()(src, dest, options);
614612
}
615613

616614
async function copyFile(src, dest, mode) {
617-
src = getValidatedPath(src, 'src');
618-
dest = getValidatedPath(dest, 'dest');
619615
return await PromisePrototypeThen(
620-
binding.copyFile(pathModule.toNamespacedPath(src),
621-
pathModule.toNamespacedPath(dest),
622-
mode,
623-
kUsePromises),
616+
binding.copyFile(
617+
getValidatedPath(src, 'src'),
618+
getValidatedPath(dest, 'dest'),
619+
mode,
620+
kUsePromises,
621+
),
624622
undefined,
625623
handleErrorFromBinding,
626624
);
@@ -633,8 +631,7 @@ async function open(path, flags, mode) {
633631
const flagsNumber = stringToFlags(flags);
634632
mode = parseFileMode(mode, 'mode', 0o666);
635633
return new FileHandle(await PromisePrototypeThen(
636-
binding.openFileHandle(pathModule.toNamespacedPath(path),
637-
flagsNumber, mode, kUsePromises),
634+
binding.openFileHandle(path, flagsNumber, mode, kUsePromises),
638635
undefined,
639636
handleErrorFromBinding,
640637
));
@@ -779,9 +776,7 @@ async function rename(oldPath, newPath) {
779776
oldPath = getValidatedPath(oldPath, 'oldPath');
780777
newPath = getValidatedPath(newPath, 'newPath');
781778
return await PromisePrototypeThen(
782-
binding.rename(pathModule.toNamespacedPath(oldPath),
783-
pathModule.toNamespacedPath(newPath),
784-
kUsePromises),
779+
binding.rename(oldPath, newPath, kUsePromises),
785780
undefined,
786781
handleErrorFromBinding,
787782
);
@@ -803,13 +798,13 @@ async function ftruncate(handle, len = 0) {
803798
}
804799

805800
async function rm(path, options) {
806-
path = pathModule.toNamespacedPath(getValidatedPath(path));
801+
path = getValidatedPath(path);
807802
options = await validateRmOptionsPromise(path, options, false);
808803
return lazyRimRaf()(path, options);
809804
}
810805

811806
async function rmdir(path, options) {
812-
path = pathModule.toNamespacedPath(getValidatedPath(path));
807+
path = getValidatedPath(path);
813808
options = validateRmdirOptions(options);
814809

815810
if (options.recursive) {
@@ -855,9 +850,12 @@ async function mkdir(path, options) {
855850
validateBoolean(recursive, 'options.recursive');
856851

857852
return await PromisePrototypeThen(
858-
binding.mkdir(pathModule.toNamespacedPath(path),
859-
parseFileMode(mode, 'mode', 0o777), recursive,
860-
kUsePromises),
853+
binding.mkdir(
854+
path,
855+
parseFileMode(mode, 'mode', 0o777),
856+
recursive,
857+
kUsePromises,
858+
),
861859
undefined,
862860
handleErrorFromBinding,
863861
);
@@ -870,7 +868,7 @@ async function readdirRecursive(originalPath, options) {
870868
originalPath,
871869
await PromisePrototypeThen(
872870
binding.readdir(
873-
pathModule.toNamespacedPath(originalPath),
871+
originalPath,
874872
options.encoding,
875873
!!options.withFileTypes,
876874
kUsePromises,
@@ -921,7 +919,7 @@ async function readdirRecursive(originalPath, options) {
921919
direntPath,
922920
await PromisePrototypeThen(
923921
binding.readdir(
924-
pathModule.toNamespacedPath(direntPath),
922+
direntPath,
925923
options.encoding,
926924
false,
927925
kUsePromises,
@@ -946,7 +944,7 @@ async function readdir(path, options) {
946944
}
947945
const result = await PromisePrototypeThen(
948946
binding.readdir(
949-
pathModule.toNamespacedPath(path),
947+
path,
950948
options.encoding,
951949
!!options.withFileTypes,
952950
kUsePromises,
@@ -963,8 +961,7 @@ async function readlink(path, options) {
963961
options = getOptions(options);
964962
path = getValidatedPath(path, 'oldPath');
965963
return await PromisePrototypeThen(
966-
binding.readlink(pathModule.toNamespacedPath(path),
967-
options.encoding, kUsePromises),
964+
binding.readlink(path, options.encoding, kUsePromises),
968965
undefined,
969966
handleErrorFromBinding,
970967
);
@@ -993,10 +990,12 @@ async function symlink(target, path, type_) {
993990
target = getValidatedPath(target, 'target');
994991
path = getValidatedPath(path);
995992
return await PromisePrototypeThen(
996-
binding.symlink(preprocessSymlinkDestination(target, type, path),
997-
pathModule.toNamespacedPath(path),
998-
stringToSymlinkType(type),
999-
kUsePromises),
993+
binding.symlink(
994+
preprocessSymlinkDestination(target, type, path),
995+
path,
996+
stringToSymlinkType(type),
997+
kUsePromises,
998+
),
1000999
undefined,
10011000
handleErrorFromBinding,
10021001
);
@@ -1012,32 +1011,26 @@ async function fstat(handle, options = { bigint: false }) {
10121011
}
10131012

10141013
async function lstat(path, options = { bigint: false }) {
1015-
path = getValidatedPath(path);
10161014
const result = await PromisePrototypeThen(
1017-
binding.lstat(pathModule.toNamespacedPath(path),
1018-
options.bigint, kUsePromises),
1015+
binding.lstat(getValidatedPath(path), options.bigint, kUsePromises),
10191016
undefined,
10201017
handleErrorFromBinding,
10211018
);
10221019
return getStatsFromBinding(result);
10231020
}
10241021

10251022
async function stat(path, options = { bigint: false }) {
1026-
path = getValidatedPath(path);
10271023
const result = await PromisePrototypeThen(
1028-
binding.stat(pathModule.toNamespacedPath(path),
1029-
options.bigint, kUsePromises),
1024+
binding.stat(getValidatedPath(path), options.bigint, kUsePromises),
10301025
undefined,
10311026
handleErrorFromBinding,
10321027
);
10331028
return getStatsFromBinding(result);
10341029
}
10351030

10361031
async function statfs(path, options = { bigint: false }) {
1037-
path = getValidatedPath(path);
10381032
const result = await PromisePrototypeThen(
1039-
binding.statfs(pathModule.toNamespacedPath(path),
1040-
options.bigint, kUsePromises),
1033+
binding.statfs(path, options.bigint, kUsePromises),
10411034
undefined,
10421035
handleErrorFromBinding,
10431036
);
@@ -1048,18 +1041,15 @@ async function link(existingPath, newPath) {
10481041
existingPath = getValidatedPath(existingPath, 'existingPath');
10491042
newPath = getValidatedPath(newPath, 'newPath');
10501043
return await PromisePrototypeThen(
1051-
binding.link(pathModule.toNamespacedPath(existingPath),
1052-
pathModule.toNamespacedPath(newPath),
1053-
kUsePromises),
1044+
binding.link(existingPath, newPath, kUsePromises),
10541045
undefined,
10551046
handleErrorFromBinding,
10561047
);
10571048
}
10581049

10591050
async function unlink(path) {
1060-
path = getValidatedPath(path);
10611051
return await PromisePrototypeThen(
1062-
binding.unlink(pathModule.toNamespacedPath(path), kUsePromises),
1052+
binding.unlink(getValidatedPath(path), kUsePromises),
10631053
undefined,
10641054
handleErrorFromBinding,
10651055
);
@@ -1078,7 +1068,7 @@ async function chmod(path, mode) {
10781068
path = getValidatedPath(path);
10791069
mode = parseFileMode(mode, 'mode');
10801070
return await PromisePrototypeThen(
1081-
binding.chmod(pathModule.toNamespacedPath(path), mode, kUsePromises),
1071+
binding.chmod(path, mode, kUsePromises),
10821072
undefined,
10831073
handleErrorFromBinding,
10841074
);
@@ -1097,7 +1087,7 @@ async function lchown(path, uid, gid) {
10971087
validateInteger(uid, 'uid', -1, kMaxUserId);
10981088
validateInteger(gid, 'gid', -1, kMaxUserId);
10991089
return await PromisePrototypeThen(
1100-
binding.lchown(pathModule.toNamespacedPath(path), uid, gid, kUsePromises),
1090+
binding.lchown(path, uid, gid, kUsePromises),
11011091
undefined,
11021092
handleErrorFromBinding,
11031093
);
@@ -1118,7 +1108,7 @@ async function chown(path, uid, gid) {
11181108
validateInteger(uid, 'uid', -1, kMaxUserId);
11191109
validateInteger(gid, 'gid', -1, kMaxUserId);
11201110
return await PromisePrototypeThen(
1121-
binding.chown(pathModule.toNamespacedPath(path), uid, gid, kUsePromises),
1111+
binding.chown(path, uid, gid, kUsePromises),
11221112
undefined,
11231113
handleErrorFromBinding,
11241114
);
@@ -1127,10 +1117,12 @@ async function chown(path, uid, gid) {
11271117
async function utimes(path, atime, mtime) {
11281118
path = getValidatedPath(path);
11291119
return await PromisePrototypeThen(
1130-
binding.utimes(pathModule.toNamespacedPath(path),
1131-
toUnixTimestamp(atime),
1132-
toUnixTimestamp(mtime),
1133-
kUsePromises),
1120+
binding.utimes(
1121+
path,
1122+
toUnixTimestamp(atime),
1123+
toUnixTimestamp(mtime),
1124+
kUsePromises,
1125+
),
11341126
undefined,
11351127
handleErrorFromBinding,
11361128
);
@@ -1147,22 +1139,22 @@ async function futimes(handle, atime, mtime) {
11471139
}
11481140

11491141
async function lutimes(path, atime, mtime) {
1150-
path = getValidatedPath(path);
11511142
return await PromisePrototypeThen(
1152-
binding.lutimes(pathModule.toNamespacedPath(path),
1153-
toUnixTimestamp(atime),
1154-
toUnixTimestamp(mtime),
1155-
kUsePromises),
1143+
binding.lutimes(
1144+
getValidatedPath(path),
1145+
toUnixTimestamp(atime),
1146+
toUnixTimestamp(mtime),
1147+
kUsePromises,
1148+
),
11561149
undefined,
11571150
handleErrorFromBinding,
11581151
);
11591152
}
11601153

11611154
async function realpath(path, options) {
11621155
options = getOptions(options);
1163-
path = getValidatedPath(path);
11641156
return await PromisePrototypeThen(
1165-
binding.realpath(pathModule.toNamespacedPath(path), options.encoding, kUsePromises),
1157+
binding.realpath(getValidatedPath(path), options.encoding, kUsePromises),
11661158
undefined,
11671159
handleErrorFromBinding,
11681160
);

src/node_blob.cc

+2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include "node_errors.h"
99
#include "node_external_reference.h"
1010
#include "node_file.h"
11+
#include "path.h"
1112
#include "permission/permission.h"
1213
#include "util.h"
1314
#include "v8.h"
@@ -96,6 +97,7 @@ void BlobFromFilePath(const FunctionCallbackInfo<Value>& args) {
9697
Environment* env = Environment::GetCurrent(args);
9798
BufferValue path(env->isolate(), args[0]);
9899
CHECK_NOT_NULL(*path);
100+
ToNamespacedPath(env, &path);
99101
THROW_IF_INSUFFICIENT_PERMISSIONS(
100102
env, permission::PermissionScope::kFileSystemRead, path.ToStringView());
101103
auto entry = DataQueue::CreateFdEntry(env, args[0]);

0 commit comments

Comments
 (0)