You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

common.cc 9.7 kB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. /**
  2. * Copyright 2020-2021 Huawei Technologies Co., Ltd
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include "debug/common.h"
  17. #include <memory>
  18. #include <iomanip>
  19. #include <optional>
  20. #include "utils/ms_context.h"
  21. #include "utils/system/env.h"
  22. #include "utils/system/file_system.h"
  23. #include "utils/log_adapter.h"
  24. namespace mindspore {
  25. std::optional<std::string> Common::GetRealPath(const std::string &input_path) {
  26. std::string out_path;
  27. auto path_split_pos = input_path.find_last_of('/');
  28. if (path_split_pos == std::string::npos) {
  29. path_split_pos = input_path.find_last_of('\\');
  30. }
  31. // get real path
  32. char real_path[PATH_MAX] = {0};
  33. if (path_split_pos != std::string::npos) {
  34. std::string prefix_path = input_path.substr(0, path_split_pos);
  35. if (prefix_path.length() >= PATH_MAX) {
  36. MS_LOG(ERROR) << "Prefix path is too longer!";
  37. return std::nullopt;
  38. }
  39. std::string last_path = input_path.substr(path_split_pos, input_path.length() - path_split_pos);
  40. auto ret = CreateNotExistDirs(prefix_path);
  41. if (!ret) {
  42. MS_LOG(ERROR) << "CreateNotExistDirs Failed!";
  43. return std::nullopt;
  44. }
  45. #if defined(SYSTEM_ENV_POSIX)
  46. if (nullptr == realpath(prefix_path.c_str(), real_path)) {
  47. MS_LOG(ERROR) << "dir " << prefix_path << " does not exist.";
  48. return std::nullopt;
  49. }
  50. #elif defined(SYSTEM_ENV_WINDOWS)
  51. if (nullptr == _fullpath(real_path, prefix_path.c_str(), PATH_MAX)) {
  52. MS_LOG(ERROR) << "dir " << prefix_path << " does not exist.";
  53. return std::nullopt;
  54. }
  55. #else
  56. MS_LOG(EXCEPTION) << "Unsupported platform.";
  57. #endif
  58. out_path = std::string(real_path) + last_path;
  59. }
  60. if (path_split_pos == std::string::npos) {
  61. if (input_path.length() >= PATH_MAX) {
  62. MS_LOG(ERROR) << "Prefix path is too longer!";
  63. return std::nullopt;
  64. }
  65. #if defined(SYSTEM_ENV_POSIX)
  66. if (nullptr == realpath(input_path.c_str(), real_path)) {
  67. MS_LOG(ERROR) << "File " << input_path << " does not exist, it will be created.";
  68. }
  69. #elif defined(SYSTEM_ENV_WINDOWS)
  70. if (nullptr == _fullpath(real_path, input_path.c_str(), PATH_MAX)) {
  71. MS_LOG(ERROR) << "File " << input_path << " does not exist, it will be created.";
  72. }
  73. #else
  74. MS_LOG(EXCEPTION) << "Unsupported platform.";
  75. #endif
  76. out_path = std::string(real_path);
  77. }
  78. return out_path;
  79. }
  80. bool Common::CreateNotExistDirs(const std::string &path) {
  81. std::shared_ptr<system::FileSystem> fs = system::Env::GetFileSystem();
  82. MS_EXCEPTION_IF_NULL(fs);
  83. char temp_path[PATH_MAX] = {0};
  84. if (path.length() > PATH_MAX) {
  85. MS_LOG(ERROR) << "Path lens is max than " << PATH_MAX;
  86. return false;
  87. }
  88. for (uint32_t i = 0; i < path.length(); i++) {
  89. temp_path[i] = path[i];
  90. if (temp_path[i] == '\\' || temp_path[i] == '/') {
  91. if (i != 0) {
  92. char tmp_char = temp_path[i];
  93. temp_path[i] = '\0';
  94. std::string path_handle(temp_path);
  95. if (!fs->FileExist(path_handle)) {
  96. MS_LOG(INFO) << "Dir " << path_handle << " does not exit, creating...";
  97. if (!fs->CreateDir(path_handle)) {
  98. MS_LOG(ERROR) << "Create " << path_handle << " dir error";
  99. return false;
  100. }
  101. }
  102. temp_path[i] = tmp_char;
  103. }
  104. }
  105. }
  106. if (!fs->FileExist(path)) {
  107. MS_LOG(INFO) << "Dir " << path << " does not exit, creating...";
  108. if (!fs->CreateDir(path)) {
  109. MS_LOG(ERROR) << "Create " << path << " dir error";
  110. return false;
  111. }
  112. }
  113. return true;
  114. }
  115. std::optional<std::string> Common::GetConfigFile(const std::string &env) {
  116. if (env.empty()) {
  117. MS_LOG(EXCEPTION) << "Invalid env";
  118. }
  119. auto config_path_str = std::getenv(env.c_str());
  120. if (config_path_str == nullptr) {
  121. MS_LOG(ERROR) << "Please export env:" << env;
  122. return {};
  123. }
  124. MS_LOG(INFO) << "Async Dump Getenv env:" << env << "=" << config_path_str;
  125. std::string dump_config_file(config_path_str);
  126. std::shared_ptr<system::FileSystem> fs = system::Env::GetFileSystem();
  127. MS_EXCEPTION_IF_NULL(fs);
  128. if (!fs->FileExist(dump_config_file)) {
  129. MS_LOG(ERROR) << dump_config_file << " not exist.";
  130. return {};
  131. }
  132. auto point_pos = dump_config_file.find_last_of('.');
  133. if (point_pos == std::string::npos) {
  134. MS_LOG(EXCEPTION) << "Invalid json file name:" << dump_config_file;
  135. }
  136. auto suffix = dump_config_file.substr(point_pos + 1);
  137. if (suffix != "json") {
  138. MS_LOG(EXCEPTION) << "[DataDump] dump config file suffix only supports json! But got:." << suffix;
  139. }
  140. return dump_config_file;
  141. }
  142. std::optional<std::string> Common::GetEnvConfigFile() {
  143. auto context = MsContext::GetInstance();
  144. MS_EXCEPTION_IF_NULL(context);
  145. std::string env_config_path = context->get_param<std::string>(MS_CTX_ENV_CONFIG_PATH);
  146. if (env_config_path.empty()) {
  147. MS_LOG(INFO) << "The env_config_path is not set in context.";
  148. return {};
  149. }
  150. MS_LOG(INFO) << "Get env_config_path: " << env_config_path;
  151. std::string config_file(env_config_path);
  152. std::shared_ptr<system::FileSystem> fs = system::Env::GetFileSystem();
  153. MS_EXCEPTION_IF_NULL(fs);
  154. if (!fs->FileExist(config_file)) {
  155. MS_LOG(ERROR) << config_file << " not exist.";
  156. return {};
  157. }
  158. auto point_pos = config_file.find_last_of('.');
  159. if (point_pos == std::string::npos) {
  160. MS_LOG(EXCEPTION) << "Invalid json file name:" << config_file;
  161. }
  162. return config_file;
  163. }
  164. bool Common::IsStrLengthValid(const std::string &str, const int &length_limit, const std::string &error_message) {
  165. const int len_str = str.length();
  166. if (len_str > length_limit) {
  167. MS_LOG(WARNING) << error_message << "The length is " << str.length() << ", exceeding the limit of " << length_limit
  168. << ".";
  169. return false;
  170. }
  171. return true;
  172. }
  173. bool Common::IsEveryFilenameValid(const std::string &path, const int &length_limit, const std::string &error_message) {
  174. int left_pos = 0;
  175. if (path.empty()) {
  176. MS_LOG(WARNING) << error_message << "The path is empty.";
  177. return false;
  178. }
  179. int len_path = path.length();
  180. for (int i = 0; i < len_path; i++) {
  181. if (i != 0) {
  182. if (path[i] == '\\' || path[i] == '/') {
  183. int cur_len = i - left_pos;
  184. if (cur_len > length_limit) {
  185. MS_LOG(WARNING) << error_message << "The name length of '" << path.substr(left_pos, cur_len) << "' is "
  186. << cur_len << ". It is out of the limit which is " << length_limit << ".";
  187. return false;
  188. }
  189. left_pos = i + 1;
  190. }
  191. }
  192. }
  193. if (!(path[len_path - 1] == '\\' || path[len_path - 1] == '/')) {
  194. int cur_len = len_path - left_pos;
  195. if (cur_len > length_limit) {
  196. MS_LOG(WARNING) << error_message << "The name length of '" << path.substr(left_pos, cur_len) << "' is " << cur_len
  197. << ". It is out of the limit which is " << length_limit << ".";
  198. return false;
  199. }
  200. }
  201. return true;
  202. }
  203. bool Common::IsPathValid(const std::string &path, const int &length_limit, const std::string &error_message) {
  204. std::string err_msg = "Detail: ";
  205. if (!error_message.empty()) {
  206. err_msg = error_message + " " + err_msg;
  207. }
  208. if (path.empty()) {
  209. MS_LOG(WARNING) << err_msg << "The path is empty.";
  210. return false;
  211. }
  212. if (!IsStrLengthValid(path, length_limit, err_msg)) {
  213. return false;
  214. }
  215. if (!std::all_of(path.begin(), path.end(),
  216. [](char c) { return ::isalpha(c) || ::isdigit(c) || c == '-' || c == '_' || c == '/'; })) {
  217. MS_LOG(WARNING) << err_msg << "The path only supports alphabets, digit or {'-', '_', '/'}, but got:" << path << ".";
  218. return false;
  219. }
  220. if (path[0] != '/') {
  221. MS_LOG(WARNING) << err_msg << "The path only supports absolute path and should start with '/'.";
  222. return false;
  223. }
  224. if (!IsEveryFilenameValid(path, MAX_OS_FILENAME_LENGTH, err_msg)) {
  225. return false;
  226. }
  227. return true;
  228. }
  229. bool Common::IsFilenameValid(const std::string &filename, const int &length_limit, const std::string &error_message) {
  230. std::string err_msg = "Detail: ";
  231. if (!error_message.empty()) {
  232. err_msg = error_message + " " + err_msg;
  233. }
  234. if (filename.empty()) {
  235. MS_LOG(WARNING) << err_msg << "The filename is empty.";
  236. return false;
  237. }
  238. if (!IsStrLengthValid(filename, length_limit, err_msg)) {
  239. return false;
  240. }
  241. if (!std::all_of(filename.begin(), filename.end(),
  242. [](char c) { return ::isalpha(c) || ::isdigit(c) || c == '-' || c == '_' || c == '.'; })) {
  243. MS_LOG(WARNING) << err_msg << "The filename only supports alphabets, digit or {'-', '_', '.'}, but got:" << filename
  244. << ".";
  245. return false;
  246. }
  247. return true;
  248. }
  249. std::string Common::AddId(const std::string &filename, const std::string &suffix) {
  250. static size_t g_id = 0;
  251. std::ostringstream s;
  252. auto i = filename.rfind(suffix);
  253. if (i >= filename.size()) {
  254. s << filename;
  255. s << "_" << std::setfill('0') << std::setw(4) << g_id;
  256. } else {
  257. s << filename.substr(0, i);
  258. s << "_" << std::setfill('0') << std::setw(4) << g_id;
  259. if (i + 1 < filename.size()) {
  260. s << filename.substr(i);
  261. }
  262. }
  263. g_id++;
  264. return s.str();
  265. }
  266. } // namespace mindspore