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_system.cc 7.1 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. /**
  2. * Copyright 2019 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/system/file_system.h"
  17. #if defined(SYSTEM_ENV_POSIX)
  18. #include <sys/stat.h>
  19. #include <unistd.h>
  20. #elif defined(SYSTEM_ENV_WINDOWS)
  21. #include <direct.h>
  22. #endif
  23. namespace mindspore {
  24. namespace system {
  25. #if defined(SYSTEM_ENV_POSIX)
  26. // Implement the Posix file system
  27. WriteFilePtr PosixFileSystem::CreateWriteFile(const string &file_name) {
  28. if (file_name.empty()) {
  29. MS_LOG(ERROR) << "Create write file failed because the file name is null.";
  30. return nullptr;
  31. }
  32. auto fp = std::make_shared<PosixWriteFile>(file_name);
  33. if (fp == nullptr) {
  34. MS_LOG(ERROR) << "Create write file(" << file_name << ") failed.";
  35. return nullptr;
  36. }
  37. bool result = fp->Open();
  38. if (!result) {
  39. MS_LOG(ERROR) << "Open the write file(" << file_name << ") failed.";
  40. return nullptr;
  41. }
  42. return fp;
  43. }
  44. bool PosixFileSystem::FileExist(const string &file_name) {
  45. if (file_name.empty()) {
  46. MS_LOG(WARNING) << "The file name is null.";
  47. return false;
  48. }
  49. auto result = access(file_name.c_str(), F_OK);
  50. if (result != 0) {
  51. MS_LOG(DEBUG) << "The file(" << file_name << ") not exist.";
  52. return false;
  53. }
  54. return true;
  55. }
  56. bool PosixFileSystem::DeleteFile(const string &file_name) {
  57. if (file_name.empty()) {
  58. MS_LOG(WARNING) << "The file name is null.";
  59. return false;
  60. }
  61. auto result = unlink(file_name.c_str());
  62. if (result != 0) {
  63. MS_LOG(ERROR) << "Delete the file(" << file_name << ") failed, error(" << errno
  64. << "). ErrInfo: " << strerror(errno);
  65. return false;
  66. }
  67. return true;
  68. }
  69. static const int DEFAULT_MKDIR_MODE = 0700;
  70. bool PosixFileSystem::CreateDir(const string &dir_name) {
  71. if (dir_name.empty()) {
  72. MS_LOG(WARNING) << "The directory name is null.";
  73. return false;
  74. }
  75. auto result = mkdir(dir_name.c_str(), DEFAULT_MKDIR_MODE);
  76. if (result != 0) {
  77. if (errno != EEXIST) {
  78. MS_LOG(ERROR) << "Create the dir(" << dir_name << ") failed, error(" << errno
  79. << "). ErrInfo: " << strerror(errno);
  80. return false;
  81. }
  82. }
  83. return true;
  84. }
  85. bool PosixFileSystem::DeleteDir(const string &dir_name) {
  86. if (dir_name.empty()) {
  87. MS_LOG(WARNING) << "The directory name is null.";
  88. return false;
  89. }
  90. auto result = rmdir(dir_name.c_str());
  91. if (result != 0) {
  92. MS_LOG(ERROR) << "Delete the dir(" << dir_name << ") failed, error(" << errno << "). ErrInfo: " << strerror(errno);
  93. return false;
  94. }
  95. return true;
  96. }
  97. #endif
  98. #if defined(SYSTEM_ENV_WINDOWS)
  99. // Implement the Windows file system
  100. WriteFilePtr WinFileSystem::CreateWriteFile(const string &file_name) {
  101. if (file_name.empty()) {
  102. MS_LOG(ERROR) << "Create write file failed because the file name is null.";
  103. return nullptr;
  104. }
  105. auto fp = std::make_shared<WinWriteFile>(file_name);
  106. if (fp == nullptr) {
  107. MS_LOG(ERROR) << "Create write file(" << file_name << ") failed.";
  108. return nullptr;
  109. }
  110. bool result = fp->Open();
  111. if (!result) {
  112. MS_LOG(ERROR) << "Open the write file(" << file_name << ") failed.";
  113. return nullptr;
  114. }
  115. return fp;
  116. }
  117. bool WinFileSystem::FileExist(const string &file_name) {
  118. if (file_name.empty()) {
  119. MS_LOG(WARNING) << "The file name is null.";
  120. return false;
  121. }
  122. auto result = access(file_name.c_str(), F_OK);
  123. if (result != 0) {
  124. MS_LOG(DEBUG) << "The file(" << file_name << ") not exist.";
  125. return false;
  126. }
  127. return true;
  128. }
  129. bool WinFileSystem::CreateDir(const string &dir_name) {
  130. if (dir_name.empty()) {
  131. MS_LOG(WARNING) << "The directory name is null.";
  132. return false;
  133. }
  134. auto result = mkdir(dir_name.c_str());
  135. if (result != 0) {
  136. MS_LOG(ERROR) << "Create the dir(" << dir_name << ") is failed, error(" << result << ").";
  137. return false;
  138. }
  139. return true;
  140. }
  141. bool WinFileSystem::DeleteDir(const string &dir_name) {
  142. if (dir_name.empty()) {
  143. MS_LOG(WARNING) << "The directory name is null.";
  144. return false;
  145. }
  146. auto result = rmdir(dir_name.c_str());
  147. if (result != 0) {
  148. MS_LOG(ERROR) << "Delete the dir(" << dir_name << ") is failed, error(" << result << ").";
  149. return false;
  150. }
  151. return true;
  152. }
  153. bool WinFileSystem::DeleteFile(const string &file_name) {
  154. if (file_name.empty()) {
  155. MS_LOG(WARNING) << "The file name is null.";
  156. return false;
  157. }
  158. auto result = unlink(file_name.c_str());
  159. if (result != 0) {
  160. MS_LOG(ERROR) << "Delete the file(" << file_name << ") is failed, error(" << errno
  161. << "). ErrInfo: " << strerror(errno);
  162. return false;
  163. }
  164. return true;
  165. }
  166. bool WinWriteFile::Open() {
  167. if (file_ != nullptr) {
  168. MS_LOG(WARNING) << "The File(" << file_name_ << ") already open.";
  169. return true;
  170. }
  171. // check the path
  172. if (file_name_.c_str() == nullptr) {
  173. MS_LOG(EXCEPTION) << "The file path is null.";
  174. }
  175. char path[PATH_MAX] = {0x00};
  176. if (file_name_.size() >= PATH_MAX || _fullpath(path, file_name_.c_str(), PATH_MAX) == nullptr) {
  177. MS_LOG(EXCEPTION) << "Convert to real path fail, file name is " << file_name_ << ".";
  178. }
  179. // open the file
  180. file_ = fopen(path, "w+");
  181. if (file_ == nullptr) {
  182. MS_LOG(ERROR) << "File(" << path << ") IO ERROR: " << errno << ". ErrInfo: " << strerror(errno);
  183. return false;
  184. }
  185. return true;
  186. }
  187. bool WinWriteFile::Write(const std::string &data) {
  188. MS_LOG(DEBUG) << "Write data(" << data.size() << ") to file(" << this->file_name_ << ").";
  189. size_t r = fwrite(data.data(), 1, data.size(), file_);
  190. if (r != data.size()) {
  191. MS_LOG(ERROR) << "File(" << file_name_ << ") IO ERROR: " << errno << ". ErrInfo: " << strerror(errno);
  192. return false;
  193. }
  194. return true;
  195. }
  196. bool WinWriteFile::Close() {
  197. if (file_ == nullptr) {
  198. MS_LOG(WARNING) << "File(" << file_name_ << ") already close.";
  199. return true;
  200. }
  201. bool result = true;
  202. if (fclose(file_) != 0) {
  203. MS_LOG(ERROR) << "File(" << file_name_ << ") IO ERROR: " << errno << ". ErrInfo: " << strerror(errno);
  204. result = false;
  205. }
  206. file_ = nullptr;
  207. return result;
  208. }
  209. bool WinWriteFile::Flush() {
  210. if (fflush(file_) != 0) {
  211. MS_LOG(ERROR) << "File(" << file_name_ << ") IO ERROR: " << EBADF << ".";
  212. return false;
  213. }
  214. return true;
  215. }
  216. bool WinWriteFile::Sync() { return Flush(); }
  217. WinWriteFile::~WinWriteFile() {
  218. try {
  219. if (file_ != nullptr) {
  220. (void)fclose(file_);
  221. file_ = nullptr;
  222. }
  223. } catch (const std::exception &e) {
  224. MS_LOG(ERROR) << "Exception when closing file.";
  225. } catch (...) {
  226. MS_LOG(ERROR) << "Non standard exception when closing file.";
  227. }
  228. }
  229. #endif
  230. } // namespace system
  231. } // namespace mindspore