|
|
@ -51,14 +51,15 @@ namespace {
|
|
|
|
* If such an exception is encountered during operation,
|
|
|
|
* If such an exception is encountered during operation,
|
|
|
|
* the proto file can be divided into several small files or the limit value can be increased.
|
|
|
|
* the proto file can be divided into several small files or the limit value can be increased.
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
const int kProtoReadBytesLimit = INT_MAX; // Max size of 2 GB minus 1 byte.
|
|
|
|
const int kFileSizeOutLimitedOrOpenFailed = -1;
|
|
|
|
const int kWarningThreshold = 536870912 * 2; // 536870912 represent 512M
|
|
|
|
const int kProtoReadBytesLimit = INT_MAX; // Max size of 2 GB minus 1 byte.
|
|
|
|
|
|
|
|
const int kWarningThreshold = 1073741824; // 536870912 * 2 536870912 represent 512M
|
|
|
|
|
|
|
|
|
|
|
|
/// The maximum length of the file.
|
|
|
|
/// The maximum length of the file.
|
|
|
|
const uint32_t kMaxFileSizeLimit = UINT32_MAX; // 4G for now
|
|
|
|
const uint32_t kMaxFileSizeLimit = UINT32_MAX; // 4G for now
|
|
|
|
const int kMaxBuffSize = 256;
|
|
|
|
const int kMaxBuffSize = 256;
|
|
|
|
const char *const kPathValidReason = "The path can only contain 'a-z' 'A-Z' '0-9' '-' '.' '_' and chinese character";
|
|
|
|
const char *const kPathValidReason = "The path can only contain 'a-z' 'A-Z' '0-9' '-' '.' '_' and chinese character";
|
|
|
|
constexpr uint32_t kMaxConfigFileByte = 10 * 1024 * 1024;
|
|
|
|
constexpr uint32_t kMaxConfigFileByte = 10485760; // 10 * 1024 * 1024
|
|
|
|
} // namespace
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
|
|
|
|
namespace ge {
|
|
|
|
namespace ge {
|
|
|
@ -76,7 +77,8 @@ FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY bool ReadProtoFromBinaryFile(co
|
|
|
|
std::string real_path = RealPath(file);
|
|
|
|
std::string real_path = RealPath(file);
|
|
|
|
GE_CHK_BOOL_TRUE_EXEC_WITH_LOG(real_path.empty(), return false, "pb file path '%s' not valid", file);
|
|
|
|
GE_CHK_BOOL_TRUE_EXEC_WITH_LOG(real_path.empty(), return false, "pb file path '%s' not valid", file);
|
|
|
|
|
|
|
|
|
|
|
|
GE_CHK_BOOL_TRUE_EXEC_WITH_LOG(GetFileLength(real_path) == -1, return false, "file size not valid.");
|
|
|
|
GE_CHK_BOOL_TRUE_EXEC_WITH_LOG(GetFileLength(real_path) == kFileSizeOutLimitedOrOpenFailed, return false,
|
|
|
|
|
|
|
|
"file size not valid.");
|
|
|
|
|
|
|
|
|
|
|
|
std::ifstream fs(real_path, std::ifstream::in | std::ifstream::binary);
|
|
|
|
std::ifstream fs(real_path, std::ifstream::in | std::ifstream::binary);
|
|
|
|
if (!fs.is_open()) {
|
|
|
|
if (!fs.is_open()) {
|
|
|
@ -118,20 +120,20 @@ long GetFileLength(const std::string &input_file) {
|
|
|
|
GE_CHK_BOOL_TRUE_EXEC_WITH_LOG(real_path.empty(), return -1, "input_file path '%s' not valid", input_file.c_str());
|
|
|
|
GE_CHK_BOOL_TRUE_EXEC_WITH_LOG(real_path.empty(), return -1, "input_file path '%s' not valid", input_file.c_str());
|
|
|
|
unsigned long long file_length = 0;
|
|
|
|
unsigned long long file_length = 0;
|
|
|
|
GE_CHK_BOOL_TRUE_EXEC_WITH_LOG(
|
|
|
|
GE_CHK_BOOL_TRUE_EXEC_WITH_LOG(
|
|
|
|
mmGetFileSize(input_file.c_str(), &file_length) != EN_OK,
|
|
|
|
mmGetFileSize(input_file.c_str(), &file_length) != EN_OK,
|
|
|
|
ErrorManager::GetInstance().ATCReportErrMessage("E19001", {"file", "errmsg"}, {input_file, strerror(errno)});
|
|
|
|
ErrorManager::GetInstance().ATCReportErrMessage("E19001", {"file", "errmsg"}, {input_file, strerror(errno)});
|
|
|
|
return -1, "Open file[%s] failed. %s", input_file.c_str(), strerror(errno));
|
|
|
|
return kFileSizeOutLimitedOrOpenFailed, "Open file[%s] failed. %s", input_file.c_str(), strerror(errno));
|
|
|
|
|
|
|
|
|
|
|
|
GE_CHK_BOOL_TRUE_EXEC_WITH_LOG((file_length == 0),
|
|
|
|
GE_CHK_BOOL_TRUE_EXEC_WITH_LOG((file_length == 0),
|
|
|
|
ErrorManager::GetInstance().ATCReportErrMessage("E19015", {"filepath"}, {input_file});
|
|
|
|
ErrorManager::GetInstance().ATCReportErrMessage("E19015", {"filepath"}, {input_file});
|
|
|
|
return -1, "File[%s] size is 0, not valid.", input_file.c_str());
|
|
|
|
return -1, "File[%s] size is 0, not valid.", input_file.c_str());
|
|
|
|
|
|
|
|
|
|
|
|
GE_CHK_BOOL_TRUE_EXEC_WITH_LOG(file_length > kMaxFileSizeLimit,
|
|
|
|
GE_CHK_BOOL_TRUE_EXEC_WITH_LOG(
|
|
|
|
ErrorManager::GetInstance().ATCReportErrMessage(
|
|
|
|
file_length > kMaxFileSizeLimit, ErrorManager::GetInstance().ATCReportErrMessage(
|
|
|
|
"E19016", {"filepath", "filesize", "maxlen"},
|
|
|
|
"E19016", {"filepath", "filesize", "maxlen"},
|
|
|
|
{input_file, std::to_string(file_length), std::to_string(kMaxFileSizeLimit)});
|
|
|
|
{input_file, std::to_string(file_length), std::to_string(kMaxFileSizeLimit)});
|
|
|
|
return -1, "File[%s] size %lld is out of limit: %d.", input_file.c_str(), file_length,
|
|
|
|
return kFileSizeOutLimitedOrOpenFailed, "File[%s] size %lld is out of limit: %d.", input_file.c_str(), file_length,
|
|
|
|
kMaxFileSizeLimit);
|
|
|
|
kMaxFileSizeLimit);
|
|
|
|
return static_cast<long>(file_length);
|
|
|
|
return static_cast<long>(file_length);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -187,7 +189,7 @@ FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY bool ReadBytesFromBinaryFile(co
|
|
|
|
std::streamsize size = file.tellg();
|
|
|
|
std::streamsize size = file.tellg();
|
|
|
|
|
|
|
|
|
|
|
|
GE_CHK_BOOL_TRUE_EXEC_WITH_LOG((size <= 0), file.close(); return false, "file length <= 0, not valid.");
|
|
|
|
GE_CHK_BOOL_TRUE_EXEC_WITH_LOG((size <= 0), file.close(); return false, "file length <= 0, not valid.");
|
|
|
|
GE_CHK_BOOL_TRUE_EXEC_WITH_LOG(size > static_cast<int64_t >(kMaxFileSizeLimit), file.close();
|
|
|
|
GE_CHK_BOOL_TRUE_EXEC_WITH_LOG(size > static_cast<int64_t>(kMaxFileSizeLimit), file.close();
|
|
|
|
return false, "file size %ld is out of limit: %d.", size, kMaxFileSizeLimit);
|
|
|
|
return false, "file size %ld is out of limit: %d.", size, kMaxFileSizeLimit);
|
|
|
|
|
|
|
|
|
|
|
|
file.seekg(0, std::ios::beg); // [no need to check value]
|
|
|
|
file.seekg(0, std::ios::beg); // [no need to check value]
|
|
|
@ -210,8 +212,8 @@ FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY int CreateDirectory(const std::
|
|
|
|
GE_CHK_BOOL_EXEC(!directory_path.empty(), return -1, "directory path is empty.");
|
|
|
|
GE_CHK_BOOL_EXEC(!directory_path.empty(), return -1, "directory path is empty.");
|
|
|
|
auto dir_path_len = directory_path.length();
|
|
|
|
auto dir_path_len = directory_path.length();
|
|
|
|
if (dir_path_len >= MMPA_MAX_PATH) {
|
|
|
|
if (dir_path_len >= MMPA_MAX_PATH) {
|
|
|
|
ErrorManager::GetInstance().ATCReportErrMessage(
|
|
|
|
ErrorManager::GetInstance().ATCReportErrMessage("E19002", {"filepath", "size"},
|
|
|
|
"E19002", {"filepath", "size"}, {directory_path, std::to_string(MMPA_MAX_PATH)});
|
|
|
|
{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);
|
|
|
|
GELOGW("Path[%s] len is too long, it must be less than %d", directory_path.c_str(), MMPA_MAX_PATH);
|
|
|
|
return -1;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -224,8 +226,7 @@ FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY int CreateDirectory(const std::
|
|
|
|
if (ret != 0) {
|
|
|
|
if (ret != 0) {
|
|
|
|
if (errno != EEXIST) {
|
|
|
|
if (errno != EEXIST) {
|
|
|
|
ErrorManager::GetInstance().ATCReportErrMessage("E19006", {"path"}, {directory_path});
|
|
|
|
ErrorManager::GetInstance().ATCReportErrMessage("E19006", {"path"}, {directory_path});
|
|
|
|
GELOGW("Can not create directory %s. Make sure the directory exists and writable.",
|
|
|
|
GELOGW("Can not create directory %s. Make sure the directory exists and writable.", directory_path.c_str());
|
|
|
|
directory_path.c_str());
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -265,7 +266,7 @@ FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY bool ReadProtoFromText(const ch
|
|
|
|
|
|
|
|
|
|
|
|
std::string real_path = RealPath(file);
|
|
|
|
std::string real_path = RealPath(file);
|
|
|
|
GE_CHK_BOOL_TRUE_EXEC_WITH_LOG(real_path.empty(), ErrorManager::GetInstance().ATCReportErrMessage(
|
|
|
|
GE_CHK_BOOL_TRUE_EXEC_WITH_LOG(real_path.empty(), ErrorManager::GetInstance().ATCReportErrMessage(
|
|
|
|
"E19000", {"path", "errmsg"}, {file, strerror(errno)});
|
|
|
|
"E19000", {"path", "errmsg"}, {file, strerror(errno)});
|
|
|
|
return false, "Path[%s]'s realpath is empty, errmsg[%s]", file, strerror(errno));
|
|
|
|
return false, "Path[%s]'s realpath is empty, errmsg[%s]", file, strerror(errno));
|
|
|
|
|
|
|
|
|
|
|
|
GE_CHK_BOOL_TRUE_EXEC_WITH_LOG(GetFileLength(real_path) == -1, return false, "file size not valid.");
|
|
|
|
GE_CHK_BOOL_TRUE_EXEC_WITH_LOG(GetFileLength(real_path) == -1, return false, "file size not valid.");
|
|
|
@ -301,13 +302,13 @@ FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY bool ReadProtoFromMem(const cha
|
|
|
|
google::protobuf::io::IstreamInputStream input(&fs);
|
|
|
|
google::protobuf::io::IstreamInputStream input(&fs);
|
|
|
|
bool ret = google::protobuf::TextFormat::Parse(&input, message);
|
|
|
|
bool ret = google::protobuf::TextFormat::Parse(&input, message);
|
|
|
|
GE_IF_BOOL_EXEC(
|
|
|
|
GE_IF_BOOL_EXEC(
|
|
|
|
!ret, GELOGE(ret, "Call [google::protobuf::TextFormat::Parse] func ret fail, please check your text file."));
|
|
|
|
!ret, GELOGE(ret, "Call [google::protobuf::TextFormat::Parse] func ret fail, please check your text file."));
|
|
|
|
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY uint64_t GetCurrentTimestamp() {
|
|
|
|
FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY uint64_t GetCurrentTimestamp() {
|
|
|
|
mmTimeval tv {};
|
|
|
|
mmTimeval tv{};
|
|
|
|
int ret = mmGetTimeOfDay(&tv, nullptr);
|
|
|
|
int ret = mmGetTimeOfDay(&tv, nullptr);
|
|
|
|
GE_LOGE_IF(ret != EN_OK, "Func gettimeofday may failed: ret=%d", ret);
|
|
|
|
GE_LOGE_IF(ret != EN_OK, "Func gettimeofday may failed: ret=%d", ret);
|
|
|
|
auto total_use_time = tv.tv_usec + tv.tv_sec * 1000000; // 1000000: seconds to microseconds
|
|
|
|
auto total_use_time = tv.tv_usec + tv.tv_sec * 1000000; // 1000000: seconds to microseconds
|
|
|
@ -315,7 +316,7 @@ FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY uint64_t GetCurrentTimestamp()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY uint32_t GetCurrentSecondTimestap() {
|
|
|
|
FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY uint32_t GetCurrentSecondTimestap() {
|
|
|
|
mmTimeval tv {};
|
|
|
|
mmTimeval tv{};
|
|
|
|
int ret = mmGetTimeOfDay(&tv, nullptr);
|
|
|
|
int ret = mmGetTimeOfDay(&tv, nullptr);
|
|
|
|
GE_LOGE_IF(ret != EN_OK, "Func gettimeofday may failed: ret=%d", ret);
|
|
|
|
GE_LOGE_IF(ret != EN_OK, "Func gettimeofday may failed: ret=%d", ret);
|
|
|
|
auto total_use_time = tv.tv_sec; // seconds
|
|
|
|
auto total_use_time = tv.tv_sec; // seconds
|
|
|
@ -350,8 +351,9 @@ FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY bool CheckInt64MulOverflow(int6
|
|
|
|
FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY std::string RealPath(const char *path) {
|
|
|
|
FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY 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(path == nullptr, return "", "path pointer is NULL.");
|
|
|
|
GE_CHK_BOOL_TRUE_EXEC_WITH_LOG(strlen(path) >= MMPA_MAX_PATH,
|
|
|
|
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)});
|
|
|
|
ErrorManager::GetInstance().ATCReportErrMessage("E19002", {"filepath", "size"},
|
|
|
|
return "", "Path[%s] len is too long, it must be less than %d", path, MMPA_MAX_PATH);
|
|
|
|
{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
|
|
|
|
// Nullptr is returned when the path does not exist or there is no permission
|
|
|
|
// Return absolute path when path is accessible
|
|
|
|
// Return absolute path when path is accessible
|
|
|
@ -385,16 +387,16 @@ FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY bool CheckInputPathValid(const
|
|
|
|
// Path section: Support upper and lower case letters, numbers dots(.) chinese and underscores
|
|
|
|
// 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(.)
|
|
|
|
// File name section: Support upper and lower case letters, numbers, underscores chinese and dots(.)
|
|
|
|
#ifdef __GNUC__
|
|
|
|
#ifdef __GNUC__
|
|
|
|
std::string mode = "^[\u4e00-\u9fa5A-Za-z0-9./_-]+$";
|
|
|
|
std::string mode = "^[\u4e00-\u9fa5A-Za-z0-9./_-]+$";
|
|
|
|
#else
|
|
|
|
#else
|
|
|
|
std::string mode = "^[a-zA-Z]:([\\\\/][^\\s\\\\/:*?<>\"|][^\\\\/:*?<>\"|]*)*([/\\\\][^\\s\\\\/:*?<>\"|])?$";
|
|
|
|
std::string mode = "^[a-zA-Z]:([\\\\/][^\\s\\\\/:*?<>\"|][^\\\\/:*?<>\"|]*)*([/\\\\][^\\s\\\\/:*?<>\"|])?$";
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
GE_CHK_BOOL_TRUE_EXEC_WITH_LOG(
|
|
|
|
GE_CHK_BOOL_TRUE_EXEC_WITH_LOG(
|
|
|
|
!ValidateStr(real_path, mode),
|
|
|
|
!ValidateStr(real_path, mode),
|
|
|
|
ErrorManager::GetInstance().ATCReportErrMessage("E10001", {"parameter", "value", "reason"},
|
|
|
|
ErrorManager::GetInstance().ATCReportErrMessage("E10001", {"parameter", "value", "reason"},
|
|
|
|
{atc_param, real_path, kPathValidReason});
|
|
|
|
{atc_param, real_path, kPathValidReason});
|
|
|
|
return false, "Invalid value for %s[%s], %s.", atc_param.c_str(), real_path.c_str(), 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
|
|
|
|
// The absolute path points to a file that is not readable
|
|
|
|
if (mmAccess2(real_path.c_str(), M_R_OK) != EN_OK) {
|
|
|
|
if (mmAccess2(real_path.c_str(), M_R_OK) != EN_OK) {
|
|
|
@ -416,24 +418,25 @@ FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY bool CheckOutputPathValid(const
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
GE_CHK_BOOL_TRUE_EXEC_WITH_LOG(strlen(file_path.c_str()) >= MMPA_MAX_PATH,
|
|
|
|
GE_CHK_BOOL_TRUE_EXEC_WITH_LOG(strlen(file_path.c_str()) >= MMPA_MAX_PATH,
|
|
|
|
ErrorManager::GetInstance().ATCReportErrMessage(
|
|
|
|
ErrorManager::GetInstance().ATCReportErrMessage(
|
|
|
|
"E19002", {"filepath", "size"}, {file_path, std::to_string(MMPA_MAX_PATH)});
|
|
|
|
"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);
|
|
|
|
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
|
|
|
|
// 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
|
|
|
|
// 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(.)
|
|
|
|
// File name section: Support upper and lower case letters, numbers, underscores chinese and dots(.)
|
|
|
|
#ifdef __GNUC__
|
|
|
|
#ifdef __GNUC__
|
|
|
|
std::string mode = "^[\u4e00-\u9fa5A-Za-z0-9./_-]+$";
|
|
|
|
std::string mode = "^[\u4e00-\u9fa5A-Za-z0-9./_-]+$";
|
|
|
|
#else
|
|
|
|
#else
|
|
|
|
std::string mode = "^[a-zA-Z]:([\\\\/][^\\s\\\\/:*?<>\"|][^\\\\/:*?<>\"|]*)*([/\\\\][^\\s\\\\/:*?<>\"|])?$";
|
|
|
|
std::string mode = "^[a-zA-Z]:([\\\\/][^\\s\\\\/:*?<>\"|][^\\\\/:*?<>\"|]*)*([/\\\\][^\\s\\\\/:*?<>\"|])?$";
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
GE_CHK_BOOL_TRUE_EXEC_WITH_LOG(
|
|
|
|
GE_CHK_BOOL_TRUE_EXEC_WITH_LOG(
|
|
|
|
!ValidateStr(file_path, mode),
|
|
|
|
!ValidateStr(file_path, mode),
|
|
|
|
ErrorManager::GetInstance().ATCReportErrMessage("E10001", {"parameter", "value", "reason"},
|
|
|
|
ErrorManager::GetInstance().ATCReportErrMessage("E10001", {"parameter", "value", "reason"},
|
|
|
|
{atc_param, file_path, kPathValidReason});
|
|
|
|
{atc_param, file_path, kPathValidReason});
|
|
|
|
return false, "Invalid value for %s[%s], %s.", atc_param.c_str(), file_path.c_str(), 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());
|
|
|
|
std::string real_path = RealPath(file_path.c_str());
|
|
|
|
// Can get absolute path (file exists)
|
|
|
|
// Can get absolute path (file exists)
|
|
|
|