|
- /**
- * Copyright 2020 Huawei Technologies 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
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
- #include "util.h"
-
- #include <sys/stat.h>
- #ifdef __GNUC__
- #include <regex.h>
- #else
- #include <regex>
- #endif
- #include <algorithm>
- #include <climits>
- #include <cstdlib>
- #include <ctime>
- #include <fstream>
-
- #include "common/util/error_manager/error_manager.h"
- #include "external/ge/ge_api_error_codes.h"
- #include "framework/common/debug/ge_log.h"
- #include "mmpa/mmpa_api.h"
-
- namespace {
- const int kMaxBuffSize = 256;
- const char *const kPathValidReason = "The path can only contain 'a-z' 'A-Z' '0-9' '-' '.' '_' and chinese character";
- } // namespace
-
- namespace ge {
- /**
- * @ingroup domi_common
- * @brief Create directory, support to create multi-level directory
- * @param [in] directory_path Path, can be multi-level directory
- * @return -1 fail
- * @return 0 success
- */
- int CreateDirectory(const std::string &directory_path) {
- GE_CHK_BOOL_EXEC(!directory_path.empty(), return -1, "directory path is empty.");
- auto dir_path_len = directory_path.length();
- if (dir_path_len >= MMPA_MAX_PATH) {
- ErrorManager::GetInstance().ATCReportErrMessage("E19002", {"filepath", "size"},
- {directory_path, std::to_string(MMPA_MAX_PATH)});
- GELOGW("Path[%s] len is too long, it must be less than %d", directory_path.c_str(), MMPA_MAX_PATH);
- return -1;
- }
- char tmp_dir_path[MMPA_MAX_PATH] = {0};
- for (size_t i = 0; i < dir_path_len; i++) {
- tmp_dir_path[i] = directory_path[i];
- if ((tmp_dir_path[i] == '\\') || (tmp_dir_path[i] == '/')) {
- if (mmAccess2(tmp_dir_path, M_F_OK) != EN_OK) {
- int32_t ret = mmMkdir(tmp_dir_path, M_IRUSR | M_IWUSR | M_IXUSR); // 700
- if (ret != 0) {
- if (errno != EEXIST) {
- ErrorManager::GetInstance().ATCReportErrMessage("E19006", {"path"}, {directory_path});
- GELOGW("Can not create directory %s. Make sure the directory exists and writable.", directory_path.c_str());
- return ret;
- }
- }
- }
- }
- }
- int32_t ret = mmMkdir(const_cast<char *>(directory_path.c_str()), M_IRUSR | M_IWUSR | M_IXUSR); // 700
- if (ret != 0) {
- if (errno != EEXIST) {
- ErrorManager::GetInstance().ATCReportErrMessage("E19006", {"path"}, {directory_path});
- GELOGW("Can not create directory %s. Make sure the directory exists and writable.", directory_path.c_str());
- return ret;
- }
- }
- return 0;
- }
-
- std::string CurrentTimeInStr() {
- std::time_t now = std::time(nullptr);
- std::tm *ptm = std::localtime(&now);
- if (ptm == nullptr) {
- GELOGE(ge::FAILED, "Localtime failed.");
- return "";
- }
-
- const int kTimeBufferLen = 32;
- char buffer[kTimeBufferLen + 1] = {0};
- // format: 20171122042550
- std::strftime(buffer, kTimeBufferLen, "%Y%m%d%H%M%S", ptm);
- return std::string(buffer);
- }
-
- std::string RealPath(const char *path) {
- GE_CHK_BOOL_TRUE_EXEC_WITH_LOG(path == nullptr, return "", "path pointer is NULL.");
- GE_CHK_BOOL_TRUE_EXEC_WITH_LOG(strlen(path) >= MMPA_MAX_PATH,
- ErrorManager::GetInstance().ATCReportErrMessage("E19002", {"filepath", "size"},
- {path, std::to_string(MMPA_MAX_PATH)});
- return "", "Path[%s] len is too long, it must be less than %d", path, MMPA_MAX_PATH);
-
- // Nullptr is returned when the path does not exist or there is no permission
- // Return absolute path when path is accessible
- std::string res;
- char resolved_path[MMPA_MAX_PATH] = {0};
- if (mmRealPath(path, resolved_path, MMPA_MAX_PATH) == EN_OK) {
- res = resolved_path;
- }
-
- return res;
- }
-
- bool CheckInputPathValid(const std::string &file_path, const std::string &atc_param) {
- // The specified path is empty
- std::map<std::string, std::string> args_map;
- if (file_path.empty()) {
- ErrorManager::GetInstance().ATCReportErrMessage("E10004", {"parameter"}, {atc_param});
- GELOGW("Input parameter %s is empty.", file_path.c_str());
- return false;
- }
- std::string real_path = RealPath(file_path.c_str());
- // Unable to get absolute path (does not exist or does not have permission to access)
- if (real_path.empty()) {
- ErrorManager::GetInstance().ATCReportErrMessage("E19000", {"path", "errmsg"}, {file_path, strerror(errno)});
- GELOGW("Path[%s]'s realpath is empty, errmsg[%s]", file_path.c_str(), strerror(errno));
- return false;
- }
-
- // A regular matching expression to verify the validity of the input file path
- // Path section: Support upper and lower case letters, numbers dots(.) chinese and underscores
- // File name section: Support upper and lower case letters, numbers, underscores chinese and dots(.)
- #ifdef __GNUC__
- std::string mode = "^[\u4e00-\u9fa5A-Za-z0-9./_-]+$";
- #else
- std::string mode = "^[a-zA-Z]:([\\\\/][^\\s\\\\/:*?<>\"|][^\\\\/:*?<>\"|]*)*([/\\\\][^\\s\\\\/:*?<>\"|])?$";
- #endif
-
- GE_CHK_BOOL_TRUE_EXEC_WITH_LOG(
- !ValidateStr(real_path, mode),
- ErrorManager::GetInstance().ATCReportErrMessage("E10001", {"parameter", "value", "reason"},
- {atc_param, real_path, kPathValidReason});
- return false, "Invalid value for %s[%s], %s.", atc_param.c_str(), real_path.c_str(), kPathValidReason);
-
- // The absolute path points to a file that is not readable
- if (mmAccess2(real_path.c_str(), M_R_OK) != EN_OK) {
- ErrorManager::GetInstance().ATCReportErrMessage("E19003", {"file", "errmsg"}, {file_path.c_str(), strerror(errno)});
- GELOGW("Read file[%s] failed, errmsg[%s]", file_path.c_str(), strerror(errno));
- return false;
- }
-
- return true;
- }
-
- bool CheckOutputPathValid(const std::string &file_path, const std::string &atc_param) {
- // The specified path is empty
- if (file_path.empty()) {
- ErrorManager::GetInstance().ATCReportErrMessage("E10004", {"parameter"}, {atc_param});
- GELOGW("Input parameter's value is empty.");
- return false;
- }
-
- GE_CHK_BOOL_TRUE_EXEC_WITH_LOG(strlen(file_path.c_str()) >= MMPA_MAX_PATH,
- ErrorManager::GetInstance().ATCReportErrMessage(
- "E19002", {"filepath", "size"}, {file_path, std::to_string(MMPA_MAX_PATH)});
- return "", "Path[%s] len is too long, it must be less than %d", file_path.c_str(),
- MMPA_MAX_PATH);
-
- // A regular matching expression to verify the validity of the input file path
- // Path section: Support upper and lower case letters, numbers dots(.) chinese and underscores
- // File name section: Support upper and lower case letters, numbers, underscores chinese and dots(.)
- #ifdef __GNUC__
- std::string mode = "^[\u4e00-\u9fa5A-Za-z0-9./_-]+$";
- #else
- std::string mode = "^[a-zA-Z]:([\\\\/][^\\s\\\\/:*?<>\"|][^\\\\/:*?<>\"|]*)*([/\\\\][^\\s\\\\/:*?<>\"|])?$";
- #endif
-
- GE_CHK_BOOL_TRUE_EXEC_WITH_LOG(
- !ValidateStr(file_path, mode),
- ErrorManager::GetInstance().ATCReportErrMessage("E10001", {"parameter", "value", "reason"},
- {atc_param, file_path, kPathValidReason});
- return false, "Invalid value for %s[%s], %s.", atc_param.c_str(), file_path.c_str(), kPathValidReason);
-
- std::string real_path = RealPath(file_path.c_str());
- // Can get absolute path (file exists)
- if (!real_path.empty()) {
- // File is not readable or writable
- if (mmAccess2(real_path.c_str(), M_W_OK | M_F_OK) != EN_OK) {
- ErrorManager::GetInstance().ATCReportErrMessage("E19004", {"file", "errmsg"}, {real_path, strerror(errno)});
- GELOGW("Write file[%s] failed, errmsg[%s]", real_path.c_str(), strerror(errno));
- return false;
- }
- } else {
- // Find the last separator
- int path_split_pos = static_cast<int>(file_path.size() - 1);
- for (; path_split_pos >= 0; path_split_pos--) {
- if (file_path[path_split_pos] == '\\' || file_path[path_split_pos] == '/') {
- break;
- }
- }
- if (path_split_pos == 0) {
- return true;
- }
- if (path_split_pos != -1) {
- std::string prefix_path = std::string(file_path).substr(0, static_cast<size_t>(path_split_pos));
- // Determine whether the specified path is valid by creating the path
- if (CreateDirectory(prefix_path) != 0) {
- ErrorManager::GetInstance().ATCReportErrMessage("E19006", {"path"}, {file_path});
- GELOGW("Can not create directory[%s].", file_path.c_str());
- return false;
- }
- }
- }
-
- return true;
- }
-
- bool ValidateStr(const std::string &str, const std::string &mode) {
- #ifdef __GNUC__
- char ebuff[kMaxBuffSize];
- regex_t reg;
- int cflags = REG_EXTENDED | REG_NOSUB;
- int ret = regcomp(®, mode.c_str(), cflags);
- if (ret) {
- regerror(ret, ®, ebuff, kMaxBuffSize);
- GELOGW("regcomp failed, reason: %s", ebuff);
- regfree(®);
- return true;
- }
-
- ret = regexec(®, str.c_str(), 0, NULL, 0);
- if (ret) {
- regerror(ret, ®, ebuff, kMaxBuffSize);
- GELOGE(ge::PARAM_INVALID, "regexec failed, reason: %s", ebuff);
- regfree(®);
- return false;
- }
-
- regfree(®);
- return true;
- #else
- std::wstring wstr(str.begin(), str.end());
- std::wstring wmode(mode.begin(), mode.end());
- std::wsmatch match;
- bool res = false;
-
- try {
- std::wregex reg(wmode, std::regex::icase);
- // Matching string part
- res = regex_match(wstr, match, reg);
- res = regex_search(str, std::regex("[`!@#$%^&*()|{}';',<>?]"));
- } catch (std::exception &ex) {
- GELOGW("The directory %s is invalid, error: %s.", str.c_str(), ex.what());
- return false;
- }
- return !(res) && (str.size() == match.str().size());
- #endif
- }
-
- bool GetNameFromFileName(const std::string &file_name, std::string &base_name) {
- if (file_name.empty()) {
- GELOGW("File path may not valid, check params --output");
- return false;
- }
- size_t start_position = 0;
- // using output as base_name (ignore ".om")
- size_t filename_suffixes = 3;
- if (file_name.find_last_of('/') != std::string::npos) {
- start_position = file_name.find_last_of('/') + 1;
- }
- size_t end_position = file_name.length() - filename_suffixes;
- base_name = file_name.substr(start_position, end_position - start_position);
- if (base_name.empty()) {
- GELOGW("File path may not valid, check params --output");
- return false;
- }
- return true;
- }
- } // namespace ge
|