dir_access_unix.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  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-2017 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2017 Godot Engine contributors (cf. AUTHORS.md) */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #include "dir_access_unix.h"
  31. #if defined(UNIX_ENABLED) || defined(LIBC_FILEIO_ENABLED)
  32. #ifndef ANDROID_ENABLED
  33. #include <sys/statvfs.h>
  34. #endif
  35. #include "os/memory.h"
  36. #include "print_string.h"
  37. #include <errno.h>
  38. #include <stdio.h>
  39. DirAccess *DirAccessUnix::create_fs() {
  40. return memnew(DirAccessUnix);
  41. }
  42. Error DirAccessUnix::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 ERR_CANT_OPEN; //error!
  51. return OK;
  52. }
  53. bool DirAccessUnix::file_exists(String p_file) {
  54. GLOBAL_LOCK_FUNCTION
  55. if (p_file.is_rel_path())
  56. p_file = current_dir.plus_file(p_file);
  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. p_dir = fix_path(p_dir);
  70. struct stat flags;
  71. bool success = (stat(p_dir.utf8().get_data(), &flags) == 0);
  72. if (success && S_ISDIR(flags.st_mode))
  73. return true;
  74. return false;
  75. }
  76. uint64_t DirAccessUnix::get_modified_time(String p_file) {
  77. if (p_file.is_rel_path())
  78. p_file = current_dir.plus_file(p_file);
  79. p_file = fix_path(p_file);
  80. struct stat flags;
  81. bool success = (stat(p_file.utf8().get_data(), &flags) == 0);
  82. if (success) {
  83. return flags.st_mtime;
  84. } else {
  85. ERR_FAIL_V(0);
  86. };
  87. return 0;
  88. };
  89. String DirAccessUnix::get_next() {
  90. if (!dir_stream)
  91. return "";
  92. dirent *entry;
  93. entry = readdir(dir_stream);
  94. if (entry == NULL) {
  95. list_dir_end();
  96. return "";
  97. }
  98. //typedef struct stat Stat;
  99. struct stat flags;
  100. String fname = fix_unicode_name(entry->d_name);
  101. String f = current_dir.plus_file(fname);
  102. if (stat(f.utf8().get_data(), &flags) == 0) {
  103. if (S_ISDIR(flags.st_mode)) {
  104. _cisdir = true;
  105. } else {
  106. _cisdir = false;
  107. }
  108. } else {
  109. _cisdir = false;
  110. }
  111. _cishidden = (fname != "." && fname != ".." && fname.begins_with("."));
  112. return fname;
  113. }
  114. bool DirAccessUnix::current_is_dir() const {
  115. return _cisdir;
  116. }
  117. bool DirAccessUnix::current_is_hidden() const {
  118. return _cishidden;
  119. }
  120. void DirAccessUnix::list_dir_end() {
  121. if (dir_stream)
  122. closedir(dir_stream);
  123. dir_stream = 0;
  124. _cisdir = false;
  125. }
  126. int DirAccessUnix::get_drive_count() {
  127. return 0;
  128. }
  129. String DirAccessUnix::get_drive(int p_drive) {
  130. return "";
  131. }
  132. Error DirAccessUnix::make_dir(String p_dir) {
  133. GLOBAL_LOCK_FUNCTION
  134. if (p_dir.is_rel_path())
  135. p_dir = get_current_dir().plus_file(p_dir);
  136. p_dir = fix_path(p_dir);
  137. #if 1
  138. bool success = (mkdir(p_dir.utf8().get_data(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) == 0);
  139. int err = errno;
  140. #else
  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. #endif
  148. if (success) {
  149. return OK;
  150. };
  151. if (err == EEXIST) {
  152. return ERR_ALREADY_EXISTS;
  153. };
  154. return ERR_CANT_CREATE;
  155. }
  156. Error DirAccessUnix::change_dir(String p_dir) {
  157. GLOBAL_LOCK_FUNCTION
  158. // make sure current_dir is valid absolute path
  159. if (current_dir == "." || current_dir == "") {
  160. char real_current_dir_name[2048];
  161. getcwd(real_current_dir_name, 2048);
  162. current_dir.parse_utf8(real_current_dir_name);
  163. }
  164. if (p_dir == ".") {
  165. return OK;
  166. }
  167. p_dir = fix_path(p_dir);
  168. String prev_dir = current_dir;
  169. if (p_dir.is_rel_path()) {
  170. String next_dir = current_dir + "/" + p_dir;
  171. next_dir = next_dir.simplify_path();
  172. current_dir = next_dir;
  173. } else {
  174. current_dir = p_dir;
  175. }
  176. bool worked = (chdir(current_dir.utf8().get_data()) == 0); // we can only give this utf8
  177. if (!worked) {
  178. current_dir = prev_dir;
  179. return ERR_INVALID_PARAMETER;
  180. }
  181. return OK;
  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