Skip to content

Commit 105b006

Browse files
anonriglemire
authored andcommitted
fs: move ToNamespacedPath to c++
Co-Authored-By: Daniel Lemire <[email protected]> PR-URL: #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 30fdd48 commit 105b006

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

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

622620
async function copyFile(src, dest, mode) {
623-
src = getValidatedPath(src, 'src');
624-
dest = getValidatedPath(dest, 'dest');
625621
return await PromisePrototypeThen(
626-
binding.copyFile(pathModule.toNamespacedPath(src),
627-
pathModule.toNamespacedPath(dest),
628-
mode,
629-
kUsePromises),
622+
binding.copyFile(
623+
getValidatedPath(src, 'src'),
624+
getValidatedPath(dest, 'dest'),
625+
mode,
626+
kUsePromises,
627+
),
630628
undefined,
631629
handleErrorFromBinding,
632630
);
@@ -639,8 +637,7 @@ async function open(path, flags, mode) {
639637
const flagsNumber = stringToFlags(flags);
640638
mode = parseFileMode(mode, 'mode', 0o666);
641639
return new FileHandle(await PromisePrototypeThen(
642-
binding.openFileHandle(pathModule.toNamespacedPath(path),
643-
flagsNumber, mode, kUsePromises),
640+
binding.openFileHandle(path, flagsNumber, mode, kUsePromises),
644641
undefined,
645642
handleErrorFromBinding,
646643
));
@@ -785,9 +782,7 @@ async function rename(oldPath, newPath) {
785782
oldPath = getValidatedPath(oldPath, 'oldPath');
786783
newPath = getValidatedPath(newPath, 'newPath');
787784
return await PromisePrototypeThen(
788-
binding.rename(pathModule.toNamespacedPath(oldPath),
789-
pathModule.toNamespacedPath(newPath),
790-
kUsePromises),
785+
binding.rename(oldPath, newPath, kUsePromises),
791786
undefined,
792787
handleErrorFromBinding,
793788
);
@@ -809,13 +804,13 @@ async function ftruncate(handle, len = 0) {
809804
}
810805

811806
async function rm(path, options) {
812-
path = pathModule.toNamespacedPath(getValidatedPath(path));
807+
path = getValidatedPath(path);
813808
options = await validateRmOptionsPromise(path, options, false);
814809
return lazyRimRaf()(path, options);
815810
}
816811

817812
async function rmdir(path, options) {
818-
path = pathModule.toNamespacedPath(getValidatedPath(path));
813+
path = getValidatedPath(path);
819814
options = validateRmdirOptions(options);
820815

821816
if (options.recursive) {
@@ -861,9 +856,12 @@ async function mkdir(path, options) {
861856
validateBoolean(recursive, 'options.recursive');
862857

863858
return await PromisePrototypeThen(
864-
binding.mkdir(pathModule.toNamespacedPath(path),
865-
parseFileMode(mode, 'mode', 0o777), recursive,
866-
kUsePromises),
859+
binding.mkdir(
860+
path,
861+
parseFileMode(mode, 'mode', 0o777),
862+
recursive,
863+
kUsePromises,
864+
),
867865
undefined,
868866
handleErrorFromBinding,
869867
);
@@ -876,7 +874,7 @@ async function readdirRecursive(originalPath, options) {
876874
originalPath,
877875
await PromisePrototypeThen(
878876
binding.readdir(
879-
pathModule.toNamespacedPath(originalPath),
877+
originalPath,
880878
options.encoding,
881879
!!options.withFileTypes,
882880
kUsePromises,
@@ -927,7 +925,7 @@ async function readdirRecursive(originalPath, options) {
927925
direntPath,
928926
await PromisePrototypeThen(
929927
binding.readdir(
930-
pathModule.toNamespacedPath(direntPath),
928+
direntPath,
931929
options.encoding,
932930
false,
933931
kUsePromises,
@@ -952,7 +950,7 @@ async function readdir(path, options) {
952950
}
953951
const result = await PromisePrototypeThen(
954952
binding.readdir(
955-
pathModule.toNamespacedPath(path),
953+
path,
956954
options.encoding,
957955
!!options.withFileTypes,
958956
kUsePromises,
@@ -969,8 +967,7 @@ async function readlink(path, options) {
969967
options = getOptions(options);
970968
path = getValidatedPath(path, 'oldPath');
971969
return await PromisePrototypeThen(
972-
binding.readlink(pathModule.toNamespacedPath(path),
973-
options.encoding, kUsePromises),
970+
binding.readlink(path, options.encoding, kUsePromises),
974971
undefined,
975972
handleErrorFromBinding,
976973
);
@@ -1003,10 +1000,12 @@ async function symlink(target, path, type_) {
10031000
target = getValidatedPath(target, 'target');
10041001
path = getValidatedPath(path);
10051002
return await PromisePrototypeThen(
1006-
binding.symlink(preprocessSymlinkDestination(target, type, path),
1007-
pathModule.toNamespacedPath(path),
1008-
stringToSymlinkType(type),
1009-
kUsePromises),
1003+
binding.symlink(
1004+
preprocessSymlinkDestination(target, type, path),
1005+
path,
1006+
stringToSymlinkType(type),
1007+
kUsePromises,
1008+
),
10101009
undefined,
10111010
handleErrorFromBinding,
10121011
);
@@ -1022,32 +1021,26 @@ async function fstat(handle, options = { bigint: false }) {
10221021
}
10231022

10241023
async function lstat(path, options = { bigint: false }) {
1025-
path = getValidatedPath(path);
10261024
const result = await PromisePrototypeThen(
1027-
binding.lstat(pathModule.toNamespacedPath(path),
1028-
options.bigint, kUsePromises),
1025+
binding.lstat(getValidatedPath(path), options.bigint, kUsePromises),
10291026
undefined,
10301027
handleErrorFromBinding,
10311028
);
10321029
return getStatsFromBinding(result);
10331030
}
10341031

10351032
async function stat(path, options = { bigint: false }) {
1036-
path = getValidatedPath(path);
10371033
const result = await PromisePrototypeThen(
1038-
binding.stat(pathModule.toNamespacedPath(path),
1039-
options.bigint, kUsePromises),
1034+
binding.stat(getValidatedPath(path), options.bigint, kUsePromises),
10401035
undefined,
10411036
handleErrorFromBinding,
10421037
);
10431038
return getStatsFromBinding(result);
10441039
}
10451040

10461041
async function statfs(path, options = { bigint: false }) {
1047-
path = getValidatedPath(path);
10481042
const result = await PromisePrototypeThen(
1049-
binding.statfs(pathModule.toNamespacedPath(path),
1050-
options.bigint, kUsePromises),
1043+
binding.statfs(path, options.bigint, kUsePromises),
10511044
undefined,
10521045
handleErrorFromBinding,
10531046
);
@@ -1058,18 +1051,15 @@ async function link(existingPath, newPath) {
10581051
existingPath = getValidatedPath(existingPath, 'existingPath');
10591052
newPath = getValidatedPath(newPath, 'newPath');
10601053
return await PromisePrototypeThen(
1061-
binding.link(pathModule.toNamespacedPath(existingPath),
1062-
pathModule.toNamespacedPath(newPath),
1063-
kUsePromises),
1054+
binding.link(existingPath, newPath, kUsePromises),
10641055
undefined,
10651056
handleErrorFromBinding,
10661057
);
10671058
}
10681059

10691060
async function unlink(path) {
1070-
path = getValidatedPath(path);
10711061
return await PromisePrototypeThen(
1072-
binding.unlink(pathModule.toNamespacedPath(path), kUsePromises),
1062+
binding.unlink(getValidatedPath(path), kUsePromises),
10731063
undefined,
10741064
handleErrorFromBinding,
10751065
);
@@ -1088,7 +1078,7 @@ async function chmod(path, mode) {
10881078
path = getValidatedPath(path);
10891079
mode = parseFileMode(mode, 'mode');
10901080
return await PromisePrototypeThen(
1091-
binding.chmod(pathModule.toNamespacedPath(path), mode, kUsePromises),
1081+
binding.chmod(path, mode, kUsePromises),
10921082
undefined,
10931083
handleErrorFromBinding,
10941084
);
@@ -1107,7 +1097,7 @@ async function lchown(path, uid, gid) {
11071097
validateInteger(uid, 'uid', -1, kMaxUserId);
11081098
validateInteger(gid, 'gid', -1, kMaxUserId);
11091099
return await PromisePrototypeThen(
1110-
binding.lchown(pathModule.toNamespacedPath(path), uid, gid, kUsePromises),
1100+
binding.lchown(path, uid, gid, kUsePromises),
11111101
undefined,
11121102
handleErrorFromBinding,
11131103
);
@@ -1128,7 +1118,7 @@ async function chown(path, uid, gid) {
11281118
validateInteger(uid, 'uid', -1, kMaxUserId);
11291119
validateInteger(gid, 'gid', -1, kMaxUserId);
11301120
return await PromisePrototypeThen(
1131-
binding.chown(pathModule.toNamespacedPath(path), uid, gid, kUsePromises),
1121+
binding.chown(path, uid, gid, kUsePromises),
11321122
undefined,
11331123
handleErrorFromBinding,
11341124
);
@@ -1137,10 +1127,12 @@ async function chown(path, uid, gid) {
11371127
async function utimes(path, atime, mtime) {
11381128
path = getValidatedPath(path);
11391129
return await PromisePrototypeThen(
1140-
binding.utimes(pathModule.toNamespacedPath(path),
1141-
toUnixTimestamp(atime),
1142-
toUnixTimestamp(mtime),
1143-
kUsePromises),
1130+
binding.utimes(
1131+
path,
1132+
toUnixTimestamp(atime),
1133+
toUnixTimestamp(mtime),
1134+
kUsePromises,
1135+
),
11441136
undefined,
11451137
handleErrorFromBinding,
11461138
);
@@ -1157,22 +1149,22 @@ async function futimes(handle, atime, mtime) {
11571149
}
11581150

11591151
async function lutimes(path, atime, mtime) {
1160-
path = getValidatedPath(path);
11611152
return await PromisePrototypeThen(
1162-
binding.lutimes(pathModule.toNamespacedPath(path),
1163-
toUnixTimestamp(atime),
1164-
toUnixTimestamp(mtime),
1165-
kUsePromises),
1153+
binding.lutimes(
1154+
getValidatedPath(path),
1155+
toUnixTimestamp(atime),
1156+
toUnixTimestamp(mtime),
1157+
kUsePromises,
1158+
),
11661159
undefined,
11671160
handleErrorFromBinding,
11681161
);
11691162
}
11701163

11711164
async function realpath(path, options) {
11721165
options = getOptions(options);
1173-
path = getValidatedPath(path);
11741166
return await PromisePrototypeThen(
1175-
binding.realpath(pathModule.toNamespacedPath(path), options.encoding, kUsePromises),
1167+
binding.realpath(getValidatedPath(path), options.encoding, kUsePromises),
11761168
undefined,
11771169
handleErrorFromBinding,
11781170
);

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)