Skip to content

Commit 74e5916

Browse files
anonriglemire
authored andcommitted
fs: move ToNamespacedPath to c++
Co-Authored-By: Daniel Lemire <[email protected]> PR-URL: nodejs#52135 Reviewed-By: Geoffrey Booth <[email protected]> Reviewed-By: Vinícius Lourenço Claro Cardoso <[email protected]> Reviewed-By: Daniel Lemire <[email protected]> Reviewed-By: Rafael Gonzaga <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent ba99a3b commit 74e5916

File tree

10 files changed

+303
-231
lines changed

10 files changed

+303
-231
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
@@ -604,30 +604,28 @@ async function readFileHandle(filehandle, options) {
604604
// All of the functions are defined as async in order to ensure that errors
605605
// thrown cause promise rejections rather than being thrown synchronously.
606606
async function access(path, mode = F_OK) {
607-
path = getValidatedPath(path);
608-
609607
return await PromisePrototypeThen(
610-
binding.access(pathModule.toNamespacedPath(path), mode, kUsePromises),
608+
binding.access(getValidatedPath(path), mode, kUsePromises),
611609
undefined,
612610
handleErrorFromBinding,
613611
);
614612
}
615613

616614
async function cp(src, dest, options) {
617615
options = validateCpOptions(options);
618-
src = pathModule.toNamespacedPath(getValidatedPath(src, 'src'));
619-
dest = pathModule.toNamespacedPath(getValidatedPath(dest, 'dest'));
616+
src = getValidatedPath(src, 'src');
617+
dest = getValidatedPath(dest, 'dest');
620618
return lazyLoadCpPromises()(src, dest, options);
621619
}
622620

623621
async function copyFile(src, dest, mode) {
624-
src = getValidatedPath(src, 'src');
625-
dest = getValidatedPath(dest, 'dest');
626622
return await PromisePrototypeThen(
627-
binding.copyFile(pathModule.toNamespacedPath(src),
628-
pathModule.toNamespacedPath(dest),
629-
mode,
630-
kUsePromises),
623+
binding.copyFile(
624+
getValidatedPath(src, 'src'),
625+
getValidatedPath(dest, 'dest'),
626+
mode,
627+
kUsePromises,
628+
),
631629
undefined,
632630
handleErrorFromBinding,
633631
);
@@ -640,8 +638,7 @@ async function open(path, flags, mode) {
640638
const flagsNumber = stringToFlags(flags);
641639
mode = parseFileMode(mode, 'mode', 0o666);
642640
return new FileHandle(await PromisePrototypeThen(
643-
binding.openFileHandle(pathModule.toNamespacedPath(path),
644-
flagsNumber, mode, kUsePromises),
641+
binding.openFileHandle(path, flagsNumber, mode, kUsePromises),
645642
undefined,
646643
handleErrorFromBinding,
647644
));
@@ -786,9 +783,7 @@ async function rename(oldPath, newPath) {
786783
oldPath = getValidatedPath(oldPath, 'oldPath');
787784
newPath = getValidatedPath(newPath, 'newPath');
788785
return await PromisePrototypeThen(
789-
binding.rename(pathModule.toNamespacedPath(oldPath),
790-
pathModule.toNamespacedPath(newPath),
791-
kUsePromises),
786+
binding.rename(oldPath, newPath, kUsePromises),
792787
undefined,
793788
handleErrorFromBinding,
794789
);
@@ -810,13 +805,13 @@ async function ftruncate(handle, len = 0) {
810805
}
811806

812807
async function rm(path, options) {
813-
path = pathModule.toNamespacedPath(getValidatedPath(path));
808+
path = getValidatedPath(path);
814809
options = await validateRmOptionsPromise(path, options, false);
815810
return lazyRimRaf()(path, options);
816811
}
817812

818813
async function rmdir(path, options) {
819-
path = pathModule.toNamespacedPath(getValidatedPath(path));
814+
path = getValidatedPath(path);
820815
options = validateRmdirOptions(options);
821816

822817
if (options.recursive) {
@@ -862,9 +857,12 @@ async function mkdir(path, options) {
862857
validateBoolean(recursive, 'options.recursive');
863858

864859
return await PromisePrototypeThen(
865-
binding.mkdir(pathModule.toNamespacedPath(path),
866-
parseFileMode(mode, 'mode', 0o777), recursive,
867-
kUsePromises),
860+
binding.mkdir(
861+
path,
862+
parseFileMode(mode, 'mode', 0o777),
863+
recursive,
864+
kUsePromises,
865+
),
868866
undefined,
869867
handleErrorFromBinding,
870868
);
@@ -877,7 +875,7 @@ async function readdirRecursive(originalPath, options) {
877875
originalPath,
878876
await PromisePrototypeThen(
879877
binding.readdir(
880-
pathModule.toNamespacedPath(originalPath),
878+
originalPath,
881879
options.encoding,
882880
!!options.withFileTypes,
883881
kUsePromises,
@@ -928,7 +926,7 @@ async function readdirRecursive(originalPath, options) {
928926
direntPath,
929927
await PromisePrototypeThen(
930928
binding.readdir(
931-
pathModule.toNamespacedPath(direntPath),
929+
direntPath,
932930
options.encoding,
933931
false,
934932
kUsePromises,
@@ -953,7 +951,7 @@ async function readdir(path, options) {
953951
}
954952
const result = await PromisePrototypeThen(
955953
binding.readdir(
956-
pathModule.toNamespacedPath(path),
954+
path,
957955
options.encoding,
958956
!!options.withFileTypes,
959957
kUsePromises,
@@ -970,8 +968,7 @@ async function readlink(path, options) {
970968
options = getOptions(options);
971969
path = getValidatedPath(path, 'oldPath');
972970
return await PromisePrototypeThen(
973-
binding.readlink(pathModule.toNamespacedPath(path),
974-
options.encoding, kUsePromises),
971+
binding.readlink(path, options.encoding, kUsePromises),
975972
undefined,
976973
handleErrorFromBinding,
977974
);
@@ -1004,10 +1001,12 @@ async function symlink(target, path, type) {
10041001
target = getValidatedPath(target, 'target');
10051002
path = getValidatedPath(path);
10061003
return await PromisePrototypeThen(
1007-
binding.symlink(preprocessSymlinkDestination(target, type, path),
1008-
pathModule.toNamespacedPath(path),
1009-
stringToSymlinkType(type),
1010-
kUsePromises),
1004+
binding.symlink(
1005+
preprocessSymlinkDestination(target, type, path),
1006+
path,
1007+
stringToSymlinkType(type),
1008+
kUsePromises,
1009+
),
10111010
undefined,
10121011
handleErrorFromBinding,
10131012
);
@@ -1023,32 +1022,26 @@ async function fstat(handle, options = { bigint: false }) {
10231022
}
10241023

10251024
async function lstat(path, options = { bigint: false }) {
1026-
path = getValidatedPath(path);
10271025
const result = await PromisePrototypeThen(
1028-
binding.lstat(pathModule.toNamespacedPath(path),
1029-
options.bigint, kUsePromises),
1026+
binding.lstat(getValidatedPath(path), options.bigint, kUsePromises),
10301027
undefined,
10311028
handleErrorFromBinding,
10321029
);
10331030
return getStatsFromBinding(result);
10341031
}
10351032

10361033
async function stat(path, options = { bigint: false }) {
1037-
path = getValidatedPath(path);
10381034
const result = await PromisePrototypeThen(
1039-
binding.stat(pathModule.toNamespacedPath(path),
1040-
options.bigint, kUsePromises),
1035+
binding.stat(getValidatedPath(path), options.bigint, kUsePromises),
10411036
undefined,
10421037
handleErrorFromBinding,
10431038
);
10441039
return getStatsFromBinding(result);
10451040
}
10461041

10471042
async function statfs(path, options = { bigint: false }) {
1048-
path = getValidatedPath(path);
10491043
const result = await PromisePrototypeThen(
1050-
binding.statfs(pathModule.toNamespacedPath(path),
1051-
options.bigint, kUsePromises),
1044+
binding.statfs(path, options.bigint, kUsePromises),
10521045
undefined,
10531046
handleErrorFromBinding,
10541047
);
@@ -1059,18 +1052,15 @@ async function link(existingPath, newPath) {
10591052
existingPath = getValidatedPath(existingPath, 'existingPath');
10601053
newPath = getValidatedPath(newPath, 'newPath');
10611054
return await PromisePrototypeThen(
1062-
binding.link(pathModule.toNamespacedPath(existingPath),
1063-
pathModule.toNamespacedPath(newPath),
1064-
kUsePromises),
1055+
binding.link(existingPath, newPath, kUsePromises),
10651056
undefined,
10661057
handleErrorFromBinding,
10671058
);
10681059
}
10691060

10701061
async function unlink(path) {
1071-
path = getValidatedPath(path);
10721062
return await PromisePrototypeThen(
1073-
binding.unlink(pathModule.toNamespacedPath(path), kUsePromises),
1063+
binding.unlink(getValidatedPath(path), kUsePromises),
10741064
undefined,
10751065
handleErrorFromBinding,
10761066
);
@@ -1089,7 +1079,7 @@ async function chmod(path, mode) {
10891079
path = getValidatedPath(path);
10901080
mode = parseFileMode(mode, 'mode');
10911081
return await PromisePrototypeThen(
1092-
binding.chmod(pathModule.toNamespacedPath(path), mode, kUsePromises),
1082+
binding.chmod(path, mode, kUsePromises),
10931083
undefined,
10941084
handleErrorFromBinding,
10951085
);
@@ -1108,7 +1098,7 @@ async function lchown(path, uid, gid) {
11081098
validateInteger(uid, 'uid', -1, kMaxUserId);
11091099
validateInteger(gid, 'gid', -1, kMaxUserId);
11101100
return await PromisePrototypeThen(
1111-
binding.lchown(pathModule.toNamespacedPath(path), uid, gid, kUsePromises),
1101+
binding.lchown(path, uid, gid, kUsePromises),
11121102
undefined,
11131103
handleErrorFromBinding,
11141104
);
@@ -1129,7 +1119,7 @@ async function chown(path, uid, gid) {
11291119
validateInteger(uid, 'uid', -1, kMaxUserId);
11301120
validateInteger(gid, 'gid', -1, kMaxUserId);
11311121
return await PromisePrototypeThen(
1132-
binding.chown(pathModule.toNamespacedPath(path), uid, gid, kUsePromises),
1122+
binding.chown(path, uid, gid, kUsePromises),
11331123
undefined,
11341124
handleErrorFromBinding,
11351125
);
@@ -1138,10 +1128,12 @@ async function chown(path, uid, gid) {
11381128
async function utimes(path, atime, mtime) {
11391129
path = getValidatedPath(path);
11401130
return await PromisePrototypeThen(
1141-
binding.utimes(pathModule.toNamespacedPath(path),
1142-
toUnixTimestamp(atime),
1143-
toUnixTimestamp(mtime),
1144-
kUsePromises),
1131+
binding.utimes(
1132+
path,
1133+
toUnixTimestamp(atime),
1134+
toUnixTimestamp(mtime),
1135+
kUsePromises,
1136+
),
11451137
undefined,
11461138
handleErrorFromBinding,
11471139
);
@@ -1158,22 +1150,22 @@ async function futimes(handle, atime, mtime) {
11581150
}
11591151

11601152
async function lutimes(path, atime, mtime) {
1161-
path = getValidatedPath(path);
11621153
return await PromisePrototypeThen(
1163-
binding.lutimes(pathModule.toNamespacedPath(path),
1164-
toUnixTimestamp(atime),
1165-
toUnixTimestamp(mtime),
1166-
kUsePromises),
1154+
binding.lutimes(
1155+
getValidatedPath(path),
1156+
toUnixTimestamp(atime),
1157+
toUnixTimestamp(mtime),
1158+
kUsePromises,
1159+
),
11671160
undefined,
11681161
handleErrorFromBinding,
11691162
);
11701163
}
11711164

11721165
async function realpath(path, options) {
11731166
options = getOptions(options);
1174-
path = getValidatedPath(path);
11751167
return await PromisePrototypeThen(
1176-
binding.realpath(pathModule.toNamespacedPath(path), options.encoding, kUsePromises),
1168+
binding.realpath(getValidatedPath(path), options.encoding, kUsePromises),
11771169
undefined,
11781170
handleErrorFromBinding,
11791171
);

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)