From 7e64a16d8ffb33c4d2a5efcaeddaa2ade8f5d5b3 Mon Sep 17 00:00:00 2001 From: huangke11 Date: Sat, 19 Mar 2022 13:10:19 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E4=BC=98=E5=8C=96=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: huangke11 --- README.md | 2 +- README_zh.md | 2 +- .../config_policy/src/config_policy_utils.c | 15 +++---- .../innerkits/include/config_policy_utils.h | 14 +------ .../kits/js/include/config_policy_napi.h | 2 +- interfaces/kits/js/src/config_policy_napi.cpp | 40 +++++++------------ test/unittest/config_policy_utils_test.cpp | 30 +++----------- test/unittest/config_policy_utils_test.h | 1 - 8 files changed, 29 insertions(+), 77 deletions(-) diff --git a/README.md b/README.md index 531bb95..450fc00 100644 --- a/README.md +++ b/README.md @@ -29,7 +29,7 @@ Call the APIs of the config policy to obtain the configuration directories at di const char *testPathSuffix = "user.xml"; // Set the name of the configuration file. char buf[MAX_PATH_LEN]; -char *filePath = GetOneCfgFile(testPathSuffix, CUST_TYPE_CONFIG, buf, MAX_PATH_LEN); // Obtain the path of the configuration file with the highest priority. +char *filePath = GetOneCfgFile(testPathSuffix, buf, MAX_PATH_LEN); // Obtain the path of the configuration file with the highest priority. ``` ## Constraints diff --git a/README_zh.md b/README_zh.md index 31f6fbe..70c0932 100644 --- a/README_zh.md +++ b/README_zh.md @@ -40,7 +40,7 @@ const char *testPathSuffix = "user.xml"; //设置配置文件名称 char buf[MAX_PATH_LEN]; -char *filePath = GetOneCfgFile(testPathSuffix, CUST_TYPE_CONFIG, buf, MAX_PATH_LEN); //获取最高优先级的配置文件路径 +char *filePath = GetOneCfgFile(testPathSuffix, buf, MAX_PATH_LEN); //获取最高优先级的配置文件路径 ``` ## 约束 diff --git a/frameworks/config_policy/src/config_policy_utils.c b/frameworks/config_policy/src/config_policy_utils.c index 8845290..4e80e3a 100644 --- a/frameworks/config_policy/src/config_policy_utils.c +++ b/frameworks/config_policy/src/config_policy_utils.c @@ -52,13 +52,13 @@ static void GetCfgDirRealPolicyValue(CfgDir *res) res->realPolicyValue = strdup("/system/etc:/chipset/etc:/sys-prod/etc:/chip-prod/etc"); } -char *GetOneCfgFile(const char *pathSuffix, int type, char *buf, unsigned int bufLength) +char *GetOneCfgFile(const char *pathSuffix, char *buf, unsigned int bufLength) { if (pathSuffix == NULL || buf == NULL || bufLength < MAX_PATH_LEN) { return NULL; } *buf = '\0'; - CfgDir *dirs = GetCfgDirListType(type); + CfgDir *dirs = GetCfgDirList(); if (dirs == NULL) { return NULL; } @@ -79,13 +79,13 @@ char *GetOneCfgFile(const char *pathSuffix, int type, char *buf, unsigned int bu return (*buf != '\0') ? buf : NULL; } -CfgFiles *GetCfgFiles(const char *pathSuffix, int type) +CfgFiles *GetCfgFiles(const char *pathSuffix) { if (pathSuffix == NULL) { return NULL; } char buf[MAX_PATH_LEN]; - CfgDir *dirs = GetCfgDirListType(type); + CfgDir *dirs = GetCfgDirList(); if (dirs == NULL) { return NULL; } @@ -111,7 +111,7 @@ CfgFiles *GetCfgFiles(const char *pathSuffix, int type) return files; } -CfgDir *GetCfgDirListType(int type) +CfgDir *GetCfgDirList() { CfgDir *res = (CfgDir *)(malloc(sizeof(CfgDir))); if (res == NULL) { @@ -135,8 +135,3 @@ CfgDir *GetCfgDirListType(int type) } return res; } - -CfgDir *GetCfgDirList(void) -{ - return GetCfgDirListType(CUST_TYPE_CONFIG); -} diff --git a/interfaces/innerkits/include/config_policy_utils.h b/interfaces/innerkits/include/config_policy_utils.h index ce4a9c2..9fea813 100644 --- a/interfaces/innerkits/include/config_policy_utils.h +++ b/interfaces/innerkits/include/config_policy_utils.h @@ -24,8 +24,6 @@ extern "C" { #define MAX_CFG_POLICY_DIRS_CNT 32 // max number of directories #define MAX_PATH_LEN 128 // max length of a filepath -#define CUST_TYPE_CONFIG 0 // for common configs -#define CUST_TYPE_RFU 1 // for future use // Config Files struct CfgFiles { @@ -49,23 +47,15 @@ void FreeCfgDirList(CfgDir *res); // get the highest priority config file // pathSuffixStr: the relative path of the config file, e.g. "xml/config.xml" -// type: CUST_TYPE_CONFIG = 0 for common configs, CUST_TYPE_RFU = 1 for future use // buf: recommended buffer length is MAX_PATH_LEN // return: path of the highest priority config file, return '\0' when such a file is not found -char *GetOneCfgFile(const char *pathSuffix, int type, char *buf, unsigned int bufLength); +char *GetOneCfgFile(const char *pathSuffix, char *buf, unsigned int bufLength); // get config files, ordered by priority from low to high // pathSuffixStr: the relative path of the config file, e.g. "xml/config.xml" -// type: CUST_TYPE_CONFIG = 0 for common configs, CUST_TYPE_RFU = 1 for future use // return: paths of config files // CAUTION: please use FreeCfgFiles() to avoid memory leak. -CfgFiles *GetCfgFiles(const char *pathSuffix, int type); - -// get config directories, ordered by priority from low to high -// type: CUST_TYPE_CONFIG = 0 for common configs, CUST_TYPE_RFU = 1 for future use -// return: paths of config directories -// CAUTION: please use FreeCfgDirList() to avoid memory leak. -CfgDir *GetCfgDirListType(int type); +CfgFiles *GetCfgFiles(const char *pathSuffix); // get config directories, ordered by priority from low to high // return: paths of config directories diff --git a/interfaces/kits/js/include/config_policy_napi.h b/interfaces/kits/js/include/config_policy_napi.h index 9aa5c4d..cba1cb9 100644 --- a/interfaces/kits/js/include/config_policy_napi.h +++ b/interfaces/kits/js/include/config_policy_napi.h @@ -34,7 +34,6 @@ struct ConfigAsyncContext { typedef napi_value (*CreateNapiValue)(napi_env env, ConfigAsyncContext &context); CreateNapiValue createValueFunc_; - int32_t custType_ = 0; std::string relPath_; std::string pathValue_; std::vector paths_; @@ -62,6 +61,7 @@ private: static void NativeGetCfgDirList(napi_env env, void *data); static void NativeCallbackComplete(napi_env env, napi_status status, void *data); static napi_value ParseRelPath(napi_env env, std::string ¶m, napi_value args); + static void CreateArraysValueFunc(ConfigAsyncContext &context); }; } // namespace ConfigPolicy } // namespace Customization diff --git a/interfaces/kits/js/src/config_policy_napi.cpp b/interfaces/kits/js/src/config_policy_napi.cpp index 140ba99..fe223d8 100644 --- a/interfaces/kits/js/src/config_policy_napi.cpp +++ b/interfaces/kits/js/src/config_policy_napi.cpp @@ -127,6 +127,11 @@ std::string ConfigPolicyNapi::GetStringFromNAPI(napi_env env, napi_value value) napi_value ConfigPolicyNapi::HandleAsyncWork(napi_env env, ConfigAsyncContext *context, std::string workName, napi_async_execute_callback execute, napi_async_complete_callback complete) { + if (context == nullptr) { + HiLog::Error(LABEL, "context is nullptr"); + return nullptr; + } + napi_value result = nullptr; if (context->callbackRef_ == nullptr) { napi_create_promise(env, &context->deferred_, &result); @@ -156,7 +161,7 @@ void ConfigPolicyNapi::NativeGetOneCfgFile(napi_env env, void *data) } ConfigAsyncContext *asyncCallbackInfo = (ConfigAsyncContext *)data; char outBuf[MAX_PATH_LEN]; - GetOneCfgFile(asyncCallbackInfo->relPath_.c_str(), asyncCallbackInfo->custType_, outBuf, MAX_PATH_LEN); + GetOneCfgFile(asyncCallbackInfo->relPath_.c_str(), outBuf, MAX_PATH_LEN); asyncCallbackInfo->pathValue_ = std::string(outBuf); asyncCallbackInfo->createValueFunc_ = [](napi_env env, ConfigAsyncContext &context) -> napi_value { napi_value result; @@ -177,35 +182,14 @@ void ConfigPolicyNapi::NativeGetCfgFiles(napi_env env, void *data) } ConfigAsyncContext *asyncCallbackInfo = (ConfigAsyncContext *)data; - CfgFiles *cfgFiles = GetCfgFiles(asyncCallbackInfo->relPath_.c_str(), asyncCallbackInfo->custType_); + CfgFiles *cfgFiles = GetCfgFiles(asyncCallbackInfo->relPath_.c_str()); for (size_t i = 0; i < MAX_CFG_POLICY_DIRS_CNT; i++) { if (cfgFiles != nullptr && cfgFiles->paths[i] != nullptr) { asyncCallbackInfo->paths_.push_back(cfgFiles->paths[i]); } } FreeCfgFiles(cfgFiles); - asyncCallbackInfo->createValueFunc_ = [](napi_env env, ConfigAsyncContext &context) -> napi_value { - napi_value result = nullptr; - napi_status status = napi_create_array_with_length(env, context.paths_.size(), &result); - if (status != napi_ok) { - context.SetErrorMsg("Failed to get cfg files."); - return nullptr; - } - for (size_t i = 0; i < context.paths_.size(); i++) { - napi_value element = nullptr; - status = napi_create_string_utf8(env, context.paths_[i].c_str(), NAPI_AUTO_LENGTH, &element); - if (status != napi_ok) { - context.SetErrorMsg("Failed to create string item."); - return nullptr; - } - status = napi_set_element(env, result, i, element); - if (status != napi_ok) { - context.SetErrorMsg("Failed to set array item."); - return nullptr; - } - } - return result; - }; + CreateArraysValueFunc(*asyncCallbackInfo); } void ConfigPolicyNapi::NativeGetCfgDirList(napi_env env, void *data) @@ -216,14 +200,18 @@ void ConfigPolicyNapi::NativeGetCfgDirList(napi_env env, void *data) } ConfigAsyncContext *asyncCallbackInfo = (ConfigAsyncContext *)data; - CfgDir *cfgDir = GetCfgDirListType(asyncCallbackInfo->custType_); + CfgDir *cfgDir = GetCfgDirList(); for (size_t i = 0; i < MAX_CFG_POLICY_DIRS_CNT; i++) { if (cfgDir != nullptr && cfgDir->paths[i] != nullptr) { asyncCallbackInfo->paths_.push_back(cfgDir->paths[i]); } } FreeCfgDirList(cfgDir); - asyncCallbackInfo->createValueFunc_ = [](napi_env env, ConfigAsyncContext &context) -> napi_value { + CreateArraysValueFunc(*asyncCallbackInfo); +} + +void ConfigPolicyNapi::CreateArraysValueFunc(ConfigAsyncContext &asyncCallbackInfo) { + asyncCallbackInfo.createValueFunc_ = [](napi_env env, ConfigAsyncContext &context) -> napi_value { napi_value result = nullptr; napi_status status = napi_create_array_with_length(env, context.paths_.size(), &result); if (status != napi_ok) { diff --git a/test/unittest/config_policy_utils_test.cpp b/test/unittest/config_policy_utils_test.cpp index 4d81192..7892e68 100644 --- a/test/unittest/config_policy_utils_test.cpp +++ b/test/unittest/config_policy_utils_test.cpp @@ -26,10 +26,11 @@ class ConfigPolicyUtilsTest : public testing::Test {}; bool TestGetCfgFile(const char *testPathSuffix) { - CfgFiles *cfgFiles = GetCfgFiles(testPathSuffix, CUST_TYPE_CONFIG); + CfgFiles *cfgFiles = GetCfgFiles(testPathSuffix); bool flag = false; + char *filePath = nullptr; for (size_t i = 0; i < MAX_CFG_POLICY_DIRS_CNT; i++) { - char *filePath = cfgFiles->paths[i]; + filePath = cfgFiles->paths[i]; if (filePath && *filePath != '\0') { std::cout << "filePath: " << filePath << std::endl; flag = true; @@ -37,7 +38,7 @@ bool TestGetCfgFile(const char *testPathSuffix) } FreeCfgFiles(cfgFiles); char buf[MAX_PATH_LEN]; - char *filePath = GetOneCfgFile(testPathSuffix, CUST_TYPE_CONFIG, buf, MAX_PATH_LEN); + filePath = GetOneCfgFile(testPathSuffix, buf, MAX_PATH_LEN); if (filePath && *filePath != '\0') { std::cout << "one filePath: " << filePath << std::endl; flag = flag && true; @@ -91,31 +92,10 @@ HWTEST_F(ConfigPolicyUtilsTest, CfgPolicyUtilsFuncTest004, TestSize.Level1) /** * @tc.name: CfgPolicyUtilsFuncTest005 - * @tc.desc: Test struct CfgDir *GetCfgDirListType(int type) function. - * @tc.type: FUNC - */ -HWTEST_F(ConfigPolicyUtilsTest, CfgPolicyUtilsFuncTest005, TestSize.Level1) -{ - CfgDir *cfgDir = GetCfgDirListType(CUST_TYPE_RFU); - EXPECT_TRUE(cfgDir != NULL); - bool flag = false; - for (size_t i = 0; i < MAX_CFG_POLICY_DIRS_CNT; i++) { - char *filePath = cfgDir->paths[i]; - if (filePath && *filePath != '\0') { - std::cout << "filePath: " << filePath << std::endl; - flag = true; - } - } - FreeCfgDirList(cfgDir); - EXPECT_TRUE(flag); -} - -/** - * @tc.name: CfgPolicyUtilsFuncTest006 * @tc.desc: Test struct CfgDir *GetCfgDirList(void) function. * @tc.type: FUNC */ -HWTEST_F(ConfigPolicyUtilsTest, CfgPolicyUtilsFuncTest006, TestSize.Level1) +HWTEST_F(ConfigPolicyUtilsTest, CfgPolicyUtilsFuncTest005, TestSize.Level1) { CfgDir *cfgDir = GetCfgDirList(); EXPECT_TRUE(cfgDir != NULL); diff --git a/test/unittest/config_policy_utils_test.h b/test/unittest/config_policy_utils_test.h index b81e974..a6f2afc 100644 --- a/test/unittest/config_policy_utils_test.h +++ b/test/unittest/config_policy_utils_test.h @@ -21,6 +21,5 @@ int CfgPolicyUtilsFuncTest002(void); int CfgPolicyUtilsFuncTest003(void); int CfgPolicyUtilsFuncTest004(void); int CfgPolicyUtilsFuncTest005(void); -int CfgPolicyUtilsFuncTest006(void); #endif -- Gitee From a944f992c50c93e353355d4b5238a04d24952e2c Mon Sep 17 00:00:00 2001 From: huangke11 Date: Sat, 19 Mar 2022 14:00:13 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E4=BC=98=E5=8C=96=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: huangke11 --- interfaces/kits/js/src/config_policy_napi.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/interfaces/kits/js/src/config_policy_napi.cpp b/interfaces/kits/js/src/config_policy_napi.cpp index fe223d8..dd594e7 100644 --- a/interfaces/kits/js/src/config_policy_napi.cpp +++ b/interfaces/kits/js/src/config_policy_napi.cpp @@ -210,7 +210,8 @@ void ConfigPolicyNapi::NativeGetCfgDirList(napi_env env, void *data) CreateArraysValueFunc(*asyncCallbackInfo); } -void ConfigPolicyNapi::CreateArraysValueFunc(ConfigAsyncContext &asyncCallbackInfo) { +void ConfigPolicyNapi::CreateArraysValueFunc(ConfigAsyncContext &asyncCallbackInfo) +{ asyncCallbackInfo.createValueFunc_ = [](napi_env env, ConfigAsyncContext &context) -> napi_value { napi_value result = nullptr; napi_status status = napi_create_array_with_length(env, context.paths_.size(), &result); -- Gitee