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 6.9 kB

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