file_access_windows.cpp 6.6 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-2017 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2017 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_path, int p_mode_flags) {
  49. String filename = fix_path(p_path);
  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 UWP_ENABLED
  97. // UWP 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_position() const {
  132. size_t aux_position = 0;
  133. aux_position = ftell(f);
  134. if (!aux_position) {
  135. check_errors();
  136. };
  137. return aux_position;
  138. }
  139. size_t FileAccessWindows::get_len() const {
  140. ERR_FAIL_COND_V(!f, 0);
  141. size_t pos = get_position();
  142. fseek(f, 0, SEEK_END);
  143. int size = get_position();
  144. fseek(f, pos, SEEK_SET);
  145. return size;
  146. }
  147. bool FileAccessWindows::eof_reached() const {
  148. check_errors();
  149. return last_error == ERR_FILE_EOF;
  150. }
  151. uint8_t FileAccessWindows::get_8() const {
  152. ERR_FAIL_COND_V(!f, 0);
  153. uint8_t b;
  154. if (fread(&b, 1, 1, f) == 0) {
  155. check_errors();
  156. };
  157. return b;
  158. }
  159. int FileAccessWindows::get_buffer(uint8_t *p_dst, int p_length) const {
  160. ERR_FAIL_COND_V(!f, -1);
  161. int read = fread(p_dst, 1, p_length, f);
  162. check_errors();
  163. return read;
  164. };
  165. Error FileAccessWindows::get_error() const {
  166. return last_error;
  167. }
  168. void FileAccessWindows::flush() {
  169. ERR_FAIL_COND(!f);
  170. fflush(f);
  171. }
  172. void FileAccessWindows::store_8(uint8_t p_dest) {
  173. ERR_FAIL_COND(!f);
  174. fwrite(&p_dest, 1, 1, f);
  175. }
  176. bool FileAccessWindows::file_exists(const String &p_name) {
  177. FILE *g;
  178. //printf("opening file %s\n", p_fname.c_str());
  179. String filename = fix_path(p_name);
  180. g = _wfopen(filename.c_str(), L"rb");
  181. if (g == NULL) {
  182. return false;
  183. } else {
  184. fclose(g);
  185. return true;
  186. }
  187. }
  188. uint64_t FileAccessWindows::_get_modified_time(const String &p_file) {
  189. String file = fix_path(p_file);
  190. if (file.ends_with("/") && file != "/")
  191. file = file.substr(0, file.length() - 1);
  192. struct _stat st;
  193. int rv = _wstat(file.c_str(), &st);
  194. if (rv == 0) {
  195. return st.st_mtime;
  196. } else {
  197. print_line("no access to " + file);
  198. }
  199. ERR_FAIL_V(0);
  200. };
  201. FileAccessWindows::FileAccessWindows() {
  202. f = NULL;
  203. flags = 0;
  204. last_error = OK;
  205. }
  206. FileAccessWindows::~FileAccessWindows() {
  207. close();
  208. }
  209. #endif