Skip to content

Commit

Permalink
fs: move ToNamespacedPath to c++
Browse files Browse the repository at this point in the history
  • Loading branch information
anonrig committed Mar 21, 2024
1 parent 0b67673 commit 8ab9cf7
Show file tree
Hide file tree
Showing 8 changed files with 251 additions and 195 deletions.
231 changes: 99 additions & 132 deletions lib/fs.js

Large diffs are not rendered by default.

108 changes: 50 additions & 58 deletions lib/internal/fs/promises.js
Original file line number Diff line number Diff line change
Expand Up @@ -597,30 +597,28 @@ async function readFileHandle(filehandle, options) {
// All of the functions are defined as async in order to ensure that errors
// thrown cause promise rejections rather than being thrown synchronously.
async function access(path, mode = F_OK) {
path = getValidatedPath(path);

return await PromisePrototypeThen(
binding.access(pathModule.toNamespacedPath(path), mode, kUsePromises),
binding.access(getValidatedPath(path), mode, kUsePromises),
undefined,
handleErrorFromBinding,
);
}

async function cp(src, dest, options) {
options = validateCpOptions(options);
src = pathModule.toNamespacedPath(getValidatedPath(src, 'src'));
dest = pathModule.toNamespacedPath(getValidatedPath(dest, 'dest'));
src = getValidatedPath(src, 'src');
dest = getValidatedPath(dest, 'dest');
return lazyLoadCpPromises()(src, dest, options);
}

async function copyFile(src, dest, mode) {
src = getValidatedPath(src, 'src');
dest = getValidatedPath(dest, 'dest');
return await PromisePrototypeThen(
binding.copyFile(pathModule.toNamespacedPath(src),
pathModule.toNamespacedPath(dest),
mode,
kUsePromises),
binding.copyFile(
getValidatedPath(src, 'src'),
getValidatedPath(dest, 'dest'),
mode,
kUsePromises,
),
undefined,
handleErrorFromBinding,
);
Expand All @@ -633,8 +631,7 @@ async function open(path, flags, mode) {
const flagsNumber = stringToFlags(flags);
mode = parseFileMode(mode, 'mode', 0o666);
return new FileHandle(await PromisePrototypeThen(
binding.openFileHandle(pathModule.toNamespacedPath(path),
flagsNumber, mode, kUsePromises),
binding.openFileHandle(path, flagsNumber, mode, kUsePromises),
undefined,
handleErrorFromBinding,
));
Expand Down Expand Up @@ -779,9 +776,7 @@ async function rename(oldPath, newPath) {
oldPath = getValidatedPath(oldPath, 'oldPath');
newPath = getValidatedPath(newPath, 'newPath');
return await PromisePrototypeThen(
binding.rename(pathModule.toNamespacedPath(oldPath),
pathModule.toNamespacedPath(newPath),
kUsePromises),
binding.rename(oldPath, newPath, kUsePromises),
undefined,
handleErrorFromBinding,
);
Expand All @@ -803,13 +798,13 @@ async function ftruncate(handle, len = 0) {
}

async function rm(path, options) {
path = pathModule.toNamespacedPath(getValidatedPath(path));
path = getValidatedPath(path);
options = await validateRmOptionsPromise(path, options, false);
return lazyRimRaf()(path, options);
}

async function rmdir(path, options) {
path = pathModule.toNamespacedPath(getValidatedPath(path));
path = getValidatedPath(path);
options = validateRmdirOptions(options);

if (options.recursive) {
Expand Down Expand Up @@ -855,9 +850,12 @@ async function mkdir(path, options) {
validateBoolean(recursive, 'options.recursive');

return await PromisePrototypeThen(
binding.mkdir(pathModule.toNamespacedPath(path),
parseFileMode(mode, 'mode', 0o777), recursive,
kUsePromises),
binding.mkdir(
path,
parseFileMode(mode, 'mode', 0o777),
recursive,
kUsePromises,
),
undefined,
handleErrorFromBinding,
);
Expand All @@ -870,7 +868,7 @@ async function readdirRecursive(originalPath, options) {
originalPath,
await PromisePrototypeThen(
binding.readdir(
pathModule.toNamespacedPath(originalPath),
originalPath,
options.encoding,
!!options.withFileTypes,
kUsePromises,
Expand Down Expand Up @@ -921,7 +919,7 @@ async function readdirRecursive(originalPath, options) {
direntPath,
await PromisePrototypeThen(
binding.readdir(
pathModule.toNamespacedPath(direntPath),
direntPath,
options.encoding,
false,
kUsePromises,
Expand All @@ -946,7 +944,7 @@ async function readdir(path, options) {
}
const result = await PromisePrototypeThen(
binding.readdir(
pathModule.toNamespacedPath(path),
path,
options.encoding,
!!options.withFileTypes,
kUsePromises,
Expand All @@ -963,8 +961,7 @@ async function readlink(path, options) {
options = getOptions(options);
path = getValidatedPath(path, 'oldPath');
return await PromisePrototypeThen(
binding.readlink(pathModule.toNamespacedPath(path),
options.encoding, kUsePromises),
binding.readlink(path, options.encoding, kUsePromises),
undefined,
handleErrorFromBinding,
);
Expand Down Expand Up @@ -993,10 +990,12 @@ async function symlink(target, path, type_) {
target = getValidatedPath(target, 'target');
path = getValidatedPath(path);
return await PromisePrototypeThen(
binding.symlink(preprocessSymlinkDestination(target, type, path),
pathModule.toNamespacedPath(path),
stringToSymlinkType(type),
kUsePromises),
binding.symlink(
preprocessSymlinkDestination(target, type, path),
path,
stringToSymlinkType(type),
kUsePromises,
),
undefined,
handleErrorFromBinding,
);
Expand All @@ -1012,32 +1011,26 @@ async function fstat(handle, options = { bigint: false }) {
}

async function lstat(path, options = { bigint: false }) {
path = getValidatedPath(path);
const result = await PromisePrototypeThen(
binding.lstat(pathModule.toNamespacedPath(path),
options.bigint, kUsePromises),
binding.lstat(getValidatedPath(path), options.bigint, kUsePromises),
undefined,
handleErrorFromBinding,
);
return getStatsFromBinding(result);
}

async function stat(path, options = { bigint: false }) {
path = getValidatedPath(path);
const result = await PromisePrototypeThen(
binding.stat(pathModule.toNamespacedPath(path),
options.bigint, kUsePromises),
binding.stat(getValidatedPath(path), options.bigint, kUsePromises),
undefined,
handleErrorFromBinding,
);
return getStatsFromBinding(result);
}

async function statfs(path, options = { bigint: false }) {
path = getValidatedPath(path);
const result = await PromisePrototypeThen(
binding.statfs(pathModule.toNamespacedPath(path),
options.bigint, kUsePromises),
binding.statfs(path, options.bigint, kUsePromises),
undefined,
handleErrorFromBinding,
);
Expand All @@ -1048,18 +1041,15 @@ async function link(existingPath, newPath) {
existingPath = getValidatedPath(existingPath, 'existingPath');
newPath = getValidatedPath(newPath, 'newPath');
return await PromisePrototypeThen(
binding.link(pathModule.toNamespacedPath(existingPath),
pathModule.toNamespacedPath(newPath),
kUsePromises),
binding.link(existingPath, newPath, kUsePromises),
undefined,
handleErrorFromBinding,
);
}

async function unlink(path) {
path = getValidatedPath(path);
return await PromisePrototypeThen(
binding.unlink(pathModule.toNamespacedPath(path), kUsePromises),
binding.unlink(getValidatedPath(path), kUsePromises),
undefined,
handleErrorFromBinding,
);
Expand All @@ -1078,7 +1068,7 @@ async function chmod(path, mode) {
path = getValidatedPath(path);
mode = parseFileMode(mode, 'mode');
return await PromisePrototypeThen(
binding.chmod(pathModule.toNamespacedPath(path), mode, kUsePromises),
binding.chmod(path, mode, kUsePromises),
undefined,
handleErrorFromBinding,
);
Expand All @@ -1097,7 +1087,7 @@ async function lchown(path, uid, gid) {
validateInteger(uid, 'uid', -1, kMaxUserId);
validateInteger(gid, 'gid', -1, kMaxUserId);
return await PromisePrototypeThen(
binding.lchown(pathModule.toNamespacedPath(path), uid, gid, kUsePromises),
binding.lchown(path, uid, gid, kUsePromises),
undefined,
handleErrorFromBinding,
);
Expand All @@ -1118,7 +1108,7 @@ async function chown(path, uid, gid) {
validateInteger(uid, 'uid', -1, kMaxUserId);
validateInteger(gid, 'gid', -1, kMaxUserId);
return await PromisePrototypeThen(
binding.chown(pathModule.toNamespacedPath(path), uid, gid, kUsePromises),
binding.chown(path, uid, gid, kUsePromises),
undefined,
handleErrorFromBinding,
);
Expand All @@ -1127,10 +1117,12 @@ async function chown(path, uid, gid) {
async function utimes(path, atime, mtime) {
path = getValidatedPath(path);
return await PromisePrototypeThen(
binding.utimes(pathModule.toNamespacedPath(path),
toUnixTimestamp(atime),
toUnixTimestamp(mtime),
kUsePromises),
binding.utimes(
path,
toUnixTimestamp(atime),
toUnixTimestamp(mtime),
kUsePromises,
),
undefined,
handleErrorFromBinding,
);
Expand All @@ -1147,22 +1139,22 @@ async function futimes(handle, atime, mtime) {
}

async function lutimes(path, atime, mtime) {
path = getValidatedPath(path);
return await PromisePrototypeThen(
binding.lutimes(pathModule.toNamespacedPath(path),
toUnixTimestamp(atime),
toUnixTimestamp(mtime),
kUsePromises),
binding.lutimes(
getValidatedPath(path),
toUnixTimestamp(atime),
toUnixTimestamp(mtime),
kUsePromises,
),
undefined,
handleErrorFromBinding,
);
}

async function realpath(path, options) {
options = getOptions(options);
path = getValidatedPath(path);
return await PromisePrototypeThen(
binding.realpath(pathModule.toNamespacedPath(path), options.encoding, kUsePromises),
binding.realpath(getValidatedPath(path), options.encoding, kUsePromises),
undefined,
handleErrorFromBinding,
);
Expand Down
2 changes: 2 additions & 0 deletions src/node_blob.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "node_errors.h"
#include "node_external_reference.h"
#include "node_file.h"
#include "path.h"
#include "permission/permission.h"
#include "util.h"
#include "v8.h"
Expand Down Expand Up @@ -96,6 +97,7 @@ void BlobFromFilePath(const FunctionCallbackInfo<Value>& args) {
Environment* env = Environment::GetCurrent(args);
BufferValue path(env->isolate(), args[0]);
CHECK_NOT_NULL(*path);
ToNamespacedPath(env, &path);
THROW_IF_INSUFFICIENT_PERMISSIONS(
env, permission::PermissionScope::kFileSystemRead, path.ToStringView());
auto entry = DataQueue::CreateFdEntry(env, args[0]);
Expand Down
Loading

0 comments on commit 8ab9cf7

Please sign in to comment.