file_access_windows.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. /*************************************************************************/
  2. /* file_access_windows.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #ifdef WINDOWS_ENABLED
  31. #include "file_access_windows.h"
  32. #include "shlwapi.h"
  33. #include <windows.h>
  34. #include "print_string.h"
  35. #include <sys/stat.h>
  36. #include <sys/types.h>
  37. #include <tchar.h>
  38. #include <wchar.h>
  39. #ifdef _MSC_VER
  40. #define S_ISREG(m) ((m)&_S_IFREG)
  41. #endif
  42. void FileAccessWindows::check_errors() const {
  43. ERR_FAIL_COND(!f);
  44. if (feof(f)) {
  45. last_error = ERR_FILE_EOF;
  46. }
  47. }
  48. Error FileAccessWindows::_open(const String &p_filename, int p_mode_flags) {
  49. String filename = fix_path(p_filename);
  50. if (f)
  51. close();
  52. const wchar_t *mode_string;
  53. if (p_mode_flags == READ)
  54. mode_string = L"rb";
  55. else if (p_mode_flags == WRITE)
  56. mode_string = L"wb";
  57. else if (p_mode_flags == READ_WRITE)
  58. mode_string = L"rb+";
  59. else if (p_mode_flags == WRITE_READ)
  60. mode_string = L"wb+";
  61. else
  62. return ERR_INVALID_PARAMETER;
  63. /* pretty much every implementation that uses fopen as primary
  64. backend supports utf8 encoding */
  65. struct _stat st;
  66. if (_wstat(filename.c_str(), &st) == 0) {
  67. if (!S_ISREG(st.st_mode))
  68. return ERR_FILE_CANT_OPEN;
  69. };
  70. if (is_backup_save_enabled() && p_mode_flags & WRITE && !(p_mode_flags & READ)) {
  71. save_path = filename;
  72. filename = filename + ".tmp";
  73. //print_line("saving instead to "+path);
  74. }
  75. f = _wfopen(filename.c_str(), mode_string);
  76. if (f == NULL) {
  77. last_error = ERR_FILE_CANT_OPEN;
  78. return ERR_FILE_CANT_OPEN;
  79. } else {
  80. last_error = OK;
  81. flags = p_mode_flags;
  82. return OK;
  83. }
  84. }
  85. void FileAccessWindows::close() {
  86. if (!f)
  87. return;
  88. fclose(f);
  89. f = NULL;
  90. if (save_path != "") {
  91. //unlink(save_path.utf8().get_data());
  92. //print_line("renaming..");
  93. //_wunlink(save_path.c_str()); //unlink if exists
  94. //int rename_error = _wrename((save_path+".tmp").c_str(),save_path.c_str());
  95. bool rename_error;
  96. #ifdef WINRT_ENABLED
  97. // WinRT has no PathFileExists, so we check attributes instead
  98. DWORD fileAttr;
  99. fileAttr = GetFileAttributesW(save_path.c_str());
  100. if (INVALID_FILE_ATTRIBUTES == fileAttr) {
  101. #else
  102. if (!PathFileExistsW(save_path.c_str())) {
  103. #endif
  104. //creating new file
  105. rename_error = _wrename((save_path + ".tmp").c_str(), save_path.c_str()) != 0;
  106. } else {
  107. //atomic replace for existing file
  108. rename_error = !ReplaceFileW(save_path.c_str(), (save_path + ".tmp").c_str(), NULL, 2 | 4, NULL, NULL);
  109. }
  110. if (rename_error && close_fail_notify) {
  111. close_fail_notify(save_path);
  112. }
  113. save_path = "";
  114. ERR_FAIL_COND(rename_error);
  115. }
  116. }
  117. bool FileAccessWindows::is_open() const {
  118. return (f != NULL);
  119. }
  120. void FileAccessWindows::seek(size_t p_position) {
  121. ERR_FAIL_COND(!f);
  122. last_error = OK;
  123. if (fseek(f, p_position, SEEK_SET))
  124. check_errors();
  125. }
  126. void FileAccessWindows::seek_end(int64_t p_position) {
  127. ERR_FAIL_COND(!f);
  128. if (fseek(f, p_position, SEEK_END))
  129. check_errors();
  130. }
  131. size_t FileAccessWindows::get_pos() const {
  132. size_t aux_position = 0;
  133. if (!(aux_position = ftell(f))) {
  134. check_errors();
  135. };
  136. return aux_position;
  137. }
  138. size_t FileAccessWindows::get_len() const {
  139. ERR_FAIL_COND_V(!f, 0);
  140. size_t pos = get_pos();
  141. fseek(f, 0, SEEK_END);
  142. int size = get_pos();
  143. fseek(f, pos, SEEK_SET);
  144. return size;
  145. }
  146. bool FileAccessWindows::eof_reached() const {
  147. check_errors();
  148. return last_error == ERR_FILE_EOF;
  149. }
  150. uint8_t FileAccessWindows::get_8() const {
  151. ERR_FAIL_COND_V(!f, 0);
  152. uint8_t b;
  153. if (fread(&b, 1, 1, f) == 0) {
  154. check_errors();
  155. };
  156. return b;
  157. }
  158. int FileAccessWindows::get_buffer(uint8_t *p_dst, int p_length) const {
  159. ERR_FAIL_COND_V(!f, -1);
  160. int read = fread(p_dst, 1, p_length, f);
  161. check_errors();
  162. return read;
  163. };
  164. Error FileAccessWindows::get_error() const {
  165. return last_error;
  166. }
  167. void FileAccessWindows::store_8(uint8_t p_dest) {
  168. ERR_FAIL_COND(!f);
  169. fwrite(&p_dest, 1, 1, f);
  170. }
  171. void FileAccessWindows::store_buffer(const uint8_t *p_src, int p_length) {
  172. ERR_FAIL_COND(!f);
  173. ERR_FAIL_COND(fwrite(p_src, 1, p_length, f) != p_length);
  174. }
  175. bool FileAccessWindows::file_exists(const String &p_name) {
  176. FILE *g;
  177. //printf("opening file %s\n", p_fname.c_str());
  178. String filename = fix_path(p_name);
  179. g = _wfopen(filename.c_str(), L"rb");
  180. if (g == NULL) {
  181. return false;
  182. } else {
  183. fclose(g);
  184. return true;
  185. }
  186. }
  187. uint64_t FileAccessWindows::_get_modified_time(const String &p_file) {
  188. String file = fix_path(p_file);
  189. if (file.ends_with("/") && file != "/")
  190. file = file.substr(0, file.length() - 1);
  191. struct _stat st;
  192. int rv = _wstat(file.c_str(), &st);
  193. if (rv == 0) {
  194. return st.st_mtime;
  195. } else {
  196. print_line("no access to " + file);
  197. }
  198. ERR_FAIL_V(0);
  199. };
  200. FileAccessWindows::FileAccessWindows() {
  201. f = NULL;
  202. flags = 0;
  203. last_error = OK;
  204. }
  205. FileAccessWindows::~FileAccessWindows() {
  206. close();
  207. }
  208. #endif