From 7e78464c0338b98235c6fc1effcd341365f8698c Mon Sep 17 00:00:00 2001 From: fengjq Date: Sun, 25 Feb 2024 18:42:54 +0800 Subject: [PATCH] Added SDK version judgment for chmod API Signed-off-by: fengjq --- interfaces/kits/js/BUILD.gn | 7 +- .../kits/js/src/mod_fileio/common_func.cpp | 24 +++++++ .../kits/js/src/mod_fileio/common_func.h | 3 + .../js/src/mod_fileio/properties/chmod.cpp | 65 +++++++++++++++++- .../js/src/mod_fileio/properties/fchmod.cpp | 67 ++++++++++++++++++- 5 files changed, 159 insertions(+), 7 deletions(-) diff --git a/interfaces/kits/js/BUILD.gn b/interfaces/kits/js/BUILD.gn index 20648a0b7..1f32001d7 100644 --- a/interfaces/kits/js/BUILD.gn +++ b/interfaces/kits/js/BUILD.gn @@ -1,4 +1,4 @@ -# Copyright (c) 2021-2023 Huawei Device Co., Ltd. +# Copyright (c) 2021-2024 Huawei Device Co., Ltd. # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at @@ -103,8 +103,13 @@ ohos_shared_library("fileio") { external_deps = [ "bounds_checking_function:libsec_shared", + "bundle_framework:appexecfwk_base", + "bundle_framework:appexecfwk_core", + "c_utils:utils", "hilog:libhilog", + "ipc:ipc_core", "napi:ace_napi", + "samgr:samgr_proxy", ] defines = [ "OPENSSL_SUPPRESS_DEPRECATED" ] } diff --git a/interfaces/kits/js/src/mod_fileio/common_func.cpp b/interfaces/kits/js/src/mod_fileio/common_func.cpp index e62e54069..c0d31f69d 100644 --- a/interfaces/kits/js/src/mod_fileio/common_func.cpp +++ b/interfaces/kits/js/src/mod_fileio/common_func.cpp @@ -21,6 +21,9 @@ #include #include +#include "bundle_mgr_interface.h" +#include "iservice_registry.h" +#include "system_ability_definition.h" #include "../common/log.h" #include "../common/napi/n_class.h" #include "../common/napi/n_func_arg.h" @@ -363,6 +366,27 @@ tuple, void *, int64_t, bool, int64_t> CommonFunc::GetW return { true, move(bufferGuard), buf, retLen, hasPos, retPos }; } +bool CommonFunc::GetBundleInfo(AppExecFwk::BundleInfo &bundleInfo) +{ + sptr systemAbilityManager = + SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); + if (!systemAbilityManager) { + HILOGE("Failed to get system ability manager."); + return false; + } + sptr remoteObject = systemAbilityManager->GetSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID); + if (!remoteObject) { + HILOGE("Failed to get bundle manager service."); + return false; + } + auto iBundleMgr = iface_cast(remoteObject); + if (iBundleMgr == nullptr) { + HILOGE("Bundle manager is nullptr."); + return false; + } + return (iBundleMgr->GetBundleInfoForSelf( + (static_cast(AppExecFwk::GetBundleInfoFlag::GET_BUNDLE_INFO_WITH_APPLICATION)), bundleInfo) == ERR_OK); +} } // namespace ModuleFileIO } // namespace DistributedFS } // namespace OHOS diff --git a/interfaces/kits/js/src/mod_fileio/common_func.h b/interfaces/kits/js/src/mod_fileio/common_func.h index b76ad5469..0605111cf 100644 --- a/interfaces/kits/js/src/mod_fileio/common_func.h +++ b/interfaces/kits/js/src/mod_fileio/common_func.h @@ -16,6 +16,7 @@ #ifndef INTERFACES_KITS_JS_SRC_MOD_FILEIO_COMMON_FUNC_H #define INTERFACES_KITS_JS_SRC_MOD_FILEIO_COMMON_FUNC_H +#include "bundle_info.h" #include "../common/napi/uni_header.h" namespace OHOS { @@ -32,6 +33,7 @@ constexpr int NONBLOCK = 04000; constexpr int DIRECTORY = 0200000; constexpr int NOFOLLOW = 0400000; constexpr int SYNC = 04010000; +constexpr int32_t API_TARGET_VERSION = 11; void InitOpenMode(napi_env env, napi_value exports); @@ -50,6 +52,7 @@ struct CommonFunc { napi_value argWBuf, napi_value argOption); static std::tuple, std::unique_ptr> GetCopyPathArg(napi_env env, napi_value srcPath, napi_value dstPath); + static bool GetBundleInfo(AppExecFwk::BundleInfo &bundleInfo); }; } // namespace ModuleFileIO } // namespace DistributedFS diff --git a/interfaces/kits/js/src/mod_fileio/properties/chmod.cpp b/interfaces/kits/js/src/mod_fileio/properties/chmod.cpp index adcce280d..43dbef751 100644 --- a/interfaces/kits/js/src/mod_fileio/properties/chmod.cpp +++ b/interfaces/kits/js/src/mod_fileio/properties/chmod.cpp @@ -20,6 +20,7 @@ #include #include +#include "../common_func.h" #include "../../common/napi/n_async/n_async_work_callback.h" #include "../../common/napi/n_async/n_async_work_promise.h" #include "../../common/napi/n_func_arg.h" @@ -31,18 +32,76 @@ using namespace std; napi_value Chmod::Sync(napi_env env, napi_callback_info info) { + NFuncArg funcArg(env, info); + if (!funcArg.InitArgs(NARG_CNT::TWO)) { + UniError(EINVAL).ThrowErr(env, "Number of arguments unmatched"); + return nullptr; + } + + auto [resGetFirstArg, path, unused] = NVal(env, funcArg[NARG_POS::FIRST]).ToUTF8String(); + if (!resGetFirstArg) { + UniError(EINVAL).ThrowErr(env, "Invalid path"); + return nullptr; + } + + auto [resGetSecondArg, mode] = NVal(env, funcArg[NARG_POS::SECOND]).ToInt32(); + if (!resGetSecondArg) { + UniError(EINVAL).ThrowErr(env, "Invalid mode"); + return nullptr; + } + + AppExecFwk::BundleInfo bundleInfo; + if (!CommonFunc::GetBundleInfo(bundleInfo)) { + UniError(E_UKERR).ThrowErr(env, "GetBundleInfo failed."); + return nullptr; + } + if (bundleInfo.applicationInfo.apiTargetVersion >= API_TARGET_VERSION) { + return NVal::CreateUndefined(env).val_; + } + + if (chmod(path.get(), mode) == -1) { + UniError(errno).ThrowErr(env); + return nullptr; + } + return NVal::CreateUndefined(env).val_; } napi_value Chmod::Async(napi_env env, napi_callback_info info) { NFuncArg funcArg(env, info); - funcArg.InitArgs(NARG_CNT::TWO, NARG_CNT::THREE); - auto cbExec = [](napi_env env) -> UniError { - return UniError(ERRNO_NOERR); + if (!funcArg.InitArgs(NARG_CNT::TWO, NARG_CNT::THREE)) { + UniError(EINVAL).ThrowErr(env, "Number of arguments unmatched"); + return nullptr; + } + + auto [resGetFirstArg, tmp, unused] = NVal(env, funcArg[NARG_POS::FIRST]).ToUTF8String(); + if (!resGetFirstArg) { + UniError(EINVAL).ThrowErr(env, "Invalid path"); + return nullptr; + } + + auto [resGetSecondArg, mode] = NVal(env, funcArg[NARG_POS::SECOND]).ToInt32(); + if (!resGetSecondArg) { + UniError(EINVAL).ThrowErr(env, "Invalid mode"); + } + + auto cbExec = [path = string(tmp.get()), mode = mode](napi_env env) -> UniError { + AppExecFwk::BundleInfo bundleInfo; + if (!CommonFunc::GetBundleInfo(bundleInfo)) { + return UniError(E_UKERR); + } + if (bundleInfo.applicationInfo.apiTargetVersion >= API_TARGET_VERSION || chmod(path.c_str(), mode) == 0) { + return UniError(ERRNO_NOERR); + } else { + return UniError(errno); + } }; auto cbComplete = [](napi_env env, UniError err) -> NVal { + if (err) { + return { env, err.GetNapiErr(env) }; + } return { NVal::CreateUndefined(env) }; }; diff --git a/interfaces/kits/js/src/mod_fileio/properties/fchmod.cpp b/interfaces/kits/js/src/mod_fileio/properties/fchmod.cpp index 97f75b336..62bd80741 100644 --- a/interfaces/kits/js/src/mod_fileio/properties/fchmod.cpp +++ b/interfaces/kits/js/src/mod_fileio/properties/fchmod.cpp @@ -20,9 +20,11 @@ #include #include +#include "../common_func.h" #include "../../common/napi/n_async/n_async_work_callback.h" #include "../../common/napi/n_async/n_async_work_promise.h" #include "../../common/napi/n_func_arg.h" + namespace OHOS { namespace DistributedFS { namespace ModuleFileIO { @@ -30,18 +32,77 @@ using namespace std; napi_value Fchmod::Sync(napi_env env, napi_callback_info info) { + NFuncArg funcArg(env, info); + if (!funcArg.InitArgs(NARG_CNT::TWO)) { + UniError(EINVAL).ThrowErr(env, "Number of arguments unmatched"); + return nullptr; + } + + auto [resGetFirstArg, fd] = NVal(env, funcArg[NARG_POS::FIRST]).ToInt32(); + if (!resGetFirstArg) { + UniError(EINVAL).ThrowErr(env, "Invalid fd"); + return nullptr; + } + + auto [resGetSecondArg, mode] = NVal(env, funcArg[NARG_POS::SECOND]).ToInt32(); + if (!resGetSecondArg) { + UniError(EINVAL).ThrowErr(env, "Invalid mode"); + return nullptr; + } + + AppExecFwk::BundleInfo bundleInfo; + if (!CommonFunc::GetBundleInfo(bundleInfo)) { + UniError(E_UKERR).ThrowErr(env, "GetBundleInfo failed."); + return nullptr; + } + if (bundleInfo.applicationInfo.apiTargetVersion >= API_TARGET_VERSION) { + return NVal::CreateUndefined(env).val_; + } + + if (fchmod(fd, mode) == -1) { + UniError(errno).ThrowErr(env); + return nullptr; + } + return NVal::CreateUndefined(env).val_; } napi_value Fchmod::Async(napi_env env, napi_callback_info info) { NFuncArg funcArg(env, info); - funcArg.InitArgs(NARG_CNT::TWO, NARG_CNT::THREE); - auto cbExec = [](napi_env env) -> UniError { - return UniError(ERRNO_NOERR); + if (!funcArg.InitArgs(NARG_CNT::TWO, NARG_CNT::THREE)) { + UniError(EINVAL).ThrowErr(env, "Number of arguments unmatched"); + return nullptr; + } + + auto [resGetFirstArg, fd] = NVal(env, funcArg[NARG_POS::FIRST]).ToInt32(); + if (!resGetFirstArg) { + UniError(EINVAL).ThrowErr(env, "Invalid fd"); + return nullptr; + } + + auto [resGetSecondArg, mode] = NVal(env, funcArg[NARG_POS::SECOND]).ToInt32(); + if (!resGetSecondArg) { + UniError(EINVAL).ThrowErr(env, "Invalid owner"); + return nullptr; + } + + auto cbExec = [fd = fd, mode = mode](napi_env env) -> UniError { + AppExecFwk::BundleInfo bundleInfo; + if (!CommonFunc::GetBundleInfo(bundleInfo)) { + return UniError(E_UKERR); + } + if (bundleInfo.applicationInfo.apiTargetVersion >= API_TARGET_VERSION || fchmod(fd, mode) == 0) { + return UniError(ERRNO_NOERR); + } else { + return UniError(errno); + } }; auto cbComplCallback = [](napi_env env, UniError err) -> NVal { + if (err) { + return { env, err.GetNapiErr(env) }; + } return { NVal::CreateUndefined(env) }; }; -- Gitee