2
0

dir_access_unix.cpp 10 KB

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