dir_access_unix.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  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) || defined(LIBC_FILEIO_ENABLED)
  32. #include "core/list.h"
  33. #include "core/os/memory.h"
  34. #include "core/print_string.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_rel_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_rel_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. uint64_t DirAccessUnix::get_modified_time(String p_file) {
  81. if (p_file.is_rel_path()) {
  82. p_file = current_dir.plus_file(p_file);
  83. }
  84. p_file = fix_path(p_file);
  85. struct stat flags;
  86. bool success = (stat(p_file.utf8().get_data(), &flags) == 0);
  87. if (success) {
  88. return flags.st_mtime;
  89. } else {
  90. ERR_FAIL_V(0);
  91. };
  92. return 0;
  93. };
  94. String DirAccessUnix::get_next() {
  95. if (!dir_stream) {
  96. return "";
  97. }
  98. dirent *entry = readdir(dir_stream);
  99. if (entry == nullptr) {
  100. list_dir_end();
  101. return "";
  102. }
  103. String fname = fix_unicode_name(entry->d_name);
  104. // Look at d_type to determine if the entry is a directory, unless
  105. // its type is unknown (the file system does not support it) or if
  106. // the type is a link, in that case we want to resolve the link to
  107. // known if it points to a directory. stat() will resolve the link
  108. // for us.
  109. if (entry->d_type == DT_UNKNOWN || entry->d_type == DT_LNK) {
  110. String f = current_dir.plus_file(fname);
  111. struct stat flags;
  112. if (stat(f.utf8().get_data(), &flags) == 0) {
  113. _cisdir = S_ISDIR(flags.st_mode);
  114. } else {
  115. _cisdir = false;
  116. }
  117. } else {
  118. _cisdir = (entry->d_type == DT_DIR);
  119. }
  120. _cishidden = is_hidden(fname);
  121. return fname;
  122. }
  123. bool DirAccessUnix::current_is_dir() const {
  124. return _cisdir;
  125. }
  126. bool DirAccessUnix::current_is_hidden() const {
  127. return _cishidden;
  128. }
  129. void DirAccessUnix::list_dir_end() {
  130. if (dir_stream) {
  131. closedir(dir_stream);
  132. }
  133. dir_stream = nullptr;
  134. _cisdir = false;
  135. }
  136. #if defined(HAVE_MNTENT) && defined(X11_ENABLED)
  137. static bool _filter_drive(struct mntent *mnt) {
  138. // Ignore devices that don't point to /dev
  139. if (strncmp(mnt->mnt_fsname, "/dev", 4) != 0) {
  140. return false;
  141. }
  142. // Accept devices mounted at common locations
  143. if (strncmp(mnt->mnt_dir, "/media", 6) == 0 ||
  144. strncmp(mnt->mnt_dir, "/mnt", 4) == 0 ||
  145. strncmp(mnt->mnt_dir, "/home", 5) == 0 ||
  146. strncmp(mnt->mnt_dir, "/run/media", 10) == 0) {
  147. return true;
  148. }
  149. // Ignore everything else
  150. return false;
  151. }
  152. #endif
  153. static void _get_drives(List<String> *list) {
  154. list->push_back("/");
  155. #if defined(HAVE_MNTENT) && defined(X11_ENABLED)
  156. // Check /etc/mtab for the list of mounted partitions
  157. FILE *mtab = setmntent("/etc/mtab", "r");
  158. if (mtab) {
  159. struct mntent mnt;
  160. char strings[4096];
  161. while (getmntent_r(mtab, &mnt, strings, sizeof(strings))) {
  162. if (mnt.mnt_dir != nullptr && _filter_drive(&mnt)) {
  163. // Avoid duplicates
  164. if (!list->find(mnt.mnt_dir)) {
  165. list->push_back(mnt.mnt_dir);
  166. }
  167. }
  168. }
  169. endmntent(mtab);
  170. }
  171. #endif
  172. // Add $HOME
  173. const char *home = getenv("HOME");
  174. if (home) {
  175. // Only add if it's not a duplicate
  176. if (!list->find(home)) {
  177. list->push_back(home);
  178. }
  179. // Check $HOME/.config/gtk-3.0/bookmarks
  180. char path[1024];
  181. snprintf(path, 1024, "%s/.config/gtk-3.0/bookmarks", home);
  182. FILE *fd = fopen(path, "r");
  183. if (fd) {
  184. char string[1024];
  185. while (fgets(string, 1024, fd)) {
  186. // Parse only file:// links
  187. if (strncmp(string, "file://", 7) == 0) {
  188. // Strip any unwanted edges on the strings and push_back if it's not a duplicate
  189. String fpath = String(string + 7).strip_edges().split_spaces()[0].percent_decode();
  190. if (!list->find(fpath)) {
  191. list->push_back(fpath);
  192. }
  193. }
  194. }
  195. fclose(fd);
  196. }
  197. }
  198. list->sort();
  199. }
  200. int DirAccessUnix::get_drive_count() {
  201. List<String> list;
  202. _get_drives(&list);
  203. return list.size();
  204. }
  205. String DirAccessUnix::get_drive(int p_drive) {
  206. List<String> list;
  207. _get_drives(&list);
  208. ERR_FAIL_INDEX_V(p_drive, list.size(), "");
  209. return list[p_drive];
  210. }
  211. int DirAccessUnix::get_current_drive() {
  212. int drive = 0;
  213. int max_length = -1;
  214. const String path = get_current_dir().to_lower();
  215. for (int i = 0; i < get_drive_count(); i++) {
  216. const String d = get_drive(i).to_lower();
  217. if (max_length < d.length() && path.begins_with(d)) {
  218. max_length = d.length();
  219. drive = i;
  220. }
  221. }
  222. return drive;
  223. }
  224. bool DirAccessUnix::drives_are_shortcuts() {
  225. return true;
  226. }
  227. Error DirAccessUnix::make_dir(String p_dir) {
  228. GLOBAL_LOCK_FUNCTION
  229. if (p_dir.is_rel_path()) {
  230. p_dir = get_current_dir().plus_file(p_dir);
  231. }
  232. p_dir = fix_path(p_dir);
  233. bool success = (mkdir(p_dir.utf8().get_data(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) == 0);
  234. int err = errno;
  235. if (success) {
  236. return OK;
  237. };
  238. if (err == EEXIST) {
  239. return ERR_ALREADY_EXISTS;
  240. };
  241. return ERR_CANT_CREATE;
  242. }
  243. Error DirAccessUnix::change_dir(String p_dir) {
  244. GLOBAL_LOCK_FUNCTION
  245. p_dir = fix_path(p_dir);
  246. // prev_dir is the directory we are changing out of
  247. String prev_dir;
  248. char real_current_dir_name[2048];
  249. ERR_FAIL_COND_V(getcwd(real_current_dir_name, 2048) == nullptr, ERR_BUG);
  250. if (prev_dir.parse_utf8(real_current_dir_name)) {
  251. prev_dir = real_current_dir_name; //no utf8, maybe latin?
  252. }
  253. // try_dir is the directory we are trying to change into
  254. String try_dir = "";
  255. if (p_dir.is_rel_path()) {
  256. String next_dir = current_dir.plus_file(p_dir);
  257. next_dir = next_dir.simplify_path();
  258. try_dir = next_dir;
  259. } else {
  260. try_dir = p_dir;
  261. }
  262. bool worked = (chdir(try_dir.utf8().get_data()) == 0); // we can only give this utf8
  263. if (!worked) {
  264. return ERR_INVALID_PARAMETER;
  265. }
  266. String base = _get_root_path();
  267. if (base != String() && !try_dir.begins_with(base)) {
  268. ERR_FAIL_COND_V(getcwd(real_current_dir_name, 2048) == nullptr, ERR_BUG);
  269. String new_dir;
  270. new_dir.parse_utf8(real_current_dir_name);
  271. if (!new_dir.begins_with(base)) {
  272. try_dir = current_dir; //revert
  273. }
  274. }
  275. // the directory exists, so set current_dir to try_dir
  276. current_dir = try_dir;
  277. ERR_FAIL_COND_V(chdir(prev_dir.utf8().get_data()) != 0, ERR_BUG);
  278. return OK;
  279. }
  280. String DirAccessUnix::get_current_dir() {
  281. String base = _get_root_path();
  282. if (base != "") {
  283. String bd = current_dir.replace_first(base, "");
  284. if (bd.begins_with("/")) {
  285. return _get_root_string() + bd.substr(1, bd.length());
  286. } else {
  287. return _get_root_string() + bd;
  288. }
  289. }
  290. return current_dir;
  291. }
  292. Error DirAccessUnix::rename(String p_path, String p_new_path) {
  293. if (p_path.is_rel_path()) {
  294. p_path = get_current_dir().plus_file(p_path);
  295. }
  296. p_path = fix_path(p_path);
  297. if (p_new_path.is_rel_path()) {
  298. p_new_path = get_current_dir().plus_file(p_new_path);
  299. }
  300. p_new_path = fix_path(p_new_path);
  301. return ::rename(p_path.utf8().get_data(), p_new_path.utf8().get_data()) == 0 ? OK : FAILED;
  302. }
  303. Error DirAccessUnix::remove(String p_path) {
  304. if (p_path.is_rel_path()) {
  305. p_path = get_current_dir().plus_file(p_path);
  306. }
  307. p_path = fix_path(p_path);
  308. struct stat flags;
  309. if ((stat(p_path.utf8().get_data(), &flags) != 0)) {
  310. return FAILED;
  311. }
  312. if (S_ISDIR(flags.st_mode)) {
  313. return ::rmdir(p_path.utf8().get_data()) == 0 ? OK : FAILED;
  314. } else {
  315. return ::unlink(p_path.utf8().get_data()) == 0 ? OK : FAILED;
  316. }
  317. }
  318. bool DirAccessUnix::is_link(String p_file) {
  319. if (p_file.is_rel_path())
  320. p_file = get_current_dir().plus_file(p_file);
  321. p_file = fix_path(p_file);
  322. struct stat flags;
  323. if ((lstat(p_file.utf8().get_data(), &flags) != 0))
  324. return FAILED;
  325. return S_ISLNK(flags.st_mode);
  326. }
  327. String DirAccessUnix::read_link(String p_file) {
  328. if (p_file.is_rel_path())
  329. p_file = get_current_dir().plus_file(p_file);
  330. p_file = fix_path(p_file);
  331. char buf[256];
  332. memset(buf, 0, 256);
  333. ssize_t len = readlink(p_file.utf8().get_data(), buf, sizeof(buf));
  334. String link;
  335. if (len > 0) {
  336. link.parse_utf8(buf, len);
  337. }
  338. return link;
  339. }
  340. Error DirAccessUnix::create_link(String p_source, String p_target) {
  341. if (p_target.is_rel_path())
  342. p_target = get_current_dir().plus_file(p_target);
  343. p_source = fix_path(p_source);
  344. p_target = fix_path(p_target);
  345. if (symlink(p_source.utf8().get_data(), p_target.utf8().get_data()) == 0) {
  346. return OK;
  347. } else {
  348. return FAILED;
  349. }
  350. }
  351. uint64_t DirAccessUnix::get_space_left() {
  352. #ifndef NO_STATVFS
  353. struct statvfs vfs;
  354. if (statvfs(current_dir.utf8().get_data(), &vfs) != 0) {
  355. return 0;
  356. };
  357. return (uint64_t)vfs.f_bavail * (uint64_t)vfs.f_frsize;
  358. #else
  359. // FIXME: Implement this.
  360. return 0;
  361. #endif
  362. };
  363. String DirAccessUnix::get_filesystem_type() const {
  364. return ""; //TODO this should be implemented
  365. }
  366. bool DirAccessUnix::is_hidden(const String &p_name) {
  367. return p_name != "." && p_name != ".." && p_name.begins_with(".");
  368. }
  369. DirAccessUnix::DirAccessUnix() {
  370. dir_stream = nullptr;
  371. _cisdir = false;
  372. /* determine drive count */
  373. // set current directory to an absolute path of the current directory
  374. char real_current_dir_name[2048];
  375. ERR_FAIL_COND(getcwd(real_current_dir_name, 2048) == nullptr);
  376. if (current_dir.parse_utf8(real_current_dir_name)) {
  377. current_dir = real_current_dir_name;
  378. }
  379. change_dir(current_dir);
  380. }
  381. DirAccessUnix::~DirAccessUnix() {
  382. list_dir_end();
  383. }
  384. #endif // UNIX_ENABLED || LIBC_FILEIO_ENABLED