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