Skip to content

Commit 1b6b918

Browse files
committed
fixup! fs: move getValidMode to c++ for better performance
1 parent 354e343 commit 1b6b918

File tree

2 files changed

+47
-23
lines changed

2 files changed

+47
-23
lines changed

src/node_file.cc

+45-23
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ using v8::HandleScope;
6666
using v8::Int32;
6767
using v8::Integer;
6868
using v8::Isolate;
69+
using v8::Just;
6970
using v8::JustVoid;
7071
using v8::Local;
7172
using v8::Maybe;
@@ -89,6 +90,12 @@ constexpr char kPathSeparator = '/';
8990
const char* const kPathSeparator = "\\/";
9091
#endif
9192

93+
#ifdef _WIN32
94+
#include "uv.h"
95+
#else
96+
#include <unistd.h>
97+
#endif
98+
9299
// The access modes can be any of F_OK, R_OK, W_OK or X_OK. Some might not be
93100
// available on specific systems. They can be used in combination as well
94101
// (F_OK | R_OK | W_OK | X_OK).
@@ -872,41 +879,39 @@ void FromNamespacedPath(std::string* path) {
872879
#endif
873880
}
874881

875-
static inline int GetValidMode(Environment* env,
876-
Local<Value> mode_v,
877-
std::string_view type) {
882+
static inline Maybe<int> GetValidMode(Environment* env,
883+
Local<Value> mode_v,
884+
uv_fs_type type) {
878885
if (!mode_v->IsInt32() && !mode_v->IsNullOrUndefined()) {
879886
THROW_ERR_INVALID_ARG_TYPE(env, "mode must be int32 or null/undefined");
880-
return -1;
887+
return Nothing<int>();
881888
}
882889

883890
int min = kMinimumAccessMode;
884891
int max = kMaximumAccessMode;
885892
int def = F_OK;
886893

887-
if (type == "copyFile") {
894+
CHECK(type == UV_FS_ACCESS || type == UV_FS_COPYFILE);
895+
896+
if (type == UV_FS_COPYFILE) {
888897
min = kMinimumCopyMode;
889898
max = kMaximumCopyMode;
890899
def = mode_v->IsNullOrUndefined() ? kDefaultCopyMode
891900
: mode_v.As<Int32>()->Value();
892-
} else if (type != "access") {
893-
THROW_ERR_INVALID_ARG_TYPE(
894-
env, "type must be equal to \"copyFile\" or \"access\"");
895-
return -1;
896901
}
897902

898903
if (mode_v->IsNullOrUndefined()) {
899-
return def;
904+
return Just(def);
900905
}
901906

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

909-
return mode;
914+
return Just(mode);
910915
}
911916

912917
void AfterMkdirp(uv_fs_t* req) {
@@ -1028,8 +1033,8 @@ void Access(const FunctionCallbackInfo<Value>& args) {
10281033
const int argc = args.Length();
10291034
CHECK_GE(argc, 2);
10301035

1031-
int mode = GetValidMode(env, args[1], "access");
1032-
if (mode == -1) return;
1036+
Maybe<int> mode = GetValidMode(env, args[1], UV_FS_ACCESS);
1037+
if (mode.IsNothing()) return;
10331038

10341039
BufferValue path(isolate, args[0]);
10351040
CHECK_NOT_NULL(*path);
@@ -1041,12 +1046,20 @@ void Access(const FunctionCallbackInfo<Value>& args) {
10411046
CHECK_NOT_NULL(req_wrap_async);
10421047
FS_ASYNC_TRACE_BEGIN1(
10431048
UV_FS_ACCESS, req_wrap_async, "path", TRACE_STR_COPY(*path))
1044-
AsyncCall(env, req_wrap_async, args, "access", UTF8, AfterNoArgs,
1045-
uv_fs_access, *path, mode);
1049+
AsyncCall(env,
1050+
req_wrap_async,
1051+
args,
1052+
"access",
1053+
UTF8,
1054+
AfterNoArgs,
1055+
uv_fs_access,
1056+
*path,
1057+
mode.FromJust());
10461058
} else { // access(path, mode)
10471059
FSReqWrapSync req_wrap_sync("access", *path);
10481060
FS_SYNC_TRACE_BEGIN(access);
1049-
SyncCallAndThrowOnError(env, &req_wrap_sync, uv_fs_access, *path, mode);
1061+
SyncCallAndThrowOnError(
1062+
env, &req_wrap_sync, uv_fs_access, *path, mode.FromJust());
10501063
FS_SYNC_TRACE_END(access);
10511064
}
10521065
}
@@ -2141,8 +2154,8 @@ static void CopyFile(const FunctionCallbackInfo<Value>& args) {
21412154
const int argc = args.Length();
21422155
CHECK_GE(argc, 3);
21432156

2144-
const int flags = GetValidMode(env, args[2], "copyFile");
2145-
if (flags == -1) return;
2157+
Maybe<int> flags = GetValidMode(env, args[2], UV_FS_COPYFILE);
2158+
if (flags.IsNothing()) return;
21462159

21472160
BufferValue src(isolate, args[0]);
21482161
CHECK_NOT_NULL(*src);
@@ -2162,14 +2175,23 @@ static void CopyFile(const FunctionCallbackInfo<Value>& args) {
21622175
TRACE_STR_COPY(*src),
21632176
"dest",
21642177
TRACE_STR_COPY(*dest))
2165-
AsyncDestCall(env, req_wrap_async, args, "copyfile",
2166-
*dest, dest.length(), UTF8, AfterNoArgs,
2167-
uv_fs_copyfile, *src, *dest, flags);
2178+
AsyncDestCall(env,
2179+
req_wrap_async,
2180+
args,
2181+
"copyfile",
2182+
*dest,
2183+
dest.length(),
2184+
UTF8,
2185+
AfterNoArgs,
2186+
uv_fs_copyfile,
2187+
*src,
2188+
*dest,
2189+
flags.FromJust());
21682190
} else { // copyFile(src, dest, flags)
21692191
FSReqWrapSync req_wrap_sync("copyfile", *src, *dest);
21702192
FS_SYNC_TRACE_BEGIN(copyfile);
21712193
SyncCallAndThrowOnError(
2172-
env, &req_wrap_sync, uv_fs_copyfile, *src, *dest, flags);
2194+
env, &req_wrap_sync, uv_fs_copyfile, *src, *dest, flags.FromJust());
21732195
FS_SYNC_TRACE_END(copyfile);
21742196
}
21752197
}

src/node_file.h

+2
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ enum class FsStatFsOffset {
5252
kFsStatFsFieldsNumber
5353
};
5454

55+
enum class ModeOpType { access = 0, copyFile };
56+
5557
constexpr size_t kFsStatFsBufferLength =
5658
static_cast<size_t>(FsStatFsOffset::kFsStatFsFieldsNumber);
5759

0 commit comments

Comments
 (0)