@@ -66,6 +66,7 @@ using v8::HandleScope;
6666using v8::Int32;
6767using v8::Integer;
6868using v8::Isolate;
69+ using v8::Just;
6970using v8::JustVoid;
7071using v8::Local;
7172using v8::Maybe;
@@ -89,6 +90,10 @@ constexpr char kPathSeparator = '/';
8990const char * const kPathSeparator = " \\ /" ;
9091#endif
9192
93+ #ifdef _WIN32
94+ #include " uv.h"
95+ #endif
96+
9297// The access modes can be any of F_OK, R_OK, W_OK or X_OK. Some might not be
9398// available on specific systems. They can be used in combination as well
9499// (F_OK | R_OK | W_OK | X_OK).
@@ -872,41 +877,39 @@ void FromNamespacedPath(std::string* path) {
872877#endif
873878}
874879
875- static inline int GetValidMode (Environment* env,
876- Local<Value> mode_v,
877- std::string_view type) {
880+ static inline Maybe< int > GetValidMode (Environment* env,
881+ Local<Value> mode_v,
882+ uv_fs_type type) {
878883 if (!mode_v->IsInt32 () && !mode_v->IsNullOrUndefined ()) {
879884 THROW_ERR_INVALID_ARG_TYPE (env, " mode must be int32 or null/undefined" );
880- return - 1 ;
885+ return Nothing< int >() ;
881886 }
882887
883888 int min = kMinimumAccessMode ;
884889 int max = kMaximumAccessMode ;
885890 int def = F_OK;
886891
887- if (type == " copyFile" ) {
892+ CHECK (type == UV_FS_ACCESS || type == UV_FS_COPYFILE);
893+
894+ if (type == UV_FS_COPYFILE) {
888895 min = kMinimumCopyMode ;
889896 max = kMaximumCopyMode ;
890897 def = mode_v->IsNullOrUndefined () ? kDefaultCopyMode
891898 : 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 ;
896899 }
897900
898901 if (mode_v->IsNullOrUndefined ()) {
899- return def;
902+ return Just ( def) ;
900903 }
901904
902905 const int mode = mode_v.As <Int32>()->Value ();
903906 if (mode < min || mode > max) {
904907 THROW_ERR_OUT_OF_RANGE (
905908 env, " mode is out of range: >= %d && <= %d" , min, max);
906- return - 1 ;
909+ return Nothing< int >() ;
907910 }
908911
909- return mode;
912+ return Just ( mode) ;
910913}
911914
912915void AfterMkdirp (uv_fs_t * req) {
@@ -1028,8 +1031,8 @@ void Access(const FunctionCallbackInfo<Value>& args) {
10281031 const int argc = args.Length ();
10291032 CHECK_GE (argc, 2 );
10301033
1031- int mode = GetValidMode (env, args[1 ], " access " );
1032- if (mode == - 1 ) return ;
1034+ Maybe< int > mode = GetValidMode (env, args[1 ], UV_FS_ACCESS );
1035+ if (mode. IsNothing () ) return ;
10331036
10341037 BufferValue path (isolate, args[0 ]);
10351038 CHECK_NOT_NULL (*path);
@@ -1041,12 +1044,20 @@ void Access(const FunctionCallbackInfo<Value>& args) {
10411044 CHECK_NOT_NULL (req_wrap_async);
10421045 FS_ASYNC_TRACE_BEGIN1 (
10431046 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);
1047+ AsyncCall (env,
1048+ req_wrap_async,
1049+ args,
1050+ " access" ,
1051+ UTF8,
1052+ AfterNoArgs,
1053+ uv_fs_access,
1054+ *path,
1055+ mode.FromJust ());
10461056 } else { // access(path, mode)
10471057 FSReqWrapSync req_wrap_sync (" access" , *path);
10481058 FS_SYNC_TRACE_BEGIN (access);
1049- SyncCallAndThrowOnError (env, &req_wrap_sync, uv_fs_access, *path, mode);
1059+ SyncCallAndThrowOnError (
1060+ env, &req_wrap_sync, uv_fs_access, *path, mode.FromJust ());
10501061 FS_SYNC_TRACE_END (access);
10511062 }
10521063}
@@ -2141,8 +2152,8 @@ static void CopyFile(const FunctionCallbackInfo<Value>& args) {
21412152 const int argc = args.Length ();
21422153 CHECK_GE (argc, 3 );
21432154
2144- const int flags = GetValidMode (env, args[2 ], " copyFile " );
2145- if (flags == - 1 ) return ;
2155+ Maybe< int > flags = GetValidMode (env, args[2 ], UV_FS_COPYFILE );
2156+ if (flags. IsNothing () ) return ;
21462157
21472158 BufferValue src (isolate, args[0 ]);
21482159 CHECK_NOT_NULL (*src);
@@ -2162,14 +2173,23 @@ static void CopyFile(const FunctionCallbackInfo<Value>& args) {
21622173 TRACE_STR_COPY (*src),
21632174 " dest" ,
21642175 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);
2176+ AsyncDestCall (env,
2177+ req_wrap_async,
2178+ args,
2179+ " copyfile" ,
2180+ *dest,
2181+ dest.length (),
2182+ UTF8,
2183+ AfterNoArgs,
2184+ uv_fs_copyfile,
2185+ *src,
2186+ *dest,
2187+ flags.FromJust ());
21682188 } else { // copyFile(src, dest, flags)
21692189 FSReqWrapSync req_wrap_sync (" copyfile" , *src, *dest);
21702190 FS_SYNC_TRACE_BEGIN (copyfile);
21712191 SyncCallAndThrowOnError (
2172- env, &req_wrap_sync, uv_fs_copyfile, *src, *dest, flags);
2192+ env, &req_wrap_sync, uv_fs_copyfile, *src, *dest, flags. FromJust () );
21732193 FS_SYNC_TRACE_END (copyfile);
21742194 }
21752195}
0 commit comments