file_access_windows.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  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_NULL(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.get_file().get_basename().to_lower();
  55. return invalid_files.has(fname);
  56. }
  57. String FileAccessWindows::fix_path(const String &p_path) const {
  58. String r_path = FileAccess::fix_path(p_path);
  59. if (r_path.is_absolute_path() && !r_path.is_network_share_path() && r_path.length() > MAX_PATH) {
  60. r_path = "\\\\?\\" + r_path.replace("/", "\\");
  61. }
  62. return r_path;
  63. }
  64. Error FileAccessWindows::open_internal(const String &p_path, int p_mode_flags) {
  65. if (is_path_invalid(p_path)) {
  66. #ifdef DEBUG_ENABLED
  67. if (p_mode_flags != READ) {
  68. WARN_PRINT("The path :" + p_path + " is a reserved Windows system pipe, so it can't be used for creating files.");
  69. }
  70. #endif
  71. return ERR_INVALID_PARAMETER;
  72. }
  73. _close();
  74. path_src = p_path;
  75. path = fix_path(p_path);
  76. const WCHAR *mode_string;
  77. if (p_mode_flags == READ) {
  78. mode_string = L"rb";
  79. } else if (p_mode_flags == WRITE) {
  80. mode_string = L"wb";
  81. } else if (p_mode_flags == READ_WRITE) {
  82. mode_string = L"rb+";
  83. } else if (p_mode_flags == WRITE_READ) {
  84. mode_string = L"wb+";
  85. } else {
  86. return ERR_INVALID_PARAMETER;
  87. }
  88. /* Pretty much every implementation that uses fopen as primary
  89. backend supports utf8 encoding. */
  90. struct _stat st;
  91. if (_wstat((LPCWSTR)(path.utf16().get_data()), &st) == 0) {
  92. if (!S_ISREG(st.st_mode)) {
  93. return ERR_FILE_CANT_OPEN;
  94. }
  95. }
  96. #ifdef TOOLS_ENABLED
  97. // Windows is case insensitive, but all other platforms are sensitive to it
  98. // To ease cross-platform development, we issue a warning if users try to access
  99. // a file using the wrong case (which *works* on Windows, but won't on other
  100. // platforms).
  101. if (p_mode_flags == READ) {
  102. WIN32_FIND_DATAW d;
  103. HANDLE fnd = FindFirstFileW((LPCWSTR)(path.utf16().get_data()), &d);
  104. if (fnd != INVALID_HANDLE_VALUE) {
  105. String fname = String::utf16((const char16_t *)(d.cFileName));
  106. if (!fname.is_empty()) {
  107. String base_file = path.get_file();
  108. if (base_file != fname && base_file.findn(fname) == 0) {
  109. 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.");
  110. }
  111. }
  112. FindClose(fnd);
  113. }
  114. }
  115. #endif
  116. if (is_backup_save_enabled() && p_mode_flags == WRITE) {
  117. save_path = path;
  118. // Create a temporary file in the same directory as the target file.
  119. WCHAR tmpFileName[MAX_PATH];
  120. if (GetTempFileNameW((LPCWSTR)(path.get_base_dir().utf16().get_data()), (LPCWSTR)(path.get_file().utf16().get_data()), 0, tmpFileName) == 0) {
  121. last_error = ERR_FILE_CANT_OPEN;
  122. return last_error;
  123. }
  124. path = tmpFileName;
  125. }
  126. f = _wfsopen((LPCWSTR)(path.utf16().get_data()), mode_string, is_backup_save_enabled() ? _SH_SECURE : _SH_DENYNO);
  127. if (f == nullptr) {
  128. switch (errno) {
  129. case ENOENT: {
  130. last_error = ERR_FILE_NOT_FOUND;
  131. } break;
  132. default: {
  133. last_error = ERR_FILE_CANT_OPEN;
  134. } break;
  135. }
  136. return last_error;
  137. } else {
  138. last_error = OK;
  139. flags = p_mode_flags;
  140. return OK;
  141. }
  142. }
  143. void FileAccessWindows::_close() {
  144. if (!f) {
  145. return;
  146. }
  147. fclose(f);
  148. f = nullptr;
  149. if (!save_path.is_empty()) {
  150. // This workaround of trying multiple times is added to deal with paranoid Windows
  151. // antiviruses that love reading just written files even if they are not executable, thus
  152. // locking the file and preventing renaming from happening.
  153. bool rename_error = true;
  154. const Char16String &path_utf16 = path.utf16();
  155. const Char16String &save_path_utf16 = save_path.utf16();
  156. for (int i = 0; i < 1000; i++) {
  157. if (ReplaceFileW((LPCWSTR)(save_path_utf16.get_data()), (LPCWSTR)(path_utf16.get_data()), nullptr, REPLACEFILE_IGNORE_MERGE_ERRORS | REPLACEFILE_IGNORE_ACL_ERRORS, nullptr, nullptr)) {
  158. rename_error = false;
  159. } else {
  160. // Either the target exists and is locked (temporarily, hopefully)
  161. // or it doesn't exist; let's assume the latter before re-trying.
  162. rename_error = _wrename((LPCWSTR)(path_utf16.get_data()), (LPCWSTR)(save_path_utf16.get_data())) != 0;
  163. }
  164. if (!rename_error) {
  165. break;
  166. }
  167. OS::get_singleton()->delay_usec(1000);
  168. }
  169. if (rename_error) {
  170. if (close_fail_notify) {
  171. close_fail_notify(save_path);
  172. }
  173. }
  174. save_path = "";
  175. 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.");
  176. }
  177. }
  178. String FileAccessWindows::get_path() const {
  179. return path_src;
  180. }
  181. String FileAccessWindows::get_path_absolute() const {
  182. return path;
  183. }
  184. bool FileAccessWindows::is_open() const {
  185. return (f != nullptr);
  186. }
  187. void FileAccessWindows::seek(uint64_t p_position) {
  188. ERR_FAIL_NULL(f);
  189. last_error = OK;
  190. if (_fseeki64(f, p_position, SEEK_SET)) {
  191. check_errors();
  192. }
  193. prev_op = 0;
  194. }
  195. void FileAccessWindows::seek_end(int64_t p_position) {
  196. ERR_FAIL_NULL(f);
  197. if (_fseeki64(f, p_position, SEEK_END)) {
  198. check_errors();
  199. }
  200. prev_op = 0;
  201. }
  202. uint64_t FileAccessWindows::get_position() const {
  203. int64_t aux_position = _ftelli64(f);
  204. if (aux_position < 0) {
  205. check_errors();
  206. }
  207. return aux_position;
  208. }
  209. uint64_t FileAccessWindows::get_length() const {
  210. ERR_FAIL_NULL_V(f, 0);
  211. uint64_t pos = get_position();
  212. _fseeki64(f, 0, SEEK_END);
  213. uint64_t size = get_position();
  214. _fseeki64(f, pos, SEEK_SET);
  215. return size;
  216. }
  217. bool FileAccessWindows::eof_reached() const {
  218. check_errors();
  219. return last_error == ERR_FILE_EOF;
  220. }
  221. uint8_t FileAccessWindows::get_8() const {
  222. ERR_FAIL_NULL_V(f, 0);
  223. if (flags == READ_WRITE || flags == WRITE_READ) {
  224. if (prev_op == WRITE) {
  225. fflush(f);
  226. }
  227. prev_op = READ;
  228. }
  229. uint8_t b;
  230. if (fread(&b, 1, 1, f) == 0) {
  231. check_errors();
  232. b = '\0';
  233. }
  234. return b;
  235. }
  236. uint64_t FileAccessWindows::get_buffer(uint8_t *p_dst, uint64_t p_length) const {
  237. ERR_FAIL_COND_V(!p_dst && p_length > 0, -1);
  238. ERR_FAIL_NULL_V(f, -1);
  239. if (flags == READ_WRITE || flags == WRITE_READ) {
  240. if (prev_op == WRITE) {
  241. fflush(f);
  242. }
  243. prev_op = READ;
  244. }
  245. uint64_t read = fread(p_dst, 1, p_length, f);
  246. check_errors();
  247. return read;
  248. }
  249. Error FileAccessWindows::get_error() const {
  250. return last_error;
  251. }
  252. void FileAccessWindows::flush() {
  253. ERR_FAIL_NULL(f);
  254. fflush(f);
  255. if (prev_op == WRITE) {
  256. prev_op = 0;
  257. }
  258. }
  259. void FileAccessWindows::store_8(uint8_t p_dest) {
  260. ERR_FAIL_NULL(f);
  261. if (flags == READ_WRITE || flags == WRITE_READ) {
  262. if (prev_op == READ) {
  263. if (last_error != ERR_FILE_EOF) {
  264. fseek(f, 0, SEEK_CUR);
  265. }
  266. }
  267. prev_op = WRITE;
  268. }
  269. fwrite(&p_dest, 1, 1, f);
  270. }
  271. void FileAccessWindows::store_buffer(const uint8_t *p_src, uint64_t p_length) {
  272. ERR_FAIL_NULL(f);
  273. ERR_FAIL_COND(!p_src && p_length > 0);
  274. if (flags == READ_WRITE || flags == WRITE_READ) {
  275. if (prev_op == READ) {
  276. if (last_error != ERR_FILE_EOF) {
  277. fseek(f, 0, SEEK_CUR);
  278. }
  279. }
  280. prev_op = WRITE;
  281. }
  282. ERR_FAIL_COND(fwrite(p_src, 1, p_length, f) != (size_t)p_length);
  283. }
  284. bool FileAccessWindows::file_exists(const String &p_name) {
  285. if (is_path_invalid(p_name)) {
  286. return false;
  287. }
  288. String filename = fix_path(p_name);
  289. FILE *g = _wfsopen((LPCWSTR)(filename.utf16().get_data()), L"rb", _SH_DENYNO);
  290. if (g == nullptr) {
  291. return false;
  292. } else {
  293. fclose(g);
  294. return true;
  295. }
  296. }
  297. uint64_t FileAccessWindows::_get_modified_time(const String &p_file) {
  298. if (is_path_invalid(p_file)) {
  299. return 0;
  300. }
  301. String file = fix_path(p_file);
  302. if (file.ends_with("/") && file != "/") {
  303. file = file.substr(0, file.length() - 1);
  304. }
  305. struct _stat st;
  306. int rv = _wstat((LPCWSTR)(file.utf16().get_data()), &st);
  307. if (rv == 0) {
  308. return st.st_mtime;
  309. } else {
  310. print_verbose("Failed to get modified time for: " + p_file + "");
  311. return 0;
  312. }
  313. }
  314. BitField<FileAccess::UnixPermissionFlags> FileAccessWindows::_get_unix_permissions(const String &p_file) {
  315. return 0;
  316. }
  317. Error FileAccessWindows::_set_unix_permissions(const String &p_file, BitField<FileAccess::UnixPermissionFlags> p_permissions) {
  318. return ERR_UNAVAILABLE;
  319. }
  320. bool FileAccessWindows::_get_hidden_attribute(const String &p_file) {
  321. String file = fix_path(p_file);
  322. DWORD attrib = GetFileAttributesW((LPCWSTR)file.utf16().get_data());
  323. ERR_FAIL_COND_V_MSG(attrib == INVALID_FILE_ATTRIBUTES, false, "Failed to get attributes for: " + p_file);
  324. return (attrib & FILE_ATTRIBUTE_HIDDEN);
  325. }
  326. Error FileAccessWindows::_set_hidden_attribute(const String &p_file, bool p_hidden) {
  327. String file = fix_path(p_file);
  328. DWORD attrib = GetFileAttributesW((LPCWSTR)file.utf16().get_data());
  329. ERR_FAIL_COND_V_MSG(attrib == INVALID_FILE_ATTRIBUTES, FAILED, "Failed to get attributes for: " + p_file);
  330. BOOL ok;
  331. if (p_hidden) {
  332. ok = SetFileAttributesW((LPCWSTR)file.utf16().get_data(), attrib | FILE_ATTRIBUTE_HIDDEN);
  333. } else {
  334. ok = SetFileAttributesW((LPCWSTR)file.utf16().get_data(), attrib & ~FILE_ATTRIBUTE_HIDDEN);
  335. }
  336. ERR_FAIL_COND_V_MSG(!ok, FAILED, "Failed to set attributes for: " + p_file);
  337. return OK;
  338. }
  339. bool FileAccessWindows::_get_read_only_attribute(const String &p_file) {
  340. String file = fix_path(p_file);
  341. DWORD attrib = GetFileAttributesW((LPCWSTR)file.utf16().get_data());
  342. ERR_FAIL_COND_V_MSG(attrib == INVALID_FILE_ATTRIBUTES, false, "Failed to get attributes for: " + p_file);
  343. return (attrib & FILE_ATTRIBUTE_READONLY);
  344. }
  345. Error FileAccessWindows::_set_read_only_attribute(const String &p_file, bool p_ro) {
  346. String file = fix_path(p_file);
  347. DWORD attrib = GetFileAttributesW((LPCWSTR)file.utf16().get_data());
  348. ERR_FAIL_COND_V_MSG(attrib == INVALID_FILE_ATTRIBUTES, FAILED, "Failed to get attributes for: " + p_file);
  349. BOOL ok;
  350. if (p_ro) {
  351. ok = SetFileAttributesW((LPCWSTR)file.utf16().get_data(), attrib | FILE_ATTRIBUTE_READONLY);
  352. } else {
  353. ok = SetFileAttributesW((LPCWSTR)file.utf16().get_data(), attrib & ~FILE_ATTRIBUTE_READONLY);
  354. }
  355. ERR_FAIL_COND_V_MSG(!ok, FAILED, "Failed to set attributes for: " + p_file);
  356. return OK;
  357. }
  358. void FileAccessWindows::close() {
  359. _close();
  360. }
  361. FileAccessWindows::~FileAccessWindows() {
  362. _close();
  363. }
  364. HashSet<String> FileAccessWindows::invalid_files;
  365. void FileAccessWindows::initialize() {
  366. static const char *reserved_files[]{
  367. "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
  368. };
  369. int reserved_file_index = 0;
  370. while (reserved_files[reserved_file_index] != nullptr) {
  371. invalid_files.insert(reserved_files[reserved_file_index]);
  372. reserved_file_index++;
  373. }
  374. }
  375. void FileAccessWindows::finalize() {
  376. invalid_files.clear();
  377. }
  378. #endif // WINDOWS_ENABLED