dir_access_windows.cpp 10 KB

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