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.

file_utils.cc 3.9 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /**
  2. * Copyright 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 "utils/file_utils.h"
  17. #include <limits.h>
  18. #include <string.h>
  19. #include <string>
  20. #include <optional>
  21. #include <memory>
  22. #include "utils/system/file_system.h"
  23. #include "utils/system/env.h"
  24. #include "utils/utils.h"
  25. namespace mindspore {
  26. std::optional<std::string> FileUtils::GetRealPath(const char *path) {
  27. if (path == nullptr) {
  28. MS_LOG(ERROR) << "Input path is nullptr";
  29. return std::nullopt;
  30. }
  31. char real_path[PATH_MAX] = {0};
  32. #if defined(_WIN32) || defined(_WIN64)
  33. if (strlen(path) >= PATH_MAX || _fullpath(real_path, path, PATH_MAX) == nullptr) {
  34. MS_LOG(ERROR) << "Get _fullpath failed";
  35. return std::nullopt;
  36. }
  37. #else
  38. if (strlen(path) >= PATH_MAX || realpath(path, real_path) == nullptr) {
  39. MS_LOG(ERROR) << "Get realpath failed, path[" << path << "]";
  40. return std::nullopt;
  41. }
  42. #endif
  43. return std::string(real_path);
  44. }
  45. void FileUtils::SplitDirAndFileName(const std::string &path, std::optional<std::string> *prefix_path,
  46. std::optional<std::string> *file_name) {
  47. auto path_split_pos = path.find_last_of('/');
  48. if (path_split_pos == std::string::npos) {
  49. path_split_pos = path.find_last_of('\\');
  50. }
  51. MS_EXCEPTION_IF_NULL(prefix_path);
  52. MS_EXCEPTION_IF_NULL(file_name);
  53. if (path_split_pos != std::string::npos) {
  54. *prefix_path = path.substr(0, path_split_pos);
  55. *file_name = path.substr(path_split_pos + 1);
  56. } else {
  57. *prefix_path = std::nullopt;
  58. *file_name = path;
  59. }
  60. }
  61. void FileUtils::ConcatDirAndFileName(const std::optional<std::string> *dir, const std::optional<std::string> *file_name,
  62. std::optional<std::string> *path) {
  63. MS_EXCEPTION_IF_NULL(dir);
  64. MS_EXCEPTION_IF_NULL(file_name);
  65. MS_EXCEPTION_IF_NULL(path);
  66. #if defined(_WIN32) || defined(_WIN64)
  67. *path = dir->value() + "\\" + file_name->value();
  68. #else
  69. *path = dir->value() + "/" + file_name->value();
  70. #endif
  71. }
  72. std::optional<std::string> FileUtils::CreateNotExistDirs(const std::string &path, const bool support_relative_path) {
  73. if (path.size() >= PATH_MAX) {
  74. MS_LOG(ERROR) << "The length of the path is greater than or equal to:" << PATH_MAX;
  75. return std::nullopt;
  76. }
  77. if (!support_relative_path) {
  78. auto dot_pos = path.find("..");
  79. if (dot_pos != std::string::npos) {
  80. MS_LOG(ERROR) << "Do not support relative path";
  81. return std::nullopt;
  82. }
  83. }
  84. std::shared_ptr<system::FileSystem> fs = system::Env::GetFileSystem();
  85. MS_EXCEPTION_IF_NULL(fs);
  86. char temp_path[PATH_MAX] = {0};
  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. if (!fs->CreateDir(path_handle)) {
  96. MS_LOG(ERROR) << "Create " << path_handle << " dir error";
  97. return std::nullopt;
  98. }
  99. }
  100. temp_path[i] = tmp_char;
  101. }
  102. }
  103. }
  104. if (!fs->FileExist(path)) {
  105. if (!fs->CreateDir(path)) {
  106. MS_LOG(ERROR) << "Create " << path << " dir error";
  107. return std::nullopt;
  108. }
  109. }
  110. return GetRealPath(path.c_str());
  111. }
  112. } // namespace mindspore