diff --git a/README.md b/README.md index 531bb9551c0f0411773c5a8e8554e8946221c033..450fc0074040b89c7e286ea856ed3c787024d0a6 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 31f6fbe91eed59df40230a82a3ae19b1025742fa..70c09328bb982c2a8ad12eb2e7edcb290da47b55 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 8845290f0da5e986aeea625119ff7b3457327713..70084186b9e2ba29c5142a79854b42c5327e4a24 100644 --- a/frameworks/config_policy/src/config_policy_utils.c +++ b/frameworks/config_policy/src/config_policy_utils.c @@ -49,16 +49,16 @@ static void GetCfgDirRealPolicyValue(CfgDir *res) if (res == NULL) { return; } - res->realPolicyValue = strdup("/system/etc:/chipset/etc:/sys-prod/etc:/chip-prod/etc"); + res->realPolicyValue = strdup("/system:/chipset:/sys_prod:/chip_prod"); } -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 ce4a9c22c465788b2042aa950c35ab29074921ca..9b39afc7b5469fad49fecf8994d8801f4e800047 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 { @@ -48,24 +46,16 @@ void FreeCfgFiles(CfgFiles *res); 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 +// pathSuffixStr: the relative path of the config file, e.g. "etc/xml/config.xml" // 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 +// pathSuffixStr: the relative path of the config file, e.g. "etc/xml/config.xml" // 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 9aa5c4d37bf3f3ecd41527188511e83b3b038f10..cba1cb9ae10cc84c31e31a013ccbff4ad4d693ab 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 140ba9928722513906c15c4c8ac36f3b1ef731df..dd594e7807acbbe02ed224350f987583fe4aca35 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,19 @@ 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/resource/ohos_test.xml b/test/resource/ohos_test.xml index 4fff2f72b4734b2c87b160014a2cb440ecba9b29..bef5f403b58e01c96898d6ccea92eae27cc0bcd5 100644 --- a/test/resource/ohos_test.xml +++ b/test/resource/ohos_test.xml @@ -16,18 +16,18 @@ - diff --git a/test/unittest/config_policy_utils_test.cpp b/test/unittest/config_policy_utils_test.cpp index 4d8119264ccbcfb53481093df346b7fa2bb60198..7892e6817953a17eaf2d066617916f0d2810a08c 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 b81e974e2efe523e741fde29b763d6eba4136a4f..a6f2afca67869282e6b4bfcd318d3a82495433fe 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