dir_access_windows.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  1. /*************************************************************************/
  2. /* dir_access_windows.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
  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. /*
  39. [03:57] <reduz> yessopie, so i don't havemak to rely on unicows
  40. [03:58] <yessopie> reduz- yeah, all of the functions fail, and then you can call GetLastError () which will return 120
  41. [03:58] <drumstick> CategoryApl, hehe, what? :)
  42. [03:59] <CategoryApl> didn't Verona lead to some trouble
  43. [03:59] <yessopie> 120 = ERROR_CALL_NOT_IMPLEMENTED
  44. [03:59] <yessopie> (you can use that constant if you include winerr.h)
  45. [03:59] <CategoryApl> well answer with winning a compo
  46. [04:02] <yessopie> if ( SetCurrentDirectoryW ( L"." ) == FALSE && GetLastError () == ERROR_CALL_NOT_IMPLEMENTED ) { use ANSI }
  47. */
  48. struct DirAccessWindowsPrivate {
  49. HANDLE h; //handle for findfirstfile
  50. WIN32_FIND_DATA f;
  51. WIN32_FIND_DATAW fu; //unicode version
  52. };
  53. // CreateFolderAsync
  54. Error DirAccessWindows::list_dir_begin() {
  55. _cisdir = false;
  56. _cishidden = false;
  57. list_dir_end();
  58. p->h = FindFirstFileExW((LPCWSTR)(String(current_dir + "\\*").utf16().get_data()), FindExInfoStandard, &p->fu, FindExSearchNameMatch, nullptr, 0);
  59. if (p->h == INVALID_HANDLE_VALUE) {
  60. return ERR_CANT_OPEN;
  61. }
  62. return OK;
  63. }
  64. String DirAccessWindows::get_next() {
  65. if (p->h == INVALID_HANDLE_VALUE) {
  66. return "";
  67. }
  68. _cisdir = (p->fu.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY);
  69. _cishidden = (p->fu.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN);
  70. String name = String::utf16((const char16_t *)(p->fu.cFileName));
  71. if (FindNextFileW(p->h, &p->fu) == 0) {
  72. FindClose(p->h);
  73. p->h = INVALID_HANDLE_VALUE;
  74. }
  75. return name;
  76. }
  77. bool DirAccessWindows::current_is_dir() const {
  78. return _cisdir;
  79. }
  80. bool DirAccessWindows::current_is_hidden() const {
  81. return _cishidden;
  82. }
  83. void DirAccessWindows::list_dir_end() {
  84. if (p->h != INVALID_HANDLE_VALUE) {
  85. FindClose(p->h);
  86. p->h = INVALID_HANDLE_VALUE;
  87. }
  88. }
  89. int DirAccessWindows::get_drive_count() {
  90. return drive_count;
  91. }
  92. String DirAccessWindows::get_drive(int p_drive) {
  93. if (p_drive < 0 || p_drive >= drive_count) {
  94. return "";
  95. }
  96. return String::chr(drives[p_drive]) + ":";
  97. }
  98. Error DirAccessWindows::change_dir(String p_dir) {
  99. GLOBAL_LOCK_FUNCTION
  100. p_dir = fix_path(p_dir);
  101. WCHAR real_current_dir_name[2048];
  102. GetCurrentDirectoryW(2048, real_current_dir_name);
  103. String prev_dir = String::utf16((const char16_t *)real_current_dir_name);
  104. SetCurrentDirectoryW((LPCWSTR)(current_dir.utf16().get_data()));
  105. bool worked = (SetCurrentDirectoryW((LPCWSTR)(p_dir.utf16().get_data())) != 0);
  106. String base = _get_root_path();
  107. if (!base.is_empty()) {
  108. GetCurrentDirectoryW(2048, real_current_dir_name);
  109. String new_dir = String::utf16((const char16_t *)real_current_dir_name).replace("\\", "/");
  110. if (!new_dir.begins_with(base)) {
  111. worked = false;
  112. }
  113. }
  114. if (worked) {
  115. GetCurrentDirectoryW(2048, real_current_dir_name);
  116. current_dir = String::utf16((const char16_t *)real_current_dir_name);
  117. current_dir = current_dir.replace("\\", "/");
  118. }
  119. SetCurrentDirectoryW((LPCWSTR)(prev_dir.utf16().get_data()));
  120. return worked ? OK : ERR_INVALID_PARAMETER;
  121. }
  122. Error DirAccessWindows::make_dir(String p_dir) {
  123. GLOBAL_LOCK_FUNCTION
  124. p_dir = fix_path(p_dir);
  125. if (p_dir.is_relative_path()) {
  126. p_dir = current_dir.plus_file(p_dir);
  127. }
  128. p_dir = p_dir.replace("/", "\\");
  129. bool success;
  130. int err;
  131. if (!p_dir.is_network_share_path()) {
  132. p_dir = "\\\\?\\" + p_dir;
  133. // Add "\\?\" to the path to extend max. path length past 248, if it's not a network share UNC path.
  134. // See https://msdn.microsoft.com/en-us/library/windows/desktop/aa363855(v=vs.85).aspx
  135. }
  136. success = CreateDirectoryW((LPCWSTR)(p_dir.utf16().get_data()), nullptr);
  137. err = GetLastError();
  138. if (success) {
  139. return OK;
  140. }
  141. if (err == ERROR_ALREADY_EXISTS || err == ERROR_ACCESS_DENIED) {
  142. return ERR_ALREADY_EXISTS;
  143. }
  144. return ERR_CANT_CREATE;
  145. }
  146. String DirAccessWindows::get_current_dir(bool p_include_drive) const {
  147. String base = _get_root_path();
  148. if (!base.is_empty()) {
  149. String bd = current_dir.replace("\\", "/").replace_first(base, "");
  150. if (bd.begins_with("/")) {
  151. return _get_root_string() + bd.substr(1, bd.length());
  152. } else {
  153. return _get_root_string() + bd;
  154. }
  155. }
  156. if (p_include_drive) {
  157. return current_dir;
  158. } else {
  159. if (_get_root_string().is_empty()) {
  160. int p = current_dir.find(":");
  161. if (p != -1) {
  162. return current_dir.substr(p + 1);
  163. }
  164. }
  165. return current_dir;
  166. }
  167. }
  168. bool DirAccessWindows::file_exists(String p_file) {
  169. GLOBAL_LOCK_FUNCTION
  170. if (!p_file.is_absolute_path()) {
  171. p_file = get_current_dir().plus_file(p_file);
  172. }
  173. p_file = fix_path(p_file);
  174. DWORD fileAttr;
  175. fileAttr = GetFileAttributesW((LPCWSTR)(p_file.utf16().get_data()));
  176. if (INVALID_FILE_ATTRIBUTES == fileAttr) {
  177. return false;
  178. }
  179. return !(fileAttr & FILE_ATTRIBUTE_DIRECTORY);
  180. }
  181. bool DirAccessWindows::dir_exists(String p_dir) {
  182. GLOBAL_LOCK_FUNCTION
  183. if (p_dir.is_relative_path()) {
  184. p_dir = get_current_dir().plus_file(p_dir);
  185. }
  186. p_dir = fix_path(p_dir);
  187. DWORD fileAttr;
  188. fileAttr = GetFileAttributesW((LPCWSTR)(p_dir.utf16().get_data()));
  189. if (INVALID_FILE_ATTRIBUTES == fileAttr) {
  190. return false;
  191. }
  192. return (fileAttr & FILE_ATTRIBUTE_DIRECTORY);
  193. }
  194. Error DirAccessWindows::rename(String p_path, String p_new_path) {
  195. if (p_path.is_relative_path()) {
  196. p_path = get_current_dir().plus_file(p_path);
  197. }
  198. p_path = fix_path(p_path);
  199. if (p_new_path.is_relative_path()) {
  200. p_new_path = get_current_dir().plus_file(p_new_path);
  201. }
  202. p_new_path = fix_path(p_new_path);
  203. // If we're only changing file name case we need to do a little juggling
  204. if (p_path.to_lower() == p_new_path.to_lower()) {
  205. if (dir_exists(p_path)) {
  206. // The path is a dir; just rename
  207. return ::_wrename((LPCWSTR)(p_path.utf16().get_data()), (LPCWSTR)(p_new_path.utf16().get_data())) == 0 ? OK : FAILED;
  208. }
  209. // The path is a file; juggle
  210. WCHAR tmpfile[MAX_PATH];
  211. if (!GetTempFileNameW((LPCWSTR)(fix_path(get_current_dir()).utf16().get_data()), nullptr, 0, tmpfile)) {
  212. return FAILED;
  213. }
  214. if (!::ReplaceFileW(tmpfile, (LPCWSTR)(p_path.utf16().get_data()), nullptr, 0, nullptr, nullptr)) {
  215. DeleteFileW(tmpfile);
  216. return FAILED;
  217. }
  218. return ::_wrename(tmpfile, (LPCWSTR)(p_new_path.utf16().get_data())) == 0 ? OK : FAILED;
  219. } else {
  220. if (file_exists(p_new_path)) {
  221. if (remove(p_new_path) != OK) {
  222. return FAILED;
  223. }
  224. }
  225. return ::_wrename((LPCWSTR)(p_path.utf16().get_data()), (LPCWSTR)(p_new_path.utf16().get_data())) == 0 ? OK : FAILED;
  226. }
  227. }
  228. Error DirAccessWindows::remove(String p_path) {
  229. if (p_path.is_relative_path()) {
  230. p_path = get_current_dir().plus_file(p_path);
  231. }
  232. p_path = fix_path(p_path);
  233. DWORD fileAttr;
  234. fileAttr = GetFileAttributesW((LPCWSTR)(p_path.utf16().get_data()));
  235. if (INVALID_FILE_ATTRIBUTES == fileAttr) {
  236. return FAILED;
  237. }
  238. if ((fileAttr & FILE_ATTRIBUTE_DIRECTORY)) {
  239. return ::_wrmdir((LPCWSTR)(p_path.utf16().get_data())) == 0 ? OK : FAILED;
  240. } else {
  241. return ::_wunlink((LPCWSTR)(p_path.utf16().get_data())) == 0 ? OK : FAILED;
  242. }
  243. }
  244. /*
  245. FileType DirAccessWindows::get_file_type(const String& p_file) const {
  246. WCHAR real_current_dir_name[2048];
  247. GetCurrentDirectoryW(2048, real_current_dir_name);
  248. String prev_dir = Strong::utf16((const char16_t *)real_current_dir_name);
  249. bool worked = SetCurrentDirectoryW((LPCWSTR)(current_dir.utf16().get_data()));
  250. DWORD attr;
  251. if (worked) {
  252. WIN32_FILE_ATTRIBUTE_DATA fileInfo;
  253. attr = GetFileAttributesExW((LPCWSTR)(p_file.utf16().get_data()), GetFileExInfoStandard, &fileInfo);
  254. }
  255. SetCurrentDirectoryW((LPCWSTR)(prev_dir.utf16().get_data()));
  256. if (!worked) {
  257. return FILE_TYPE_NONE;
  258. }
  259. return (attr & FILE_ATTRIBUTE_DIRECTORY) ? FILE_TYPE_
  260. }
  261. */
  262. uint64_t DirAccessWindows::get_space_left() {
  263. uint64_t bytes = 0;
  264. if (!GetDiskFreeSpaceEx(nullptr, (PULARGE_INTEGER)&bytes, nullptr, nullptr)) {
  265. return 0;
  266. }
  267. //this is either 0 or a value in bytes.
  268. return bytes;
  269. }
  270. String DirAccessWindows::get_filesystem_type() const {
  271. String path = fix_path(const_cast<DirAccessWindows *>(this)->get_current_dir());
  272. int unit_end = path.find(":");
  273. ERR_FAIL_COND_V(unit_end == -1, String());
  274. String unit = path.substr(0, unit_end + 1) + "\\";
  275. if (path.is_network_share_path()) {
  276. return "Network Share";
  277. }
  278. WCHAR szVolumeName[100];
  279. WCHAR szFileSystemName[10];
  280. DWORD dwSerialNumber = 0;
  281. DWORD dwMaxFileNameLength = 0;
  282. DWORD dwFileSystemFlags = 0;
  283. if (::GetVolumeInformationW((LPCWSTR)(unit.utf16().get_data()),
  284. szVolumeName,
  285. sizeof(szVolumeName),
  286. &dwSerialNumber,
  287. &dwMaxFileNameLength,
  288. &dwFileSystemFlags,
  289. szFileSystemName,
  290. sizeof(szFileSystemName)) == TRUE) {
  291. return String::utf16((const char16_t *)szFileSystemName);
  292. }
  293. ERR_FAIL_V("");
  294. }
  295. DirAccessWindows::DirAccessWindows() {
  296. p = memnew(DirAccessWindowsPrivate);
  297. p->h = INVALID_HANDLE_VALUE;
  298. current_dir = ".";
  299. #ifdef UWP_ENABLED
  300. Windows::Storage::StorageFolder ^ install_folder = Windows::ApplicationModel::Package::Current->InstalledLocation;
  301. change_dir(install_folder->Path->Data());
  302. #else
  303. DWORD mask = GetLogicalDrives();
  304. for (int i = 0; i < MAX_DRIVES; i++) {
  305. if (mask & (1 << i)) { //DRIVE EXISTS
  306. drives[drive_count] = 'A' + i;
  307. drive_count++;
  308. }
  309. }
  310. change_dir(".");
  311. #endif
  312. }
  313. DirAccessWindows::~DirAccessWindows() {
  314. list_dir_end();
  315. memdelete(p);
  316. }
  317. #endif //windows DirAccess support