2
0

dir_access_unix.cpp 13 KB

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