file_access_windows.cpp 11 KB

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