diff --git a/interfaces/kits/js/src/mod_fs/common_func.cpp b/interfaces/kits/js/src/mod_fs/common_func.cpp index 092a9523881d7011333a4b741dfcf144473920ed..c719213b1759cc965de3fb5c5598b7edacd4c186 100644 --- a/interfaces/kits/js/src/mod_fs/common_func.cpp +++ b/interfaces/kits/js/src/mod_fs/common_func.cpp @@ -47,6 +47,33 @@ namespace { }; } +void InitAccessModeType(napi_env env, napi_value exports) +{ + char propertyName[] = "AccessModeType"; + napi_property_descriptor desc[] = { + DECLARE_NAPI_STATIC_PROPERTY("EXIST", NVal::CreateInt32(env, MODE_EXIST).val_), + DECLARE_NAPI_STATIC_PROPERTY("WRITE", NVal::CreateInt32(env, MODE_WRITE).val_), + DECLARE_NAPI_STATIC_PROPERTY("READ", NVal::CreateInt32(env, MODE_READ).val_), + DECLARE_NAPI_STATIC_PROPERTY("READ_WRITE", NVal::CreateInt32(env, MODE_READ_WRITE).val_), + }; + napi_value obj = nullptr; + napi_status status = napi_create_object(env, &obj); + if (status != napi_ok) { + HILOGE("Failed to create object at initializing openMode"); + return; + } + status = napi_define_properties(env, obj, sizeof(desc) / sizeof(desc[0]), desc); + if (status != napi_ok) { + HILOGE("Failed to set properties of character at initializing openMode"); + return; + } + status = napi_set_named_property(env, exports, propertyName, obj); + if (status != napi_ok) { + HILOGE("Failed to set direction property at initializing openMode"); + return; + } +} + void InitOpenMode(napi_env env, napi_value exports) { char propertyName[] = "OpenMode"; diff --git a/interfaces/kits/js/src/mod_fs/common_func.h b/interfaces/kits/js/src/mod_fs/common_func.h index 0b02b3340f502464a9097e7bb8ea7f5a28b8b06a..bfccef135c9dbcac93067437d84c412a19e41c01 100644 --- a/interfaces/kits/js/src/mod_fs/common_func.h +++ b/interfaces/kits/js/src/mod_fs/common_func.h @@ -35,6 +35,11 @@ constexpr int DIRECTORY = UV_FS_O_DIRECTORY; constexpr int NOFOLLOW = UV_FS_O_NOFOLLOW; constexpr int SYNC = UV_FS_O_SYNC; +constexpr unsigned int MODE_EXIST = 00; +constexpr unsigned int MODE_WRITE = 02; +constexpr unsigned int MODE_READ = 04; +constexpr unsigned int MODE_READ_WRITE = 06; + constexpr unsigned int USR_READ_ONLY = 00; constexpr unsigned int USR_WRITE_ONLY = 01; constexpr unsigned int USR_RDWR = 02; @@ -55,6 +60,7 @@ struct FileInfo { std::unique_ptr fdg = { nullptr }; }; +void InitAccessModeType(napi_env env, napi_value exports); void InitOpenMode(napi_env env, napi_value exports); void InitWhenceType(napi_env env, napi_value exports); diff --git a/interfaces/kits/js/src/mod_fs/module.cpp b/interfaces/kits/js/src/mod_fs/module.cpp index 70086e99452fb9fcd5ba0825956b6f3cc3d00b51..2322aad76e4c861ee2702f292180e8767eb3d3ee 100644 --- a/interfaces/kits/js/src/mod_fs/module.cpp +++ b/interfaces/kits/js/src/mod_fs/module.cpp @@ -36,6 +36,7 @@ namespace FileManagement { namespace ModuleFileIO { static napi_value Export(napi_env env, napi_value exports) { + InitAccessModeType(env, exports); InitOpenMode(env, exports); InitWhenceType(env, exports); std::vector> products; diff --git a/interfaces/kits/js/src/mod_fs/properties/prop_n_exporter.cpp b/interfaces/kits/js/src/mod_fs/properties/prop_n_exporter.cpp index 0bd0310e3e20b8b2f57f15cebb17178dd6ef79b8..578d724d9a2007c9e2870ea17ab697faa6eeddee 100644 --- a/interfaces/kits/js/src/mod_fs/properties/prop_n_exporter.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/prop_n_exporter.cpp @@ -66,7 +66,7 @@ namespace ModuleFileIO { using namespace std; using namespace OHOS::FileManagement::LibN; -static int AccessCore(const string &path) +static int AccessCore(const string &path, int mode) { std::unique_ptr access_req = { new uv_fs_t, CommonFunc::fs_req_cleanup }; @@ -74,14 +74,29 @@ static int AccessCore(const string &path) HILOGE("Failed to request heap memory."); return ENOMEM; } - int ret = uv_fs_access(nullptr, access_req.get(), path.c_str(), 0, nullptr); + int ret = uv_fs_access(nullptr, access_req.get(), path.c_str(), mode, nullptr); return ret; } +static int GetMode(NVal secondVar, bool *hasMode) +{ + if (secondVar.TypeIs(napi_number)) { + bool succ = false; + int mode = 0; + *hasMode = true; + tie(succ, mode) = secondVar.ToInt32(); + if (succ && (mode & 0x06) == mode) { + return mode; + } + } + + return -1; +} + napi_value PropNExporter::AccessSync(napi_env env, napi_callback_info info) { NFuncArg funcArg(env, info); - if (!funcArg.InitArgs(NARG_CNT::ONE)) { + if (!funcArg.InitArgs(NARG_CNT::ONE, NARG_CNT::TWO)) { HILOGE("Number of arguments unmatched"); NError(EINVAL).ThrowErr(env); return nullptr; @@ -94,8 +109,19 @@ napi_value PropNExporter::AccessSync(napi_env env, napi_callback_info info) return nullptr; } + bool hasMode = false; + int mode = 0; + if (funcArg.GetArgc() == NARG_CNT::TWO) { + mode = GetMode(NVal(env, funcArg[NARG_POS::SECOND]), &hasMode); + } + if (mode < 0 && hasMode) { + HILOGE("Invalid mode from JS second argument"); + NError(EINVAL).ThrowErr(env); + return nullptr; + } + bool isAccess = false; - int ret = AccessCore(path.get()); + int ret = AccessCore(path.get(), mode); if (ret < 0 && (string_view(uv_err_name(ret)) != "ENOENT")) { HILOGE("Failed to access file by path"); NError(ret).ThrowErr(env); @@ -123,14 +149,25 @@ napi_value PropNExporter::Access(napi_env env, napi_callback_info info) return nullptr; } + bool hasMode = false; + int mode = 0; + if (funcArg.GetArgc() == NARG_CNT::TWO) { + mode = GetMode(NVal(env, funcArg[NARG_POS::SECOND]), &hasMode); + } + if (mode < 0 && hasMode) { + HILOGE("Invalid mode from JS second argument"); + NError(EINVAL).ThrowErr(env); + return nullptr; + } + auto result = CreateSharedPtr(); if (result == nullptr) { HILOGE("Failed to request heap memory."); NError(ENOMEM).ThrowErr(env); return nullptr; } - auto cbExec = [path = string(tmp.get()), result]() -> NError { - int ret = AccessCore(path); + auto cbExec = [path = string(tmp.get()), result, mode]() -> NError { + int ret = AccessCore(path, mode); if (ret == 0) { result->isAccess = true; } @@ -145,7 +182,7 @@ napi_value PropNExporter::Access(napi_env env, napi_callback_info info) }; NVal thisVar(env, funcArg.GetThisVar()); - if (funcArg.GetArgc() == NARG_CNT::ONE) { + if (funcArg.GetArgc() == NARG_CNT::ONE || hasMode) { return NAsyncWorkPromise(env, thisVar).Schedule(PROCEDURE_ACCESS_NAME, cbExec, cbComplete).val_; } else { NVal cb(env, funcArg[NARG_POS::SECOND]); @@ -248,7 +285,7 @@ static NError MkdirExec(const string &path, bool recursion, bool hasOption) { #if !defined(WIN_PLATFORM) && !defined(IOS_PLATFORM) if (hasOption) { - int ret = AccessCore(path); + int ret = AccessCore(path, 0); if (ret == ERRNO_NOERR) { HILOGE("The path already exists"); return NError(EEXIST); @@ -261,7 +298,7 @@ static NError MkdirExec(const string &path, bool recursion, bool hasOption) HILOGE("Failed to create directories, error: %{public}d", errno); return NError(errno); } - ret = AccessCore(path); + ret = AccessCore(path, 0); if (ret) { HILOGE("Failed to verify the result of Mkdirs function"); return NError(ret);