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.

dir_utils.cc 3.7 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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 "coder/utils/dir_utils.h"
  17. #include <sys/stat.h>
  18. #if defined(_WIN32) || defined(_WIN64)
  19. #include <direct.h>
  20. #endif
  21. #include <string>
  22. #include <fstream>
  23. #include <array>
  24. #include "include/errorcode.h"
  25. #include "src/common/log_adapter.h"
  26. namespace mindspore::lite::micro {
  27. #if defined(_WIN32) || defined(_WIN64)
  28. #ifdef _MSC_VER
  29. constexpr unsigned int kMicroDirMode = 511;
  30. #else
  31. constexpr _mode_t kMicroDirMode = 0777;
  32. #endif
  33. #else
  34. constexpr __mode_t kMicroDirMode = 0777;
  35. #endif
  36. static std::array<std::string, 2> kWorkDirs = {"src", "benchmark"};
  37. bool DirExists(const std::string &dir_path) {
  38. struct stat file_info;
  39. if (stat(dir_path.c_str(), &file_info) != 0) {
  40. return false;
  41. }
  42. return (file_info.st_mode & S_IFDIR) != 0;
  43. }
  44. bool FileExists(const std::string &path) {
  45. std::ifstream file(path);
  46. return file.good();
  47. }
  48. static int MkMicroDir(const std::string &currentDir) {
  49. #if defined(_WIN32) || defined(_WIN64)
  50. std::ofstream currentFile;
  51. std::string readMeFile = currentDir + "\\readMe.txt";
  52. currentFile.open(readMeFile);
  53. currentFile << "This is a directory for generating coding files. Do not edit !!!\n";
  54. if (!currentFile.is_open()) {
  55. if (_mkdir(currentDir.c_str()) != 0) {
  56. MS_LOG(ERROR) << currentDir << ": mkdir failed, please check filePath!!!";
  57. currentFile.close();
  58. return RET_ERROR;
  59. }
  60. }
  61. currentFile.close();
  62. #else
  63. std::ifstream currentFile;
  64. currentFile.open(currentDir);
  65. if (!currentFile.is_open()) {
  66. if (mkdir(currentDir.c_str(), kMicroDirMode) != 0) {
  67. MS_LOG(ERROR) << currentDir << ": mkdir failed, please check filePath!!!";
  68. currentFile.close();
  69. return RET_ERROR;
  70. }
  71. }
  72. currentFile.close();
  73. #endif
  74. return RET_OK;
  75. }
  76. int InitProjDirs(const std::string &project_root_dir, const std::string &proj_name) {
  77. #if defined(_WIN32) || defined(_WIN64)
  78. std::ofstream pro_file;
  79. std::string read_me_file = project_root_dir + "\\readMe.txt";
  80. pro_file.open(read_me_file.c_str());
  81. pro_file << "This is a directory for generating coding files. Do not edit !!!\n";
  82. #else
  83. std::ifstream pro_file;
  84. pro_file.open(project_root_dir.c_str());
  85. #endif
  86. if (!pro_file.is_open()) {
  87. MS_LOG(ERROR) << project_root_dir << ": model's root dir not exists or have no access to open, please check it!!!";
  88. pro_file.close();
  89. return RET_ERROR;
  90. }
  91. // check other dirs && make them if not exists
  92. // 1. coderDir 2.WorkRootDir 3. WorkChildDir
  93. std::string current_dir;
  94. std::string slashCh = std::string(kSlash);
  95. if (project_root_dir.back() != slashCh.back()) {
  96. current_dir = project_root_dir + slashCh;
  97. }
  98. current_dir += proj_name;
  99. std::string work_dir = current_dir;
  100. STATUS ret = MkMicroDir(current_dir);
  101. if (ret == RET_ERROR) {
  102. pro_file.close();
  103. return ret;
  104. }
  105. for (const auto &work : kWorkDirs) {
  106. current_dir = work_dir + slashCh + work;
  107. ret = MkMicroDir(current_dir);
  108. if (ret == RET_ERROR) {
  109. pro_file.close();
  110. return ret;
  111. }
  112. }
  113. pro_file.close();
  114. return RET_OK;
  115. }
  116. } // namespace mindspore::lite::micro