Skip to content

Commit

Permalink
fs: move ToNamespacedPath to c++
Browse files Browse the repository at this point in the history
Co-Authored-By: Daniel Lemire <daniel@lemire.me>
PR-URL: nodejs#52135
Reviewed-By: Geoffrey Booth <webadmin@geoffreybooth.com>
Reviewed-By: Vinícius Lourenço Claro Cardoso <contact@viniciusl.com.br>
Reviewed-By: Daniel Lemire <daniel@lemire.me>
Reviewed-By: Rafael Gonzaga <rafael.nunu@hotmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
  • Loading branch information
2 people authored and sophoniie committed Jun 20, 2024
1 parent ba99a3b commit 74e5916
Show file tree
Hide file tree
Showing 10 changed files with 303 additions and 231 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 @@ -604,30 +604,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 @@ -640,8 +638,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 @@ -786,9 +783,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 @@ -810,13 +805,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 @@ -862,9 +857,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 @@ -877,7 +875,7 @@ async function readdirRecursive(originalPath, options) {
originalPath,
await PromisePrototypeThen(
binding.readdir(
pathModule.toNamespacedPath(originalPath),
originalPath,
options.encoding,
!!options.withFileTypes,
kUsePromises,
Expand Down Expand Up @@ -928,7 +926,7 @@ async function readdirRecursive(originalPath, options) {
direntPath,
await PromisePrototypeThen(
binding.readdir(
pathModule.toNamespacedPath(direntPath),
direntPath,
options.encoding,
false,
kUsePromises,
Expand All @@ -953,7 +951,7 @@ async function readdir(path, options) {
}
const result = await PromisePrototypeThen(
binding.readdir(
pathModule.toNamespacedPath(path),
path,
options.encoding,
!!options.withFileTypes,
kUsePromises,
Expand All @@ -970,8 +968,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 @@ -1004,10 +1001,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 @@ -1023,32 +1022,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 @@ -1059,18 +1052,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 @@ -1089,7 +1079,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 @@ -1108,7 +1098,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 @@ -1129,7 +1119,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 @@ -1138,10 +1128,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 @@ -1158,22 +1150,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 74e5916

Please sign in to comment.