dir_access_windows.cpp 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. /*************************************************************************/
  2. /* dir_access_windows.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
  9. /* */
  10. /* Permission is hereby granted, free of charge, to any person obtaining */
  11. /* a copy of this software and associated documentation files (the */
  12. /* "Software"), to deal in the Software without restriction, including */
  13. /* without limitation the rights to use, copy, modify, merge, publish, */
  14. /* distribute, sublicense, and/or sell copies of the Software, and to */
  15. /* permit persons to whom the Software is furnished to do so, subject to */
  16. /* the following conditions: */
  17. /* */
  18. /* The above copyright notice and this permission notice shall be */
  19. /* included in all copies or substantial portions of the Software. */
  20. /* */
  21. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  22. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  23. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  24. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  25. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  26. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  27. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  28. /*************************************************************************/
  29. #if defined(WINDOWS_ENABLED)
  30. #include "dir_access_windows.h"
  31. #include "os/memory.h"
  32. #include "print_string.h"
  33. #include <stdio.h>
  34. #include <wchar.h>
  35. #include <windows.h>
  36. /*
  37. [03:57] <reduz> yessopie, so i don't havemak to rely on unicows
  38. [03:58] <yessopie> reduz- yeah, all of the functions fail, and then you can call GetLastError () which will return 120
  39. [03:58] <drumstick> CategoryApl, hehe, what? :)
  40. [03:59] <CategoryApl> didn't Verona lead to some trouble
  41. [03:59] <yessopie> 120 = ERROR_CALL_NOT_IMPLEMENTED
  42. [03:59] <yessopie> (you can use that constant if you include winerr.h)
  43. [03:59] <CategoryApl> well answer with winning a compo
  44. [04:02] <yessopie> if ( SetCurrentDirectoryW ( L"." ) == FALSE && GetLastError () == ERROR_CALL_NOT_IMPLEMENTED ) { use ANSI }
  45. */
  46. struct DirAccessWindowsPrivate {
  47. HANDLE h; //handle for findfirstfile
  48. WIN32_FIND_DATA f;
  49. WIN32_FIND_DATAW fu; //unicode version
  50. };
  51. // CreateFolderAsync
  52. Error DirAccessWindows::list_dir_begin() {
  53. _cisdir = false;
  54. _cishidden = false;
  55. list_dir_end();
  56. p->h = FindFirstFileExW((current_dir + "\\*").c_str(), FindExInfoStandard, &p->fu, FindExSearchNameMatch, NULL, 0);
  57. return (p->h == INVALID_HANDLE_VALUE) ? ERR_CANT_OPEN : OK;
  58. }
  59. String DirAccessWindows::get_next() {
  60. if (p->h == INVALID_HANDLE_VALUE)
  61. return "";
  62. _cisdir = (p->fu.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY);
  63. _cishidden = (p->fu.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN);
  64. String name = p->fu.cFileName;
  65. if (FindNextFileW(p->h, &p->fu) == 0) {
  66. FindClose(p->h);
  67. p->h = INVALID_HANDLE_VALUE;
  68. }
  69. return name;
  70. }
  71. bool DirAccessWindows::current_is_dir() const {
  72. return _cisdir;
  73. }
  74. bool DirAccessWindows::current_is_hidden() const {
  75. return _cishidden;
  76. }
  77. void DirAccessWindows::list_dir_end() {
  78. if (p->h != INVALID_HANDLE_VALUE) {
  79. FindClose(p->h);
  80. p->h = INVALID_HANDLE_VALUE;
  81. }
  82. }
  83. int DirAccessWindows::get_drive_count() {
  84. return drive_count;
  85. }
  86. String DirAccessWindows::get_drive(int p_drive) {
  87. if (p_drive < 0 || p_drive >= drive_count)
  88. return "";
  89. return String::chr(drives[p_drive]) + ":";
  90. }
  91. Error DirAccessWindows::change_dir(String p_dir) {
  92. GLOBAL_LOCK_FUNCTION
  93. p_dir = fix_path(p_dir);
  94. wchar_t real_current_dir_name[2048];
  95. GetCurrentDirectoryW(2048, real_current_dir_name);
  96. String prev_dir = real_current_dir_name;
  97. SetCurrentDirectoryW(current_dir.c_str());
  98. bool worked = (SetCurrentDirectoryW(p_dir.c_str()) != 0);
  99. String base = _get_root_path();
  100. if (base != "") {
  101. GetCurrentDirectoryW(2048, real_current_dir_name);
  102. String new_dir;
  103. new_dir = String(real_current_dir_name).replace("\\", "/");
  104. if (!new_dir.begins_with(base)) {
  105. worked = false;
  106. }
  107. }
  108. if (worked) {
  109. GetCurrentDirectoryW(2048, real_current_dir_name);
  110. current_dir = real_current_dir_name; // TODO, utf8 parser
  111. current_dir = current_dir.replace("\\", "/");
  112. } //else {
  113. SetCurrentDirectoryW(prev_dir.c_str());
  114. //}
  115. return worked ? OK : ERR_INVALID_PARAMETER;
  116. }
  117. Error DirAccessWindows::make_dir(String p_dir) {
  118. GLOBAL_LOCK_FUNCTION
  119. if (p_dir.is_rel_path())
  120. p_dir = get_current_dir().plus_file(p_dir);
  121. p_dir = fix_path(p_dir);
  122. p_dir = p_dir.replace("/", "\\");
  123. bool success;
  124. int err;
  125. p_dir = "\\\\?\\" + p_dir; //done according to
  126. // https://msdn.microsoft.com/en-us/library/windows/desktop/aa363855(v=vs.85).aspx
  127. success = CreateDirectoryW(p_dir.c_str(), NULL);
  128. err = GetLastError();
  129. if (success) {
  130. return OK;
  131. };
  132. if (err == ERROR_ALREADY_EXISTS || err == ERROR_ACCESS_DENIED) {
  133. return ERR_ALREADY_EXISTS;
  134. };
  135. return ERR_CANT_CREATE;
  136. }
  137. String DirAccessWindows::get_current_dir() {
  138. String base = _get_root_path();
  139. if (base != "") {
  140. String bd = current_dir.replace("\\", "/").replace_first(base, "");
  141. if (bd.begins_with("/"))
  142. return _get_root_string() + bd.substr(1, bd.length());
  143. else
  144. return _get_root_string() + bd;
  145. } else {
  146. }
  147. return current_dir;
  148. }
  149. bool DirAccessWindows::file_exists(String p_file) {
  150. GLOBAL_LOCK_FUNCTION
  151. if (!p_file.is_abs_path())
  152. p_file = get_current_dir().plus_file(p_file);
  153. p_file = fix_path(p_file);
  154. //p_file.replace("/","\\");
  155. //WIN32_FILE_ATTRIBUTE_DATA fileInfo;
  156. DWORD fileAttr;
  157. fileAttr = GetFileAttributesW(p_file.c_str());
  158. if (INVALID_FILE_ATTRIBUTES == fileAttr)
  159. return false;
  160. return !(fileAttr & FILE_ATTRIBUTE_DIRECTORY);
  161. }
  162. bool DirAccessWindows::dir_exists(String p_dir) {
  163. GLOBAL_LOCK_FUNCTION
  164. if (p_dir.is_rel_path())
  165. p_dir = get_current_dir().plus_file(p_dir);
  166. p_dir = fix_path(p_dir);
  167. //p_dir.replace("/","\\");
  168. //WIN32_FILE_ATTRIBUTE_DATA fileInfo;
  169. DWORD fileAttr;
  170. fileAttr = GetFileAttributesW(p_dir.c_str());
  171. if (INVALID_FILE_ATTRIBUTES == fileAttr)
  172. return false;
  173. return (fileAttr & FILE_ATTRIBUTE_DIRECTORY);
  174. }
  175. Error DirAccessWindows::rename(String p_path, String p_new_path) {
  176. if (p_path.is_rel_path())
  177. p_path = get_current_dir().plus_file(p_path);
  178. p_path = fix_path(p_path);
  179. if (p_new_path.is_rel_path())
  180. p_new_path = get_current_dir().plus_file(p_new_path);
  181. p_new_path = fix_path(p_new_path);
  182. if (file_exists(p_new_path)) {
  183. if (remove(p_new_path) != OK) {
  184. return FAILED;
  185. };
  186. };
  187. return ::_wrename(p_path.c_str(), p_new_path.c_str()) == 0 ? OK : FAILED;
  188. }
  189. Error DirAccessWindows::remove(String p_path) {
  190. if (p_path.is_rel_path())
  191. p_path = get_current_dir().plus_file(p_path);
  192. p_path = fix_path(p_path);
  193. printf("erasing %s\n", p_path.utf8().get_data());
  194. //WIN32_FILE_ATTRIBUTE_DATA fileInfo;
  195. //DWORD fileAttr = GetFileAttributesExW(p_path.c_str(), GetFileExInfoStandard, &fileInfo);
  196. DWORD fileAttr;
  197. fileAttr = GetFileAttributesW(p_path.c_str());
  198. if (INVALID_FILE_ATTRIBUTES == fileAttr)
  199. return FAILED;
  200. if ((fileAttr & FILE_ATTRIBUTE_DIRECTORY))
  201. return ::_wrmdir(p_path.c_str()) == 0 ? OK : FAILED;
  202. else
  203. return ::_wunlink(p_path.c_str()) == 0 ? OK : FAILED;
  204. }
  205. /*
  206. FileType DirAccessWindows::get_file_type(const String& p_file) const {
  207. wchar_t real_current_dir_name[2048];
  208. GetCurrentDirectoryW(2048,real_current_dir_name);
  209. String prev_dir=real_current_dir_name;
  210. bool worked SetCurrentDirectoryW(current_dir.c_str());
  211. DWORD attr;
  212. if (worked) {
  213. WIN32_FILE_ATTRIBUTE_DATA fileInfo;
  214. attr = GetFileAttributesExW(p_file.c_str(), GetFileExInfoStandard, &fileInfo);
  215. }
  216. SetCurrentDirectoryW(prev_dir.c_str());
  217. if (!worked)
  218. return FILE_TYPE_NONE;
  219. return (attr&FILE_ATTRIBUTE_DIRECTORY)?FILE_TYPE_
  220. }
  221. */
  222. size_t DirAccessWindows::get_space_left() {
  223. uint64_t bytes = 0;
  224. if (!GetDiskFreeSpaceEx(NULL, (PULARGE_INTEGER)&bytes, NULL, NULL))
  225. return 0;
  226. //this is either 0 or a value in bytes.
  227. return (size_t)bytes;
  228. }
  229. DirAccessWindows::DirAccessWindows() {
  230. p = memnew(DirAccessWindowsPrivate);
  231. p->h = INVALID_HANDLE_VALUE;
  232. current_dir = ".";
  233. drive_count = 0;
  234. #ifdef UWP_ENABLED
  235. Windows::Storage::StorageFolder ^ install_folder = Windows::ApplicationModel::Package::Current->InstalledLocation;
  236. change_dir(install_folder->Path->Data());
  237. #else
  238. DWORD mask = GetLogicalDrives();
  239. for (int i = 0; i < MAX_DRIVES; i++) {
  240. if (mask & (1 << i)) { //DRIVE EXISTS
  241. drives[drive_count] = 'a' + i;
  242. drive_count++;
  243. }
  244. }
  245. change_dir(".");
  246. #endif
  247. }
  248. DirAccessWindows::~DirAccessWindows() {
  249. memdelete(p);
  250. }
  251. #endif //windows DirAccess support