dir_access_unix.cpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. /*************************************************************************/
  2. /* dir_access_unix.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://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. bool success = (mkdir(p_dir.utf8().get_data(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) == 0);
  138. int err = errno;
  139. if (success) {
  140. return OK;
  141. };
  142. if (err == EEXIST) {
  143. return ERR_ALREADY_EXISTS;
  144. };
  145. return ERR_CANT_CREATE;
  146. }
  147. Error DirAccessUnix::change_dir(String p_dir) {
  148. GLOBAL_LOCK_FUNCTION
  149. p_dir = fix_path(p_dir);
  150. char real_current_dir_name[2048];
  151. getcwd(real_current_dir_name, 2048);
  152. String prev_dir;
  153. if (prev_dir.parse_utf8(real_current_dir_name))
  154. prev_dir = real_current_dir_name; //no utf8, maybe latin?
  155. chdir(current_dir.utf8().get_data()); //ascii since this may be unicode or wathever the host os wants
  156. bool worked = (chdir(p_dir.utf8().get_data()) == 0); // we can only give this utf8
  157. String base = _get_root_path();
  158. if (base != "") {
  159. getcwd(real_current_dir_name, 2048);
  160. String new_dir;
  161. new_dir.parse_utf8(real_current_dir_name);
  162. if (!new_dir.begins_with(base))
  163. worked = false;
  164. }
  165. if (worked) {
  166. getcwd(real_current_dir_name, 2048);
  167. if (current_dir.parse_utf8(real_current_dir_name))
  168. current_dir = real_current_dir_name; //no utf8, maybe latin?
  169. }
  170. chdir(prev_dir.utf8().get_data());
  171. return worked ? OK : ERR_INVALID_PARAMETER;
  172. }
  173. String DirAccessUnix::get_current_dir() {
  174. String base = _get_root_path();
  175. if (base != "") {
  176. String bd = current_dir.replace_first(base, "");
  177. if (bd.begins_with("/"))
  178. return _get_root_string() + bd.substr(1, bd.length());
  179. else
  180. return _get_root_string() + bd;
  181. }
  182. return current_dir;
  183. }
  184. Error DirAccessUnix::rename(String p_path, String p_new_path) {
  185. if (p_path.is_rel_path())
  186. p_path = get_current_dir().plus_file(p_path);
  187. p_path = fix_path(p_path);
  188. if (p_new_path.is_rel_path())
  189. p_new_path = get_current_dir().plus_file(p_new_path);
  190. p_new_path = fix_path(p_new_path);
  191. return ::rename(p_path.utf8().get_data(), p_new_path.utf8().get_data()) == 0 ? OK : FAILED;
  192. }
  193. Error DirAccessUnix::remove(String p_path) {
  194. if (p_path.is_rel_path())
  195. p_path = get_current_dir().plus_file(p_path);
  196. p_path = fix_path(p_path);
  197. struct stat flags;
  198. if ((stat(p_path.utf8().get_data(), &flags) != 0))
  199. return FAILED;
  200. if (S_ISDIR(flags.st_mode))
  201. return ::rmdir(p_path.utf8().get_data()) == 0 ? OK : FAILED;
  202. else
  203. return ::unlink(p_path.utf8().get_data()) == 0 ? OK : FAILED;
  204. }
  205. size_t DirAccessUnix::get_space_left() {
  206. #ifndef NO_STATVFS
  207. struct statvfs vfs;
  208. if (statvfs(current_dir.utf8().get_data(), &vfs) != 0) {
  209. return 0;
  210. };
  211. return vfs.f_bfree * vfs.f_bsize;
  212. #else
  213. #warning THIS IS BROKEN
  214. return 0;
  215. #endif
  216. };
  217. DirAccessUnix::DirAccessUnix() {
  218. dir_stream = 0;
  219. current_dir = ".";
  220. _cisdir = false;
  221. /* determine drive count */
  222. change_dir(current_dir);
  223. }
  224. DirAccessUnix::~DirAccessUnix() {
  225. list_dir_end();
  226. }
  227. #endif //posix_enabled