dir_access_unix.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498
  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. String name = String::utf8(mnt.mnt_dir);
  183. if (!list->find(name)) {
  184. list->push_back(name);
  185. }
  186. }
  187. }
  188. endmntent(mtab);
  189. }
  190. #endif
  191. // Add $HOME
  192. const char *home = getenv("HOME");
  193. if (home) {
  194. // Only add if it's not a duplicate
  195. String home_name = String::utf8(home);
  196. if (!list->find(home_name)) {
  197. list->push_back(home_name);
  198. }
  199. // Check $HOME/.config/gtk-3.0/bookmarks
  200. char path[1024];
  201. snprintf(path, 1024, "%s/.config/gtk-3.0/bookmarks", home);
  202. FILE *fd = fopen(path, "r");
  203. if (fd) {
  204. char string[1024];
  205. while (fgets(string, 1024, fd)) {
  206. // Parse only file:// links
  207. if (strncmp(string, "file://", 7) == 0) {
  208. // Strip any unwanted edges on the strings and push_back if it's not a duplicate
  209. String fpath = String::utf8(string + 7).strip_edges().split_spaces()[0].uri_decode();
  210. if (!list->find(fpath)) {
  211. list->push_back(fpath);
  212. }
  213. }
  214. }
  215. fclose(fd);
  216. }
  217. }
  218. list->sort();
  219. }
  220. int DirAccessUnix::get_drive_count() {
  221. List<String> list;
  222. _get_drives(&list);
  223. return list.size();
  224. }
  225. String DirAccessUnix::get_drive(int p_drive) {
  226. List<String> list;
  227. _get_drives(&list);
  228. ERR_FAIL_INDEX_V(p_drive, list.size(), "");
  229. return list[p_drive];
  230. }
  231. bool DirAccessUnix::drives_are_shortcuts() {
  232. return true;
  233. }
  234. Error DirAccessUnix::make_dir(String p_dir) {
  235. GLOBAL_LOCK_FUNCTION
  236. if (p_dir.is_rel_path()) {
  237. p_dir = get_current_dir().plus_file(p_dir);
  238. }
  239. p_dir = fix_path(p_dir);
  240. bool success = (mkdir(p_dir.utf8().get_data(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) == 0);
  241. int err = errno;
  242. if (success) {
  243. return OK;
  244. };
  245. if (err == EEXIST) {
  246. return ERR_ALREADY_EXISTS;
  247. };
  248. return ERR_CANT_CREATE;
  249. }
  250. Error DirAccessUnix::change_dir(String p_dir) {
  251. GLOBAL_LOCK_FUNCTION
  252. p_dir = fix_path(p_dir);
  253. // prev_dir is the directory we are changing out of
  254. String prev_dir;
  255. char real_current_dir_name[2048];
  256. ERR_FAIL_COND_V(getcwd(real_current_dir_name, 2048) == nullptr, ERR_BUG);
  257. if (prev_dir.parse_utf8(real_current_dir_name)) {
  258. prev_dir = real_current_dir_name; //no utf8, maybe latin?
  259. }
  260. // try_dir is the directory we are trying to change into
  261. String try_dir = "";
  262. if (p_dir.is_rel_path()) {
  263. String next_dir = current_dir.plus_file(p_dir);
  264. next_dir = next_dir.simplify_path();
  265. try_dir = next_dir;
  266. } else {
  267. try_dir = p_dir;
  268. }
  269. bool worked = (chdir(try_dir.utf8().get_data()) == 0); // we can only give this utf8
  270. if (!worked) {
  271. return ERR_INVALID_PARAMETER;
  272. }
  273. String base = _get_root_path();
  274. if (base != String() && !try_dir.begins_with(base)) {
  275. ERR_FAIL_COND_V(getcwd(real_current_dir_name, 2048) == nullptr, ERR_BUG);
  276. String new_dir;
  277. new_dir.parse_utf8(real_current_dir_name);
  278. if (!new_dir.begins_with(base)) {
  279. try_dir = current_dir; //revert
  280. }
  281. }
  282. // the directory exists, so set current_dir to try_dir
  283. current_dir = try_dir;
  284. ERR_FAIL_COND_V(chdir(prev_dir.utf8().get_data()) != 0, ERR_BUG);
  285. return OK;
  286. }
  287. String DirAccessUnix::get_current_dir(bool p_include_drive) {
  288. String base = _get_root_path();
  289. if (base != "") {
  290. String bd = current_dir.replace_first(base, "");
  291. if (bd.begins_with("/")) {
  292. return _get_root_string() + bd.substr(1, bd.length());
  293. } else {
  294. return _get_root_string() + bd;
  295. }
  296. }
  297. return current_dir;
  298. }
  299. Error DirAccessUnix::rename(String p_path, String p_new_path) {
  300. if (p_path.is_rel_path()) {
  301. p_path = get_current_dir().plus_file(p_path);
  302. }
  303. p_path = fix_path(p_path);
  304. if (p_new_path.is_rel_path()) {
  305. p_new_path = get_current_dir().plus_file(p_new_path);
  306. }
  307. p_new_path = fix_path(p_new_path);
  308. return ::rename(p_path.utf8().get_data(), p_new_path.utf8().get_data()) == 0 ? OK : FAILED;
  309. }
  310. Error DirAccessUnix::remove(String p_path) {
  311. if (p_path.is_rel_path()) {
  312. p_path = get_current_dir().plus_file(p_path);
  313. }
  314. p_path = fix_path(p_path);
  315. struct stat flags;
  316. if ((stat(p_path.utf8().get_data(), &flags) != 0)) {
  317. return FAILED;
  318. }
  319. if (S_ISDIR(flags.st_mode)) {
  320. return ::rmdir(p_path.utf8().get_data()) == 0 ? OK : FAILED;
  321. } else {
  322. return ::unlink(p_path.utf8().get_data()) == 0 ? OK : FAILED;
  323. }
  324. }
  325. bool DirAccessUnix::is_link(String p_file) {
  326. if (p_file.is_rel_path()) {
  327. p_file = get_current_dir().plus_file(p_file);
  328. }
  329. p_file = fix_path(p_file);
  330. struct stat flags;
  331. if ((lstat(p_file.utf8().get_data(), &flags) != 0)) {
  332. return FAILED;
  333. }
  334. return S_ISLNK(flags.st_mode);
  335. }
  336. String DirAccessUnix::read_link(String p_file) {
  337. if (p_file.is_rel_path()) {
  338. p_file = get_current_dir().plus_file(p_file);
  339. }
  340. p_file = fix_path(p_file);
  341. char buf[256];
  342. memset(buf, 0, 256);
  343. ssize_t len = readlink(p_file.utf8().get_data(), buf, sizeof(buf));
  344. String link;
  345. if (len > 0) {
  346. link.parse_utf8(buf, len);
  347. }
  348. return link;
  349. }
  350. Error DirAccessUnix::create_link(String p_source, String p_target) {
  351. if (p_target.is_rel_path()) {
  352. p_target = get_current_dir().plus_file(p_target);
  353. }
  354. p_source = fix_path(p_source);
  355. p_target = fix_path(p_target);
  356. if (symlink(p_source.utf8().get_data(), p_target.utf8().get_data()) == 0) {
  357. return OK;
  358. } else {
  359. return FAILED;
  360. }
  361. }
  362. uint64_t DirAccessUnix::get_space_left() {
  363. #ifndef NO_STATVFS
  364. struct statvfs vfs;
  365. if (statvfs(current_dir.utf8().get_data(), &vfs) != 0) {
  366. return 0;
  367. };
  368. return (uint64_t)vfs.f_bavail * (uint64_t)vfs.f_frsize;
  369. #else
  370. // FIXME: Implement this.
  371. return 0;
  372. #endif
  373. };
  374. String DirAccessUnix::get_filesystem_type() const {
  375. return ""; //TODO this should be implemented
  376. }
  377. bool DirAccessUnix::is_hidden(const String &p_name) {
  378. return p_name != "." && p_name != ".." && p_name.begins_with(".");
  379. }
  380. DirAccessUnix::DirAccessUnix() {
  381. dir_stream = nullptr;
  382. _cisdir = false;
  383. /* determine drive count */
  384. // set current directory to an absolute path of the current directory
  385. char real_current_dir_name[2048];
  386. ERR_FAIL_COND(getcwd(real_current_dir_name, 2048) == nullptr);
  387. if (current_dir.parse_utf8(real_current_dir_name)) {
  388. current_dir = real_current_dir_name;
  389. }
  390. change_dir(current_dir);
  391. }
  392. DirAccessUnix::~DirAccessUnix() {
  393. list_dir_end();
  394. }
  395. #endif //posix_enabled