dir_access_windows.cpp 14 KB

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