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.3 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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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 <optional>
  19. #include "utils/ms_context.h"
  20. #include "utils/system/env.h"
  21. #include "utils/system/file_system.h"
  22. #include "utils/log_adapter.h"
  23. namespace mindspore {
  24. std::optional<std::string> Common::GetRealPath(const std::string &input_path) {
  25. std::string out_path;
  26. auto path_split_pos = input_path.find_last_of('/');
  27. if (path_split_pos == std::string::npos) {
  28. path_split_pos = input_path.find_last_of('\\');
  29. }
  30. // get real path
  31. char real_path[PATH_MAX] = {0};
  32. if (path_split_pos != std::string::npos) {
  33. std::string prefix_path = input_path.substr(0, path_split_pos);
  34. if (prefix_path.length() >= PATH_MAX) {
  35. MS_LOG(ERROR) << "Prefix path is too longer!";
  36. return std::nullopt;
  37. }
  38. std::string last_path = input_path.substr(path_split_pos, input_path.length() - path_split_pos);
  39. auto ret = CreateNotExistDirs(prefix_path);
  40. if (!ret) {
  41. MS_LOG(ERROR) << "CreateNotExistDirs Failed!";
  42. return std::nullopt;
  43. }
  44. #if defined(SYSTEM_ENV_POSIX)
  45. if (nullptr == realpath(prefix_path.c_str(), real_path)) {
  46. MS_LOG(ERROR) << "dir " << prefix_path << " does not exist.";
  47. return std::nullopt;
  48. }
  49. #elif defined(SYSTEM_ENV_WINDOWS)
  50. if (nullptr == _fullpath(real_path, prefix_path.c_str(), PATH_MAX)) {
  51. MS_LOG(ERROR) << "dir " << prefix_path << " does not exist.";
  52. return std::nullopt;
  53. }
  54. #else
  55. MS_LOG(EXCEPTION) << "Unsupported platform.";
  56. #endif
  57. out_path = std::string(real_path) + last_path;
  58. }
  59. if (path_split_pos == std::string::npos) {
  60. if (input_path.length() >= PATH_MAX) {
  61. MS_LOG(ERROR) << "Prefix path is too longer!";
  62. return std::nullopt;
  63. }
  64. #if defined(SYSTEM_ENV_POSIX)
  65. if (nullptr == realpath(input_path.c_str(), real_path)) {
  66. MS_LOG(ERROR) << "File " << input_path << " does not exist, it will be created.";
  67. }
  68. #elif defined(SYSTEM_ENV_WINDOWS)
  69. if (nullptr == _fullpath(real_path, input_path.c_str(), PATH_MAX)) {
  70. MS_LOG(ERROR) << "File " << input_path << " does not exist, it will be created.";
  71. }
  72. #else
  73. MS_LOG(EXCEPTION) << "Unsupported platform.";
  74. #endif
  75. out_path = std::string(real_path);
  76. }
  77. return out_path;
  78. }
  79. bool Common::CreateNotExistDirs(const std::string &path) {
  80. std::shared_ptr<system::FileSystem> fs = system::Env::GetFileSystem();
  81. MS_EXCEPTION_IF_NULL(fs);
  82. char temp_path[PATH_MAX] = {0};
  83. if (path.length() > PATH_MAX) {
  84. MS_LOG(ERROR) << "Path lens is max than " << PATH_MAX;
  85. return false;
  86. }
  87. for (uint32_t i = 0; i < path.length(); i++) {
  88. temp_path[i] = path[i];
  89. if (temp_path[i] == '\\' || temp_path[i] == '/') {
  90. if (i != 0) {
  91. char tmp_char = temp_path[i];
  92. temp_path[i] = '\0';
  93. std::string path_handle(temp_path);
  94. if (!fs->FileExist(path_handle)) {
  95. MS_LOG(INFO) << "Dir " << path_handle << " does not exit, creating...";
  96. if (!fs->CreateDir(path_handle)) {
  97. MS_LOG(ERROR) << "Create " << path_handle << " dir error";
  98. return false;
  99. }
  100. }
  101. temp_path[i] = tmp_char;
  102. }
  103. }
  104. }
  105. if (!fs->FileExist(path)) {
  106. MS_LOG(INFO) << "Dir " << path << " does not exit, creating...";
  107. if (!fs->CreateDir(path)) {
  108. MS_LOG(ERROR) << "Create " << path << " dir error";
  109. return false;
  110. }
  111. }
  112. return true;
  113. }
  114. std::optional<std::string> Common::GetConfigFile(const std::string &env) {
  115. if (env.empty()) {
  116. MS_LOG(EXCEPTION) << "Invalid env";
  117. }
  118. auto config_path_str = std::getenv(env.c_str());
  119. if (config_path_str == nullptr) {
  120. MS_LOG(ERROR) << "Please export env:" << env;
  121. return {};
  122. }
  123. MS_LOG(INFO) << "Async Dump Getenv env:" << env << "=" << config_path_str;
  124. std::string dump_config_file(config_path_str);
  125. std::shared_ptr<system::FileSystem> fs = system::Env::GetFileSystem();
  126. MS_EXCEPTION_IF_NULL(fs);
  127. if (!fs->FileExist(dump_config_file)) {
  128. MS_LOG(ERROR) << dump_config_file << " not exist.";
  129. return {};
  130. }
  131. auto point_pos = dump_config_file.find_last_of('.');
  132. if (point_pos == std::string::npos) {
  133. MS_LOG(EXCEPTION) << "Invalid json file name:" << dump_config_file;
  134. }
  135. auto suffix = dump_config_file.substr(point_pos + 1);
  136. if (suffix != "json") {
  137. MS_LOG(EXCEPTION) << "[DataDump] dump config file suffix only supports json! But got:." << suffix;
  138. }
  139. return dump_config_file;
  140. }
  141. std::optional<std::string> Common::GetEnvConfigFile() {
  142. auto context = MsContext::GetInstance();
  143. MS_EXCEPTION_IF_NULL(context);
  144. std::string env_config_path = context->get_param<std::string>(MS_CTX_ENV_CONFIG_PATH);
  145. if (env_config_path.empty()) {
  146. MS_LOG(INFO) << "The env_config_path is not set in context.";
  147. return {};
  148. }
  149. MS_LOG(INFO) << "Get env_config_path: " << env_config_path;
  150. std::string config_file(env_config_path);
  151. std::shared_ptr<system::FileSystem> fs = system::Env::GetFileSystem();
  152. MS_EXCEPTION_IF_NULL(fs);
  153. if (!fs->FileExist(config_file)) {
  154. MS_LOG(ERROR) << config_file << " not exist.";
  155. return {};
  156. }
  157. auto point_pos = config_file.find_last_of('.');
  158. if (point_pos == std::string::npos) {
  159. MS_LOG(EXCEPTION) << "Invalid json file name:" << config_file;
  160. }
  161. return config_file;
  162. }
  163. bool Common::IsStrLengthValid(const std::string &str, const int &length_limit, const std::string &error_message,
  164. const bool &print_str) {
  165. const int len_str = str.length();
  166. if (len_str > length_limit) {
  167. std::ostringstream msg;
  168. if (print_str) {
  169. msg << error_message << "The string is " << str << ", its length is " << str.length();
  170. } else {
  171. msg << error_message << "The length is " << str.length();
  172. }
  173. msg << ", exceeding the limit of " << length_limit << ".";
  174. MS_LOG(WARNING) << msg.str();
  175. return false;
  176. }
  177. return true;
  178. }
  179. bool Common::IsEveryFilenameValid(const std::string &path, const int &length_limit, const std::string &error_message) {
  180. int left_pos = 0;
  181. int len_path = path.length();
  182. for (int i = 0; i < len_path; i++) {
  183. if (i != 0) {
  184. if (path[i] == '\\' || path[i] == '/') {
  185. int cur_len = i - left_pos;
  186. if (cur_len > length_limit) {
  187. MS_LOG(WARNING) << error_message << "The name length of '" << path.substr(left_pos, cur_len) << "' is "
  188. << cur_len << ". It is out of the limit which is " << length_limit << ".";
  189. return false;
  190. }
  191. left_pos = i + 1;
  192. }
  193. }
  194. }
  195. if (!(path[len_path - 1] == '\\' || path[len_path - 1] == '/')) {
  196. int cur_len = len_path - left_pos;
  197. if (cur_len > length_limit) {
  198. MS_LOG(WARNING) << error_message << "The name length of '" << path.substr(left_pos, cur_len) << "' is " << cur_len
  199. << ". It is out of the limit which is " << length_limit << ".";
  200. return false;
  201. }
  202. }
  203. return true;
  204. }
  205. bool Common::IsPathValid(const std::string &path, const int &length_limit, const std::string &error_message,
  206. const bool &print_str) {
  207. std::string err_msg = "Detail: ";
  208. if (!error_message.empty()) {
  209. err_msg = error_message + " " + err_msg;
  210. }
  211. if (path.empty()) {
  212. MS_LOG(WARNING) << err_msg << "The path is empty.";
  213. return false;
  214. }
  215. if (!IsStrLengthValid(path, length_limit, err_msg, print_str)) {
  216. return false;
  217. }
  218. if (!std::all_of(path.begin(), path.end(),
  219. [](char c) { return ::isalpha(c) || ::isdigit(c) || c == '-' || c == '_' || c == '/'; })) {
  220. MS_LOG(WARNING) << err_msg << "The path only supports alphabets, digit or {'-', '_', '/'}, but got:" << path << ".";
  221. return false;
  222. }
  223. if (path[0] != '/') {
  224. MS_LOG(WARNING) << err_msg << "The path only supports absolute path and should start with '/'.";
  225. return false;
  226. }
  227. if (!IsEveryFilenameValid(path, maxOSFilenameLength, err_msg)) {
  228. return false;
  229. }
  230. return true;
  231. }
  232. bool Common::IsFilenameValid(const std::string &filename, const int &length_limit, const std::string &error_message) {
  233. std::string err_msg = "Detail: ";
  234. if (!error_message.empty()) {
  235. err_msg = error_message + " " + err_msg;
  236. }
  237. if (filename.empty()) {
  238. MS_LOG(WARNING) << err_msg << "The filename is empty.";
  239. return false;
  240. }
  241. if (!IsStrLengthValid(filename, length_limit, err_msg)) {
  242. return false;
  243. }
  244. if (!std::all_of(filename.begin(), filename.end(),
  245. [](char c) { return ::isalpha(c) || ::isdigit(c) || c == '-' || c == '_' || c == '.'; })) {
  246. MS_LOG(WARNING) << err_msg << "The filename only supports alphabets, digit or {'-', '_', '.'}, but got:" << filename
  247. << ".";
  248. return false;
  249. }
  250. return true;
  251. }
  252. } // namespace mindspore