dir_access_windows.cpp 11 KB

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