PageRenderTime 450ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/drivers/unix/dir_access_unix.cpp

https://gitlab.com/godotengine/godot
C++ | 514 lines | 355 code | 105 blank | 54 comment | 101 complexity | 88062f3c0e289a8d6615c8519bef7eca MD5 | raw file
  1. /*************************************************************************/
  2. /* dir_access_unix.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
  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/os/memory.h"
  33. #include "core/string/print_string.h"
  34. #include "core/templates/list.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. Ref<DirAccess> DirAccessUnix::create_fs() {
  46. return memnew(DirAccessUnix);
  47. }
  48. Error DirAccessUnix::list_dir_begin() {
  49. list_dir_end(); //close any previous dir opening!
  50. //char real_current_dir_name[2048]; //is this enough?!
  51. //getcwd(real_current_dir_name,2048);
  52. //chdir(current_path.utf8().get_data());
  53. dir_stream = opendir(current_dir.utf8().get_data());
  54. //chdir(real_current_dir_name);
  55. if (!dir_stream) {
  56. return ERR_CANT_OPEN; //error!
  57. }
  58. return OK;
  59. }
  60. bool DirAccessUnix::file_exists(String p_file) {
  61. GLOBAL_LOCK_FUNCTION
  62. if (p_file.is_relative_path()) {
  63. p_file = current_dir.plus_file(p_file);
  64. }
  65. p_file = fix_path(p_file);
  66. struct stat flags;
  67. bool success = (stat(p_file.utf8().get_data(), &flags) == 0);
  68. if (success && S_ISDIR(flags.st_mode)) {
  69. success = false;
  70. }
  71. return success;
  72. }
  73. bool DirAccessUnix::dir_exists(String p_dir) {
  74. GLOBAL_LOCK_FUNCTION
  75. if (p_dir.is_relative_path()) {
  76. p_dir = get_current_dir().plus_file(p_dir);
  77. }
  78. p_dir = fix_path(p_dir);
  79. struct stat flags;
  80. bool success = (stat(p_dir.utf8().get_data(), &flags) == 0);
  81. return (success && S_ISDIR(flags.st_mode));
  82. }
  83. bool DirAccessUnix::is_readable(String p_dir) {
  84. GLOBAL_LOCK_FUNCTION
  85. if (p_dir.is_relative_path()) {
  86. p_dir = get_current_dir().plus_file(p_dir);
  87. }
  88. p_dir = fix_path(p_dir);
  89. return (access(p_dir.utf8().get_data(), R_OK) == 0);
  90. }
  91. bool DirAccessUnix::is_writable(String p_dir) {
  92. GLOBAL_LOCK_FUNCTION
  93. if (p_dir.is_relative_path()) {
  94. p_dir = get_current_dir().plus_file(p_dir);
  95. }
  96. p_dir = fix_path(p_dir);
  97. return (access(p_dir.utf8().get_data(), W_OK) == 0);
  98. }
  99. uint64_t DirAccessUnix::get_modified_time(String p_file) {
  100. if (p_file.is_relative_path()) {
  101. p_file = current_dir.plus_file(p_file);
  102. }
  103. p_file = fix_path(p_file);
  104. struct stat flags;
  105. bool success = (stat(p_file.utf8().get_data(), &flags) == 0);
  106. if (success) {
  107. return flags.st_mtime;
  108. } else {
  109. ERR_FAIL_V(0);
  110. }
  111. return 0;
  112. }
  113. String DirAccessUnix::get_next() {
  114. if (!dir_stream) {
  115. return "";
  116. }
  117. dirent *entry = readdir(dir_stream);
  118. if (entry == nullptr) {
  119. list_dir_end();
  120. return "";
  121. }
  122. String fname = fix_unicode_name(entry->d_name);
  123. // Look at d_type to determine if the entry is a directory, unless
  124. // its type is unknown (the file system does not support it) or if
  125. // the type is a link, in that case we want to resolve the link to
  126. // known if it points to a directory. stat() will resolve the link
  127. // for us.
  128. if (entry->d_type == DT_UNKNOWN || entry->d_type == DT_LNK) {
  129. String f = current_dir.plus_file(fname);
  130. struct stat flags;
  131. if (stat(f.utf8().get_data(), &flags) == 0) {
  132. _cisdir = S_ISDIR(flags.st_mode);
  133. } else {
  134. _cisdir = false;
  135. }
  136. } else {
  137. _cisdir = (entry->d_type == DT_DIR);
  138. }
  139. _cishidden = is_hidden(fname);
  140. return fname;
  141. }
  142. bool DirAccessUnix::current_is_dir() const {
  143. return _cisdir;
  144. }
  145. bool DirAccessUnix::current_is_hidden() const {
  146. return _cishidden;
  147. }
  148. void DirAccessUnix::list_dir_end() {
  149. if (dir_stream) {
  150. closedir(dir_stream);
  151. }
  152. dir_stream = nullptr;
  153. _cisdir = false;
  154. }
  155. #if defined(HAVE_MNTENT) && defined(X11_ENABLED)
  156. static bool _filter_drive(struct mntent *mnt) {
  157. // Ignore devices that don't point to /dev
  158. if (strncmp(mnt->mnt_fsname, "/dev", 4) != 0) {
  159. return false;
  160. }
  161. // Accept devices mounted at common locations
  162. if (strncmp(mnt->mnt_dir, "/media", 6) == 0 ||
  163. strncmp(mnt->mnt_dir, "/mnt", 4) == 0 ||
  164. strncmp(mnt->mnt_dir, "/home", 5) == 0 ||
  165. strncmp(mnt->mnt_dir, "/run/media", 10) == 0) {
  166. return true;
  167. }
  168. // Ignore everything else
  169. return false;
  170. }
  171. #endif
  172. static void _get_drives(List<String> *list) {
  173. list->push_back("/");
  174. #if defined(HAVE_MNTENT) && defined(X11_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 $HOME/.config/gtk-3.0/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. }
  219. list->sort();
  220. }
  221. int DirAccessUnix::get_drive_count() {
  222. List<String> list;
  223. _get_drives(&list);
  224. return list.size();
  225. }
  226. String DirAccessUnix::get_drive(int p_drive) {
  227. List<String> list;
  228. _get_drives(&list);
  229. ERR_FAIL_INDEX_V(p_drive, list.size(), "");
  230. return list[p_drive];
  231. }
  232. int DirAccessUnix::get_current_drive() {
  233. int drive = 0;
  234. int max_length = -1;
  235. const String path = get_current_dir().to_lower();
  236. for (int i = 0; i < get_drive_count(); i++) {
  237. const String d = get_drive(i).to_lower();
  238. if (max_length < d.length() && path.begins_with(d)) {
  239. max_length = d.length();
  240. drive = i;
  241. }
  242. }
  243. return drive;
  244. }
  245. bool DirAccessUnix::drives_are_shortcuts() {
  246. return true;
  247. }
  248. Error DirAccessUnix::make_dir(String p_dir) {
  249. GLOBAL_LOCK_FUNCTION
  250. if (p_dir.is_relative_path()) {
  251. p_dir = get_current_dir().plus_file(p_dir);
  252. }
  253. p_dir = fix_path(p_dir);
  254. bool success = (mkdir(p_dir.utf8().get_data(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) == 0);
  255. int err = errno;
  256. if (success) {
  257. return OK;
  258. }
  259. if (err == EEXIST) {
  260. return ERR_ALREADY_EXISTS;
  261. }
  262. return ERR_CANT_CREATE;
  263. }
  264. Error DirAccessUnix::change_dir(String p_dir) {
  265. GLOBAL_LOCK_FUNCTION
  266. p_dir = fix_path(p_dir);
  267. // prev_dir is the directory we are changing out of
  268. String prev_dir;
  269. char real_current_dir_name[2048];
  270. ERR_FAIL_COND_V(getcwd(real_current_dir_name, 2048) == nullptr, ERR_BUG);
  271. if (prev_dir.parse_utf8(real_current_dir_name)) {
  272. prev_dir = real_current_dir_name; //no utf8, maybe latin?
  273. }
  274. // try_dir is the directory we are trying to change into
  275. String try_dir = "";
  276. if (p_dir.is_relative_path()) {
  277. String next_dir = current_dir.plus_file(p_dir);
  278. next_dir = next_dir.simplify_path();
  279. try_dir = next_dir;
  280. } else {
  281. try_dir = p_dir;
  282. }
  283. bool worked = (chdir(try_dir.utf8().get_data()) == 0); // we can only give this utf8
  284. if (!worked) {
  285. return ERR_INVALID_PARAMETER;
  286. }
  287. String base = _get_root_path();
  288. if (!base.is_empty() && !try_dir.begins_with(base)) {
  289. ERR_FAIL_COND_V(getcwd(real_current_dir_name, 2048) == nullptr, ERR_BUG);
  290. String new_dir;
  291. new_dir.parse_utf8(real_current_dir_name);
  292. if (!new_dir.begins_with(base)) {
  293. try_dir = current_dir; //revert
  294. }
  295. }
  296. // the directory exists, so set current_dir to try_dir
  297. current_dir = try_dir;
  298. ERR_FAIL_COND_V(chdir(prev_dir.utf8().get_data()) != 0, ERR_BUG);
  299. return OK;
  300. }
  301. String DirAccessUnix::get_current_dir(bool p_include_drive) const {
  302. String base = _get_root_path();
  303. if (!base.is_empty()) {
  304. String bd = current_dir.replace_first(base, "");
  305. if (bd.begins_with("/")) {
  306. return _get_root_string() + bd.substr(1, bd.length());
  307. } else {
  308. return _get_root_string() + bd;
  309. }
  310. }
  311. return current_dir;
  312. }
  313. Error DirAccessUnix::rename(String p_path, String p_new_path) {
  314. if (p_path.is_relative_path()) {
  315. p_path = get_current_dir().plus_file(p_path);
  316. }
  317. p_path = fix_path(p_path);
  318. if (p_new_path.is_relative_path()) {
  319. p_new_path = get_current_dir().plus_file(p_new_path);
  320. }
  321. p_new_path = fix_path(p_new_path);
  322. return ::rename(p_path.utf8().get_data(), p_new_path.utf8().get_data()) == 0 ? OK : FAILED;
  323. }
  324. Error DirAccessUnix::remove(String p_path) {
  325. if (p_path.is_relative_path()) {
  326. p_path = get_current_dir().plus_file(p_path);
  327. }
  328. p_path = fix_path(p_path);
  329. struct stat flags;
  330. if ((stat(p_path.utf8().get_data(), &flags) != 0)) {
  331. return FAILED;
  332. }
  333. if (S_ISDIR(flags.st_mode)) {
  334. return ::rmdir(p_path.utf8().get_data()) == 0 ? OK : FAILED;
  335. } else {
  336. return ::unlink(p_path.utf8().get_data()) == 0 ? OK : FAILED;
  337. }
  338. }
  339. bool DirAccessUnix::is_link(String p_file) {
  340. if (p_file.is_relative_path()) {
  341. p_file = get_current_dir().plus_file(p_file);
  342. }
  343. p_file = fix_path(p_file);
  344. struct stat flags;
  345. if ((lstat(p_file.utf8().get_data(), &flags) != 0)) {
  346. return FAILED;
  347. }
  348. return S_ISLNK(flags.st_mode);
  349. }
  350. String DirAccessUnix::read_link(String p_file) {
  351. if (p_file.is_relative_path()) {
  352. p_file = get_current_dir().plus_file(p_file);
  353. }
  354. p_file = fix_path(p_file);
  355. char buf[256];
  356. memset(buf, 0, 256);
  357. ssize_t len = readlink(p_file.utf8().get_data(), buf, sizeof(buf));
  358. String link;
  359. if (len > 0) {
  360. link.parse_utf8(buf, len);
  361. }
  362. return link;
  363. }
  364. Error DirAccessUnix::create_link(String p_source, String p_target) {
  365. if (p_target.is_relative_path()) {
  366. p_target = get_current_dir().plus_file(p_target);
  367. }
  368. p_source = fix_path(p_source);
  369. p_target = fix_path(p_target);
  370. if (symlink(p_source.utf8().get_data(), p_target.utf8().get_data()) == 0) {
  371. return OK;
  372. } else {
  373. return FAILED;
  374. }
  375. }
  376. uint64_t DirAccessUnix::get_space_left() {
  377. #ifndef NO_STATVFS
  378. struct statvfs vfs;
  379. if (statvfs(current_dir.utf8().get_data(), &vfs) != 0) {
  380. return 0;
  381. }
  382. return (uint64_t)vfs.f_bavail * (uint64_t)vfs.f_frsize;
  383. #else
  384. // FIXME: Implement this.
  385. return 0;
  386. #endif
  387. }
  388. String DirAccessUnix::get_filesystem_type() const {
  389. return ""; //TODO this should be implemented
  390. }
  391. bool DirAccessUnix::is_hidden(const String &p_name) {
  392. return p_name != "." && p_name != ".." && p_name.begins_with(".");
  393. }
  394. DirAccessUnix::DirAccessUnix() {
  395. dir_stream = nullptr;
  396. _cisdir = false;
  397. /* determine drive count */
  398. // set current directory to an absolute path of the current directory
  399. char real_current_dir_name[2048];
  400. ERR_FAIL_COND(getcwd(real_current_dir_name, 2048) == nullptr);
  401. if (current_dir.parse_utf8(real_current_dir_name)) {
  402. current_dir = real_current_dir_name;
  403. }
  404. change_dir(current_dir);
  405. }
  406. DirAccessUnix::~DirAccessUnix() {
  407. list_dir_end();
  408. }
  409. #endif //posix_enabled