dir_access_unix.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589
  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 <errno.h>
  37. #include <fcntl.h>
  38. #include <stdio.h>
  39. #include <stdlib.h>
  40. #include <string.h>
  41. #include <sys/ioctl.h>
  42. #include <sys/stat.h>
  43. #include <sys/statvfs.h>
  44. #ifdef HAVE_MNTENT
  45. #include <mntent.h>
  46. #endif
  47. Error DirAccessUnix::list_dir_begin() {
  48. list_dir_end(); //close any previous dir opening!
  49. //char real_current_dir_name[2048]; //is this enough?!
  50. //getcwd(real_current_dir_name,2048);
  51. //chdir(current_path.utf8().get_data());
  52. dir_stream = opendir(current_dir.utf8().get_data());
  53. //chdir(real_current_dir_name);
  54. if (!dir_stream) {
  55. return ERR_CANT_OPEN; //error!
  56. }
  57. return OK;
  58. }
  59. bool DirAccessUnix::file_exists(String p_file) {
  60. GLOBAL_LOCK_FUNCTION
  61. if (p_file.is_relative_path()) {
  62. p_file = current_dir.path_join(p_file);
  63. }
  64. p_file = fix_path(p_file);
  65. struct stat flags = {};
  66. bool success = (stat(p_file.utf8().get_data(), &flags) == 0);
  67. if (success && S_ISDIR(flags.st_mode)) {
  68. success = false;
  69. }
  70. return success;
  71. }
  72. bool DirAccessUnix::dir_exists(String p_dir) {
  73. GLOBAL_LOCK_FUNCTION
  74. if (p_dir.is_relative_path()) {
  75. p_dir = get_current_dir().path_join(p_dir);
  76. }
  77. p_dir = fix_path(p_dir);
  78. struct stat flags = {};
  79. bool success = (stat(p_dir.utf8().get_data(), &flags) == 0);
  80. return (success && S_ISDIR(flags.st_mode));
  81. }
  82. bool DirAccessUnix::is_readable(String p_dir) {
  83. GLOBAL_LOCK_FUNCTION
  84. if (p_dir.is_relative_path()) {
  85. p_dir = get_current_dir().path_join(p_dir);
  86. }
  87. p_dir = fix_path(p_dir);
  88. return (access(p_dir.utf8().get_data(), R_OK) == 0);
  89. }
  90. bool DirAccessUnix::is_writable(String p_dir) {
  91. GLOBAL_LOCK_FUNCTION
  92. if (p_dir.is_relative_path()) {
  93. p_dir = get_current_dir().path_join(p_dir);
  94. }
  95. p_dir = fix_path(p_dir);
  96. return (access(p_dir.utf8().get_data(), W_OK) == 0);
  97. }
  98. uint64_t DirAccessUnix::get_modified_time(String p_file) {
  99. if (p_file.is_relative_path()) {
  100. p_file = current_dir.path_join(p_file);
  101. }
  102. p_file = fix_path(p_file);
  103. struct stat flags = {};
  104. bool success = (stat(p_file.utf8().get_data(), &flags) == 0);
  105. if (success) {
  106. return flags.st_mtime;
  107. } else {
  108. ERR_FAIL_V(0);
  109. }
  110. return 0;
  111. }
  112. String DirAccessUnix::get_next() {
  113. if (!dir_stream) {
  114. return "";
  115. }
  116. dirent *entry = readdir(dir_stream);
  117. if (entry == nullptr) {
  118. list_dir_end();
  119. return "";
  120. }
  121. String fname = fix_unicode_name(entry->d_name);
  122. // Look at d_type to determine if the entry is a directory, unless
  123. // its type is unknown (the file system does not support it) or if
  124. // the type is a link, in that case we want to resolve the link to
  125. // known if it points to a directory. stat() will resolve the link
  126. // for us.
  127. if (entry->d_type == DT_UNKNOWN || entry->d_type == DT_LNK) {
  128. String f = current_dir.path_join(fname);
  129. struct stat flags = {};
  130. if (stat(f.utf8().get_data(), &flags) == 0) {
  131. _cisdir = S_ISDIR(flags.st_mode);
  132. } else {
  133. _cisdir = false;
  134. }
  135. } else {
  136. _cisdir = (entry->d_type == DT_DIR);
  137. }
  138. _cishidden = is_hidden(fname);
  139. return fname;
  140. }
  141. bool DirAccessUnix::current_is_dir() const {
  142. return _cisdir;
  143. }
  144. bool DirAccessUnix::current_is_hidden() const {
  145. return _cishidden;
  146. }
  147. void DirAccessUnix::list_dir_end() {
  148. if (dir_stream) {
  149. closedir(dir_stream);
  150. }
  151. dir_stream = nullptr;
  152. _cisdir = false;
  153. }
  154. #if defined(HAVE_MNTENT) && defined(LINUXBSD_ENABLED)
  155. static bool _filter_drive(struct mntent *mnt) {
  156. // Ignore devices that don't point to /dev
  157. if (strncmp(mnt->mnt_fsname, "/dev", 4) != 0) {
  158. return false;
  159. }
  160. // Accept devices mounted at common locations
  161. if (strncmp(mnt->mnt_dir, "/media", 6) == 0 ||
  162. strncmp(mnt->mnt_dir, "/mnt", 4) == 0 ||
  163. strncmp(mnt->mnt_dir, "/home", 5) == 0 ||
  164. strncmp(mnt->mnt_dir, "/run/media", 10) == 0) {
  165. return true;
  166. }
  167. // Ignore everything else
  168. return false;
  169. }
  170. #endif
  171. static void _get_drives(List<String> *list) {
  172. // Add root.
  173. list->push_back("/");
  174. #if defined(HAVE_MNTENT) && defined(LINUXBSD_ENABLED)
  175. // Check /etc/mtab for the list of mounted partitions.
  176. FILE *mtab = setmntent("/etc/mtab", "r");
  177. if (mtab) {
  178. struct mntent mnt;
  179. char strings[4096];
  180. while (getmntent_r(mtab, &mnt, strings, sizeof(strings))) {
  181. if (mnt.mnt_dir != nullptr && _filter_drive(&mnt)) {
  182. // Avoid duplicates
  183. String name = String::utf8(mnt.mnt_dir);
  184. if (!list->find(name)) {
  185. list->push_back(name);
  186. }
  187. }
  188. }
  189. endmntent(mtab);
  190. }
  191. #endif
  192. // Add $HOME.
  193. const char *home = getenv("HOME");
  194. if (home) {
  195. // Only add if it's not a duplicate
  196. String home_name = String::utf8(home);
  197. if (!list->find(home_name)) {
  198. list->push_back(home_name);
  199. }
  200. // Check GTK+3 bookmarks for both XDG locations (Documents, Downloads, etc.)
  201. // and potential user-defined bookmarks.
  202. char path[1024];
  203. snprintf(path, 1024, "%s/.config/gtk-3.0/bookmarks", home);
  204. FILE *fd = fopen(path, "r");
  205. if (fd) {
  206. char string[1024];
  207. while (fgets(string, 1024, fd)) {
  208. // Parse only file:// links
  209. if (strncmp(string, "file://", 7) == 0) {
  210. // Strip any unwanted edges on the strings and push_back if it's not a duplicate.
  211. String fpath = String::utf8(string + 7).strip_edges().split_spaces()[0].uri_decode();
  212. if (!list->find(fpath)) {
  213. list->push_back(fpath);
  214. }
  215. }
  216. }
  217. fclose(fd);
  218. }
  219. // Add Desktop dir.
  220. String dpath = OS::get_singleton()->get_system_dir(OS::SystemDir::SYSTEM_DIR_DESKTOP);
  221. if (dpath.length() > 0 && !list->find(dpath)) {
  222. list->push_back(dpath);
  223. }
  224. }
  225. list->sort();
  226. }
  227. int DirAccessUnix::get_drive_count() {
  228. List<String> list;
  229. _get_drives(&list);
  230. return list.size();
  231. }
  232. String DirAccessUnix::get_drive(int p_drive) {
  233. List<String> list;
  234. _get_drives(&list);
  235. ERR_FAIL_INDEX_V(p_drive, list.size(), "");
  236. return list.get(p_drive);
  237. }
  238. int DirAccessUnix::get_current_drive() {
  239. int drive = 0;
  240. int max_length = -1;
  241. const String path = get_current_dir().to_lower();
  242. for (int i = 0; i < get_drive_count(); i++) {
  243. const String d = get_drive(i).to_lower();
  244. if (max_length < d.length() && path.begins_with(d)) {
  245. max_length = d.length();
  246. drive = i;
  247. }
  248. }
  249. return drive;
  250. }
  251. bool DirAccessUnix::drives_are_shortcuts() {
  252. return true;
  253. }
  254. Error DirAccessUnix::make_dir(String p_dir) {
  255. GLOBAL_LOCK_FUNCTION
  256. if (p_dir.is_relative_path()) {
  257. p_dir = get_current_dir().path_join(p_dir);
  258. }
  259. p_dir = fix_path(p_dir);
  260. bool success = (mkdir(p_dir.utf8().get_data(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) == 0);
  261. int err = errno;
  262. if (success) {
  263. return OK;
  264. }
  265. if (err == EEXIST) {
  266. return ERR_ALREADY_EXISTS;
  267. }
  268. return ERR_CANT_CREATE;
  269. }
  270. Error DirAccessUnix::change_dir(String p_dir) {
  271. GLOBAL_LOCK_FUNCTION
  272. p_dir = fix_path(p_dir);
  273. // prev_dir is the directory we are changing out of
  274. String prev_dir;
  275. char real_current_dir_name[2048];
  276. ERR_FAIL_NULL_V(getcwd(real_current_dir_name, 2048), ERR_BUG);
  277. if (prev_dir.append_utf8(real_current_dir_name) != OK) {
  278. prev_dir = real_current_dir_name; //no utf8, maybe latin?
  279. }
  280. // try_dir is the directory we are trying to change into
  281. String try_dir = "";
  282. if (p_dir.is_relative_path()) {
  283. String next_dir = current_dir.path_join(p_dir);
  284. next_dir = next_dir.simplify_path();
  285. try_dir = next_dir;
  286. } else {
  287. try_dir = p_dir;
  288. }
  289. bool worked = (chdir(try_dir.utf8().get_data()) == 0); // we can only give this utf8
  290. if (!worked) {
  291. return ERR_INVALID_PARAMETER;
  292. }
  293. String base = _get_root_path();
  294. if (!base.is_empty() && !try_dir.begins_with(base)) {
  295. ERR_FAIL_NULL_V(getcwd(real_current_dir_name, 2048), ERR_BUG);
  296. String new_dir;
  297. new_dir.append_utf8(real_current_dir_name);
  298. if (!new_dir.begins_with(base)) {
  299. try_dir = current_dir; //revert
  300. }
  301. }
  302. // the directory exists, so set current_dir to try_dir
  303. current_dir = try_dir;
  304. ERR_FAIL_COND_V(chdir(prev_dir.utf8().get_data()) != 0, ERR_BUG);
  305. return OK;
  306. }
  307. String DirAccessUnix::get_current_dir(bool p_include_drive) const {
  308. String base = _get_root_path();
  309. if (!base.is_empty()) {
  310. String bd = current_dir.replace_first(base, "");
  311. if (bd.begins_with("/")) {
  312. return _get_root_string() + bd.substr(1);
  313. } else {
  314. return _get_root_string() + bd;
  315. }
  316. }
  317. return current_dir;
  318. }
  319. Error DirAccessUnix::rename(String p_path, String p_new_path) {
  320. if (p_path.is_relative_path()) {
  321. p_path = get_current_dir().path_join(p_path);
  322. }
  323. p_path = fix_path(p_path);
  324. if (p_path.ends_with("/")) {
  325. p_path = p_path.left(-1);
  326. }
  327. if (p_new_path.is_relative_path()) {
  328. p_new_path = get_current_dir().path_join(p_new_path);
  329. }
  330. p_new_path = fix_path(p_new_path);
  331. if (p_new_path.ends_with("/")) {
  332. p_new_path = p_new_path.left(-1);
  333. }
  334. int res = ::rename(p_path.utf8().get_data(), p_new_path.utf8().get_data());
  335. if (res != 0 && errno == EXDEV) { // Cross-device move, use copy and remove.
  336. Error err = OK;
  337. err = copy(p_path, p_new_path);
  338. if (err != OK) {
  339. return err;
  340. }
  341. return remove(p_path);
  342. } else {
  343. return (res == 0) ? OK : FAILED;
  344. }
  345. }
  346. Error DirAccessUnix::remove(String p_path) {
  347. if (p_path.is_relative_path()) {
  348. p_path = get_current_dir().path_join(p_path);
  349. }
  350. p_path = fix_path(p_path);
  351. if (p_path.ends_with("/")) {
  352. p_path = p_path.left(-1);
  353. }
  354. struct stat flags = {};
  355. if ((lstat(p_path.utf8().get_data(), &flags) != 0)) {
  356. return FAILED;
  357. }
  358. int err;
  359. if (S_ISDIR(flags.st_mode) && !is_link(p_path)) {
  360. err = ::rmdir(p_path.utf8().get_data());
  361. } else {
  362. err = ::unlink(p_path.utf8().get_data());
  363. }
  364. if (err != 0) {
  365. return FAILED;
  366. }
  367. if (remove_notification_func != nullptr) {
  368. remove_notification_func(p_path);
  369. }
  370. return OK;
  371. }
  372. bool DirAccessUnix::is_link(String p_file) {
  373. if (p_file.is_relative_path()) {
  374. p_file = get_current_dir().path_join(p_file);
  375. }
  376. p_file = fix_path(p_file);
  377. if (p_file.ends_with("/")) {
  378. p_file = p_file.left(-1);
  379. }
  380. struct stat flags = {};
  381. if ((lstat(p_file.utf8().get_data(), &flags) != 0)) {
  382. return false;
  383. }
  384. return S_ISLNK(flags.st_mode);
  385. }
  386. String DirAccessUnix::read_link(String p_file) {
  387. if (p_file.is_relative_path()) {
  388. p_file = get_current_dir().path_join(p_file);
  389. }
  390. p_file = fix_path(p_file);
  391. if (p_file.ends_with("/")) {
  392. p_file = p_file.left(-1);
  393. }
  394. char buf[256];
  395. memset(buf, 0, 256);
  396. ssize_t len = readlink(p_file.utf8().get_data(), buf, sizeof(buf));
  397. String link;
  398. if (len > 0) {
  399. link.append_utf8(buf, len);
  400. }
  401. return link;
  402. }
  403. Error DirAccessUnix::create_link(String p_source, String p_target) {
  404. if (p_target.is_relative_path()) {
  405. p_target = get_current_dir().path_join(p_target);
  406. }
  407. p_source = fix_path(p_source);
  408. p_target = fix_path(p_target);
  409. if (symlink(p_source.utf8().get_data(), p_target.utf8().get_data()) == 0) {
  410. return OK;
  411. } else {
  412. return FAILED;
  413. }
  414. }
  415. uint64_t DirAccessUnix::get_space_left() {
  416. struct statvfs vfs;
  417. if (statvfs(current_dir.utf8().get_data(), &vfs) != 0) {
  418. return 0;
  419. }
  420. return (uint64_t)vfs.f_bavail * (uint64_t)vfs.f_frsize;
  421. }
  422. String DirAccessUnix::get_filesystem_type() const {
  423. return ""; //TODO this should be implemented
  424. }
  425. bool DirAccessUnix::is_hidden(const String &p_name) {
  426. return p_name != "." && p_name != ".." && p_name.begins_with(".");
  427. }
  428. bool DirAccessUnix::is_case_sensitive(const String &p_path) const {
  429. #if defined(LINUXBSD_ENABLED)
  430. String f = p_path;
  431. if (!f.is_absolute_path()) {
  432. f = get_current_dir().path_join(f);
  433. }
  434. f = fix_path(f);
  435. int fd = ::open(f.utf8().get_data(), O_RDONLY | O_NONBLOCK);
  436. if (fd) {
  437. long flags = 0;
  438. if (ioctl(fd, _IOR('f', 1, long), &flags) >= 0) {
  439. ::close(fd);
  440. return !(flags & 0x40000000 /* FS_CASEFOLD_FL */);
  441. }
  442. ::close(fd);
  443. }
  444. #endif
  445. return true;
  446. }
  447. bool DirAccessUnix::is_equivalent(const String &p_path_a, const String &p_path_b) const {
  448. String f1 = fix_path(p_path_a);
  449. struct stat st1 = {};
  450. int err = stat(f1.utf8().get_data(), &st1);
  451. if (err) {
  452. return DirAccess::is_equivalent(p_path_a, p_path_b);
  453. }
  454. String f2 = fix_path(p_path_b);
  455. struct stat st2 = {};
  456. err = stat(f2.utf8().get_data(), &st2);
  457. if (err) {
  458. return DirAccess::is_equivalent(p_path_a, p_path_b);
  459. }
  460. return (st1.st_dev == st2.st_dev) && (st1.st_ino == st2.st_ino);
  461. }
  462. DirAccessUnix::DirAccessUnix() {
  463. dir_stream = nullptr;
  464. _cisdir = false;
  465. /* determine drive count */
  466. // set current directory to an absolute path of the current directory
  467. char real_current_dir_name[2048];
  468. ERR_FAIL_NULL(getcwd(real_current_dir_name, 2048));
  469. current_dir.clear();
  470. if (current_dir.append_utf8(real_current_dir_name) != OK) {
  471. current_dir = real_current_dir_name;
  472. }
  473. change_dir(current_dir);
  474. }
  475. DirAccessUnix::RemoveNotificationFunc DirAccessUnix::remove_notification_func = nullptr;
  476. DirAccessUnix::~DirAccessUnix() {
  477. list_dir_end();
  478. }
  479. #endif // UNIX_ENABLED