diff --git a/include/resconfig_parser.h b/include/resconfig_parser.h index 5a74e0d..1230734 100644 --- a/include/resconfig_parser.h +++ b/include/resconfig_parser.h @@ -39,6 +39,7 @@ private: uint32_t GetModuleNames(const cJSON *node, int c, HandleBack callback); uint32_t GetBool(const std::string &nodeName, const cJSON *node, int c, HandleBack callback); uint32_t GetNumber(const std::string &nodeName, const cJSON *node, int c, HandleBack callback); + uint32_t GetIgnorePatterns(const std::string &nodeName, const cJSON *node, int c); cJSON *root_; std::string filePath_; }; diff --git a/include/resource_util.h b/include/resource_util.h index 862dc20..de0254d 100644 --- a/include/resource_util.h +++ b/include/resource_util.h @@ -271,8 +271,9 @@ public: * @brief add ignore file regex pattern * @param regex: the regex pattern * @param ignoreType: the ignore file type + * @return if add succeed, return true */ - static void AddIgnoreFileRegex(const std::string ®ex, IgnoreType ignoreType); + static bool AddIgnoreFileRegex(const std::string ®ex, IgnoreType ignoreType); private: static const std::map DEFAULT_IGNORE_FILE_REGEX; diff --git a/src/cmd/package_parser.cpp b/src/cmd/package_parser.cpp index 987c9be..fdc7966 100644 --- a/src/cmd/package_parser.cpp +++ b/src/cmd/package_parser.cpp @@ -486,13 +486,10 @@ uint32_t PackageParser::ParseIgnoreFileRegex(const std::string &argValue) std::stringstream in(argValue); std::string regex; while (getline(in, regex, ':')) { - try { - std::regex rg(regex); - } catch (std::regex_error err) { - PrintError(GetError(ERR_CODE_INVALID_IGNORE_FILE).FormatCause(regex.c_str(), err.what())); + bool isSucceed = ResourceUtil::AddIgnoreFileRegex(regex, IgnoreType::IGNORE_ALL); + if (!isSucceed) { return RESTOOL_ERROR; } - ResourceUtil::AddIgnoreFileRegex(regex, IgnoreType::IGNORE_ALL); } return RESTOOL_SUCCESS; } diff --git a/src/resconfig_parser.cpp b/src/resconfig_parser.cpp index bcff7d9..fc3bde2 100644 --- a/src/resconfig_parser.cpp +++ b/src/resconfig_parser.cpp @@ -94,8 +94,8 @@ void ResConfigParser::InitFileListCommand(HandleBack callback) Option::COMPRESSED_CONFIG, callback)); fileListHandles_.emplace("thread", bind(&ResConfigParser::GetNumber, this, "thread", _1, Option::THREAD, callback)); - fileListHandles_.emplace("ignoreResourcePattern", bind(&ResConfigParser::GetArray, this, "ignoreResourcePattern", - _1, Option::IGNORED_FILE, callback)); + fileListHandles_.emplace("ignoreResourcePattern", bind(&ResConfigParser::GetIgnorePatterns, this, + "ignoreResourcePattern", _1, Option::IGNORED_FILE)); } uint32_t ResConfigParser::GetString(const std::string &nodeName, const cJSON *node, int c, HandleBack callback) @@ -198,6 +198,25 @@ uint32_t ResConfigParser::GetNumber(const std::string &nodeName, const cJSON *no } return RESTOOL_SUCCESS; } + +uint32_t ResConfigParser::GetIgnorePatterns(const std::string &nodeName, const cJSON *node, int c) +{ + if (!node) { + PrintError(GetError(ERR_CODE_JSON_NODE_MISSING).FormatCause(nodeName.c_str()).SetPosition(filePath_)); + return RESTOOL_ERROR; + } + HandleBack callback = [](int c, const string &argValue) { + bool isSucceed = ResourceUtil::AddIgnoreFileRegex(argValue, IgnoreType::IGNORE_ALL); + if (!isSucceed) { + return RESTOOL_ERROR; + } + return RESTOOL_SUCCESS; + }; + if (GetArray(nodeName, node, c, callback) != RESTOOL_SUCCESS) { + return RESTOOL_ERROR; + } + return RESTOOL_SUCCESS; +} } } } diff --git a/src/resource_util.cpp b/src/resource_util.cpp index ee84f39..96d25de 100644 --- a/src/resource_util.cpp +++ b/src/resource_util.cpp @@ -519,9 +519,20 @@ string ResourceUtil::KeyTypeToStr(KeyType type) return ret; } -void ResourceUtil::AddIgnoreFileRegex(const std::string ®ex, IgnoreType ignoreType) +bool ResourceUtil::AddIgnoreFileRegex(const std::string ®ex, IgnoreType ignoreType) { + if (regex.empty()) { + PrintError(GetError(ERR_CODE_INVALID_IGNORE_FILE).FormatCause(regex.c_str(), "empty value.")); + return false; + } + try { + std::regex rg(regex); + } catch (std::regex_error err) { + PrintError(GetError(ERR_CODE_INVALID_IGNORE_FILE).FormatCause(regex.c_str(), err.what())); + return false; + } userIgnoreFileRegex[regex] = ignoreType; + return true; } } }