dir_access_unix.cpp 14 KB

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