dir_access_unix.cpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  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_unix.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. DirAccess *DirAccessUnix::create_fs() {
  39. return memnew( DirAccessUnix );
  40. }
  41. bool DirAccessUnix::list_dir_begin() {
  42. list_dir_end(); //close any previous dir opening!
  43. // char real_current_dir_name[2048]; //is this enough?!
  44. //getcwd(real_current_dir_name,2048);
  45. //chdir(curent_path.utf8().get_data());
  46. dir_stream = opendir(current_dir.utf8().get_data());
  47. //chdir(real_current_dir_name);
  48. if (!dir_stream)
  49. return true; //error!
  50. return false;
  51. }
  52. bool DirAccessUnix::file_exists(String p_file) {
  53. GLOBAL_LOCK_FUNCTION
  54. if (p_file.is_rel_path())
  55. p_file=current_dir.plus_file(p_file);
  56. p_file=fix_path(p_file);
  57. struct stat flags;
  58. bool success = (stat(p_file.utf8().get_data(),&flags)==0);
  59. if (success && S_ISDIR(flags.st_mode)) {
  60. success=false;
  61. }
  62. return success;
  63. }
  64. bool DirAccessUnix::dir_exists(String p_dir) {
  65. GLOBAL_LOCK_FUNCTION
  66. if (p_dir.is_rel_path())
  67. p_dir=get_current_dir().plus_file(p_dir);
  68. p_dir=fix_path(p_dir);
  69. struct stat flags;
  70. bool success = (stat(p_dir.utf8().get_data(),&flags)==0);
  71. if (success && S_ISDIR(flags.st_mode))
  72. return true;
  73. return false;
  74. }
  75. uint64_t DirAccessUnix::get_modified_time(String p_file) {
  76. if (p_file.is_rel_path())
  77. p_file=current_dir.plus_file(p_file);
  78. p_file=fix_path(p_file);
  79. struct stat flags;
  80. bool success = (stat(p_file.utf8().get_data(),&flags)==0);
  81. if (success) {
  82. return flags.st_mtime;
  83. } else {
  84. ERR_FAIL_V(0);
  85. };
  86. return 0;
  87. };
  88. String DirAccessUnix::get_next() {
  89. if (!dir_stream)
  90. return "";
  91. dirent *entry;
  92. entry=readdir(dir_stream);
  93. if (entry==NULL) {
  94. list_dir_end();
  95. return "";
  96. }
  97. //typedef struct stat Stat;
  98. struct stat flags;
  99. String fname = fix_unicode_name(entry->d_name);
  100. String f=current_dir.plus_file(fname);
  101. if (stat(f.utf8().get_data(),&flags)==0) {
  102. if (S_ISDIR(flags.st_mode)) {
  103. _cisdir=true;
  104. } else {
  105. _cisdir=false;
  106. }
  107. } else {
  108. _cisdir=false;
  109. }
  110. _cishidden=(fname!="." && fname!=".." && fname.begins_with("."));
  111. return fname;
  112. }
  113. bool DirAccessUnix::current_is_dir() const {
  114. return _cisdir;
  115. }
  116. bool DirAccessUnix::current_is_hidden() const {
  117. return _cishidden;
  118. }
  119. void DirAccessUnix::list_dir_end() {
  120. if (dir_stream)
  121. closedir(dir_stream);
  122. dir_stream=0;
  123. _cisdir=false;
  124. }
  125. int DirAccessUnix::get_drive_count() {
  126. return 0;
  127. }
  128. String DirAccessUnix::get_drive(int p_drive) {
  129. return "";
  130. }
  131. Error DirAccessUnix::make_dir(String p_dir) {
  132. GLOBAL_LOCK_FUNCTION
  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. #if 1
  137. bool success=(mkdir(p_dir.utf8().get_data(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH)==0);
  138. int err = errno;
  139. #else
  140. char real_current_dir_name[2048];
  141. getcwd(real_current_dir_name,2048);
  142. chdir(current_dir.utf8().get_data()); //ascii since this may be unicode or wathever the host os wants
  143. bool success=(mkdir(p_dir.utf8().get_data(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH)==0);
  144. int err = errno;
  145. chdir(real_current_dir_name);
  146. #endif
  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 DirAccessUnix::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 DirAccessUnix::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 DirAccessUnix::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. p_path=fix_path(p_path);
  198. if (p_new_path.is_rel_path())
  199. p_new_path=get_current_dir().plus_file(p_new_path);
  200. p_new_path=fix_path(p_new_path);
  201. return ::rename(p_path.utf8().get_data(),p_new_path.utf8().get_data())==0?OK:FAILED;
  202. }
  203. Error DirAccessUnix::remove(String p_path) {
  204. if (p_path.is_rel_path())
  205. p_path=get_current_dir().plus_file(p_path);
  206. p_path=fix_path(p_path);
  207. struct stat flags;
  208. if ((stat(p_path.utf8().get_data(),&flags)!=0))
  209. return FAILED;
  210. if (S_ISDIR(flags.st_mode))
  211. return ::rmdir(p_path.utf8().get_data())==0?OK:FAILED;
  212. else
  213. return ::unlink(p_path.utf8().get_data())==0?OK:FAILED;
  214. }
  215. size_t DirAccessUnix::get_space_left() {
  216. #ifndef NO_STATVFS
  217. struct statvfs vfs;
  218. if (statvfs(current_dir.utf8().get_data(), &vfs) != 0) {
  219. return 0;
  220. };
  221. return vfs.f_bfree * vfs.f_bsize;
  222. #else
  223. #warning THIS IS BROKEN
  224. return 0;
  225. #endif
  226. };
  227. DirAccessUnix::DirAccessUnix() {
  228. dir_stream=0;
  229. current_dir=".";
  230. _cisdir=false;
  231. /* determine drive count */
  232. change_dir(current_dir);
  233. }
  234. DirAccessUnix::~DirAccessUnix() {
  235. list_dir_end();
  236. }
  237. #endif //posix_enabled