Skip to content

Commit

Permalink
fixup! fs: move getValidMode to c++ for better performance
Browse files Browse the repository at this point in the history
  • Loading branch information
andremralves committed Oct 1, 2023
1 parent d9623aa commit 6cce9a8
Showing 1 changed file with 28 additions and 20 deletions.
48 changes: 28 additions & 20 deletions src/node_file.cc
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ using v8::HandleScope;
using v8::Int32;
using v8::Integer;
using v8::Isolate;
using v8::Just;
using v8::JustVoid;
using v8::Local;
using v8::Maybe;
Expand All @@ -89,6 +90,13 @@ constexpr char kPathSeparator = '/';
const char* const kPathSeparator = "\\/";
#endif

// F_OK etc. constants
#ifdef _WIN32
#include "uv.h"
#else
#include <unistd.h>
#endif

// The access modes can be any of F_OK, R_OK, W_OK or X_OK. Some might not be
// available on specific systems. They can be used in combination as well
// (F_OK | R_OK | W_OK | X_OK).
Expand Down Expand Up @@ -872,41 +880,39 @@ void FromNamespacedPath(std::string* path) {
#endif
}

static inline int GetValidMode(Environment* env,
Local<Value> mode_v,
std::string_view type) {
static inline Maybe<int> GetValidMode(Environment* env,
Local<Value> mode_v,
uv_fs_type type) {
if (!mode_v->IsInt32() && !mode_v->IsNullOrUndefined()) {
THROW_ERR_INVALID_ARG_TYPE(env, "mode must be int32 or null/undefined");
return -1;
return Nothing<int>();
}

int min = kMinimumAccessMode;
int max = kMaximumAccessMode;
int def = F_OK;

if (type == "copyFile") {
CHECK(type == UV_FS_ACCESS || type == UV_FS_COPYFILE);

if (type == UV_FS_COPYFILE) {
min = kMinimumCopyMode;
max = kMaximumCopyMode;
def = mode_v->IsNullOrUndefined() ? kDefaultCopyMode
: mode_v.As<Int32>()->Value();
} else if (type != "access") {
THROW_ERR_INVALID_ARG_TYPE(
env, "type must be equal to \"copyFile\" or \"access\"");
return -1;
}

if (mode_v->IsNullOrUndefined()) {
return def;
return Just(def);
}

const int mode = mode_v.As<Int32>()->Value();
if (mode < min || mode > max) {
THROW_ERR_OUT_OF_RANGE(
env, "mode is out of range: >= %d && <= %d", min, max);
return -1;
return Nothing<int>();
}

return mode;
return Just(mode);
}

void AfterMkdirp(uv_fs_t* req) {
Expand Down Expand Up @@ -1028,8 +1034,9 @@ void Access(const FunctionCallbackInfo<Value>& args) {
const int argc = args.Length();
CHECK_GE(argc, 2);

int mode = GetValidMode(env, args[1], "access");
if (mode == -1) return;
Maybe<int> maybe_mode = GetValidMode(env, args[1], UV_FS_ACCESS);
int mode;
if (!maybe_mode.To(&mode)) return;

BufferValue path(isolate, args[0]);
CHECK_NOT_NULL(*path);
Expand Down Expand Up @@ -2141,8 +2148,9 @@ static void CopyFile(const FunctionCallbackInfo<Value>& args) {
const int argc = args.Length();
CHECK_GE(argc, 3);

const int flags = GetValidMode(env, args[2], "copyFile");
if (flags == -1) return;
Maybe<int> maybe_mode = GetValidMode(env, args[2], UV_FS_COPYFILE);
int mode;
if (!maybe_mode.To(&mode)) return;

BufferValue src(isolate, args[0]);
CHECK_NOT_NULL(*src);
Expand All @@ -2154,7 +2162,7 @@ static void CopyFile(const FunctionCallbackInfo<Value>& args) {
THROW_IF_INSUFFICIENT_PERMISSIONS(
env, permission::PermissionScope::kFileSystemWrite, dest.ToStringView());

if (argc > 3) { // copyFile(src, dest, flags, req)
if (argc > 3) { // copyFile(src, dest, mode, req)
FSReqBase* req_wrap_async = GetReqWrap(args, 3);
FS_ASYNC_TRACE_BEGIN2(UV_FS_COPYFILE,
req_wrap_async,
Expand All @@ -2164,12 +2172,12 @@ static void CopyFile(const FunctionCallbackInfo<Value>& args) {
TRACE_STR_COPY(*dest))
AsyncDestCall(env, req_wrap_async, args, "copyfile",
*dest, dest.length(), UTF8, AfterNoArgs,
uv_fs_copyfile, *src, *dest, flags);
} else { // copyFile(src, dest, flags)
uv_fs_copyfile, *src, *dest, mode);
} else { // copyFile(src, dest, mode)
FSReqWrapSync req_wrap_sync("copyfile", *src, *dest);
FS_SYNC_TRACE_BEGIN(copyfile);
SyncCallAndThrowOnError(
env, &req_wrap_sync, uv_fs_copyfile, *src, *dest, flags);
env, &req_wrap_sync, uv_fs_copyfile, *src, *dest, mode);
FS_SYNC_TRACE_END(copyfile);
}
}
Expand Down

0 comments on commit 6cce9a8

Please sign in to comment.