dir_access_unix.cpp 11 KB

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