dir_access_windows.cpp 11 KB

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