@@ -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,13 @@ constexpr char kPathSeparator = '/';
8990const char * const kPathSeparator = " \\ /" ;
9091#endif
9192
93+ // F_OK etc. constants
94+ #ifdef _WIN32
95+ #include " uv.h"
96+ #else
97+ #include < unistd.h>
98+ #endif
99+
92100// The access modes can be any of F_OK, R_OK, W_OK or X_OK. Some might not be
93101// available on specific systems. They can be used in combination as well
94102// (F_OK | R_OK | W_OK | X_OK).
@@ -872,41 +880,39 @@ void FromNamespacedPath(std::string* path) {
872880#endif
873881}
874882
875- static inline int GetValidMode (Environment* env,
876- Local<Value> mode_v,
877- std::string_view type) {
883+ static inline Maybe< int > GetValidMode (Environment* env,
884+ Local<Value> mode_v,
885+ uv_fs_type type) {
878886 if (!mode_v->IsInt32 () && !mode_v->IsNullOrUndefined ()) {
879887 THROW_ERR_INVALID_ARG_TYPE (env, " mode must be int32 or null/undefined" );
880- return - 1 ;
888+ return Nothing< int >() ;
881889 }
882890
883891 int min = kMinimumAccessMode ;
884892 int max = kMaximumAccessMode ;
885893 int def = F_OK;
886894
887- if (type == " copyFile" ) {
895+ CHECK (type == UV_FS_ACCESS || type == UV_FS_COPYFILE);
896+
897+ if (type == UV_FS_COPYFILE) {
888898 min = kMinimumCopyMode ;
889899 max = kMaximumCopyMode ;
890900 def = mode_v->IsNullOrUndefined () ? kDefaultCopyMode
891901 : 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 ;
896902 }
897903
898904 if (mode_v->IsNullOrUndefined ()) {
899- return def;
905+ return Just ( def) ;
900906 }
901907
902908 const int mode = mode_v.As <Int32>()->Value ();
903909 if (mode < min || mode > max) {
904910 THROW_ERR_OUT_OF_RANGE (
905911 env, " mode is out of range: >= %d && <= %d" , min, max);
906- return - 1 ;
912+ return Nothing< int >() ;
907913 }
908914
909- return mode;
915+ return Just ( mode) ;
910916}
911917
912918void AfterMkdirp (uv_fs_t * req) {
@@ -1028,8 +1034,9 @@ void Access(const FunctionCallbackInfo<Value>& args) {
10281034 const int argc = args.Length ();
10291035 CHECK_GE (argc, 2 );
10301036
1031- int mode = GetValidMode (env, args[1 ], " access" );
1032- if (mode == -1 ) return ;
1037+ Maybe<int > maybe_mode = GetValidMode (env, args[1 ], UV_FS_ACCESS);
1038+ int mode;
1039+ if (!maybe_mode.To (&mode)) return ;
10331040
10341041 BufferValue path (isolate, args[0 ]);
10351042 CHECK_NOT_NULL (*path);
@@ -2141,8 +2148,9 @@ static void CopyFile(const FunctionCallbackInfo<Value>& args) {
21412148 const int argc = args.Length ();
21422149 CHECK_GE (argc, 3 );
21432150
2144- const int flags = GetValidMode (env, args[2 ], " copyFile" );
2145- if (flags == -1 ) return ;
2151+ Maybe<int > maybe_mode = GetValidMode (env, args[2 ], UV_FS_COPYFILE);
2152+ int mode;
2153+ if (!maybe_mode.To (&mode)) return ;
21462154
21472155 BufferValue src (isolate, args[0 ]);
21482156 CHECK_NOT_NULL (*src);
@@ -2154,22 +2162,31 @@ static void CopyFile(const FunctionCallbackInfo<Value>& args) {
21542162 THROW_IF_INSUFFICIENT_PERMISSIONS (
21552163 env, permission::PermissionScope::kFileSystemWrite , dest.ToStringView ());
21562164
2157- if (argc > 3 ) { // copyFile(src, dest, flags , req)
2165+ if (argc > 3 ) { // copyFile(src, dest, mode , req)
21582166 FSReqBase* req_wrap_async = GetReqWrap (args, 3 );
21592167 FS_ASYNC_TRACE_BEGIN2 (UV_FS_COPYFILE,
21602168 req_wrap_async,
21612169 " src" ,
21622170 TRACE_STR_COPY (*src),
21632171 " dest" ,
21642172 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);
2168- } else { // copyFile(src, dest, flags)
2173+ AsyncDestCall (env,
2174+ req_wrap_async,
2175+ args,
2176+ " copyfile" ,
2177+ *dest,
2178+ dest.length (),
2179+ UTF8,
2180+ AfterNoArgs,
2181+ uv_fs_copyfile,
2182+ *src,
2183+ *dest,
2184+ mode);
2185+ } else { // copyFile(src, dest, mode)
21692186 FSReqWrapSync req_wrap_sync (" copyfile" , *src, *dest);
21702187 FS_SYNC_TRACE_BEGIN (copyfile);
21712188 SyncCallAndThrowOnError (
2172- env, &req_wrap_sync, uv_fs_copyfile, *src, *dest, flags );
2189+ env, &req_wrap_sync, uv_fs_copyfile, *src, *dest, mode );
21732190 FS_SYNC_TRACE_END (copyfile);
21742191 }
21752192}
0 commit comments