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.6 kB

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