dir_access_unix.cpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  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-2015 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+"/"+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+"/"+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;
  103. if (fname.parse_utf8(entry->d_name))
  104. fname=entry->d_name; //no utf8, maybe latin?
  105. String f=current_dir+"/"+fname;
  106. if (stat(f.utf8().get_data(),&flags)==0) {
  107. if (S_ISDIR(flags.st_mode)) {
  108. _cisdir=true;
  109. } else {
  110. _cisdir=false;
  111. }
  112. } else {
  113. _cisdir=false;
  114. }
  115. _cishidden=(fname!="." && fname!=".." && fname.begins_with("."));
  116. return fname;
  117. }
  118. bool DirAccessUnix::current_is_dir() const {
  119. return _cisdir;
  120. }
  121. bool DirAccessUnix::current_is_hidden() const {
  122. return _cishidden;
  123. }
  124. void DirAccessUnix::list_dir_end() {
  125. if (dir_stream)
  126. closedir(dir_stream);
  127. dir_stream=0;
  128. _cisdir=false;
  129. }
  130. int DirAccessUnix::get_drive_count() {
  131. return 0;
  132. }
  133. String DirAccessUnix::get_drive(int p_drive) {
  134. return "";
  135. }
  136. Error DirAccessUnix::make_dir(String p_dir) {
  137. GLOBAL_LOCK_FUNCTION
  138. p_dir=fix_path(p_dir);
  139. char real_current_dir_name[2048];
  140. getcwd(real_current_dir_name,2048);
  141. chdir(current_dir.utf8().get_data()); //ascii since this may be unicode or wathever the host os wants
  142. bool success=(mkdir(p_dir.utf8().get_data(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH)==0);
  143. int err = errno;
  144. chdir(real_current_dir_name);
  145. if (success) {
  146. return OK;
  147. };
  148. if (err == EEXIST) {
  149. return ERR_ALREADY_EXISTS;
  150. };
  151. return ERR_CANT_CREATE;
  152. }
  153. Error DirAccessUnix::change_dir(String p_dir) {
  154. GLOBAL_LOCK_FUNCTION
  155. p_dir=fix_path(p_dir);
  156. char real_current_dir_name[2048];
  157. getcwd(real_current_dir_name,2048);
  158. String prev_dir;
  159. if (prev_dir.parse_utf8(real_current_dir_name))
  160. prev_dir=real_current_dir_name; //no utf8, maybe latin?
  161. chdir(current_dir.utf8().get_data()); //ascii since this may be unicode or wathever the host os wants
  162. bool worked=(chdir(p_dir.utf8().get_data())==0); // we can only give this utf8
  163. #ifndef IPHONE_ENABLED
  164. String base = _get_root_path();
  165. if (base!="") {
  166. getcwd(real_current_dir_name,2048);
  167. String new_dir;
  168. new_dir.parse_utf8(real_current_dir_name);
  169. if (!new_dir.begins_with(base))
  170. worked=false;
  171. }
  172. #endif
  173. if (worked) {
  174. getcwd(real_current_dir_name,2048);
  175. if (current_dir.parse_utf8(real_current_dir_name))
  176. current_dir=real_current_dir_name; //no utf8, maybe latin?
  177. }
  178. chdir(prev_dir.utf8().get_data());
  179. return worked?OK:ERR_INVALID_PARAMETER;
  180. }
  181. String DirAccessUnix::get_current_dir() {
  182. String base = _get_root_path();
  183. if (base!="") {
  184. String bd = current_dir.replace_first(base,"");
  185. if (bd.begins_with("/"))
  186. return _get_root_string()+bd.substr(1,bd.length());
  187. else
  188. return _get_root_string()+bd;
  189. }
  190. return current_dir;
  191. }
  192. Error DirAccessUnix::rename(String p_path,String p_new_path) {
  193. if (p_path.is_rel_path())
  194. p_path=get_current_dir().plus_file(p_path);
  195. else
  196. p_path=fix_path(p_path);
  197. if (p_new_path.is_rel_path())
  198. p_new_path=get_current_dir().plus_file(p_new_path);
  199. else
  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. p_path=fix_path(p_path);
  205. struct stat flags;
  206. if ((stat(p_path.utf8().get_data(),&flags)!=0))
  207. return FAILED;
  208. if (S_ISDIR(flags.st_mode))
  209. return ::rmdir(p_path.utf8().get_data())==0?OK:FAILED;
  210. else
  211. return ::unlink(p_path.utf8().get_data())==0?OK:FAILED;
  212. }
  213. size_t DirAccessUnix::get_space_left() {
  214. #ifndef NO_STATVFS
  215. struct statvfs vfs;
  216. if (statvfs(current_dir.utf8().get_data(), &vfs) != 0) {
  217. return -1;
  218. };
  219. return vfs.f_bfree * vfs.f_bsize;
  220. #else
  221. #warning THIS IS BROKEN
  222. return 0;
  223. #endif
  224. };
  225. DirAccessUnix::DirAccessUnix() {
  226. dir_stream=0;
  227. current_dir=".";
  228. _cisdir=false;
  229. /* determine drive count */
  230. change_dir(current_dir);
  231. }
  232. DirAccessUnix::~DirAccessUnix() {
  233. list_dir_end();
  234. }
  235. #endif //posix_enabled