file_access_windows.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  1. /**************************************************************************/
  2. /* file_access_windows.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  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 "core/os/os.h"
  33. #include "core/string/print_string.h"
  34. #include <share.h> // _SH_DENYNO
  35. #include <shlwapi.h>
  36. #define WIN32_LEAN_AND_MEAN
  37. #include <windows.h>
  38. #include <errno.h>
  39. #include <sys/stat.h>
  40. #include <sys/types.h>
  41. #include <tchar.h>
  42. #include <wchar.h>
  43. #ifdef _MSC_VER
  44. #define S_ISREG(m) ((m)&_S_IFREG)
  45. #endif
  46. void FileAccessWindows::check_errors() const {
  47. ERR_FAIL_COND(!f);
  48. if (feof(f)) {
  49. last_error = ERR_FILE_EOF;
  50. }
  51. }
  52. bool FileAccessWindows::is_path_invalid(const String &p_path) {
  53. // Check for invalid operating system file.
  54. String fname = p_path;
  55. int dot = fname.find(".");
  56. if (dot != -1) {
  57. fname = fname.substr(0, dot);
  58. }
  59. fname = fname.to_lower();
  60. return invalid_files.has(fname);
  61. }
  62. Error FileAccessWindows::open_internal(const String &p_path, int p_mode_flags) {
  63. if (is_path_invalid(p_path)) {
  64. #ifdef DEBUG_ENABLED
  65. if (p_mode_flags != READ) {
  66. WARN_PRINT("The path :" + p_path + " is a reserved Windows system pipe, so it can't be used for creating files.");
  67. }
  68. #endif
  69. return ERR_INVALID_PARAMETER;
  70. }
  71. _close();
  72. path_src = p_path;
  73. path = fix_path(p_path);
  74. const WCHAR *mode_string;
  75. if (p_mode_flags == READ) {
  76. mode_string = L"rb";
  77. } else if (p_mode_flags == WRITE) {
  78. mode_string = L"wb";
  79. } else if (p_mode_flags == READ_WRITE) {
  80. mode_string = L"rb+";
  81. } else if (p_mode_flags == WRITE_READ) {
  82. mode_string = L"wb+";
  83. } else {
  84. return ERR_INVALID_PARAMETER;
  85. }
  86. /* Pretty much every implementation that uses fopen as primary
  87. backend supports utf8 encoding. */
  88. struct _stat st;
  89. if (_wstat((LPCWSTR)(path.utf16().get_data()), &st) == 0) {
  90. if (!S_ISREG(st.st_mode)) {
  91. return ERR_FILE_CANT_OPEN;
  92. }
  93. }
  94. #ifdef TOOLS_ENABLED
  95. // Windows is case insensitive, but all other platforms are sensitive to it
  96. // To ease cross-platform development, we issue a warning if users try to access
  97. // a file using the wrong case (which *works* on Windows, but won't on other
  98. // platforms).
  99. if (p_mode_flags == READ) {
  100. WIN32_FIND_DATAW d;
  101. HANDLE fnd = FindFirstFileW((LPCWSTR)(path.utf16().get_data()), &d);
  102. if (fnd != INVALID_HANDLE_VALUE) {
  103. String fname = String::utf16((const char16_t *)(d.cFileName));
  104. if (!fname.is_empty()) {
  105. String base_file = path.get_file();
  106. if (base_file != fname && base_file.findn(fname) == 0) {
  107. WARN_PRINT("Case mismatch opening requested file '" + base_file + "', stored as '" + fname + "' in the filesystem. This file will not open when exported to other case-sensitive platforms.");
  108. }
  109. }
  110. FindClose(fnd);
  111. }
  112. }
  113. #endif
  114. if (is_backup_save_enabled() && p_mode_flags == WRITE) {
  115. save_path = path;
  116. path = path + ".tmp";
  117. }
  118. f = _wfsopen((LPCWSTR)(path.utf16().get_data()), mode_string, _SH_DENYNO);
  119. if (f == nullptr) {
  120. switch (errno) {
  121. case ENOENT: {
  122. last_error = ERR_FILE_NOT_FOUND;
  123. } break;
  124. default: {
  125. last_error = ERR_FILE_CANT_OPEN;
  126. } break;
  127. }
  128. return last_error;
  129. } else {
  130. last_error = OK;
  131. flags = p_mode_flags;
  132. return OK;
  133. }
  134. }
  135. void FileAccessWindows::_close() {
  136. if (!f) {
  137. return;
  138. }
  139. fclose(f);
  140. f = nullptr;
  141. if (!save_path.is_empty()) {
  142. bool rename_error = true;
  143. int attempts = 4;
  144. while (rename_error && attempts) {
  145. // This workaround of trying multiple times is added to deal with paranoid Windows
  146. // antiviruses that love reading just written files even if they are not executable, thus
  147. // locking the file and preventing renaming from happening.
  148. #ifdef UWP_ENABLED
  149. // UWP has no PathFileExists, so we check attributes instead
  150. DWORD fileAttr;
  151. fileAttr = GetFileAttributesW((LPCWSTR)(save_path.utf16().get_data()));
  152. if (INVALID_FILE_ATTRIBUTES == fileAttr) {
  153. #else
  154. if (!PathFileExistsW((LPCWSTR)(save_path.utf16().get_data()))) {
  155. #endif
  156. // Creating new file
  157. rename_error = _wrename((LPCWSTR)((save_path + ".tmp").utf16().get_data()), (LPCWSTR)(save_path.utf16().get_data())) != 0;
  158. } else {
  159. // Atomic replace for existing file
  160. rename_error = !ReplaceFileW((LPCWSTR)(save_path.utf16().get_data()), (LPCWSTR)((save_path + ".tmp").utf16().get_data()), nullptr, 2 | 4, nullptr, nullptr);
  161. }
  162. if (rename_error) {
  163. attempts--;
  164. OS::get_singleton()->delay_usec(100000); // wait 100msec and try again
  165. }
  166. }
  167. if (rename_error) {
  168. if (close_fail_notify) {
  169. close_fail_notify(save_path);
  170. }
  171. }
  172. save_path = "";
  173. ERR_FAIL_COND_MSG(rename_error, "Safe save failed. This may be a permissions problem, but also may happen because you are running a paranoid antivirus. If this is the case, please switch to Windows Defender or disable the 'safe save' option in editor settings. This makes it work, but increases the risk of file corruption in a crash.");
  174. }
  175. }
  176. String FileAccessWindows::get_path() const {
  177. return path_src;
  178. }
  179. String FileAccessWindows::get_path_absolute() const {
  180. return path;
  181. }
  182. bool FileAccessWindows::is_open() const {
  183. return (f != nullptr);
  184. }
  185. void FileAccessWindows::seek(uint64_t p_position) {
  186. ERR_FAIL_COND(!f);
  187. last_error = OK;
  188. if (_fseeki64(f, p_position, SEEK_SET)) {
  189. check_errors();
  190. }
  191. prev_op = 0;
  192. }
  193. void FileAccessWindows::seek_end(int64_t p_position) {
  194. ERR_FAIL_COND(!f);
  195. if (_fseeki64(f, p_position, SEEK_END)) {
  196. check_errors();
  197. }
  198. prev_op = 0;
  199. }
  200. uint64_t FileAccessWindows::get_position() const {
  201. int64_t aux_position = _ftelli64(f);
  202. if (aux_position < 0) {
  203. check_errors();
  204. }
  205. return aux_position;
  206. }
  207. uint64_t FileAccessWindows::get_length() const {
  208. ERR_FAIL_COND_V(!f, 0);
  209. uint64_t pos = get_position();
  210. _fseeki64(f, 0, SEEK_END);
  211. uint64_t size = get_position();
  212. _fseeki64(f, pos, SEEK_SET);
  213. return size;
  214. }
  215. bool FileAccessWindows::eof_reached() const {
  216. check_errors();
  217. return last_error == ERR_FILE_EOF;
  218. }
  219. uint8_t FileAccessWindows::get_8() const {
  220. ERR_FAIL_COND_V(!f, 0);
  221. if (flags == READ_WRITE || flags == WRITE_READ) {
  222. if (prev_op == WRITE) {
  223. fflush(f);
  224. }
  225. prev_op = READ;
  226. }
  227. uint8_t b;
  228. if (fread(&b, 1, 1, f) == 0) {
  229. check_errors();
  230. b = '\0';
  231. }
  232. return b;
  233. }
  234. uint64_t FileAccessWindows::get_buffer(uint8_t *p_dst, uint64_t p_length) const {
  235. ERR_FAIL_COND_V(!p_dst && p_length > 0, -1);
  236. ERR_FAIL_COND_V(!f, -1);
  237. if (flags == READ_WRITE || flags == WRITE_READ) {
  238. if (prev_op == WRITE) {
  239. fflush(f);
  240. }
  241. prev_op = READ;
  242. }
  243. uint64_t read = fread(p_dst, 1, p_length, f);
  244. check_errors();
  245. return read;
  246. }
  247. Error FileAccessWindows::get_error() const {
  248. return last_error;
  249. }
  250. void FileAccessWindows::flush() {
  251. ERR_FAIL_COND(!f);
  252. fflush(f);
  253. if (prev_op == WRITE) {
  254. prev_op = 0;
  255. }
  256. }
  257. void FileAccessWindows::store_8(uint8_t p_dest) {
  258. ERR_FAIL_COND(!f);
  259. if (flags == READ_WRITE || flags == WRITE_READ) {
  260. if (prev_op == READ) {
  261. if (last_error != ERR_FILE_EOF) {
  262. fseek(f, 0, SEEK_CUR);
  263. }
  264. }
  265. prev_op = WRITE;
  266. }
  267. fwrite(&p_dest, 1, 1, f);
  268. }
  269. void FileAccessWindows::store_buffer(const uint8_t *p_src, uint64_t p_length) {
  270. ERR_FAIL_COND(!f);
  271. ERR_FAIL_COND(!p_src && p_length > 0);
  272. if (flags == READ_WRITE || flags == WRITE_READ) {
  273. if (prev_op == READ) {
  274. if (last_error != ERR_FILE_EOF) {
  275. fseek(f, 0, SEEK_CUR);
  276. }
  277. }
  278. prev_op = WRITE;
  279. }
  280. ERR_FAIL_COND(fwrite(p_src, 1, p_length, f) != (size_t)p_length);
  281. }
  282. bool FileAccessWindows::file_exists(const String &p_name) {
  283. if (is_path_invalid(p_name)) {
  284. return false;
  285. }
  286. String filename = fix_path(p_name);
  287. FILE *g = _wfsopen((LPCWSTR)(filename.utf16().get_data()), L"rb", _SH_DENYNO);
  288. if (g == nullptr) {
  289. return false;
  290. } else {
  291. fclose(g);
  292. return true;
  293. }
  294. }
  295. uint64_t FileAccessWindows::_get_modified_time(const String &p_file) {
  296. if (is_path_invalid(p_file)) {
  297. return 0;
  298. }
  299. String file = fix_path(p_file);
  300. if (file.ends_with("/") && file != "/") {
  301. file = file.substr(0, file.length() - 1);
  302. }
  303. struct _stat st;
  304. int rv = _wstat((LPCWSTR)(file.utf16().get_data()), &st);
  305. if (rv == 0) {
  306. return st.st_mtime;
  307. } else {
  308. print_verbose("Failed to get modified time for: " + p_file + "");
  309. return 0;
  310. }
  311. }
  312. uint32_t FileAccessWindows::_get_unix_permissions(const String &p_file) {
  313. return 0;
  314. }
  315. Error FileAccessWindows::_set_unix_permissions(const String &p_file, uint32_t p_permissions) {
  316. return ERR_UNAVAILABLE;
  317. }
  318. void FileAccessWindows::close() {
  319. _close();
  320. }
  321. FileAccessWindows::~FileAccessWindows() {
  322. _close();
  323. }
  324. HashSet<String> FileAccessWindows::invalid_files;
  325. void FileAccessWindows::initialize() {
  326. static const char *reserved_files[]{
  327. "con", "prn", "aux", "nul", "com0", "com1", "com2", "com3", "com4", "com5", "com6", "com7", "com8", "com9", "lpt0", "lpt1", "lpt2", "lpt3", "lpt4", "lpt5", "lpt6", "lpt7", "lpt8", "lpt9", nullptr
  328. };
  329. int reserved_file_index = 0;
  330. while (reserved_files[reserved_file_index] != nullptr) {
  331. invalid_files.insert(reserved_files[reserved_file_index]);
  332. reserved_file_index++;
  333. }
  334. }
  335. void FileAccessWindows::finalize() {
  336. invalid_files.clear();
  337. }
  338. #endif // WINDOWS_ENABLED