2
0

dir_access_unix.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750
  1. /**************************************************************************/
  2. /* dir_access_unix.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  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)
  32. #include "core/os/memory.h"
  33. #include "core/os/os.h"
  34. #include "core/string/print_string.h"
  35. #include "core/templates/list.h"
  36. #include <fcntl.h>
  37. #include <sys/ioctl.h>
  38. #include <sys/stat.h>
  39. #ifdef __linux__
  40. #include <sys/statfs.h>
  41. #endif
  42. #include <sys/statvfs.h>
  43. #include <cerrno>
  44. #include <cstdio>
  45. #include <cstdlib>
  46. #if __has_include(<mntent.h>)
  47. #include <mntent.h>
  48. #endif
  49. Error DirAccessUnix::list_dir_begin() {
  50. list_dir_end(); //close any previous dir opening!
  51. //char real_current_dir_name[2048]; //is this enough?!
  52. //getcwd(real_current_dir_name,2048);
  53. //chdir(current_path.utf8().get_data());
  54. dir_stream = opendir(current_dir.utf8().get_data());
  55. //chdir(real_current_dir_name);
  56. if (!dir_stream) {
  57. return ERR_CANT_OPEN; //error!
  58. }
  59. return OK;
  60. }
  61. bool DirAccessUnix::file_exists(String p_file) {
  62. GLOBAL_LOCK_FUNCTION
  63. if (p_file.is_relative_path()) {
  64. p_file = current_dir.path_join(p_file);
  65. }
  66. p_file = fix_path(p_file);
  67. struct stat flags = {};
  68. bool success = (stat(p_file.utf8().get_data(), &flags) == 0);
  69. if (success && S_ISDIR(flags.st_mode)) {
  70. success = false;
  71. }
  72. return success;
  73. }
  74. bool DirAccessUnix::dir_exists(String p_dir) {
  75. GLOBAL_LOCK_FUNCTION
  76. if (p_dir.is_relative_path()) {
  77. p_dir = get_current_dir().path_join(p_dir);
  78. }
  79. p_dir = fix_path(p_dir);
  80. struct stat flags = {};
  81. bool success = (stat(p_dir.utf8().get_data(), &flags) == 0);
  82. return (success && S_ISDIR(flags.st_mode));
  83. }
  84. bool DirAccessUnix::is_readable(String p_dir) {
  85. GLOBAL_LOCK_FUNCTION
  86. if (p_dir.is_relative_path()) {
  87. p_dir = get_current_dir().path_join(p_dir);
  88. }
  89. p_dir = fix_path(p_dir);
  90. return (access(p_dir.utf8().get_data(), R_OK) == 0);
  91. }
  92. bool DirAccessUnix::is_writable(String p_dir) {
  93. GLOBAL_LOCK_FUNCTION
  94. if (p_dir.is_relative_path()) {
  95. p_dir = get_current_dir().path_join(p_dir);
  96. }
  97. p_dir = fix_path(p_dir);
  98. return (access(p_dir.utf8().get_data(), W_OK) == 0);
  99. }
  100. uint64_t DirAccessUnix::get_modified_time(String p_file) {
  101. if (p_file.is_relative_path()) {
  102. p_file = current_dir.path_join(p_file);
  103. }
  104. p_file = fix_path(p_file);
  105. struct stat flags = {};
  106. bool success = (stat(p_file.utf8().get_data(), &flags) == 0);
  107. if (success) {
  108. return flags.st_mtime;
  109. } else {
  110. ERR_FAIL_V(0);
  111. }
  112. return 0;
  113. }
  114. String DirAccessUnix::get_next() {
  115. if (!dir_stream) {
  116. return "";
  117. }
  118. dirent *entry = readdir(dir_stream);
  119. if (entry == nullptr) {
  120. list_dir_end();
  121. return "";
  122. }
  123. String fname = fix_unicode_name(entry->d_name);
  124. // Look at d_type to determine if the entry is a directory, unless
  125. // its type is unknown (the file system does not support it) or if
  126. // the type is a link, in that case we want to resolve the link to
  127. // known if it points to a directory. stat() will resolve the link
  128. // for us.
  129. if (entry->d_type == DT_UNKNOWN || entry->d_type == DT_LNK) {
  130. String f = current_dir.path_join(fname);
  131. struct stat flags = {};
  132. if (stat(f.utf8().get_data(), &flags) == 0) {
  133. _cisdir = S_ISDIR(flags.st_mode);
  134. } else {
  135. _cisdir = false;
  136. }
  137. } else {
  138. _cisdir = (entry->d_type == DT_DIR);
  139. }
  140. _cishidden = is_hidden(fname);
  141. return fname;
  142. }
  143. bool DirAccessUnix::current_is_dir() const {
  144. return _cisdir;
  145. }
  146. bool DirAccessUnix::current_is_hidden() const {
  147. return _cishidden;
  148. }
  149. void DirAccessUnix::list_dir_end() {
  150. if (dir_stream) {
  151. closedir(dir_stream);
  152. }
  153. dir_stream = nullptr;
  154. _cisdir = false;
  155. }
  156. #if __has_include(<mntent.h>) && defined(LINUXBSD_ENABLED)
  157. static bool _filter_drive(struct mntent *mnt) {
  158. // Ignore devices that don't point to /dev
  159. if (strncmp(mnt->mnt_fsname, "/dev", 4) != 0) {
  160. return false;
  161. }
  162. // Accept devices mounted at common locations
  163. if (strncmp(mnt->mnt_dir, "/media", 6) == 0 ||
  164. strncmp(mnt->mnt_dir, "/mnt", 4) == 0 ||
  165. strncmp(mnt->mnt_dir, "/home", 5) == 0 ||
  166. strncmp(mnt->mnt_dir, "/run/media", 10) == 0) {
  167. return true;
  168. }
  169. // Ignore everything else
  170. return false;
  171. }
  172. #endif
  173. static void _get_drives(List<String> *list) {
  174. // Add root.
  175. list->push_back("/");
  176. #if __has_include(<mntent.h>) && defined(LINUXBSD_ENABLED)
  177. // Check /etc/mtab for the list of mounted partitions.
  178. FILE *mtab = setmntent("/etc/mtab", "r");
  179. if (mtab) {
  180. struct mntent mnt;
  181. char strings[4096];
  182. while (getmntent_r(mtab, &mnt, strings, sizeof(strings))) {
  183. if (mnt.mnt_dir != nullptr && _filter_drive(&mnt)) {
  184. // Avoid duplicates
  185. String name = String::utf8(mnt.mnt_dir);
  186. if (!list->find(name)) {
  187. list->push_back(name);
  188. }
  189. }
  190. }
  191. endmntent(mtab);
  192. }
  193. #endif
  194. // Add $HOME.
  195. const char *home = getenv("HOME");
  196. if (home) {
  197. // Only add if it's not a duplicate
  198. String home_name = String::utf8(home);
  199. if (!list->find(home_name)) {
  200. list->push_back(home_name);
  201. }
  202. // Check GTK+3 bookmarks for both XDG locations (Documents, Downloads, etc.)
  203. // and potential user-defined bookmarks.
  204. char path[1024];
  205. snprintf(path, 1024, "%s/.config/gtk-3.0/bookmarks", home);
  206. FILE *fd = fopen(path, "r");
  207. if (fd) {
  208. char string[1024];
  209. while (fgets(string, 1024, fd)) {
  210. // Parse only file:// links
  211. if (strncmp(string, "file://", 7) == 0) {
  212. // Strip any unwanted edges on the strings and push_back if it's not a duplicate.
  213. String fpath = String::utf8(string + 7).strip_edges().split_spaces()[0].uri_file_decode();
  214. if (!list->find(fpath)) {
  215. list->push_back(fpath);
  216. }
  217. }
  218. }
  219. fclose(fd);
  220. }
  221. // Add Desktop dir.
  222. String dpath = OS::get_singleton()->get_system_dir(OS::SystemDir::SYSTEM_DIR_DESKTOP);
  223. if (dpath.length() > 0 && !list->find(dpath)) {
  224. list->push_back(dpath);
  225. }
  226. }
  227. list->sort();
  228. }
  229. int DirAccessUnix::get_drive_count() {
  230. List<String> list;
  231. _get_drives(&list);
  232. return list.size();
  233. }
  234. String DirAccessUnix::get_drive(int p_drive) {
  235. List<String> list;
  236. _get_drives(&list);
  237. ERR_FAIL_INDEX_V(p_drive, list.size(), "");
  238. return list.get(p_drive);
  239. }
  240. int DirAccessUnix::get_current_drive() {
  241. int drive = 0;
  242. int max_length = -1;
  243. const String path = get_current_dir().to_lower();
  244. for (int i = 0; i < get_drive_count(); i++) {
  245. const String d = get_drive(i).to_lower();
  246. if (max_length < d.length() && path.begins_with(d)) {
  247. max_length = d.length();
  248. drive = i;
  249. }
  250. }
  251. return drive;
  252. }
  253. bool DirAccessUnix::drives_are_shortcuts() {
  254. return true;
  255. }
  256. Error DirAccessUnix::make_dir(String p_dir) {
  257. GLOBAL_LOCK_FUNCTION
  258. if (p_dir.is_relative_path()) {
  259. p_dir = get_current_dir().path_join(p_dir);
  260. }
  261. p_dir = fix_path(p_dir);
  262. bool success = (mkdir(p_dir.utf8().get_data(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) == 0);
  263. int err = errno;
  264. if (success) {
  265. return OK;
  266. }
  267. if (err == EEXIST) {
  268. return ERR_ALREADY_EXISTS;
  269. }
  270. return ERR_CANT_CREATE;
  271. }
  272. Error DirAccessUnix::change_dir(String p_dir) {
  273. GLOBAL_LOCK_FUNCTION
  274. p_dir = fix_path(p_dir);
  275. // prev_dir is the directory we are changing out of
  276. String prev_dir;
  277. char real_current_dir_name[2048];
  278. ERR_FAIL_NULL_V(getcwd(real_current_dir_name, 2048), ERR_BUG);
  279. if (prev_dir.append_utf8(real_current_dir_name) != OK) {
  280. prev_dir = real_current_dir_name; //no utf8, maybe latin?
  281. }
  282. // try_dir is the directory we are trying to change into
  283. String try_dir = "";
  284. if (p_dir.is_relative_path()) {
  285. String next_dir = current_dir.path_join(p_dir);
  286. next_dir = next_dir.simplify_path();
  287. try_dir = next_dir;
  288. } else {
  289. try_dir = p_dir;
  290. }
  291. bool worked = (chdir(try_dir.utf8().get_data()) == 0); // we can only give this utf8
  292. if (!worked) {
  293. return ERR_INVALID_PARAMETER;
  294. }
  295. String base = _get_root_path();
  296. if (!base.is_empty() && !try_dir.begins_with(base)) {
  297. ERR_FAIL_NULL_V(getcwd(real_current_dir_name, 2048), ERR_BUG);
  298. String new_dir;
  299. new_dir.append_utf8(real_current_dir_name);
  300. if (!new_dir.begins_with(base)) {
  301. try_dir = current_dir; //revert
  302. }
  303. }
  304. // the directory exists, so set current_dir to try_dir
  305. current_dir = try_dir;
  306. ERR_FAIL_COND_V(chdir(prev_dir.utf8().get_data()) != 0, ERR_BUG);
  307. return OK;
  308. }
  309. String DirAccessUnix::get_current_dir(bool p_include_drive) const {
  310. String base = _get_root_path();
  311. if (!base.is_empty()) {
  312. String bd = current_dir.replace_first(base, "");
  313. if (bd.begins_with("/")) {
  314. return _get_root_string() + bd.substr(1);
  315. } else {
  316. return _get_root_string() + bd;
  317. }
  318. }
  319. return current_dir;
  320. }
  321. Error DirAccessUnix::rename(String p_path, String p_new_path) {
  322. if (p_path.is_relative_path()) {
  323. p_path = get_current_dir().path_join(p_path);
  324. }
  325. p_path = fix_path(p_path);
  326. if (p_path.ends_with("/")) {
  327. p_path = p_path.left(-1);
  328. }
  329. if (p_new_path.is_relative_path()) {
  330. p_new_path = get_current_dir().path_join(p_new_path);
  331. }
  332. p_new_path = fix_path(p_new_path);
  333. if (p_new_path.ends_with("/")) {
  334. p_new_path = p_new_path.left(-1);
  335. }
  336. int res = ::rename(p_path.utf8().get_data(), p_new_path.utf8().get_data());
  337. if (res != 0 && errno == EXDEV) { // Cross-device move, use copy and remove.
  338. Error err = OK;
  339. err = copy(p_path, p_new_path);
  340. if (err != OK) {
  341. return err;
  342. }
  343. return remove(p_path);
  344. } else {
  345. return (res == 0) ? OK : FAILED;
  346. }
  347. }
  348. Error DirAccessUnix::remove(String p_path) {
  349. if (p_path.is_relative_path()) {
  350. p_path = get_current_dir().path_join(p_path);
  351. }
  352. p_path = fix_path(p_path);
  353. if (p_path.ends_with("/")) {
  354. p_path = p_path.left(-1);
  355. }
  356. struct stat flags = {};
  357. if ((lstat(p_path.utf8().get_data(), &flags) != 0)) {
  358. return FAILED;
  359. }
  360. int err;
  361. if (S_ISDIR(flags.st_mode) && !is_link(p_path)) {
  362. err = ::rmdir(p_path.utf8().get_data());
  363. } else {
  364. err = ::unlink(p_path.utf8().get_data());
  365. }
  366. if (err != 0) {
  367. return FAILED;
  368. }
  369. if (remove_notification_func != nullptr) {
  370. remove_notification_func(p_path);
  371. }
  372. return OK;
  373. }
  374. bool DirAccessUnix::is_link(String p_file) {
  375. if (p_file.is_relative_path()) {
  376. p_file = get_current_dir().path_join(p_file);
  377. }
  378. p_file = fix_path(p_file);
  379. if (p_file.ends_with("/")) {
  380. p_file = p_file.left(-1);
  381. }
  382. struct stat flags = {};
  383. if ((lstat(p_file.utf8().get_data(), &flags) != 0)) {
  384. return false;
  385. }
  386. return S_ISLNK(flags.st_mode);
  387. }
  388. String DirAccessUnix::read_link(String p_file) {
  389. if (p_file.is_relative_path()) {
  390. p_file = get_current_dir().path_join(p_file);
  391. }
  392. p_file = fix_path(p_file);
  393. if (p_file.ends_with("/")) {
  394. p_file = p_file.left(-1);
  395. }
  396. char buf[256];
  397. memset(buf, 0, 256);
  398. ssize_t len = readlink(p_file.utf8().get_data(), buf, sizeof(buf));
  399. String link;
  400. if (len > 0) {
  401. link.append_utf8(buf, len);
  402. }
  403. return link;
  404. }
  405. Error DirAccessUnix::create_link(String p_source, String p_target) {
  406. if (p_target.is_relative_path()) {
  407. p_target = get_current_dir().path_join(p_target);
  408. }
  409. p_source = fix_path(p_source);
  410. p_target = fix_path(p_target);
  411. if (symlink(p_source.utf8().get_data(), p_target.utf8().get_data()) == 0) {
  412. return OK;
  413. } else {
  414. return FAILED;
  415. }
  416. }
  417. uint64_t DirAccessUnix::get_space_left() {
  418. struct statvfs vfs;
  419. if (statvfs(current_dir.utf8().get_data(), &vfs) != 0) {
  420. return 0;
  421. }
  422. return (uint64_t)vfs.f_bavail * (uint64_t)vfs.f_frsize;
  423. }
  424. String DirAccessUnix::get_filesystem_type() const {
  425. #ifdef __linux__
  426. struct statfs fs;
  427. if (statfs(current_dir.utf8().get_data(), &fs) != 0) {
  428. return "";
  429. }
  430. switch (fs.f_type) {
  431. case 0x0000adf5:
  432. return "ADFS";
  433. case 0x0000adff:
  434. return "AFFS";
  435. case 0x5346414f:
  436. return "AFS";
  437. case 0x00000187:
  438. return "AUTOFS";
  439. case 0x00c36400:
  440. return "CEPH";
  441. case 0x73757245:
  442. return "CODA";
  443. case 0x28cd3d45:
  444. return "CRAMFS";
  445. case 0x453dcd28:
  446. return "CRAMFS";
  447. case 0x64626720:
  448. return "DEBUGFS";
  449. case 0x73636673:
  450. return "SECURITYFS";
  451. case 0xf97cff8c:
  452. return "SELINUX";
  453. case 0x43415d53:
  454. return "SMACK";
  455. case 0x858458f6:
  456. return "RAMFS";
  457. case 0x01021994:
  458. return "TMPFS";
  459. case 0x958458f6:
  460. return "HUGETLBFS";
  461. case 0x73717368:
  462. return "SQUASHFS";
  463. case 0x0000f15f:
  464. return "ECRYPTFS";
  465. case 0x00414a53:
  466. return "EFS";
  467. case 0xe0f5e1e2:
  468. return "EROFS";
  469. case 0x0000ef53:
  470. return "EXTFS";
  471. case 0xabba1974:
  472. return "XENFS";
  473. case 0x9123683e:
  474. return "BTRFS";
  475. case 0x00003434:
  476. return "NILFS";
  477. case 0xf2f52010:
  478. return "F2FS";
  479. case 0xf995e849:
  480. return "HPFS";
  481. case 0x00009660:
  482. return "ISOFS";
  483. case 0x000072b6:
  484. return "JFFS2";
  485. case 0x58465342:
  486. return "XFS";
  487. case 0x6165676c:
  488. return "PSTOREFS";
  489. case 0xde5e81e4:
  490. return "EFIVARFS";
  491. case 0x00c0ffee:
  492. return "HOSTFS";
  493. case 0x794c7630:
  494. return "OVERLAYFS";
  495. case 0x65735546:
  496. return "FUSE";
  497. case 0xca451a4e:
  498. return "BCACHEFS";
  499. case 0x00004d44:
  500. return "FAT32";
  501. case 0x2011bab0:
  502. return "EXFAT";
  503. case 0x0000564c:
  504. return "NCP";
  505. case 0x00006969:
  506. return "NFS";
  507. case 0x7461636f:
  508. return "OCFS2";
  509. case 0x00009fa1:
  510. return "OPENPROM";
  511. case 0x0000002f:
  512. return "QNX4";
  513. case 0x68191122:
  514. return "QNX6";
  515. case 0x6b414653:
  516. return "AFS";
  517. case 0x52654973:
  518. return "REISERFS";
  519. case 0x0000517b:
  520. return "SMB";
  521. case 0xff534d42:
  522. return "CIFS";
  523. case 0x0027e0eb:
  524. return "CGROUP";
  525. case 0x63677270:
  526. return "CGROUP2";
  527. case 0x07655821:
  528. return "RDTGROUP";
  529. case 0x74726163:
  530. return "TRACEFS";
  531. case 0x01021997:
  532. return "V9FS";
  533. case 0x62646576:
  534. return "BDEVFS";
  535. case 0x64646178:
  536. return "DAXFS";
  537. case 0x42494e4d:
  538. return "BINFMTFS";
  539. case 0x00001cd1:
  540. return "DEVPTS";
  541. case 0x6c6f6f70:
  542. return "BINDERFS";
  543. case 0x0bad1dea:
  544. return "FUTEXFS";
  545. case 0x50495045:
  546. return "PIPEFS";
  547. case 0x00009fa0:
  548. return "PROC";
  549. case 0x534f434b:
  550. return "SOCKFS";
  551. case 0x62656572:
  552. return "SYSFS";
  553. case 0x00009fa2:
  554. return "USBDEVICE";
  555. case 0x11307854:
  556. return "MTD_INODE";
  557. case 0x09041934:
  558. return "ANON_INODE";
  559. case 0x73727279:
  560. return "BTRFS";
  561. case 0x6e736673:
  562. return "NSFS";
  563. case 0xcafe4a11:
  564. return "BPF_FS";
  565. case 0x5a3c69f0:
  566. return "AAFS";
  567. case 0x5a4f4653:
  568. return "ZONEFS";
  569. case 0x15013346:
  570. return "UDF";
  571. case 0x444d4142:
  572. return "DMA_BUF";
  573. case 0x454d444d:
  574. return "DEVMEM";
  575. case 0x5345434d:
  576. return "SECRETMEM";
  577. case 0x50494446:
  578. return "PID_FS";
  579. default:
  580. return "";
  581. }
  582. #else
  583. return ""; //TODO this should be implemented
  584. #endif
  585. }
  586. bool DirAccessUnix::is_hidden(const String &p_name) {
  587. return p_name != "." && p_name != ".." && p_name.begins_with(".");
  588. }
  589. bool DirAccessUnix::is_case_sensitive(const String &p_path) const {
  590. #if defined(LINUXBSD_ENABLED)
  591. String f = p_path;
  592. if (!f.is_absolute_path()) {
  593. f = get_current_dir().path_join(f);
  594. }
  595. f = fix_path(f);
  596. int fd = ::open(f.utf8().get_data(), O_RDONLY | O_NONBLOCK);
  597. if (fd) {
  598. long flags = 0;
  599. if (ioctl(fd, _IOR('f', 1, long), &flags) >= 0) {
  600. ::close(fd);
  601. return !(flags & 0x40000000 /* FS_CASEFOLD_FL */);
  602. }
  603. ::close(fd);
  604. }
  605. #endif
  606. return true;
  607. }
  608. bool DirAccessUnix::is_equivalent(const String &p_path_a, const String &p_path_b) const {
  609. String f1 = fix_path(p_path_a);
  610. struct stat st1 = {};
  611. int err = stat(f1.utf8().get_data(), &st1);
  612. if (err) {
  613. return DirAccess::is_equivalent(p_path_a, p_path_b);
  614. }
  615. String f2 = fix_path(p_path_b);
  616. struct stat st2 = {};
  617. err = stat(f2.utf8().get_data(), &st2);
  618. if (err) {
  619. return DirAccess::is_equivalent(p_path_a, p_path_b);
  620. }
  621. return (st1.st_dev == st2.st_dev) && (st1.st_ino == st2.st_ino);
  622. }
  623. DirAccessUnix::DirAccessUnix() {
  624. dir_stream = nullptr;
  625. _cisdir = false;
  626. /* determine drive count */
  627. // set current directory to an absolute path of the current directory
  628. char real_current_dir_name[2048];
  629. ERR_FAIL_NULL(getcwd(real_current_dir_name, 2048));
  630. current_dir.clear();
  631. if (current_dir.append_utf8(real_current_dir_name) != OK) {
  632. current_dir = real_current_dir_name;
  633. }
  634. change_dir(current_dir);
  635. }
  636. DirAccessUnix::RemoveNotificationFunc DirAccessUnix::remove_notification_func = nullptr;
  637. DirAccessUnix::~DirAccessUnix() {
  638. list_dir_end();
  639. }
  640. #endif // UNIX_ENABLED