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.h 5.5 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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. #ifndef MINDSPORE_CCSRC_UTILS_SYSTEM_FILE_SYSTEM_H_
  17. #define MINDSPORE_CCSRC_UTILS_SYSTEM_FILE_SYSTEM_H_
  18. #include <errno.h>
  19. #include <sys/param.h>
  20. #include <cstdint>
  21. #include <cstdlib>
  22. #include <cstdio>
  23. #include <functional>
  24. #include <string>
  25. #include <memory>
  26. #include <vector>
  27. #include "utils/system/base.h"
  28. #include "utils/log_adapter.h"
  29. namespace mindspore {
  30. namespace system {
  31. class WriteFile;
  32. class PosixWriteFile;
  33. using WriteFilePtr = std::shared_ptr<WriteFile>;
  34. using PosixWriteFilePtr = std::shared_ptr<PosixWriteFile>;
  35. // File system of create or delete directory
  36. class FileSystem {
  37. public:
  38. FileSystem() = default;
  39. virtual ~FileSystem() = default;
  40. // Create a new read/write file
  41. virtual WriteFilePtr CreateWriteFile(const string &file_name) = 0;
  42. // Check the file is exist?
  43. virtual bool FileExist(const string &file_name) = 0;
  44. // Delete the file
  45. virtual bool DeleteFile(const string &file_name) = 0;
  46. // Create a directory
  47. virtual bool CreateDir(const string &dir_name) = 0;
  48. // Delete the specified directory
  49. virtual bool DeleteDir(const string &dir_name) = 0;
  50. };
  51. // A file that can be read and write
  52. class WriteFile {
  53. public:
  54. explicit WriteFile(const string &file_name) : file_name_(file_name) {}
  55. virtual ~WriteFile() = default;
  56. // Open the file
  57. virtual bool Open() = 0;
  58. // append the content to file
  59. virtual bool Write(const std::string &data) {
  60. MS_LOG(WARNING) << "Attention: Maybe not call the function.";
  61. return true;
  62. }
  63. // name: return the file name
  64. string get_file_name() { return file_name_; }
  65. // flush: flush local buffer data to filesystem.
  66. virtual bool Flush() = 0;
  67. // sync: sync the content to disk
  68. virtual bool Sync() = 0;
  69. // close the file
  70. virtual bool Close() = 0;
  71. protected:
  72. string file_name_;
  73. };
  74. #if defined(SYSTEM_ENV_POSIX)
  75. // File system of create or delete directory for posix system
  76. class PosixFileSystem : public FileSystem {
  77. public:
  78. PosixFileSystem() = default;
  79. ~PosixFileSystem() override = default;
  80. // create a new write file
  81. WriteFilePtr CreateWriteFile(const string &file_name) override;
  82. // check the file is exist?
  83. bool FileExist(const string &file_name) override;
  84. // delete the file
  85. bool DeleteFile(const string &file_name) override;
  86. // Create a Directory
  87. bool CreateDir(const string &dir_name) override;
  88. // Delete the specified directory.
  89. bool DeleteDir(const string &dir_name) override;
  90. };
  91. // A file that can be read and write for posix
  92. class PosixWriteFile : public WriteFile {
  93. public:
  94. explicit PosixWriteFile(const string &file_name) : WriteFile(file_name), file_(nullptr) {}
  95. PosixWriteFile(const PosixWriteFile &);
  96. PosixWriteFile &operator=(const PosixWriteFile &);
  97. ~PosixWriteFile() override {
  98. try {
  99. if (file_ != nullptr) {
  100. (void)fclose(file_);
  101. file_ = nullptr;
  102. }
  103. } catch (const std::exception &e) {
  104. MS_LOG(ERROR) << "Exception when closing file.";
  105. } catch (...) {
  106. MS_LOG(ERROR) << "Non standard exception when closing file.";
  107. }
  108. }
  109. bool Open() override {
  110. if (file_ != nullptr) {
  111. MS_LOG(WARNING) << "The File(" << file_name_ << ") already open.";
  112. return true;
  113. }
  114. // check the path
  115. if (nullptr == file_name_.c_str()) {
  116. MS_LOG(EXCEPTION) << "The file path is null.";
  117. }
  118. char path[PATH_MAX + 1] = {0x00};
  119. if (file_name_.size() > PATH_MAX || nullptr == realpath(file_name_.c_str(), path)) {
  120. MS_LOG(EXCEPTION) << "Convert to real path fail, file name is " << file_name_ << ".";
  121. }
  122. // open the file
  123. file_ = fopen(path, "w+");
  124. if (file_ == nullptr) {
  125. MS_LOG(ERROR) << "File(" << path << ") IO ERROR: " << errno << ".";
  126. return false;
  127. }
  128. return true;
  129. }
  130. bool Write(const std::string &data) override {
  131. MS_LOG(DEBUG) << "Write data(" << data.size() << ") to file(" << this->file_name_ << ").";
  132. size_t r = fwrite(data.data(), 1, data.size(), file_);
  133. if (r != data.size()) {
  134. MS_LOG(ERROR) << "File(" << file_name_ << ") IO ERROR: " << errno << ".";
  135. return false;
  136. }
  137. return true;
  138. }
  139. bool Close() override {
  140. if (file_ == nullptr) {
  141. MS_LOG(INFO) << "File(" << file_name_ << ") already close.";
  142. return true;
  143. }
  144. bool result = true;
  145. if (fclose(file_) != 0) {
  146. MS_LOG(ERROR) << "File(" << file_name_ << ") IO ERROR: " << errno << ".";
  147. result = false;
  148. }
  149. file_ = nullptr;
  150. return result;
  151. }
  152. bool Flush() override {
  153. if (fflush(file_) != 0) {
  154. MS_LOG(ERROR) << "File(" << file_name_ << ") IO ERROR: " << EBADF << ".";
  155. return false;
  156. }
  157. return true;
  158. }
  159. bool Sync() override { return Flush(); }
  160. private:
  161. FILE *file_;
  162. };
  163. #endif
  164. } // namespace system
  165. } // namespace mindspore
  166. #endif // MINDSPORE_CCSRC_UTILS_SYSTEM_FILE_SYSTEM_H_