diff --git a/include/cmd/package_parser.h b/include/cmd/package_parser.h index 4c77afe..21b59e5 100644 --- a/include/cmd/package_parser.h +++ b/include/cmd/package_parser.h @@ -86,7 +86,7 @@ private: uint32_t ParseTargetConfig(const std::string &argValue); uint32_t AddCompressionPath(const std::string &argValue); uint32_t ParseThread(const std::string &argValue); - uint32_t ParseIgnoreFileRegex(const std::string &argValue, const bool &isIgnorePath = false); + uint32_t ParseIgnoreRegex(const std::string &argValue, const std::string &option); static const struct option CMD_OPTS[]; static const std::string CMD_PARAMS; diff --git a/include/resconfig_parser.h b/include/resconfig_parser.h index 1fc410d..1230734 100644 --- a/include/resconfig_parser.h +++ b/include/resconfig_parser.h @@ -39,7 +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, const bool &isPath); + 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 54bce37..2873406 100644 --- a/include/resource_util.h +++ b/include/resource_util.h @@ -271,26 +271,21 @@ public: * @brief add ignore file regex pattern * @param regex: the regex pattern * @param ignoreType: the ignore file type + * @param option: the ignore option name * @return if add succeed, return true */ - static bool AddIgnoreFileRegex(const std::string ®ex, IgnoreType ignoreType); + static bool AddIgnoreRegex(const std::string ®ex, IgnoreType ignoreType, const std::string &option); /** * @brief set whether use custom ignore regex pattern - * @param isUseCustomRegex: true is use custom ignore file regex + * @param option:the ignore option name */ - static void SetUseCustomIgnoreRegex(const bool &isUseCustomRegex); + static void SetIgnoreOption(const std::string &option); /** - * @brief get whether use custom ignore regex pattern + * @brief check whether the ignore options is duplicate or conflict. */ - static bool IsUseCustomIgnoreRegex(); - - /** - * @brief set whether to use ignore regex pattern matching for file paths - * @param isUseCustomRegex: true is use custom ignore file regex - */ - static void SetIgnorePath(const bool &isIgnorePath); + static bool CheckIgnoreOption(const std::string &option); /** * @brief Add resource id of har to global set diff --git a/include/restool_errors.h b/include/restool_errors.h index 6ed2694..7e5cf45 100644 --- a/include/restool_errors.h +++ b/include/restool_errors.h @@ -168,6 +168,16 @@ public: return *this; } + template + ErrorInfo &FormatSolution(size_t index, Args... args) + { + if (index < solutions_.size()) { + std::string &solution = solutions_[index]; + solution = FormatString(solution, std::forward(args)...); + } + return *this; + } + ErrorInfo &SetPosition(const std::string &position) { position_ = position; diff --git a/src/cmd/package_parser.cpp b/src/cmd/package_parser.cpp index f7825bd..b9d29a4 100644 --- a/src/cmd/package_parser.cpp +++ b/src/cmd/package_parser.cpp @@ -331,7 +331,7 @@ uint32_t PackageParser::CheckParam() const } if (isTargetConfig_ && !append_.empty()) { - PrintError(GetError(ERR_CODE_EXCLUSIVE_OPTION).FormatCause("-x", "--target-config")); + PrintError(GetError(ERR_CODE_EXCLUSIVE_OPTION).FormatCause("-x", "--target-config", "cannot be used together")); return RESTOOL_ERROR; } @@ -350,7 +350,7 @@ uint32_t PackageParser::CheckParam() const } if (startId_ != 0 && !idDefinedInputPath_.empty()) { - PrintError(GetError(ERR_CODE_EXCLUSIVE_OPTION).FormatCause("-e", "--defined-ids")); + PrintError(GetError(ERR_CODE_EXCLUSIVE_OPTION).FormatCause("-e", "--defined-ids", "cannot be used together")); return RESTOOL_ERROR; } @@ -479,18 +479,16 @@ uint32_t PackageParser::ParseThread(const std::string &argValue) return RESTOOL_SUCCESS; } -uint32_t PackageParser::ParseIgnoreFileRegex(const std::string &argValue, const bool &isIgnorePath) +uint32_t PackageParser::ParseIgnoreRegex(const std::string &argValue, const std::string &option) { - if (ResourceUtil::IsUseCustomIgnoreRegex()) { - PrintError(GetError(ERR_CODE_EXCLUSIVE_OPTION).FormatCause("--ignored-file", "--ignored-path")); + if (!ResourceUtil::CheckIgnoreOption(option)) { return RESTOOL_ERROR; } std::stringstream in(argValue); std::string regex; - ResourceUtil::SetUseCustomIgnoreRegex(true); - ResourceUtil::SetIgnorePath(isIgnorePath); + ResourceUtil::SetIgnoreOption(option); while (getline(in, regex, ':')) { - bool isSucceed = ResourceUtil::AddIgnoreFileRegex(regex, IgnoreType::IGNORE_ALL); + bool isSucceed = ResourceUtil::AddIgnoreRegex(regex, IgnoreType::IGNORE_ALL, option); if (!isSucceed) { return RESTOOL_ERROR; } @@ -573,8 +571,8 @@ void PackageParser::InitCommand() handles_.emplace(Option::DEFINED_SYSIDS, bind(&PackageParser::AddSysIdDefined, this, _1)); handles_.emplace(Option::COMPRESSED_CONFIG, bind(&PackageParser::AddCompressionPath, this, _1)); handles_.emplace(Option::THREAD, bind(&PackageParser::ParseThread, this, _1)); - handles_.emplace(Option::IGNORED_FILE, bind(&PackageParser::ParseIgnoreFileRegex, this, _1, false)); - handles_.emplace(Option::IGNORED_PATH, bind(&PackageParser::ParseIgnoreFileRegex, this, _1, true)); + handles_.emplace(Option::IGNORED_FILE, bind(&PackageParser::ParseIgnoreRegex, this, _1, "--ignored-file")); + handles_.emplace(Option::IGNORED_PATH, bind(&PackageParser::ParseIgnoreRegex, this, _1, "--ignored-path")); } uint32_t PackageParser::HandleProcess(int c, const string &argValue) diff --git a/src/resconfig_parser.cpp b/src/resconfig_parser.cpp index 6ab7726..19e5a7a 100644 --- a/src/resconfig_parser.cpp +++ b/src/resconfig_parser.cpp @@ -95,9 +95,9 @@ void ResConfigParser::InitFileListCommand(HandleBack callback) fileListHandles_.emplace("thread", bind(&ResConfigParser::GetNumber, this, "thread", _1, Option::THREAD, callback)); fileListHandles_.emplace("ignoreResourcePattern", bind(&ResConfigParser::GetIgnorePatterns, this, - "ignoreResourcePattern", _1, Option::IGNORED_FILE, false)); + "ignoreResourcePattern", _1, Option::IGNORED_FILE)); fileListHandles_.emplace("ignoreResourcePathPattern", bind(&ResConfigParser::GetIgnorePatterns, this, - "ignoreResourcePathPattern", _1, Option::IGNORED_PATH, true)); + "ignoreResourcePathPattern", _1, Option::IGNORED_PATH)); } uint32_t ResConfigParser::GetString(const std::string &nodeName, const cJSON *node, int c, HandleBack callback) @@ -201,20 +201,18 @@ 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, const bool &isPath) +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; } - if (ResourceUtil::IsUseCustomIgnoreRegex()) { - PrintError(GetError(ERR_CODE_EXCLUSIVE_OPTION).FormatCause("--ignored-file", "--ignored-path")); + if (!ResourceUtil::CheckIgnoreOption(nodeName)) { return RESTOOL_ERROR; } - ResourceUtil::SetUseCustomIgnoreRegex(true); - ResourceUtil::SetIgnorePath(isPath); - HandleBack callback = [](int c, const string &argValue) { - bool isSucceed = ResourceUtil::AddIgnoreFileRegex(argValue, IgnoreType::IGNORE_ALL); + ResourceUtil::SetIgnoreOption(nodeName); + HandleBack callback = [nodeName](int c, const string &argValue) { + bool isSucceed = ResourceUtil::AddIgnoreRegex(argValue, IgnoreType::IGNORE_ALL, nodeName); if (!isSucceed) { return RESTOOL_ERROR; } diff --git a/src/resource_util.cpp b/src/resource_util.cpp index e2d0ff7..a3f9e6b 100644 --- a/src/resource_util.cpp +++ b/src/resource_util.cpp @@ -41,8 +41,10 @@ const map ResourceUtil::DEFAULT_IGNORE_FILE_REGEX = { { "thumbs\\.db", IgnoreType::IGNORE_ALL }, { ".+~", IgnoreType::IGNORE_ALL } }; -static std::map g_userIgnoreFileRegex; +const std::set IGNORE_PATH_OPTIONS = { "--ignored-path", "ignoreResourcePathPattern" }; +static std::map g_userIgnoreRegex; static bool g_isUseCustomRegex = false; +static std::string g_ignoreOption; static bool g_isIgnorePath = false; static std::set g_harResourceIds; @@ -262,7 +264,7 @@ bool ResourceUtil::IsIgnoreFile(const FileEntry &fileEntry) string filePath = fileEntry.GetFilePath().GetPath(); filePath = regex_replace(filePath, regex("\\\\+"), "/"); if (g_isUseCustomRegex) { - regexs = g_userIgnoreFileRegex; + regexs = g_userIgnoreRegex; regexSources = "user"; } else { regexs = DEFAULT_IGNORE_FILE_REGEX; @@ -537,31 +539,35 @@ string ResourceUtil::KeyTypeToStr(KeyType type) return ret; } -bool ResourceUtil::AddIgnoreFileRegex(const std::string ®ex, IgnoreType ignoreType) +bool ResourceUtil::AddIgnoreRegex(const std::string ®ex, IgnoreType ignoreType, const std::string &option) { try { std::regex rg(regex); } catch (std::regex_error err) { - PrintError(GetError(ERR_CODE_INVALID_IGNORE_FILE).FormatCause(regex.c_str(), err.what())); + PrintError(GetError(ERR_CODE_INVALID_IGNORE_FILE).FormatCause(regex.c_str(), err.what()) + .FormatSolution(0, option.c_str())); return false; } - g_userIgnoreFileRegex[regex] = ignoreType; + g_userIgnoreRegex[regex] = ignoreType; return true; } -void ResourceUtil::SetUseCustomIgnoreRegex(const bool &isUseCustomRegex) +void ResourceUtil::SetIgnoreOption(const std::string &option) { - g_isUseCustomRegex = isUseCustomRegex; + g_ignoreOption = option; + g_isUseCustomRegex = !option.empty(); + g_isIgnorePath = IGNORE_PATH_OPTIONS.count(option); } -void ResourceUtil::SetIgnorePath(const bool &isIgnorePath) +bool ResourceUtil::CheckIgnoreOption(const std::string &option) { - g_isIgnorePath = isIgnorePath; -} - -bool ResourceUtil::IsUseCustomIgnoreRegex() -{ - return g_isUseCustomRegex; + if (!g_ignoreOption.empty()) { + std::string msg = g_ignoreOption == option ? "are duplicate" : "cannot be used together"; + PrintError( + GetError(ERR_CODE_EXCLUSIVE_OPTION).FormatCause(g_ignoreOption.c_str(), option.c_str(), msg.c_str())); + return false; + } + return true; } void ResourceUtil::AddHarResourceId(int64_t harId) diff --git a/src/restool_errors.cpp b/src/restool_errors.cpp index 2461c89..f6d9a2e 100644 --- a/src/restool_errors.cpp +++ b/src/restool_errors.cpp @@ -271,7 +271,7 @@ const std::map ERRORS_MAP = { { ERR_CODE_EXCLUSIVE_OPTION, { ERR_CODE_EXCLUSIVE_OPTION, ERR_TYPE_COMMAND_PARSE, - "Options '%s' and '%s' cannot be used together.", + "Options '%s' and '%s' %s.", "", {}, {} } }, @@ -314,9 +314,9 @@ const std::map ERRORS_MAP = { { ERR_CODE_INVALID_IGNORE_FILE, { ERR_CODE_INVALID_IGNORE_FILE, ERR_TYPE_COMMAND_PARSE, - "Invalid ignore file pattern '%s', %s", + "Invalid ignore pattern '%s', %s", "", - { "Make sure the argument of the option --ignored-file is not empty and contains valid regular expressions." }, + { "Make sure the argument of the option '%s' contains valid regular expressions." }, {} } }, // 11211xxx