dir_access_windows.cpp 9.3 KB

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