dir_access_osx.mm 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. /*************************************************************************/
  2. /* dir_access_unix.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. #include "dir_access_osx.h"
  30. #if defined(UNIX_ENABLED) || defined(LIBC_FILEIO_ENABLED)
  31. #ifndef ANDROID_ENABLED
  32. #include <sys/statvfs.h>
  33. #endif
  34. #include <stdio.h>
  35. #include "os/memory.h"
  36. #include "print_string.h"
  37. #include <errno.h>
  38. #include <Foundation/NSString.h>
  39. DirAccess *DirAccessOSX::create_fs() {
  40. return memnew( DirAccessOSX );
  41. }
  42. bool DirAccessOSX::list_dir_begin() {
  43. list_dir_end(); //close any previous dir opening!
  44. // char real_current_dir_name[2048]; //is this enough?!
  45. //getcwd(real_current_dir_name,2048);
  46. //chdir(curent_path.utf8().get_data());
  47. dir_stream = opendir(current_dir.utf8().get_data());
  48. //chdir(real_current_dir_name);
  49. if (!dir_stream)
  50. return true; //error!
  51. return false;
  52. }
  53. bool DirAccessOSX::file_exists(String p_file) {
  54. GLOBAL_LOCK_FUNCTION
  55. if (p_file.is_rel_path())
  56. p_file=current_dir+"/"+p_file;
  57. else
  58. p_file=fix_path(p_file);
  59. struct stat flags;
  60. bool success = (stat(p_file.utf8().get_data(),&flags)==0);
  61. if (success && S_ISDIR(flags.st_mode)) {
  62. success=false;
  63. }
  64. return success;
  65. }
  66. bool DirAccessOSX::dir_exists(String p_dir) {
  67. GLOBAL_LOCK_FUNCTION
  68. if (p_dir.is_rel_path())
  69. p_dir=get_current_dir().plus_file(p_dir);
  70. else
  71. p_dir=fix_path(p_dir);
  72. struct stat flags;
  73. bool success = (stat(p_dir.utf8().get_data(),&flags)==0);
  74. if (success && S_ISDIR(flags.st_mode))
  75. return true;
  76. return false;
  77. }
  78. uint64_t DirAccessOSX::get_modified_time(String p_file) {
  79. if (p_file.is_rel_path())
  80. p_file=current_dir+"/"+p_file;
  81. else
  82. p_file=fix_path(p_file);
  83. struct stat flags;
  84. bool success = (stat(p_file.utf8().get_data(),&flags)==0);
  85. if (success) {
  86. return flags.st_mtime;
  87. } else {
  88. ERR_FAIL_V(0);
  89. };
  90. return 0;
  91. };
  92. String DirAccessOSX::get_next() {
  93. if (!dir_stream)
  94. return "";
  95. dirent *entry;
  96. entry=readdir(dir_stream);
  97. if (entry==NULL) {
  98. list_dir_end();
  99. return "";
  100. }
  101. //typedef struct stat Stat;
  102. struct stat flags;
  103. String fname;
  104. NSString* nsstr = [[NSString stringWithUTF8String: entry->d_name] precomposedStringWithCanonicalMapping];
  105. fname.parse_utf8([nsstr UTF8String]);
  106. //[nsstr autorelease];
  107. String f=current_dir+"/"+fname;
  108. if (stat(f.utf8().get_data(),&flags)==0) {
  109. if (S_ISDIR(flags.st_mode)) {
  110. _cisdir=true;
  111. } else {
  112. _cisdir=false;
  113. }
  114. } else {
  115. _cisdir=false;
  116. }
  117. _cishidden=(fname!="." && fname!=".." && fname.begins_with("."));
  118. return fname;
  119. }
  120. bool DirAccessOSX::current_is_dir() const {
  121. return _cisdir;
  122. }
  123. bool DirAccessOSX::current_is_hidden() const {
  124. return _cishidden;
  125. }
  126. void DirAccessOSX::list_dir_end() {
  127. if (dir_stream)
  128. closedir(dir_stream);
  129. dir_stream=0;
  130. _cisdir=false;
  131. }
  132. int DirAccessOSX::get_drive_count() {
  133. return 0;
  134. }
  135. String DirAccessOSX::get_drive(int p_drive) {
  136. return "";
  137. }
  138. Error DirAccessOSX::make_dir(String p_dir) {
  139. GLOBAL_LOCK_FUNCTION
  140. p_dir=fix_path(p_dir);
  141. char real_current_dir_name[2048];
  142. getcwd(real_current_dir_name,2048);
  143. chdir(current_dir.utf8().get_data()); //ascii since this may be unicode or wathever the host os wants
  144. bool success=(mkdir(p_dir.utf8().get_data(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH)==0);
  145. int err = errno;
  146. chdir(real_current_dir_name);
  147. if (success) {
  148. return OK;
  149. };
  150. if (err == EEXIST) {
  151. return ERR_ALREADY_EXISTS;
  152. };
  153. return ERR_CANT_CREATE;
  154. }
  155. Error DirAccessOSX::change_dir(String p_dir) {
  156. GLOBAL_LOCK_FUNCTION
  157. p_dir=fix_path(p_dir);
  158. char real_current_dir_name[2048];
  159. getcwd(real_current_dir_name,2048);
  160. String prev_dir;
  161. if (prev_dir.parse_utf8(real_current_dir_name))
  162. prev_dir=real_current_dir_name; //no utf8, maybe latin?
  163. chdir(current_dir.utf8().get_data()); //ascii since this may be unicode or wathever the host os wants
  164. bool worked=(chdir(p_dir.utf8().get_data())==0); // we can only give this utf8
  165. #ifndef IPHONE_ENABLED
  166. String base = _get_root_path();
  167. if (base!="") {
  168. getcwd(real_current_dir_name,2048);
  169. String new_dir;
  170. new_dir.parse_utf8(real_current_dir_name);
  171. if (!new_dir.begins_with(base))
  172. worked=false;
  173. }
  174. #endif
  175. if (worked) {
  176. getcwd(real_current_dir_name,2048);
  177. if (current_dir.parse_utf8(real_current_dir_name))
  178. current_dir=real_current_dir_name; //no utf8, maybe latin?
  179. }
  180. chdir(prev_dir.utf8().get_data());
  181. return worked?OK:ERR_INVALID_PARAMETER;
  182. }
  183. String DirAccessOSX::get_current_dir() {
  184. String base = _get_root_path();
  185. if (base!="") {
  186. String bd = current_dir.replace_first(base,"");
  187. if (bd.begins_with("/"))
  188. return _get_root_string()+bd.substr(1,bd.length());
  189. else
  190. return _get_root_string()+bd;
  191. }
  192. return current_dir;
  193. }
  194. Error DirAccessOSX::rename(String p_path,String p_new_path) {
  195. if (p_path.is_rel_path())
  196. p_path=get_current_dir().plus_file(p_path);
  197. else
  198. p_path=fix_path(p_path);
  199. if (p_new_path.is_rel_path())
  200. p_new_path=get_current_dir().plus_file(p_new_path);
  201. else
  202. p_new_path=fix_path(p_new_path);
  203. return ::rename(p_path.utf8().get_data(),p_new_path.utf8().get_data())==0?OK:FAILED;
  204. }
  205. Error DirAccessOSX::remove(String p_path) {
  206. if (p_path.is_rel_path())
  207. p_path=get_current_dir().plus_file(p_path);
  208. else
  209. p_path=fix_path(p_path);
  210. struct stat flags;
  211. if ((stat(p_path.utf8().get_data(),&flags)!=0))
  212. return FAILED;
  213. if (S_ISDIR(flags.st_mode))
  214. return ::rmdir(p_path.utf8().get_data())==0?OK:FAILED;
  215. else
  216. return ::unlink(p_path.utf8().get_data())==0?OK:FAILED;
  217. }
  218. size_t DirAccessOSX::get_space_left() {
  219. #ifndef NO_STATVFS
  220. struct statvfs vfs;
  221. if (statvfs(current_dir.utf8().get_data(), &vfs) != 0) {
  222. return -1;
  223. };
  224. return vfs.f_bfree * vfs.f_bsize;
  225. #else
  226. #warning THIS IS BROKEN
  227. return 0;
  228. #endif
  229. };
  230. DirAccessOSX::DirAccessOSX() {
  231. dir_stream=0;
  232. current_dir=".";
  233. _cisdir=false;
  234. /* determine drive count */
  235. change_dir(current_dir);
  236. }
  237. DirAccessOSX::~DirAccessOSX() {
  238. list_dir_end();
  239. }
  240. #endif //posix_enabled