dir_access_unix.cpp 7.8 KB

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