dir_access_windows.cpp 13 KB

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