@@ -66,6 +66,7 @@ using v8::HandleScope;
66
66
using v8::Int32;
67
67
using v8::Integer;
68
68
using v8::Isolate;
69
+ using v8::Just;
69
70
using v8::JustVoid;
70
71
using v8::Local;
71
72
using v8::Maybe;
@@ -89,6 +90,12 @@ constexpr char kPathSeparator = '/';
89
90
const char * const kPathSeparator = " \\ /" ;
90
91
#endif
91
92
93
+ #ifdef _WIN32
94
+ #include " uv.h"
95
+ #else
96
+ #include < unistd.h>
97
+ #endif
98
+
92
99
// The access modes can be any of F_OK, R_OK, W_OK or X_OK. Some might not be
93
100
// available on specific systems. They can be used in combination as well
94
101
// (F_OK | R_OK | W_OK | X_OK).
@@ -872,41 +879,39 @@ void FromNamespacedPath(std::string* path) {
872
879
#endif
873
880
}
874
881
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) {
878
885
if (!mode_v->IsInt32 () && !mode_v->IsNullOrUndefined ()) {
879
886
THROW_ERR_INVALID_ARG_TYPE (env, " mode must be int32 or null/undefined" );
880
- return - 1 ;
887
+ return Nothing< int >() ;
881
888
}
882
889
883
890
int min = kMinimumAccessMode ;
884
891
int max = kMaximumAccessMode ;
885
892
int def = F_OK;
886
893
887
- if (type == " copyFile" ) {
894
+ CHECK (type == UV_FS_ACCESS || type == UV_FS_COPYFILE);
895
+
896
+ if (type == UV_FS_COPYFILE) {
888
897
min = kMinimumCopyMode ;
889
898
max = kMaximumCopyMode ;
890
899
def = mode_v->IsNullOrUndefined () ? kDefaultCopyMode
891
900
: 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 ;
896
901
}
897
902
898
903
if (mode_v->IsNullOrUndefined ()) {
899
- return def;
904
+ return Just ( def) ;
900
905
}
901
906
902
907
const int mode = mode_v.As <Int32>()->Value ();
903
908
if (mode < min || mode > max) {
904
909
THROW_ERR_OUT_OF_RANGE (
905
910
env, " mode is out of range: >= %d && <= %d" , min, max);
906
- return - 1 ;
911
+ return Nothing< int >() ;
907
912
}
908
913
909
- return mode;
914
+ return Just ( mode) ;
910
915
}
911
916
912
917
void AfterMkdirp (uv_fs_t * req) {
@@ -1028,8 +1033,8 @@ void Access(const FunctionCallbackInfo<Value>& args) {
1028
1033
const int argc = args.Length ();
1029
1034
CHECK_GE (argc, 2 );
1030
1035
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 ;
1033
1038
1034
1039
BufferValue path (isolate, args[0 ]);
1035
1040
CHECK_NOT_NULL (*path);
@@ -1041,12 +1046,20 @@ void Access(const FunctionCallbackInfo<Value>& args) {
1041
1046
CHECK_NOT_NULL (req_wrap_async);
1042
1047
FS_ASYNC_TRACE_BEGIN1 (
1043
1048
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 ());
1046
1058
} else { // access(path, mode)
1047
1059
FSReqWrapSync req_wrap_sync (" access" , *path);
1048
1060
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 ());
1050
1063
FS_SYNC_TRACE_END (access );
1051
1064
}
1052
1065
}
@@ -2141,8 +2154,8 @@ static void CopyFile(const FunctionCallbackInfo<Value>& args) {
2141
2154
const int argc = args.Length ();
2142
2155
CHECK_GE (argc, 3 );
2143
2156
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 ;
2146
2159
2147
2160
BufferValue src (isolate, args[0 ]);
2148
2161
CHECK_NOT_NULL (*src);
@@ -2162,14 +2175,23 @@ static void CopyFile(const FunctionCallbackInfo<Value>& args) {
2162
2175
TRACE_STR_COPY (*src),
2163
2176
" dest" ,
2164
2177
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 ());
2168
2190
} else { // copyFile(src, dest, flags)
2169
2191
FSReqWrapSync req_wrap_sync (" copyfile" , *src, *dest);
2170
2192
FS_SYNC_TRACE_BEGIN (copyfile);
2171
2193
SyncCallAndThrowOnError (
2172
- env, &req_wrap_sync, uv_fs_copyfile, *src, *dest, flags);
2194
+ env, &req_wrap_sync, uv_fs_copyfile, *src, *dest, flags. FromJust () );
2173
2195
FS_SYNC_TRACE_END (copyfile);
2174
2196
}
2175
2197
}
0 commit comments