file.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  1. // Copyright 2021 yuzu Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include "common/fs/file.h"
  5. #include "common/fs/fs.h"
  6. #include "common/fs/path_util.h"
  7. #include "common/logging/log.h"
  8. #ifdef _WIN32
  9. #include <io.h>
  10. #include <share.h>
  11. #else
  12. #include <unistd.h>
  13. #endif
  14. #ifdef _MSC_VER
  15. #define fileno _fileno
  16. #define fseeko _fseeki64
  17. #define ftello _ftelli64
  18. #endif
  19. namespace Common::FS {
  20. namespace fs = std::filesystem;
  21. namespace {
  22. #ifdef _WIN32
  23. /**
  24. * Converts the file access mode and file type enums to a file access mode wide string.
  25. *
  26. * @param mode File access mode
  27. * @param type File type
  28. *
  29. * @returns A pointer to a wide string representing the file access mode.
  30. */
  31. [[nodiscard]] constexpr const wchar_t* AccessModeToWStr(FileAccessMode mode, FileType type) {
  32. switch (type) {
  33. case FileType::BinaryFile:
  34. switch (mode) {
  35. case FileAccessMode::Read:
  36. return L"rb";
  37. case FileAccessMode::Write:
  38. return L"wb";
  39. case FileAccessMode::Append:
  40. return L"ab";
  41. case FileAccessMode::ReadWrite:
  42. return L"r+b";
  43. case FileAccessMode::ReadAppend:
  44. return L"a+b";
  45. }
  46. break;
  47. case FileType::TextFile:
  48. switch (mode) {
  49. case FileAccessMode::Read:
  50. return L"r";
  51. case FileAccessMode::Write:
  52. return L"w";
  53. case FileAccessMode::Append:
  54. return L"a";
  55. case FileAccessMode::ReadWrite:
  56. return L"r+";
  57. case FileAccessMode::ReadAppend:
  58. return L"a+";
  59. }
  60. break;
  61. }
  62. return L"";
  63. }
  64. /**
  65. * Converts the file-share access flag enum to a Windows defined file-share access flag.
  66. *
  67. * @param flag File-share access flag
  68. *
  69. * @returns Windows defined file-share access flag.
  70. */
  71. [[nodiscard]] constexpr int ToWindowsFileShareFlag(FileShareFlag flag) {
  72. switch (flag) {
  73. case FileShareFlag::ShareNone:
  74. default:
  75. return _SH_DENYRW;
  76. case FileShareFlag::ShareReadOnly:
  77. return _SH_DENYWR;
  78. case FileShareFlag::ShareWriteOnly:
  79. return _SH_DENYRD;
  80. case FileShareFlag::ShareReadWrite:
  81. return _SH_DENYNO;
  82. }
  83. }
  84. #else
  85. /**
  86. * Converts the file access mode and file type enums to a file access mode string.
  87. *
  88. * @param mode File access mode
  89. * @param type File type
  90. *
  91. * @returns A pointer to a string representing the file access mode.
  92. */
  93. [[nodiscard]] constexpr const char* AccessModeToStr(FileAccessMode mode, FileType type) {
  94. switch (type) {
  95. case FileType::BinaryFile:
  96. switch (mode) {
  97. case FileAccessMode::Read:
  98. return "rb";
  99. case FileAccessMode::Write:
  100. return "wb";
  101. case FileAccessMode::Append:
  102. return "ab";
  103. case FileAccessMode::ReadWrite:
  104. return "r+b";
  105. case FileAccessMode::ReadAppend:
  106. return "a+b";
  107. }
  108. break;
  109. case FileType::TextFile:
  110. switch (mode) {
  111. case FileAccessMode::Read:
  112. return "r";
  113. case FileAccessMode::Write:
  114. return "w";
  115. case FileAccessMode::Append:
  116. return "a";
  117. case FileAccessMode::ReadWrite:
  118. return "r+";
  119. case FileAccessMode::ReadAppend:
  120. return "a+";
  121. }
  122. break;
  123. }
  124. return "";
  125. }
  126. #endif
  127. /**
  128. * Converts the seek origin enum to a seek origin integer.
  129. *
  130. * @param origin Seek origin
  131. *
  132. * @returns Seek origin integer.
  133. */
  134. [[nodiscard]] constexpr int ToSeekOrigin(SeekOrigin origin) {
  135. switch (origin) {
  136. case SeekOrigin::SetOrigin:
  137. default:
  138. return SEEK_SET;
  139. case SeekOrigin::CurrentPosition:
  140. return SEEK_CUR;
  141. case SeekOrigin::End:
  142. return SEEK_END;
  143. }
  144. }
  145. } // Anonymous namespace
  146. std::string ReadStringFromFile(const std::filesystem::path& path, FileType type) {
  147. if (!IsFile(path)) {
  148. return "";
  149. }
  150. IOFile io_file{path, FileAccessMode::Read, type};
  151. return io_file.ReadString(io_file.GetSize());
  152. }
  153. size_t WriteStringToFile(const std::filesystem::path& path, FileType type,
  154. std::string_view string) {
  155. if (Exists(path) && !IsFile(path)) {
  156. return 0;
  157. }
  158. IOFile io_file{path, FileAccessMode::Write, type};
  159. return io_file.WriteString(string);
  160. }
  161. size_t AppendStringToFile(const std::filesystem::path& path, FileType type,
  162. std::string_view string) {
  163. if (Exists(path) && !IsFile(path)) {
  164. return 0;
  165. }
  166. IOFile io_file{path, FileAccessMode::Append, type};
  167. return io_file.WriteString(string);
  168. }
  169. IOFile::IOFile() = default;
  170. IOFile::IOFile(const std::string& path, FileAccessMode mode, FileType type, FileShareFlag flag) {
  171. Open(path, mode, type, flag);
  172. }
  173. IOFile::IOFile(std::string_view path, FileAccessMode mode, FileType type, FileShareFlag flag) {
  174. Open(path, mode, type, flag);
  175. }
  176. IOFile::IOFile(const fs::path& path, FileAccessMode mode, FileType type, FileShareFlag flag) {
  177. Open(path, mode, type, flag);
  178. }
  179. IOFile::~IOFile() {
  180. Close();
  181. }
  182. IOFile::IOFile(IOFile&& other) noexcept {
  183. std::swap(file_path, other.file_path);
  184. std::swap(file_access_mode, other.file_access_mode);
  185. std::swap(file_type, other.file_type);
  186. std::swap(file, other.file);
  187. }
  188. IOFile& IOFile::operator=(IOFile&& other) noexcept {
  189. std::swap(file_path, other.file_path);
  190. std::swap(file_access_mode, other.file_access_mode);
  191. std::swap(file_type, other.file_type);
  192. std::swap(file, other.file);
  193. return *this;
  194. }
  195. fs::path IOFile::GetPath() const {
  196. return file_path;
  197. }
  198. FileAccessMode IOFile::GetAccessMode() const {
  199. return file_access_mode;
  200. }
  201. FileType IOFile::GetType() const {
  202. return file_type;
  203. }
  204. void IOFile::Open(const fs::path& path, FileAccessMode mode, FileType type, FileShareFlag flag) {
  205. Close();
  206. file_path = path;
  207. file_access_mode = mode;
  208. file_type = type;
  209. errno = 0;
  210. #ifdef _WIN32
  211. if (flag != FileShareFlag::ShareNone) {
  212. file = _wfsopen(path.c_str(), AccessModeToWStr(mode, type), ToWindowsFileShareFlag(flag));
  213. } else {
  214. _wfopen_s(&file, path.c_str(), AccessModeToWStr(mode, type));
  215. }
  216. #else
  217. file = std::fopen(path.c_str(), AccessModeToStr(mode, type));
  218. #endif
  219. if (!IsOpen()) {
  220. const auto ec = std::error_code{errno, std::generic_category()};
  221. LOG_ERROR(Common_Filesystem, "Failed to open the file at path={}, ec_message={}",
  222. PathToUTF8String(file_path), ec.message());
  223. }
  224. }
  225. void IOFile::Close() {
  226. if (!IsOpen()) {
  227. return;
  228. }
  229. errno = 0;
  230. const auto close_result = std::fclose(file) == 0;
  231. if (!close_result) {
  232. const auto ec = std::error_code{errno, std::generic_category()};
  233. LOG_ERROR(Common_Filesystem, "Failed to close the file at path={}, ec_message={}",
  234. PathToUTF8String(file_path), ec.message());
  235. }
  236. file = nullptr;
  237. }
  238. bool IOFile::IsOpen() const {
  239. return file != nullptr;
  240. }
  241. std::string IOFile::ReadString(size_t length) const {
  242. std::vector<char> string_buffer(length);
  243. const auto chars_read = ReadSpan<char>(string_buffer);
  244. const auto string_size = chars_read != length ? chars_read : length;
  245. return std::string{string_buffer.data(), string_size};
  246. }
  247. size_t IOFile::WriteString(std::span<const char> string) const {
  248. return WriteSpan(string);
  249. }
  250. bool IOFile::Flush() const {
  251. if (!IsOpen()) {
  252. return false;
  253. }
  254. errno = 0;
  255. #ifdef _WIN32
  256. const auto flush_result = std::fflush(file) == 0;
  257. #else
  258. const auto flush_result = std::fflush(file) == 0;
  259. #endif
  260. if (!flush_result) {
  261. const auto ec = std::error_code{errno, std::generic_category()};
  262. LOG_ERROR(Common_Filesystem, "Failed to flush the file at path={}, ec_message={}",
  263. PathToUTF8String(file_path), ec.message());
  264. }
  265. return flush_result;
  266. }
  267. bool IOFile::Commit() const {
  268. if (!IsOpen()) {
  269. return false;
  270. }
  271. errno = 0;
  272. #ifdef _WIN32
  273. const auto commit_result = std::fflush(file) == 0 && _commit(fileno(file)) == 0;
  274. #else
  275. const auto commit_result = std::fflush(file) == 0 && fsync(fileno(file)) == 0;
  276. #endif
  277. if (!commit_result) {
  278. const auto ec = std::error_code{errno, std::generic_category()};
  279. LOG_ERROR(Common_Filesystem, "Failed to commit the file at path={}, ec_message={}",
  280. PathToUTF8String(file_path), ec.message());
  281. }
  282. return commit_result;
  283. }
  284. bool IOFile::SetSize(u64 size) const {
  285. if (!IsOpen()) {
  286. return false;
  287. }
  288. errno = 0;
  289. #ifdef _WIN32
  290. const auto set_size_result = _chsize_s(fileno(file), static_cast<s64>(size)) == 0;
  291. #else
  292. const auto set_size_result = ftruncate(fileno(file), static_cast<s64>(size)) == 0;
  293. #endif
  294. if (!set_size_result) {
  295. const auto ec = std::error_code{errno, std::generic_category()};
  296. LOG_ERROR(Common_Filesystem, "Failed to resize the file at path={}, size={}, ec_message={}",
  297. PathToUTF8String(file_path), size, ec.message());
  298. }
  299. return set_size_result;
  300. }
  301. u64 IOFile::GetSize() const {
  302. if (!IsOpen()) {
  303. return 0;
  304. }
  305. // Flush any unwritten buffered data into the file prior to retrieving the file size.
  306. std::fflush(file);
  307. std::error_code ec;
  308. const auto file_size = fs::file_size(file_path, ec);
  309. if (ec) {
  310. LOG_ERROR(Common_Filesystem, "Failed to retrieve the file size of path={}, ec_message={}",
  311. PathToUTF8String(file_path), ec.message());
  312. return 0;
  313. }
  314. return file_size;
  315. }
  316. bool IOFile::Seek(s64 offset, SeekOrigin origin) const {
  317. if (!IsOpen()) {
  318. return false;
  319. }
  320. errno = 0;
  321. const auto seek_result = fseeko(file, offset, ToSeekOrigin(origin)) == 0;
  322. if (!seek_result) {
  323. const auto ec = std::error_code{errno, std::generic_category()};
  324. LOG_ERROR(Common_Filesystem,
  325. "Failed to seek the file at path={}, offset={}, origin={}, ec_message={}",
  326. PathToUTF8String(file_path), offset, origin, ec.message());
  327. }
  328. return seek_result;
  329. }
  330. s64 IOFile::Tell() const {
  331. if (!IsOpen()) {
  332. return 0;
  333. }
  334. errno = 0;
  335. return ftello(file);
  336. }
  337. } // namespace Common::FS