dir_access_windows.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  1. /**************************************************************************/
  2. /* dir_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. #if defined(WINDOWS_ENABLED)
  31. #include "dir_access_windows.h"
  32. #include "file_access_windows.h"
  33. #include "core/config/project_settings.h"
  34. #include "core/os/memory.h"
  35. #include "core/os/os.h"
  36. #include "core/string/print_string.h"
  37. #include <stdio.h>
  38. #include <wchar.h>
  39. #define WIN32_LEAN_AND_MEAN
  40. #include <windows.h>
  41. typedef struct _NT_IO_STATUS_BLOCK {
  42. union {
  43. LONG Status;
  44. PVOID Pointer;
  45. } DUMMY;
  46. ULONG_PTR Information;
  47. } NT_IO_STATUS_BLOCK;
  48. typedef struct _NT_FILE_CASE_SENSITIVE_INFO {
  49. ULONG Flags;
  50. } NT_FILE_CASE_SENSITIVE_INFO;
  51. typedef enum _NT_FILE_INFORMATION_CLASS {
  52. FileCaseSensitiveInformation = 71,
  53. } NT_FILE_INFORMATION_CLASS;
  54. #define NT_FILE_CS_FLAG_CASE_SENSITIVE_DIR 0x00000001
  55. extern "C" NTSYSAPI LONG NTAPI NtQueryInformationFile(HANDLE FileHandle, NT_IO_STATUS_BLOCK *IoStatusBlock, PVOID FileInformation, ULONG Length, NT_FILE_INFORMATION_CLASS FileInformationClass);
  56. struct DirAccessWindowsPrivate {
  57. HANDLE h; // handle for FindFirstFile.
  58. WIN32_FIND_DATA f;
  59. WIN32_FIND_DATAW fu; // Unicode version.
  60. };
  61. String DirAccessWindows::fix_path(const String &p_path) const {
  62. String r_path = DirAccess::fix_path(p_path.trim_prefix(R"(\\?\)").replace("\\", "/"));
  63. if (r_path.is_relative_path()) {
  64. r_path = current_dir.trim_prefix(R"(\\?\)").replace("\\", "/").path_join(r_path);
  65. } else if (r_path == ".") {
  66. r_path = current_dir.trim_prefix(R"(\\?\)").replace("\\", "/");
  67. }
  68. r_path = r_path.simplify_path();
  69. r_path = r_path.replace("/", "\\");
  70. if (!r_path.is_network_share_path() && !r_path.begins_with(R"(\\?\)")) {
  71. r_path = R"(\\?\)" + r_path;
  72. }
  73. return r_path;
  74. }
  75. // CreateFolderAsync
  76. Error DirAccessWindows::list_dir_begin() {
  77. _cisdir = false;
  78. _cishidden = false;
  79. list_dir_end();
  80. p->h = FindFirstFileExW((LPCWSTR)(String(current_dir + "\\*").utf16().get_data()), FindExInfoStandard, &p->fu, FindExSearchNameMatch, nullptr, 0);
  81. if (p->h == INVALID_HANDLE_VALUE) {
  82. return ERR_CANT_OPEN;
  83. }
  84. return OK;
  85. }
  86. String DirAccessWindows::get_next() {
  87. if (p->h == INVALID_HANDLE_VALUE) {
  88. return "";
  89. }
  90. _cisdir = (p->fu.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY);
  91. _cishidden = (p->fu.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN);
  92. String name = String::utf16((const char16_t *)(p->fu.cFileName));
  93. if (FindNextFileW(p->h, &p->fu) == 0) {
  94. FindClose(p->h);
  95. p->h = INVALID_HANDLE_VALUE;
  96. }
  97. return name;
  98. }
  99. bool DirAccessWindows::current_is_dir() const {
  100. return _cisdir;
  101. }
  102. bool DirAccessWindows::current_is_hidden() const {
  103. return _cishidden;
  104. }
  105. void DirAccessWindows::list_dir_end() {
  106. if (p->h != INVALID_HANDLE_VALUE) {
  107. FindClose(p->h);
  108. p->h = INVALID_HANDLE_VALUE;
  109. }
  110. }
  111. int DirAccessWindows::get_drive_count() {
  112. return drive_count;
  113. }
  114. String DirAccessWindows::get_drive(int p_drive) {
  115. if (p_drive < 0 || p_drive >= drive_count) {
  116. return "";
  117. }
  118. return String::chr(drives[p_drive]) + ":";
  119. }
  120. Error DirAccessWindows::change_dir(String p_dir) {
  121. GLOBAL_LOCK_FUNCTION
  122. String dir = fix_path(p_dir);
  123. Char16String real_current_dir_name;
  124. size_t str_len = GetCurrentDirectoryW(0, nullptr);
  125. real_current_dir_name.resize(str_len + 1);
  126. GetCurrentDirectoryW(real_current_dir_name.size(), (LPWSTR)real_current_dir_name.ptrw());
  127. String prev_dir = String::utf16((const char16_t *)real_current_dir_name.get_data());
  128. SetCurrentDirectoryW((LPCWSTR)(current_dir.utf16().get_data()));
  129. bool worked = (SetCurrentDirectoryW((LPCWSTR)(dir.utf16().get_data())) != 0);
  130. String base = _get_root_path();
  131. if (!base.is_empty()) {
  132. str_len = GetCurrentDirectoryW(0, nullptr);
  133. real_current_dir_name.resize(str_len + 1);
  134. GetCurrentDirectoryW(real_current_dir_name.size(), (LPWSTR)real_current_dir_name.ptrw());
  135. String new_dir = String::utf16((const char16_t *)real_current_dir_name.get_data()).trim_prefix(R"(\\?\)").replace("\\", "/");
  136. if (!new_dir.begins_with(base)) {
  137. worked = false;
  138. }
  139. }
  140. if (worked) {
  141. str_len = GetCurrentDirectoryW(0, nullptr);
  142. real_current_dir_name.resize(str_len + 1);
  143. GetCurrentDirectoryW(real_current_dir_name.size(), (LPWSTR)real_current_dir_name.ptrw());
  144. current_dir = String::utf16((const char16_t *)real_current_dir_name.get_data());
  145. }
  146. SetCurrentDirectoryW((LPCWSTR)(prev_dir.utf16().get_data()));
  147. return worked ? OK : ERR_INVALID_PARAMETER;
  148. }
  149. Error DirAccessWindows::make_dir(String p_dir) {
  150. GLOBAL_LOCK_FUNCTION
  151. if (FileAccessWindows::is_path_invalid(p_dir)) {
  152. #ifdef DEBUG_ENABLED
  153. WARN_PRINT("The path :" + p_dir + " is a reserved Windows system pipe, so it can't be used for creating directories.");
  154. #endif
  155. return ERR_INVALID_PARAMETER;
  156. }
  157. String dir = fix_path(p_dir);
  158. bool success;
  159. int err;
  160. success = CreateDirectoryW((LPCWSTR)(dir.utf16().get_data()), nullptr);
  161. err = GetLastError();
  162. if (success) {
  163. return OK;
  164. }
  165. if (err == ERROR_ALREADY_EXISTS || err == ERROR_ACCESS_DENIED) {
  166. return ERR_ALREADY_EXISTS;
  167. }
  168. return ERR_CANT_CREATE;
  169. }
  170. String DirAccessWindows::get_current_dir(bool p_include_drive) const {
  171. String cdir = current_dir.trim_prefix(R"(\\?\)").replace("\\", "/");
  172. String base = _get_root_path();
  173. if (!base.is_empty()) {
  174. String bd = cdir.replace_first(base, "");
  175. if (bd.begins_with("/")) {
  176. return _get_root_string() + bd.substr(1, bd.length());
  177. } else {
  178. return _get_root_string() + bd;
  179. }
  180. }
  181. if (p_include_drive) {
  182. return cdir;
  183. } else {
  184. if (_get_root_string().is_empty()) {
  185. int pos = cdir.find(":");
  186. if (pos != -1) {
  187. return cdir.substr(pos + 1);
  188. }
  189. }
  190. return cdir;
  191. }
  192. }
  193. bool DirAccessWindows::file_exists(String p_file) {
  194. GLOBAL_LOCK_FUNCTION
  195. String file = fix_path(p_file);
  196. DWORD fileAttr;
  197. fileAttr = GetFileAttributesW((LPCWSTR)(file.utf16().get_data()));
  198. if (INVALID_FILE_ATTRIBUTES == fileAttr) {
  199. return false;
  200. }
  201. return !(fileAttr & FILE_ATTRIBUTE_DIRECTORY);
  202. }
  203. bool DirAccessWindows::dir_exists(String p_dir) {
  204. GLOBAL_LOCK_FUNCTION
  205. String dir = fix_path(p_dir);
  206. DWORD fileAttr;
  207. fileAttr = GetFileAttributesW((LPCWSTR)(dir.utf16().get_data()));
  208. if (INVALID_FILE_ATTRIBUTES == fileAttr) {
  209. return false;
  210. }
  211. return (fileAttr & FILE_ATTRIBUTE_DIRECTORY);
  212. }
  213. Error DirAccessWindows::rename(String p_path, String p_new_path) {
  214. String path = fix_path(p_path);
  215. String new_path = fix_path(p_new_path);
  216. // If we're only changing file name case we need to do a little juggling
  217. if (path.to_lower() == new_path.to_lower()) {
  218. if (dir_exists(path)) {
  219. // The path is a dir; just rename
  220. return MoveFileW((LPCWSTR)(path.utf16().get_data()), (LPCWSTR)(new_path.utf16().get_data())) != 0 ? OK : FAILED;
  221. }
  222. // The path is a file; juggle
  223. // Note: do not use GetTempFileNameW, it's not long path aware!
  224. Char16String tmpfile_utf16;
  225. uint64_t id = OS::get_singleton()->get_ticks_usec();
  226. while (true) {
  227. tmpfile_utf16 = (path + itos(id++) + ".tmp").utf16();
  228. HANDLE handle = CreateFileW((LPCWSTR)tmpfile_utf16.get_data(), GENERIC_WRITE, 0, NULL, CREATE_NEW, FILE_ATTRIBUTE_NORMAL, 0);
  229. if (handle != INVALID_HANDLE_VALUE) {
  230. CloseHandle(handle);
  231. break;
  232. }
  233. if (GetLastError() != ERROR_FILE_EXISTS && GetLastError() != ERROR_SHARING_VIOLATION) {
  234. return FAILED;
  235. }
  236. }
  237. if (!::ReplaceFileW((LPCWSTR)tmpfile_utf16.get_data(), (LPCWSTR)(path.utf16().get_data()), nullptr, 0, nullptr, nullptr)) {
  238. DeleteFileW((LPCWSTR)tmpfile_utf16.get_data());
  239. return FAILED;
  240. }
  241. return MoveFileW((LPCWSTR)tmpfile_utf16.get_data(), (LPCWSTR)(new_path.utf16().get_data())) != 0 ? OK : FAILED;
  242. } else {
  243. if (file_exists(new_path)) {
  244. if (remove(new_path) != OK) {
  245. return FAILED;
  246. }
  247. }
  248. return MoveFileW((LPCWSTR)(path.utf16().get_data()), (LPCWSTR)(new_path.utf16().get_data())) != 0 ? OK : FAILED;
  249. }
  250. }
  251. Error DirAccessWindows::remove(String p_path) {
  252. String path = fix_path(p_path);
  253. const Char16String &path_utf16 = path.utf16();
  254. DWORD fileAttr;
  255. fileAttr = GetFileAttributesW((LPCWSTR)(path_utf16.get_data()));
  256. if (INVALID_FILE_ATTRIBUTES == fileAttr) {
  257. return FAILED;
  258. }
  259. if ((fileAttr & FILE_ATTRIBUTE_DIRECTORY)) {
  260. return RemoveDirectoryW((LPCWSTR)(path_utf16.get_data())) != 0 ? OK : FAILED;
  261. } else {
  262. return DeleteFileW((LPCWSTR)(path_utf16.get_data())) != 0 ? OK : FAILED;
  263. }
  264. }
  265. uint64_t DirAccessWindows::get_space_left() {
  266. uint64_t bytes = 0;
  267. if (!GetDiskFreeSpaceEx(nullptr, (PULARGE_INTEGER)&bytes, nullptr, nullptr)) {
  268. return 0;
  269. }
  270. // This is either 0 or a value in bytes.
  271. return bytes;
  272. }
  273. String DirAccessWindows::get_filesystem_type() const {
  274. String path = current_dir.trim_prefix(R"(\\?\)");
  275. if (path.is_network_share_path()) {
  276. return "Network Share";
  277. }
  278. int unit_end = path.find(":");
  279. ERR_FAIL_COND_V(unit_end == -1, String());
  280. String unit = path.substr(0, unit_end + 1) + "\\";
  281. WCHAR szVolumeName[100];
  282. WCHAR szFileSystemName[10];
  283. DWORD dwSerialNumber = 0;
  284. DWORD dwMaxFileNameLength = 0;
  285. DWORD dwFileSystemFlags = 0;
  286. if (::GetVolumeInformationW((LPCWSTR)(unit.utf16().get_data()),
  287. szVolumeName,
  288. sizeof(szVolumeName),
  289. &dwSerialNumber,
  290. &dwMaxFileNameLength,
  291. &dwFileSystemFlags,
  292. szFileSystemName,
  293. sizeof(szFileSystemName)) == TRUE) {
  294. return String::utf16((const char16_t *)szFileSystemName);
  295. }
  296. ERR_FAIL_V("");
  297. }
  298. bool DirAccessWindows::is_case_sensitive(const String &p_path) const {
  299. String f = fix_path(p_path);
  300. HANDLE h_file = ::CreateFileW((LPCWSTR)(f.utf16().get_data()), 0,
  301. FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
  302. nullptr, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, nullptr);
  303. if (h_file == INVALID_HANDLE_VALUE) {
  304. return false;
  305. }
  306. NT_IO_STATUS_BLOCK io_status_block;
  307. NT_FILE_CASE_SENSITIVE_INFO file_info;
  308. LONG out = NtQueryInformationFile(h_file, &io_status_block, &file_info, sizeof(NT_FILE_CASE_SENSITIVE_INFO), FileCaseSensitiveInformation);
  309. ::CloseHandle(h_file);
  310. if (out >= 0) {
  311. return file_info.Flags & NT_FILE_CS_FLAG_CASE_SENSITIVE_DIR;
  312. } else {
  313. return false;
  314. }
  315. }
  316. bool DirAccessWindows::is_link(String p_file) {
  317. String f = fix_path(p_file);
  318. DWORD attr = GetFileAttributesW((LPCWSTR)(f.utf16().get_data()));
  319. if (attr == INVALID_FILE_ATTRIBUTES) {
  320. return false;
  321. }
  322. return (attr & FILE_ATTRIBUTE_REPARSE_POINT);
  323. }
  324. String DirAccessWindows::read_link(String p_file) {
  325. String f = fix_path(p_file);
  326. HANDLE hfile = CreateFileW((LPCWSTR)(f.utf16().get_data()), GENERIC_READ, FILE_SHARE_READ, nullptr, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, nullptr);
  327. if (hfile == INVALID_HANDLE_VALUE) {
  328. return f;
  329. }
  330. DWORD ret = GetFinalPathNameByHandleW(hfile, nullptr, 0, VOLUME_NAME_DOS | FILE_NAME_NORMALIZED);
  331. if (ret == 0) {
  332. return f;
  333. }
  334. Char16String cs;
  335. cs.resize(ret + 1);
  336. GetFinalPathNameByHandleW(hfile, (LPWSTR)cs.ptrw(), ret, VOLUME_NAME_DOS | FILE_NAME_NORMALIZED);
  337. CloseHandle(hfile);
  338. return String::utf16((const char16_t *)cs.ptr(), ret).trim_prefix(R"(\\?\)").replace("\\", "/");
  339. }
  340. Error DirAccessWindows::create_link(String p_source, String p_target) {
  341. String source = fix_path(p_source);
  342. String target = fix_path(p_target);
  343. DWORD file_attr = GetFileAttributesW((LPCWSTR)(source.utf16().get_data()));
  344. bool is_dir = (file_attr & FILE_ATTRIBUTE_DIRECTORY);
  345. DWORD flags = ((is_dir) ? SYMBOLIC_LINK_FLAG_DIRECTORY : 0) | SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE;
  346. if (CreateSymbolicLinkW((LPCWSTR)target.utf16().get_data(), (LPCWSTR)source.utf16().get_data(), flags) != 0) {
  347. return OK;
  348. } else {
  349. return FAILED;
  350. }
  351. }
  352. DirAccessWindows::DirAccessWindows() {
  353. p = memnew(DirAccessWindowsPrivate);
  354. p->h = INVALID_HANDLE_VALUE;
  355. Char16String real_current_dir_name;
  356. size_t str_len = GetCurrentDirectoryW(0, nullptr);
  357. real_current_dir_name.resize(str_len + 1);
  358. GetCurrentDirectoryW(real_current_dir_name.size(), (LPWSTR)real_current_dir_name.ptrw());
  359. current_dir = String::utf16((const char16_t *)real_current_dir_name.get_data());
  360. DWORD mask = GetLogicalDrives();
  361. for (int i = 0; i < MAX_DRIVES; i++) {
  362. if (mask & (1 << i)) { //DRIVE EXISTS
  363. drives[drive_count] = 'A' + i;
  364. drive_count++;
  365. }
  366. }
  367. change_dir(".");
  368. }
  369. DirAccessWindows::~DirAccessWindows() {
  370. list_dir_end();
  371. memdelete(p);
  372. }
  373. #endif // WINDOWS_ENABLED