file_access_windows.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674
  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/config/project_settings.h"
  33. #include "core/os/os.h"
  34. #include "core/string/print_string.h"
  35. #include <share.h> // _SH_DENYNO
  36. #include <shlwapi.h>
  37. #define WIN32_LEAN_AND_MEAN
  38. #include <windows.h>
  39. #include <errno.h>
  40. #include <io.h>
  41. #include <sys/stat.h>
  42. #include <sys/types.h>
  43. #include <tchar.h>
  44. #include <wchar.h>
  45. #ifdef _MSC_VER
  46. #define S_ISREG(m) ((m) & _S_IFREG)
  47. #endif
  48. void FileAccessWindows::check_errors() const {
  49. ERR_FAIL_NULL(f);
  50. if (feof(f)) {
  51. last_error = ERR_FILE_EOF;
  52. }
  53. }
  54. bool FileAccessWindows::is_path_invalid(const String &p_path) {
  55. // Check for invalid operating system file.
  56. String fname = p_path.get_file().to_lower();
  57. int dot = fname.find(".");
  58. if (dot != -1) {
  59. fname = fname.substr(0, dot);
  60. }
  61. return invalid_files.has(fname);
  62. }
  63. String FileAccessWindows::fix_path(const String &p_path) const {
  64. String r_path = FileAccess::fix_path(p_path);
  65. if (r_path.is_absolute_path() && !r_path.is_network_share_path() && r_path.length() > MAX_PATH) {
  66. r_path = "\\\\?\\" + r_path.replace("/", "\\");
  67. }
  68. return r_path;
  69. }
  70. Error FileAccessWindows::open_internal(const String &p_path, int p_mode_flags) {
  71. if (is_path_invalid(p_path)) {
  72. #ifdef DEBUG_ENABLED
  73. if (p_mode_flags != READ) {
  74. WARN_PRINT("The path :" + p_path + " is a reserved Windows system pipe, so it can't be used for creating files.");
  75. }
  76. #endif
  77. return ERR_INVALID_PARAMETER;
  78. }
  79. _close();
  80. path_src = p_path;
  81. path = fix_path(p_path);
  82. const WCHAR *mode_string;
  83. if (p_mode_flags == READ) {
  84. mode_string = L"rb";
  85. } else if (p_mode_flags == WRITE) {
  86. mode_string = L"wb";
  87. } else if (p_mode_flags == READ_WRITE) {
  88. mode_string = L"rb+";
  89. } else if (p_mode_flags == WRITE_READ) {
  90. mode_string = L"wb+";
  91. } else {
  92. return ERR_INVALID_PARAMETER;
  93. }
  94. /* Pretty much every implementation that uses fopen as primary
  95. backend supports utf8 encoding. */
  96. struct _stat st;
  97. if (_wstat((LPCWSTR)(path.utf16().get_data()), &st) == 0) {
  98. if (!S_ISREG(st.st_mode)) {
  99. return ERR_FILE_CANT_OPEN;
  100. }
  101. }
  102. #ifdef TOOLS_ENABLED
  103. // Windows is case insensitive, but all other platforms are sensitive to it
  104. // To ease cross-platform development, we issue a warning if users try to access
  105. // a file using the wrong case (which *works* on Windows, but won't on other
  106. // platforms), we only check for relative paths, or paths in res:// or user://,
  107. // other paths aren't likely to be portable anyway.
  108. if (p_mode_flags == READ && (p_path.is_relative_path() || get_access_type() != ACCESS_FILESYSTEM)) {
  109. String base_path = path;
  110. String working_path;
  111. String proper_path;
  112. if (get_access_type() == ACCESS_RESOURCES) {
  113. if (ProjectSettings::get_singleton()) {
  114. working_path = ProjectSettings::get_singleton()->get_resource_path();
  115. if (!working_path.is_empty()) {
  116. base_path = working_path.path_to_file(base_path);
  117. }
  118. }
  119. proper_path = "res://";
  120. } else if (get_access_type() == ACCESS_USERDATA) {
  121. working_path = OS::get_singleton()->get_user_data_dir();
  122. if (!working_path.is_empty()) {
  123. base_path = working_path.path_to_file(base_path);
  124. }
  125. proper_path = "user://";
  126. }
  127. WIN32_FIND_DATAW d;
  128. Vector<String> parts = base_path.split("/");
  129. bool mismatch = false;
  130. for (const String &part : parts) {
  131. working_path = working_path.path_join(part);
  132. // Skip if relative.
  133. if (part == "." || part == "..") {
  134. proper_path = proper_path.path_join(part);
  135. continue;
  136. }
  137. HANDLE fnd = FindFirstFileW((LPCWSTR)(working_path.utf16().get_data()), &d);
  138. if (fnd == INVALID_HANDLE_VALUE) {
  139. mismatch = false;
  140. break;
  141. }
  142. const String fname = String::utf16((const char16_t *)(d.cFileName));
  143. FindClose(fnd);
  144. if (!mismatch) {
  145. mismatch = (part != fname && part.findn(fname) == 0);
  146. }
  147. proper_path = proper_path.path_join(fname);
  148. }
  149. if (mismatch) {
  150. WARN_PRINT("Case mismatch opening requested file '" + p_path + "', stored as '" + proper_path + "' in the filesystem. This file will not open when exported to other case-sensitive platforms.");
  151. }
  152. }
  153. #endif
  154. if (is_backup_save_enabled() && p_mode_flags == WRITE) {
  155. save_path = path;
  156. // Create a temporary file in the same directory as the target file.
  157. WCHAR tmpFileName[MAX_PATH];
  158. if (GetTempFileNameW((LPCWSTR)(path.get_base_dir().utf16().get_data()), (LPCWSTR)(path.get_file().utf16().get_data()), 0, tmpFileName) == 0) {
  159. last_error = ERR_FILE_CANT_OPEN;
  160. return last_error;
  161. }
  162. path = tmpFileName;
  163. }
  164. f = _wfsopen((LPCWSTR)(path.utf16().get_data()), mode_string, is_backup_save_enabled() ? _SH_SECURE : _SH_DENYNO);
  165. if (f == nullptr) {
  166. switch (errno) {
  167. case ENOENT: {
  168. last_error = ERR_FILE_NOT_FOUND;
  169. } break;
  170. default: {
  171. last_error = ERR_FILE_CANT_OPEN;
  172. } break;
  173. }
  174. return last_error;
  175. } else {
  176. last_error = OK;
  177. flags = p_mode_flags;
  178. return OK;
  179. }
  180. }
  181. void FileAccessWindows::_close() {
  182. if (!f) {
  183. return;
  184. }
  185. fclose(f);
  186. f = nullptr;
  187. if (!save_path.is_empty()) {
  188. // This workaround of trying multiple times is added to deal with paranoid Windows
  189. // antiviruses that love reading just written files even if they are not executable, thus
  190. // locking the file and preventing renaming from happening.
  191. bool rename_error = true;
  192. const Char16String &path_utf16 = path.utf16();
  193. const Char16String &save_path_utf16 = save_path.utf16();
  194. for (int i = 0; i < 1000; i++) {
  195. if (ReplaceFileW((LPCWSTR)(save_path_utf16.get_data()), (LPCWSTR)(path_utf16.get_data()), nullptr, REPLACEFILE_IGNORE_MERGE_ERRORS | REPLACEFILE_IGNORE_ACL_ERRORS, nullptr, nullptr)) {
  196. rename_error = false;
  197. } else {
  198. // Either the target exists and is locked (temporarily, hopefully)
  199. // or it doesn't exist; let's assume the latter before re-trying.
  200. rename_error = _wrename((LPCWSTR)(path_utf16.get_data()), (LPCWSTR)(save_path_utf16.get_data())) != 0;
  201. }
  202. if (!rename_error) {
  203. break;
  204. }
  205. OS::get_singleton()->delay_usec(1000);
  206. }
  207. if (rename_error) {
  208. if (close_fail_notify) {
  209. close_fail_notify(save_path);
  210. }
  211. }
  212. save_path = "";
  213. 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.");
  214. }
  215. }
  216. String FileAccessWindows::get_path() const {
  217. return path_src;
  218. }
  219. String FileAccessWindows::get_path_absolute() const {
  220. return path;
  221. }
  222. bool FileAccessWindows::is_open() const {
  223. return (f != nullptr);
  224. }
  225. void FileAccessWindows::seek(uint64_t p_position) {
  226. ERR_FAIL_NULL(f);
  227. last_error = OK;
  228. if (_fseeki64(f, p_position, SEEK_SET)) {
  229. check_errors();
  230. }
  231. prev_op = 0;
  232. }
  233. void FileAccessWindows::seek_end(int64_t p_position) {
  234. ERR_FAIL_NULL(f);
  235. if (_fseeki64(f, p_position, SEEK_END)) {
  236. check_errors();
  237. }
  238. prev_op = 0;
  239. }
  240. uint64_t FileAccessWindows::get_position() const {
  241. int64_t aux_position = _ftelli64(f);
  242. if (aux_position < 0) {
  243. check_errors();
  244. }
  245. return aux_position;
  246. }
  247. uint64_t FileAccessWindows::get_length() const {
  248. ERR_FAIL_NULL_V(f, 0);
  249. uint64_t pos = get_position();
  250. _fseeki64(f, 0, SEEK_END);
  251. uint64_t size = get_position();
  252. _fseeki64(f, pos, SEEK_SET);
  253. return size;
  254. }
  255. bool FileAccessWindows::eof_reached() const {
  256. check_errors();
  257. return last_error == ERR_FILE_EOF;
  258. }
  259. uint8_t FileAccessWindows::get_8() const {
  260. ERR_FAIL_NULL_V(f, 0);
  261. if (flags == READ_WRITE || flags == WRITE_READ) {
  262. if (prev_op == WRITE) {
  263. fflush(f);
  264. }
  265. prev_op = READ;
  266. }
  267. uint8_t b;
  268. if (fread(&b, 1, 1, f) == 0) {
  269. check_errors();
  270. b = '\0';
  271. }
  272. return b;
  273. }
  274. uint16_t FileAccessWindows::get_16() const {
  275. ERR_FAIL_NULL_V(f, 0);
  276. if (flags == READ_WRITE || flags == WRITE_READ) {
  277. if (prev_op == WRITE) {
  278. fflush(f);
  279. }
  280. prev_op = READ;
  281. }
  282. uint16_t b = 0;
  283. if (fread(&b, 1, 2, f) != 2) {
  284. check_errors();
  285. }
  286. if (big_endian) {
  287. b = BSWAP16(b);
  288. }
  289. return b;
  290. }
  291. uint32_t FileAccessWindows::get_32() const {
  292. ERR_FAIL_NULL_V(f, 0);
  293. if (flags == READ_WRITE || flags == WRITE_READ) {
  294. if (prev_op == WRITE) {
  295. fflush(f);
  296. }
  297. prev_op = READ;
  298. }
  299. uint32_t b = 0;
  300. if (fread(&b, 1, 4, f) != 4) {
  301. check_errors();
  302. }
  303. if (big_endian) {
  304. b = BSWAP32(b);
  305. }
  306. return b;
  307. }
  308. uint64_t FileAccessWindows::get_64() const {
  309. ERR_FAIL_NULL_V(f, 0);
  310. if (flags == READ_WRITE || flags == WRITE_READ) {
  311. if (prev_op == WRITE) {
  312. fflush(f);
  313. }
  314. prev_op = READ;
  315. }
  316. uint64_t b = 0;
  317. if (fread(&b, 1, 8, f) != 8) {
  318. check_errors();
  319. }
  320. if (big_endian) {
  321. b = BSWAP64(b);
  322. }
  323. return b;
  324. }
  325. uint64_t FileAccessWindows::get_buffer(uint8_t *p_dst, uint64_t p_length) const {
  326. ERR_FAIL_COND_V(!p_dst && p_length > 0, -1);
  327. ERR_FAIL_NULL_V(f, -1);
  328. if (flags == READ_WRITE || flags == WRITE_READ) {
  329. if (prev_op == WRITE) {
  330. fflush(f);
  331. }
  332. prev_op = READ;
  333. }
  334. uint64_t read = fread(p_dst, 1, p_length, f);
  335. check_errors();
  336. return read;
  337. }
  338. Error FileAccessWindows::get_error() const {
  339. return last_error;
  340. }
  341. Error FileAccessWindows::resize(int64_t p_length) {
  342. ERR_FAIL_NULL_V_MSG(f, FAILED, "File must be opened before use.");
  343. errno_t res = _chsize_s(_fileno(f), p_length);
  344. switch (res) {
  345. case 0:
  346. return OK;
  347. case EACCES:
  348. case EBADF:
  349. return ERR_FILE_CANT_OPEN;
  350. case ENOSPC:
  351. return ERR_OUT_OF_MEMORY;
  352. case EINVAL:
  353. return ERR_INVALID_PARAMETER;
  354. default:
  355. return FAILED;
  356. }
  357. }
  358. void FileAccessWindows::flush() {
  359. ERR_FAIL_NULL(f);
  360. fflush(f);
  361. if (prev_op == WRITE) {
  362. prev_op = 0;
  363. }
  364. }
  365. void FileAccessWindows::store_8(uint8_t p_dest) {
  366. ERR_FAIL_NULL(f);
  367. if (flags == READ_WRITE || flags == WRITE_READ) {
  368. if (prev_op == READ) {
  369. if (last_error != ERR_FILE_EOF) {
  370. fseek(f, 0, SEEK_CUR);
  371. }
  372. }
  373. prev_op = WRITE;
  374. }
  375. fwrite(&p_dest, 1, 1, f);
  376. }
  377. void FileAccessWindows::store_16(uint16_t p_dest) {
  378. ERR_FAIL_NULL(f);
  379. if (flags == READ_WRITE || flags == WRITE_READ) {
  380. if (prev_op == READ) {
  381. if (last_error != ERR_FILE_EOF) {
  382. fseek(f, 0, SEEK_CUR);
  383. }
  384. }
  385. prev_op = WRITE;
  386. }
  387. if (big_endian) {
  388. p_dest = BSWAP16(p_dest);
  389. }
  390. fwrite(&p_dest, 1, 2, f);
  391. }
  392. void FileAccessWindows::store_32(uint32_t p_dest) {
  393. ERR_FAIL_NULL(f);
  394. if (flags == READ_WRITE || flags == WRITE_READ) {
  395. if (prev_op == READ) {
  396. if (last_error != ERR_FILE_EOF) {
  397. fseek(f, 0, SEEK_CUR);
  398. }
  399. }
  400. prev_op = WRITE;
  401. }
  402. if (big_endian) {
  403. p_dest = BSWAP32(p_dest);
  404. }
  405. fwrite(&p_dest, 1, 4, f);
  406. }
  407. void FileAccessWindows::store_64(uint64_t p_dest) {
  408. ERR_FAIL_NULL(f);
  409. if (flags == READ_WRITE || flags == WRITE_READ) {
  410. if (prev_op == READ) {
  411. if (last_error != ERR_FILE_EOF) {
  412. fseek(f, 0, SEEK_CUR);
  413. }
  414. }
  415. prev_op = WRITE;
  416. }
  417. if (big_endian) {
  418. p_dest = BSWAP64(p_dest);
  419. }
  420. fwrite(&p_dest, 1, 8, f);
  421. }
  422. void FileAccessWindows::store_buffer(const uint8_t *p_src, uint64_t p_length) {
  423. ERR_FAIL_NULL(f);
  424. ERR_FAIL_COND(!p_src && p_length > 0);
  425. if (flags == READ_WRITE || flags == WRITE_READ) {
  426. if (prev_op == READ) {
  427. if (last_error != ERR_FILE_EOF) {
  428. fseek(f, 0, SEEK_CUR);
  429. }
  430. }
  431. prev_op = WRITE;
  432. }
  433. ERR_FAIL_COND(fwrite(p_src, 1, p_length, f) != (size_t)p_length);
  434. }
  435. bool FileAccessWindows::file_exists(const String &p_name) {
  436. if (is_path_invalid(p_name)) {
  437. return false;
  438. }
  439. String filename = fix_path(p_name);
  440. FILE *g = _wfsopen((LPCWSTR)(filename.utf16().get_data()), L"rb", _SH_DENYNO);
  441. if (g == nullptr) {
  442. return false;
  443. } else {
  444. fclose(g);
  445. return true;
  446. }
  447. }
  448. uint64_t FileAccessWindows::_get_modified_time(const String &p_file) {
  449. if (is_path_invalid(p_file)) {
  450. return 0;
  451. }
  452. String file = fix_path(p_file);
  453. if (file.ends_with("/") && file != "/") {
  454. file = file.substr(0, file.length() - 1);
  455. }
  456. HANDLE handle = CreateFileW((LPCWSTR)(file.utf16().get_data()), GENERIC_READ, FILE_SHARE_READ, nullptr, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, nullptr);
  457. if (handle != INVALID_HANDLE_VALUE) {
  458. FILETIME ft_create, ft_write;
  459. bool status = GetFileTime(handle, &ft_create, nullptr, &ft_write);
  460. CloseHandle(handle);
  461. if (status) {
  462. uint64_t ret = 0;
  463. // If write time is invalid, fallback to creation time.
  464. if (ft_write.dwHighDateTime == 0 && ft_write.dwLowDateTime == 0) {
  465. ret = ft_create.dwHighDateTime;
  466. ret <<= 32;
  467. ret |= ft_create.dwLowDateTime;
  468. } else {
  469. ret = ft_write.dwHighDateTime;
  470. ret <<= 32;
  471. ret |= ft_write.dwLowDateTime;
  472. }
  473. const uint64_t WINDOWS_TICKS_PER_SECOND = 10000000;
  474. const uint64_t TICKS_TO_UNIX_EPOCH = 116444736000000000LL;
  475. if (ret >= TICKS_TO_UNIX_EPOCH) {
  476. return (ret - TICKS_TO_UNIX_EPOCH) / WINDOWS_TICKS_PER_SECOND;
  477. }
  478. }
  479. }
  480. print_verbose("Failed to get modified time for: " + p_file);
  481. return 0;
  482. }
  483. BitField<FileAccess::UnixPermissionFlags> FileAccessWindows::_get_unix_permissions(const String &p_file) {
  484. return 0;
  485. }
  486. Error FileAccessWindows::_set_unix_permissions(const String &p_file, BitField<FileAccess::UnixPermissionFlags> p_permissions) {
  487. return ERR_UNAVAILABLE;
  488. }
  489. bool FileAccessWindows::_get_hidden_attribute(const String &p_file) {
  490. String file = fix_path(p_file);
  491. DWORD attrib = GetFileAttributesW((LPCWSTR)file.utf16().get_data());
  492. ERR_FAIL_COND_V_MSG(attrib == INVALID_FILE_ATTRIBUTES, false, "Failed to get attributes for: " + p_file);
  493. return (attrib & FILE_ATTRIBUTE_HIDDEN);
  494. }
  495. Error FileAccessWindows::_set_hidden_attribute(const String &p_file, bool p_hidden) {
  496. String file = fix_path(p_file);
  497. DWORD attrib = GetFileAttributesW((LPCWSTR)file.utf16().get_data());
  498. ERR_FAIL_COND_V_MSG(attrib == INVALID_FILE_ATTRIBUTES, FAILED, "Failed to get attributes for: " + p_file);
  499. BOOL ok;
  500. if (p_hidden) {
  501. ok = SetFileAttributesW((LPCWSTR)file.utf16().get_data(), attrib | FILE_ATTRIBUTE_HIDDEN);
  502. } else {
  503. ok = SetFileAttributesW((LPCWSTR)file.utf16().get_data(), attrib & ~FILE_ATTRIBUTE_HIDDEN);
  504. }
  505. ERR_FAIL_COND_V_MSG(!ok, FAILED, "Failed to set attributes for: " + p_file);
  506. return OK;
  507. }
  508. bool FileAccessWindows::_get_read_only_attribute(const String &p_file) {
  509. String file = fix_path(p_file);
  510. DWORD attrib = GetFileAttributesW((LPCWSTR)file.utf16().get_data());
  511. ERR_FAIL_COND_V_MSG(attrib == INVALID_FILE_ATTRIBUTES, false, "Failed to get attributes for: " + p_file);
  512. return (attrib & FILE_ATTRIBUTE_READONLY);
  513. }
  514. Error FileAccessWindows::_set_read_only_attribute(const String &p_file, bool p_ro) {
  515. String file = fix_path(p_file);
  516. DWORD attrib = GetFileAttributesW((LPCWSTR)file.utf16().get_data());
  517. ERR_FAIL_COND_V_MSG(attrib == INVALID_FILE_ATTRIBUTES, FAILED, "Failed to get attributes for: " + p_file);
  518. BOOL ok;
  519. if (p_ro) {
  520. ok = SetFileAttributesW((LPCWSTR)file.utf16().get_data(), attrib | FILE_ATTRIBUTE_READONLY);
  521. } else {
  522. ok = SetFileAttributesW((LPCWSTR)file.utf16().get_data(), attrib & ~FILE_ATTRIBUTE_READONLY);
  523. }
  524. ERR_FAIL_COND_V_MSG(!ok, FAILED, "Failed to set attributes for: " + p_file);
  525. return OK;
  526. }
  527. void FileAccessWindows::close() {
  528. _close();
  529. }
  530. FileAccessWindows::~FileAccessWindows() {
  531. _close();
  532. }
  533. HashSet<String> FileAccessWindows::invalid_files;
  534. void FileAccessWindows::initialize() {
  535. static const char *reserved_files[]{
  536. "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
  537. };
  538. int reserved_file_index = 0;
  539. while (reserved_files[reserved_file_index] != nullptr) {
  540. invalid_files.insert(reserved_files[reserved_file_index]);
  541. reserved_file_index++;
  542. }
  543. }
  544. void FileAccessWindows::finalize() {
  545. invalid_files.clear();
  546. }
  547. #endif // WINDOWS_ENABLED