Skip to content

Commit 2d120ca

Browse files
committed
fs: move ToNamespacedPath to c++
1 parent 8a191e4 commit 2d120ca

File tree

8 files changed

+233
-153
lines changed

8 files changed

+233
-153
lines changed

lib/fs.js

+75-98
Large diffs are not rendered by default.

lib/internal/fs/promises.js

+40-51
Original file line numberDiff line numberDiff line change
@@ -597,10 +597,8 @@ 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
);
@@ -614,13 +612,13 @@ async function cp(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
);
@@ -870,7 +865,7 @@ async function readdirRecursive(originalPath, options) {
870865
originalPath,
871866
await PromisePrototypeThen(
872867
binding.readdir(
873-
pathModule.toNamespacedPath(originalPath),
868+
originalPath,
874869
options.encoding,
875870
!!options.withFileTypes,
876871
kUsePromises,
@@ -921,7 +916,7 @@ async function readdirRecursive(originalPath, options) {
921916
direntPath,
922917
await PromisePrototypeThen(
923918
binding.readdir(
924-
pathModule.toNamespacedPath(direntPath),
919+
direntPath,
925920
options.encoding,
926921
false,
927922
kUsePromises,
@@ -946,7 +941,7 @@ async function readdir(path, options) {
946941
}
947942
const result = await PromisePrototypeThen(
948943
binding.readdir(
949-
pathModule.toNamespacedPath(path),
944+
path,
950945
options.encoding,
951946
!!options.withFileTypes,
952947
kUsePromises,
@@ -963,8 +958,7 @@ async function readlink(path, options) {
963958
options = getOptions(options);
964959
path = getValidatedPath(path, 'oldPath');
965960
return await PromisePrototypeThen(
966-
binding.readlink(pathModule.toNamespacedPath(path),
967-
options.encoding, kUsePromises),
961+
binding.readlink(path, options.encoding, kUsePromises),
968962
undefined,
969963
handleErrorFromBinding,
970964
);
@@ -993,10 +987,12 @@ async function symlink(target, path, type_) {
993987
target = getValidatedPath(target, 'target');
994988
path = getValidatedPath(path);
995989
return await PromisePrototypeThen(
996-
binding.symlink(preprocessSymlinkDestination(target, type, path),
997-
pathModule.toNamespacedPath(path),
998-
stringToSymlinkType(type),
999-
kUsePromises),
990+
binding.symlink(
991+
preprocessSymlinkDestination(target, type, path),
992+
path,
993+
stringToSymlinkType(type),
994+
kUsePromises,
995+
),
1000996
undefined,
1001997
handleErrorFromBinding,
1002998
);
@@ -1012,32 +1008,26 @@ async function fstat(handle, options = { bigint: false }) {
10121008
}
10131009

10141010
async function lstat(path, options = { bigint: false }) {
1015-
path = getValidatedPath(path);
10161011
const result = await PromisePrototypeThen(
1017-
binding.lstat(pathModule.toNamespacedPath(path),
1018-
options.bigint, kUsePromises),
1012+
binding.lstat(getValidatedPath(path), options.bigint, kUsePromises),
10191013
undefined,
10201014
handleErrorFromBinding,
10211015
);
10221016
return getStatsFromBinding(result);
10231017
}
10241018

10251019
async function stat(path, options = { bigint: false }) {
1026-
path = getValidatedPath(path);
10271020
const result = await PromisePrototypeThen(
1028-
binding.stat(pathModule.toNamespacedPath(path),
1029-
options.bigint, kUsePromises),
1021+
binding.stat(getValidatedPath(path), options.bigint, kUsePromises),
10301022
undefined,
10311023
handleErrorFromBinding,
10321024
);
10331025
return getStatsFromBinding(result);
10341026
}
10351027

10361028
async function statfs(path, options = { bigint: false }) {
1037-
path = getValidatedPath(path);
10381029
const result = await PromisePrototypeThen(
1039-
binding.statfs(pathModule.toNamespacedPath(path),
1040-
options.bigint, kUsePromises),
1030+
binding.statfs(path, options.bigint, kUsePromises),
10411031
undefined,
10421032
handleErrorFromBinding,
10431033
);
@@ -1048,18 +1038,15 @@ async function link(existingPath, newPath) {
10481038
existingPath = getValidatedPath(existingPath, 'existingPath');
10491039
newPath = getValidatedPath(newPath, 'newPath');
10501040
return await PromisePrototypeThen(
1051-
binding.link(pathModule.toNamespacedPath(existingPath),
1052-
pathModule.toNamespacedPath(newPath),
1053-
kUsePromises),
1041+
binding.link(existingPath, newPath, kUsePromises),
10541042
undefined,
10551043
handleErrorFromBinding,
10561044
);
10571045
}
10581046

10591047
async function unlink(path) {
1060-
path = getValidatedPath(path);
10611048
return await PromisePrototypeThen(
1062-
binding.unlink(pathModule.toNamespacedPath(path), kUsePromises),
1049+
binding.unlink(getValidatedPath(path), kUsePromises),
10631050
undefined,
10641051
handleErrorFromBinding,
10651052
);
@@ -1078,7 +1065,7 @@ async function chmod(path, mode) {
10781065
path = getValidatedPath(path);
10791066
mode = parseFileMode(mode, 'mode');
10801067
return await PromisePrototypeThen(
1081-
binding.chmod(pathModule.toNamespacedPath(path), mode, kUsePromises),
1068+
binding.chmod(path, mode, kUsePromises),
10821069
undefined,
10831070
handleErrorFromBinding,
10841071
);
@@ -1097,7 +1084,7 @@ async function lchown(path, uid, gid) {
10971084
validateInteger(uid, 'uid', -1, kMaxUserId);
10981085
validateInteger(gid, 'gid', -1, kMaxUserId);
10991086
return await PromisePrototypeThen(
1100-
binding.lchown(pathModule.toNamespacedPath(path), uid, gid, kUsePromises),
1087+
binding.lchown(path, uid, gid, kUsePromises),
11011088
undefined,
11021089
handleErrorFromBinding,
11031090
);
@@ -1118,7 +1105,7 @@ async function chown(path, uid, gid) {
11181105
validateInteger(uid, 'uid', -1, kMaxUserId);
11191106
validateInteger(gid, 'gid', -1, kMaxUserId);
11201107
return await PromisePrototypeThen(
1121-
binding.chown(pathModule.toNamespacedPath(path), uid, gid, kUsePromises),
1108+
binding.chown(path, uid, gid, kUsePromises),
11221109
undefined,
11231110
handleErrorFromBinding,
11241111
);
@@ -1127,10 +1114,12 @@ async function chown(path, uid, gid) {
11271114
async function utimes(path, atime, mtime) {
11281115
path = getValidatedPath(path);
11291116
return await PromisePrototypeThen(
1130-
binding.utimes(pathModule.toNamespacedPath(path),
1131-
toUnixTimestamp(atime),
1132-
toUnixTimestamp(mtime),
1133-
kUsePromises),
1117+
binding.utimes(
1118+
path,
1119+
toUnixTimestamp(atime),
1120+
toUnixTimestamp(mtime),
1121+
kUsePromises,
1122+
),
11341123
undefined,
11351124
handleErrorFromBinding,
11361125
);
@@ -1147,22 +1136,22 @@ async function futimes(handle, atime, mtime) {
11471136
}
11481137

11491138
async function lutimes(path, atime, mtime) {
1150-
path = getValidatedPath(path);
11511139
return await PromisePrototypeThen(
1152-
binding.lutimes(pathModule.toNamespacedPath(path),
1153-
toUnixTimestamp(atime),
1154-
toUnixTimestamp(mtime),
1155-
kUsePromises),
1140+
binding.lutimes(
1141+
getValidatedPath(path),
1142+
toUnixTimestamp(atime),
1143+
toUnixTimestamp(mtime),
1144+
kUsePromises,
1145+
),
11561146
undefined,
11571147
handleErrorFromBinding,
11581148
);
11591149
}
11601150

11611151
async function realpath(path, options) {
11621152
options = getOptions(options);
1163-
path = getValidatedPath(path);
11641153
return await PromisePrototypeThen(
1165-
binding.realpath(pathModule.toNamespacedPath(path), options.encoding, kUsePromises),
1154+
binding.realpath(getValidatedPath(path), options.encoding, kUsePromises),
11661155
undefined,
11671156
handleErrorFromBinding,
11681157
);

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"
@@ -98,6 +99,7 @@ void BlobFromFilePath(const FunctionCallbackInfo<Value>& args) {
9899
CHECK_NOT_NULL(*path);
99100
THROW_IF_INSUFFICIENT_PERMISSIONS(
100101
env, permission::PermissionScope::kFileSystemRead, path.ToStringView());
102+
ToNamespacedPath(env, &path);
101103
auto entry = DataQueue::CreateFdEntry(env, args[0]);
102104
if (entry == nullptr) {
103105
return THROW_ERR_INVALID_ARG_VALUE(env, "Unabled to open file as blob");

0 commit comments

Comments
 (0)