dir_access_unix.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  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-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 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. #include "core/list.h"
  33. #include "core/os/memory.h"
  34. #include "core/print_string.h"
  35. #include <errno.h>
  36. #include <stdio.h>
  37. #include <stdlib.h>
  38. #include <string.h>
  39. #ifndef ANDROID_ENABLED
  40. #include <sys/statvfs.h>
  41. #endif
  42. #ifdef HAVE_MNTENT
  43. #include <mntent.h>
  44. #endif
  45. DirAccess *DirAccessUnix::create_fs() {
  46. return memnew(DirAccessUnix);
  47. }
  48. Error DirAccessUnix::list_dir_begin() {
  49. list_dir_end(); //close any previous dir opening!
  50. //char real_current_dir_name[2048]; //is this enough?!
  51. //getcwd(real_current_dir_name,2048);
  52. //chdir(current_path.utf8().get_data());
  53. dir_stream = opendir(current_dir.utf8().get_data());
  54. //chdir(real_current_dir_name);
  55. if (!dir_stream)
  56. return ERR_CANT_OPEN; //error!
  57. return OK;
  58. }
  59. bool DirAccessUnix::file_exists(String p_file) {
  60. GLOBAL_LOCK_FUNCTION
  61. if (p_file.is_rel_path())
  62. p_file = current_dir.plus_file(p_file);
  63. p_file = fix_path(p_file);
  64. struct stat flags;
  65. bool success = (stat(p_file.utf8().get_data(), &flags) == 0);
  66. if (success && S_ISDIR(flags.st_mode)) {
  67. success = false;
  68. }
  69. return success;
  70. }
  71. bool DirAccessUnix::dir_exists(String p_dir) {
  72. GLOBAL_LOCK_FUNCTION
  73. if (p_dir.is_rel_path())
  74. p_dir = get_current_dir().plus_file(p_dir);
  75. p_dir = fix_path(p_dir);
  76. struct stat flags;
  77. bool success = (stat(p_dir.utf8().get_data(), &flags) == 0);
  78. return (success && S_ISDIR(flags.st_mode));
  79. }
  80. uint64_t DirAccessUnix::get_modified_time(String p_file) {
  81. if (p_file.is_rel_path())
  82. p_file = current_dir.plus_file(p_file);
  83. p_file = fix_path(p_file);
  84. struct stat flags;
  85. bool success = (stat(p_file.utf8().get_data(), &flags) == 0);
  86. if (success) {
  87. return flags.st_mtime;
  88. } else {
  89. ERR_FAIL_V(0);
  90. };
  91. return 0;
  92. };
  93. String DirAccessUnix::get_next() {
  94. if (!dir_stream)
  95. return "";
  96. dirent *entry = readdir(dir_stream);
  97. if (entry == NULL) {
  98. list_dir_end();
  99. return "";
  100. }
  101. String fname = fix_unicode_name(entry->d_name);
  102. // Look at d_type to determine if the entry is a directory, unless
  103. // its type is unknown (the file system does not support it) or if
  104. // the type is a link, in that case we want to resolve the link to
  105. // known if it points to a directory. stat() will resolve the link
  106. // for us.
  107. if (entry->d_type == DT_UNKNOWN || entry->d_type == DT_LNK) {
  108. String f = current_dir.plus_file(fname);
  109. struct stat flags;
  110. if (stat(f.utf8().get_data(), &flags) == 0) {
  111. _cisdir = S_ISDIR(flags.st_mode);
  112. } else {
  113. _cisdir = false;
  114. }
  115. } else {
  116. _cisdir = (entry->d_type == DT_DIR);
  117. }
  118. _cishidden = (fname != "." && fname != ".." && fname.begins_with("."));
  119. return fname;
  120. }
  121. bool DirAccessUnix::current_is_dir() const {
  122. return _cisdir;
  123. }
  124. bool DirAccessUnix::current_is_hidden() const {
  125. return _cishidden;
  126. }
  127. void DirAccessUnix::list_dir_end() {
  128. if (dir_stream)
  129. closedir(dir_stream);
  130. dir_stream = 0;
  131. _cisdir = false;
  132. }
  133. #if defined(HAVE_MNTENT) && defined(X11_ENABLED)
  134. static bool _filter_drive(struct mntent *mnt) {
  135. // Ignore devices that don't point to /dev
  136. if (strncmp(mnt->mnt_fsname, "/dev", 4) != 0) {
  137. return false;
  138. }
  139. // Accept devices mounted at common locations
  140. if (strncmp(mnt->mnt_dir, "/media", 6) == 0 ||
  141. strncmp(mnt->mnt_dir, "/mnt", 4) == 0 ||
  142. strncmp(mnt->mnt_dir, "/home", 5) == 0 ||
  143. strncmp(mnt->mnt_dir, "/run/media", 10) == 0) {
  144. return true;
  145. }
  146. // Ignore everything else
  147. return false;
  148. }
  149. #endif
  150. static void _get_drives(List<String> *list) {
  151. #if defined(HAVE_MNTENT) && defined(X11_ENABLED)
  152. // Check /etc/mtab for the list of mounted partitions
  153. FILE *mtab = setmntent("/etc/mtab", "r");
  154. if (mtab) {
  155. struct mntent mnt;
  156. char strings[4096];
  157. while (getmntent_r(mtab, &mnt, strings, sizeof(strings))) {
  158. if (mnt.mnt_dir != NULL && _filter_drive(&mnt)) {
  159. // Avoid duplicates
  160. if (!list->find(mnt.mnt_dir)) {
  161. list->push_back(mnt.mnt_dir);
  162. }
  163. }
  164. }
  165. endmntent(mtab);
  166. }
  167. #endif
  168. // Add $HOME
  169. const char *home = getenv("HOME");
  170. if (home) {
  171. // Only add if it's not a duplicate
  172. if (!list->find(home)) {
  173. list->push_back(home);
  174. }
  175. // Check $HOME/.config/gtk-3.0/bookmarks
  176. char path[1024];
  177. snprintf(path, 1024, "%s/.config/gtk-3.0/bookmarks", home);
  178. FILE *fd = fopen(path, "r");
  179. if (fd) {
  180. char string[1024];
  181. while (fgets(string, 1024, fd)) {
  182. // Parse only file:// links
  183. if (strncmp(string, "file://", 7) == 0) {
  184. // Strip any unwanted edges on the strings and push_back if it's not a duplicate
  185. String fpath = String(string + 7).strip_edges().split_spaces()[0].percent_decode();
  186. if (!list->find(fpath)) {
  187. list->push_back(fpath);
  188. }
  189. }
  190. }
  191. fclose(fd);
  192. }
  193. }
  194. list->sort();
  195. }
  196. int DirAccessUnix::get_drive_count() {
  197. List<String> list;
  198. _get_drives(&list);
  199. return list.size();
  200. }
  201. String DirAccessUnix::get_drive(int p_drive) {
  202. List<String> list;
  203. _get_drives(&list);
  204. ERR_FAIL_INDEX_V(p_drive, list.size(), "");
  205. return list[p_drive];
  206. }
  207. bool DirAccessUnix::drives_are_shortcuts() {
  208. return true;
  209. }
  210. Error DirAccessUnix::make_dir(String p_dir) {
  211. GLOBAL_LOCK_FUNCTION
  212. if (p_dir.is_rel_path())
  213. p_dir = get_current_dir().plus_file(p_dir);
  214. p_dir = fix_path(p_dir);
  215. bool success = (mkdir(p_dir.utf8().get_data(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) == 0);
  216. int err = errno;
  217. if (success) {
  218. return OK;
  219. };
  220. if (err == EEXIST) {
  221. return ERR_ALREADY_EXISTS;
  222. };
  223. return ERR_CANT_CREATE;
  224. }
  225. Error DirAccessUnix::change_dir(String p_dir) {
  226. GLOBAL_LOCK_FUNCTION
  227. p_dir = fix_path(p_dir);
  228. // prev_dir is the directory we are changing out of
  229. String prev_dir;
  230. char real_current_dir_name[2048];
  231. ERR_FAIL_COND_V(getcwd(real_current_dir_name, 2048) == NULL, ERR_BUG);
  232. if (prev_dir.parse_utf8(real_current_dir_name))
  233. prev_dir = real_current_dir_name; //no utf8, maybe latin?
  234. // try_dir is the directory we are trying to change into
  235. String try_dir = "";
  236. if (p_dir.is_rel_path()) {
  237. String next_dir = current_dir.plus_file(p_dir);
  238. next_dir = next_dir.simplify_path();
  239. try_dir = next_dir;
  240. } else {
  241. try_dir = p_dir;
  242. }
  243. bool worked = (chdir(try_dir.utf8().get_data()) == 0); // we can only give this utf8
  244. if (!worked) {
  245. return ERR_INVALID_PARAMETER;
  246. }
  247. String base = _get_root_path();
  248. if (base != String() && !try_dir.begins_with(base)) {
  249. ERR_FAIL_COND_V(getcwd(real_current_dir_name, 2048) == NULL, ERR_BUG);
  250. String new_dir;
  251. new_dir.parse_utf8(real_current_dir_name);
  252. if (!new_dir.begins_with(base)) {
  253. try_dir = current_dir; //revert
  254. }
  255. }
  256. // the directory exists, so set current_dir to try_dir
  257. current_dir = try_dir;
  258. ERR_FAIL_COND_V(chdir(prev_dir.utf8().get_data()) != 0, ERR_BUG);
  259. return OK;
  260. }
  261. String DirAccessUnix::get_current_dir(bool p_include_drive) {
  262. String base = _get_root_path();
  263. if (base != "") {
  264. String bd = current_dir.replace_first(base, "");
  265. if (bd.begins_with("/"))
  266. return _get_root_string() + bd.substr(1, bd.length());
  267. else
  268. return _get_root_string() + bd;
  269. }
  270. return current_dir;
  271. }
  272. Error DirAccessUnix::rename(String p_path, String p_new_path) {
  273. if (p_path.is_rel_path())
  274. p_path = get_current_dir().plus_file(p_path);
  275. p_path = fix_path(p_path);
  276. if (p_new_path.is_rel_path())
  277. p_new_path = get_current_dir().plus_file(p_new_path);
  278. p_new_path = fix_path(p_new_path);
  279. return ::rename(p_path.utf8().get_data(), p_new_path.utf8().get_data()) == 0 ? OK : FAILED;
  280. }
  281. Error DirAccessUnix::remove(String p_path) {
  282. if (p_path.is_rel_path())
  283. p_path = get_current_dir().plus_file(p_path);
  284. p_path = fix_path(p_path);
  285. struct stat flags;
  286. if ((stat(p_path.utf8().get_data(), &flags) != 0))
  287. return FAILED;
  288. if (S_ISDIR(flags.st_mode))
  289. return ::rmdir(p_path.utf8().get_data()) == 0 ? OK : FAILED;
  290. else
  291. return ::unlink(p_path.utf8().get_data()) == 0 ? OK : FAILED;
  292. }
  293. size_t DirAccessUnix::get_space_left() {
  294. #ifndef NO_STATVFS
  295. struct statvfs vfs;
  296. if (statvfs(current_dir.utf8().get_data(), &vfs) != 0) {
  297. return 0;
  298. };
  299. return vfs.f_bfree * vfs.f_bsize;
  300. #else
  301. // FIXME: Implement this.
  302. return 0;
  303. #endif
  304. };
  305. String DirAccessUnix::get_filesystem_type() const {
  306. return ""; //TODO this should be implemented
  307. }
  308. DirAccessUnix::DirAccessUnix() {
  309. dir_stream = 0;
  310. _cisdir = false;
  311. /* determine drive count */
  312. // set current directory to an absolute path of the current directory
  313. char real_current_dir_name[2048];
  314. ERR_FAIL_COND(getcwd(real_current_dir_name, 2048) == NULL);
  315. if (current_dir.parse_utf8(real_current_dir_name))
  316. current_dir = real_current_dir_name;
  317. change_dir(current_dir);
  318. }
  319. DirAccessUnix::~DirAccessUnix() {
  320. list_dir_end();
  321. }
  322. #endif //posix_enabled