From 721f2bae0a2134d85cbafc7b2d24971aa353052b Mon Sep 17 00:00:00 2001 From: liuzerun Date: Thu, 21 Mar 2024 08:57:41 +0000 Subject: [PATCH] access Signed-off-by: liuzerun --- interfaces/kits/js/src/mod_fs/common_func.cpp | 27 +++++++++ interfaces/kits/js/src/mod_fs/common_func.h | 6 ++ .../src/mod_fs/properties/prop_n_exporter.cpp | 58 ++++++++++++++++--- 3 files changed, 83 insertions(+), 8 deletions(-) diff --git a/interfaces/kits/js/src/mod_fs/common_func.cpp b/interfaces/kits/js/src/mod_fs/common_func.cpp index 092a95238..c719213b1 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 0b02b3340..bfccef135 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/properties/prop_n_exporter.cpp b/interfaces/kits/js/src/mod_fs/properties/prop_n_exporter.cpp index 0bd0310e3..47b511e6e 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,40 @@ 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(napi_env env, napi_callback_info info, bool *isMode) +{ + NFuncArg funcArg(env, info); + if (!funcArg.InitArgs(NARG_CNT::ONE, NARG_CNT::TWO)) { + HILOGE("Number of arguments unmatched"); + NError(EINVAL).ThrowErr(env); + return nullptr; + } + + if (funcArg.GetArgc() == NARG_CNT::ONE) { + return 0; + } + + if (NVal(env, funcArg[NARG_POS::SECOND]).TypeIS(napi_number)) { + bool succ = false; + int mode = 0; + *isMode = true; + tie(succ, mode) = NVal(env, funcArg[NARG_POS::SECOND]).ToInt32(); + if (succ && (mode == 0 || (mode & 0x06) != 0)) { + 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 +120,16 @@ napi_value PropNExporter::AccessSync(napi_env env, napi_callback_info info) return nullptr; } + bool isMode = false; + int mode = GetMode(env, funcArg, &isMode); + if (mode < 0) { + 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,6 +157,14 @@ napi_value PropNExporter::Access(napi_env env, napi_callback_info info) return nullptr; } + bool isMode = false; + int mode = GetMode(env, funcArg, &isMode); + if (mode < 0 && isMode) { + 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."); @@ -130,7 +172,7 @@ napi_value PropNExporter::Access(napi_env env, napi_callback_info info) return nullptr; } auto cbExec = [path = string(tmp.get()), result]() -> NError { - int ret = AccessCore(path); + int ret = AccessCore(path, mode); if (ret == 0) { result->isAccess = true; } @@ -145,7 +187,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 || isMode) { return NAsyncWorkPromise(env, thisVar).Schedule(PROCEDURE_ACCESS_NAME, cbExec, cbComplete).val_; } else { NVal cb(env, funcArg[NARG_POS::SECOND]); @@ -248,7 +290,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 +303,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); -- Gitee