dir_access_windows.cpp 11 KB

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