dir_access_unix.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510
  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-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 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. Error DirAccessUnix::list_dir_begin() {
  46. list_dir_end(); //close any previous dir opening!
  47. //char real_current_dir_name[2048]; //is this enough?!
  48. //getcwd(real_current_dir_name,2048);
  49. //chdir(current_path.utf8().get_data());
  50. dir_stream = opendir(current_dir.utf8().get_data());
  51. //chdir(real_current_dir_name);
  52. if (!dir_stream) {
  53. return ERR_CANT_OPEN; //error!
  54. }
  55. return OK;
  56. }
  57. bool DirAccessUnix::file_exists(String p_file) {
  58. GLOBAL_LOCK_FUNCTION
  59. if (p_file.is_relative_path()) {
  60. p_file = current_dir.plus_file(p_file);
  61. }
  62. p_file = fix_path(p_file);
  63. struct stat flags;
  64. bool success = (stat(p_file.utf8().get_data(), &flags) == 0);
  65. if (success && S_ISDIR(flags.st_mode)) {
  66. success = false;
  67. }
  68. return success;
  69. }
  70. bool DirAccessUnix::dir_exists(String p_dir) {
  71. GLOBAL_LOCK_FUNCTION
  72. if (p_dir.is_relative_path()) {
  73. p_dir = get_current_dir().plus_file(p_dir);
  74. }
  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. bool DirAccessUnix::is_readable(String p_dir) {
  81. GLOBAL_LOCK_FUNCTION
  82. if (p_dir.is_relative_path()) {
  83. p_dir = get_current_dir().plus_file(p_dir);
  84. }
  85. p_dir = fix_path(p_dir);
  86. return (access(p_dir.utf8().get_data(), R_OK) == 0);
  87. }
  88. bool DirAccessUnix::is_writable(String p_dir) {
  89. GLOBAL_LOCK_FUNCTION
  90. if (p_dir.is_relative_path()) {
  91. p_dir = get_current_dir().plus_file(p_dir);
  92. }
  93. p_dir = fix_path(p_dir);
  94. return (access(p_dir.utf8().get_data(), W_OK) == 0);
  95. }
  96. uint64_t DirAccessUnix::get_modified_time(String p_file) {
  97. if (p_file.is_relative_path()) {
  98. p_file = current_dir.plus_file(p_file);
  99. }
  100. p_file = fix_path(p_file);
  101. struct stat flags;
  102. bool success = (stat(p_file.utf8().get_data(), &flags) == 0);
  103. if (success) {
  104. return flags.st_mtime;
  105. } else {
  106. ERR_FAIL_V(0);
  107. }
  108. return 0;
  109. }
  110. String DirAccessUnix::get_next() {
  111. if (!dir_stream) {
  112. return "";
  113. }
  114. dirent *entry = readdir(dir_stream);
  115. if (entry == nullptr) {
  116. list_dir_end();
  117. return "";
  118. }
  119. String fname = fix_unicode_name(entry->d_name);
  120. // Look at d_type to determine if the entry is a directory, unless
  121. // its type is unknown (the file system does not support it) or if
  122. // the type is a link, in that case we want to resolve the link to
  123. // known if it points to a directory. stat() will resolve the link
  124. // for us.
  125. if (entry->d_type == DT_UNKNOWN || entry->d_type == DT_LNK) {
  126. String f = current_dir.plus_file(fname);
  127. struct stat flags;
  128. if (stat(f.utf8().get_data(), &flags) == 0) {
  129. _cisdir = S_ISDIR(flags.st_mode);
  130. } else {
  131. _cisdir = false;
  132. }
  133. } else {
  134. _cisdir = (entry->d_type == DT_DIR);
  135. }
  136. _cishidden = is_hidden(fname);
  137. return fname;
  138. }
  139. bool DirAccessUnix::current_is_dir() const {
  140. return _cisdir;
  141. }
  142. bool DirAccessUnix::current_is_hidden() const {
  143. return _cishidden;
  144. }
  145. void DirAccessUnix::list_dir_end() {
  146. if (dir_stream) {
  147. closedir(dir_stream);
  148. }
  149. dir_stream = nullptr;
  150. _cisdir = false;
  151. }
  152. #if defined(HAVE_MNTENT) && defined(X11_ENABLED)
  153. static bool _filter_drive(struct mntent *mnt) {
  154. // Ignore devices that don't point to /dev
  155. if (strncmp(mnt->mnt_fsname, "/dev", 4) != 0) {
  156. return false;
  157. }
  158. // Accept devices mounted at common locations
  159. if (strncmp(mnt->mnt_dir, "/media", 6) == 0 ||
  160. strncmp(mnt->mnt_dir, "/mnt", 4) == 0 ||
  161. strncmp(mnt->mnt_dir, "/home", 5) == 0 ||
  162. strncmp(mnt->mnt_dir, "/run/media", 10) == 0) {
  163. return true;
  164. }
  165. // Ignore everything else
  166. return false;
  167. }
  168. #endif
  169. static void _get_drives(List<String> *list) {
  170. list->push_back("/");
  171. #if defined(HAVE_MNTENT) && defined(X11_ENABLED)
  172. // Check /etc/mtab for the list of mounted partitions
  173. FILE *mtab = setmntent("/etc/mtab", "r");
  174. if (mtab) {
  175. struct mntent mnt;
  176. char strings[4096];
  177. while (getmntent_r(mtab, &mnt, strings, sizeof(strings))) {
  178. if (mnt.mnt_dir != nullptr && _filter_drive(&mnt)) {
  179. // Avoid duplicates
  180. String name = String::utf8(mnt.mnt_dir);
  181. if (!list->find(name)) {
  182. list->push_back(name);
  183. }
  184. }
  185. }
  186. endmntent(mtab);
  187. }
  188. #endif
  189. // Add $HOME
  190. const char *home = getenv("HOME");
  191. if (home) {
  192. // Only add if it's not a duplicate
  193. String home_name = String::utf8(home);
  194. if (!list->find(home_name)) {
  195. list->push_back(home_name);
  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::utf8(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. int DirAccessUnix::get_current_drive() {
  230. int drive = 0;
  231. int max_length = -1;
  232. const String path = get_current_dir().to_lower();
  233. for (int i = 0; i < get_drive_count(); i++) {
  234. const String d = get_drive(i).to_lower();
  235. if (max_length < d.length() && path.begins_with(d)) {
  236. max_length = d.length();
  237. drive = i;
  238. }
  239. }
  240. return drive;
  241. }
  242. bool DirAccessUnix::drives_are_shortcuts() {
  243. return true;
  244. }
  245. Error DirAccessUnix::make_dir(String p_dir) {
  246. GLOBAL_LOCK_FUNCTION
  247. if (p_dir.is_relative_path()) {
  248. p_dir = get_current_dir().plus_file(p_dir);
  249. }
  250. p_dir = fix_path(p_dir);
  251. bool success = (mkdir(p_dir.utf8().get_data(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) == 0);
  252. int err = errno;
  253. if (success) {
  254. return OK;
  255. }
  256. if (err == EEXIST) {
  257. return ERR_ALREADY_EXISTS;
  258. }
  259. return ERR_CANT_CREATE;
  260. }
  261. Error DirAccessUnix::change_dir(String p_dir) {
  262. GLOBAL_LOCK_FUNCTION
  263. p_dir = fix_path(p_dir);
  264. // prev_dir is the directory we are changing out of
  265. String prev_dir;
  266. char real_current_dir_name[2048];
  267. ERR_FAIL_COND_V(getcwd(real_current_dir_name, 2048) == nullptr, ERR_BUG);
  268. if (prev_dir.parse_utf8(real_current_dir_name)) {
  269. prev_dir = real_current_dir_name; //no utf8, maybe latin?
  270. }
  271. // try_dir is the directory we are trying to change into
  272. String try_dir = "";
  273. if (p_dir.is_relative_path()) {
  274. String next_dir = current_dir.plus_file(p_dir);
  275. next_dir = next_dir.simplify_path();
  276. try_dir = next_dir;
  277. } else {
  278. try_dir = p_dir;
  279. }
  280. bool worked = (chdir(try_dir.utf8().get_data()) == 0); // we can only give this utf8
  281. if (!worked) {
  282. return ERR_INVALID_PARAMETER;
  283. }
  284. String base = _get_root_path();
  285. if (!base.is_empty() && !try_dir.begins_with(base)) {
  286. ERR_FAIL_COND_V(getcwd(real_current_dir_name, 2048) == nullptr, ERR_BUG);
  287. String new_dir;
  288. new_dir.parse_utf8(real_current_dir_name);
  289. if (!new_dir.begins_with(base)) {
  290. try_dir = current_dir; //revert
  291. }
  292. }
  293. // the directory exists, so set current_dir to try_dir
  294. current_dir = try_dir;
  295. ERR_FAIL_COND_V(chdir(prev_dir.utf8().get_data()) != 0, ERR_BUG);
  296. return OK;
  297. }
  298. String DirAccessUnix::get_current_dir(bool p_include_drive) const {
  299. String base = _get_root_path();
  300. if (!base.is_empty()) {
  301. String bd = current_dir.replace_first(base, "");
  302. if (bd.begins_with("/")) {
  303. return _get_root_string() + bd.substr(1, bd.length());
  304. } else {
  305. return _get_root_string() + bd;
  306. }
  307. }
  308. return current_dir;
  309. }
  310. Error DirAccessUnix::rename(String p_path, String p_new_path) {
  311. if (p_path.is_relative_path()) {
  312. p_path = get_current_dir().plus_file(p_path);
  313. }
  314. p_path = fix_path(p_path);
  315. if (p_new_path.is_relative_path()) {
  316. p_new_path = get_current_dir().plus_file(p_new_path);
  317. }
  318. p_new_path = fix_path(p_new_path);
  319. return ::rename(p_path.utf8().get_data(), p_new_path.utf8().get_data()) == 0 ? OK : FAILED;
  320. }
  321. Error DirAccessUnix::remove(String p_path) {
  322. if (p_path.is_relative_path()) {
  323. p_path = get_current_dir().plus_file(p_path);
  324. }
  325. p_path = fix_path(p_path);
  326. struct stat flags;
  327. if ((stat(p_path.utf8().get_data(), &flags) != 0)) {
  328. return FAILED;
  329. }
  330. if (S_ISDIR(flags.st_mode)) {
  331. return ::rmdir(p_path.utf8().get_data()) == 0 ? OK : FAILED;
  332. } else {
  333. return ::unlink(p_path.utf8().get_data()) == 0 ? OK : FAILED;
  334. }
  335. }
  336. bool DirAccessUnix::is_link(String p_file) {
  337. if (p_file.is_relative_path()) {
  338. p_file = get_current_dir().plus_file(p_file);
  339. }
  340. p_file = fix_path(p_file);
  341. struct stat flags;
  342. if ((lstat(p_file.utf8().get_data(), &flags) != 0)) {
  343. return FAILED;
  344. }
  345. return S_ISLNK(flags.st_mode);
  346. }
  347. String DirAccessUnix::read_link(String p_file) {
  348. if (p_file.is_relative_path()) {
  349. p_file = get_current_dir().plus_file(p_file);
  350. }
  351. p_file = fix_path(p_file);
  352. char buf[256];
  353. memset(buf, 0, 256);
  354. ssize_t len = readlink(p_file.utf8().get_data(), buf, sizeof(buf));
  355. String link;
  356. if (len > 0) {
  357. link.parse_utf8(buf, len);
  358. }
  359. return link;
  360. }
  361. Error DirAccessUnix::create_link(String p_source, String p_target) {
  362. if (p_target.is_relative_path()) {
  363. p_target = get_current_dir().plus_file(p_target);
  364. }
  365. p_source = fix_path(p_source);
  366. p_target = fix_path(p_target);
  367. if (symlink(p_source.utf8().get_data(), p_target.utf8().get_data()) == 0) {
  368. return OK;
  369. } else {
  370. return FAILED;
  371. }
  372. }
  373. uint64_t DirAccessUnix::get_space_left() {
  374. #ifndef NO_STATVFS
  375. struct statvfs vfs;
  376. if (statvfs(current_dir.utf8().get_data(), &vfs) != 0) {
  377. return 0;
  378. }
  379. return (uint64_t)vfs.f_bavail * (uint64_t)vfs.f_frsize;
  380. #else
  381. // FIXME: Implement this.
  382. return 0;
  383. #endif
  384. }
  385. String DirAccessUnix::get_filesystem_type() const {
  386. return ""; //TODO this should be implemented
  387. }
  388. bool DirAccessUnix::is_hidden(const String &p_name) {
  389. return p_name != "." && p_name != ".." && p_name.begins_with(".");
  390. }
  391. DirAccessUnix::DirAccessUnix() {
  392. dir_stream = nullptr;
  393. _cisdir = false;
  394. /* determine drive count */
  395. // set current directory to an absolute path of the current directory
  396. char real_current_dir_name[2048];
  397. ERR_FAIL_COND(getcwd(real_current_dir_name, 2048) == nullptr);
  398. if (current_dir.parse_utf8(real_current_dir_name)) {
  399. current_dir = real_current_dir_name;
  400. }
  401. change_dir(current_dir);
  402. }
  403. DirAccessUnix::~DirAccessUnix() {
  404. list_dir_end();
  405. }
  406. #endif //posix_enabled