file_access_unix.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  1. /**************************************************************************/
  2. /* file_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 "file_access_unix.h"
  31. #if defined(UNIX_ENABLED)
  32. #include "core/os/os.h"
  33. #include "core/string/print_string.h"
  34. #include <fcntl.h>
  35. #include <sys/stat.h>
  36. #include <sys/types.h>
  37. #include <unistd.h>
  38. #include <cerrno>
  39. #if defined(TOOLS_ENABLED)
  40. #include <climits>
  41. #include <cstdlib>
  42. #endif
  43. void FileAccessUnix::check_errors(bool p_write) const {
  44. ERR_FAIL_NULL_MSG(f, "File must be opened before use.");
  45. last_error = OK;
  46. if (ferror(f)) {
  47. if (p_write) {
  48. last_error = ERR_FILE_CANT_WRITE;
  49. } else {
  50. last_error = ERR_FILE_CANT_READ;
  51. }
  52. }
  53. if (!p_write && feof(f)) {
  54. last_error = ERR_FILE_EOF;
  55. }
  56. }
  57. Error FileAccessUnix::open_internal(const String &p_path, int p_mode_flags) {
  58. _close();
  59. path_src = p_path;
  60. path = fix_path(p_path);
  61. //printf("opening %s, %i\n", path.utf8().get_data(), Memory::get_static_mem_usage());
  62. ERR_FAIL_COND_V_MSG(f, ERR_ALREADY_IN_USE, "File is already in use.");
  63. const char *mode_string;
  64. if (p_mode_flags == READ) {
  65. mode_string = "rb";
  66. } else if (p_mode_flags == WRITE) {
  67. mode_string = "wb";
  68. } else if (p_mode_flags == READ_WRITE) {
  69. mode_string = "rb+";
  70. } else if (p_mode_flags == WRITE_READ) {
  71. mode_string = "wb+";
  72. } else {
  73. return ERR_INVALID_PARAMETER;
  74. }
  75. /* pretty much every implementation that uses fopen as primary
  76. backend (unix-compatible mostly) supports utf8 encoding */
  77. //printf("opening %s as %s\n", p_path.utf8().get_data(), path.utf8().get_data());
  78. struct stat st = {};
  79. int err = stat(path.utf8().get_data(), &st);
  80. if (!err) {
  81. switch (st.st_mode & S_IFMT) {
  82. case S_IFLNK:
  83. case S_IFREG:
  84. break;
  85. default:
  86. return ERR_FILE_CANT_OPEN;
  87. }
  88. }
  89. #if defined(TOOLS_ENABLED)
  90. if (p_mode_flags & READ) {
  91. String real_path = get_real_path();
  92. if (real_path != path) {
  93. // Don't warn on symlinks, since they can be used to simply share addons on multiple projects.
  94. if (real_path.to_lower() == path.to_lower()) {
  95. // The File system is case insensitive, but other platforms can be sensitive to it
  96. // To ease cross-platform development, we issue a warning if users try to access
  97. // a file using the wrong case (which *works* on Windows and macOS, but won't on other
  98. // platforms).
  99. WARN_PRINT(vformat("Case mismatch opening requested file '%s', stored as '%s' in the filesystem. This file will not open when exported to other case-sensitive platforms.", path, real_path));
  100. }
  101. }
  102. }
  103. #endif
  104. if (is_backup_save_enabled() && (p_mode_flags == WRITE)) {
  105. // Set save path to the symlink target, not the link itself.
  106. String link;
  107. bool is_link = false;
  108. {
  109. CharString cs = path.utf8();
  110. struct stat lst = {};
  111. if (lstat(cs.get_data(), &lst) == 0) {
  112. is_link = S_ISLNK(lst.st_mode);
  113. }
  114. if (is_link) {
  115. char buf[PATH_MAX];
  116. memset(buf, 0, PATH_MAX);
  117. ssize_t len = readlink(cs.get_data(), buf, sizeof(buf));
  118. if (len > 0) {
  119. link.append_utf8(buf, len);
  120. }
  121. if (!link.is_absolute_path()) {
  122. link = path.get_base_dir().path_join(link);
  123. }
  124. }
  125. }
  126. save_path = is_link ? link : path;
  127. // Create a temporary file in the same directory as the target file.
  128. path = path + "-XXXXXX";
  129. CharString cs = path.utf8();
  130. int fd = mkstemp(cs.ptrw());
  131. if (fd == -1) {
  132. last_error = ERR_FILE_CANT_OPEN;
  133. return last_error;
  134. }
  135. fchmod(fd, 0644);
  136. path = String::utf8(cs.ptr());
  137. f = fdopen(fd, mode_string);
  138. if (f == nullptr) {
  139. // Delete temp file and close descriptor if open failed.
  140. ::unlink(cs.ptr());
  141. ::close(fd);
  142. last_error = ERR_FILE_CANT_OPEN;
  143. return last_error;
  144. }
  145. } else {
  146. f = fopen(path.utf8().get_data(), mode_string);
  147. }
  148. if (f == nullptr) {
  149. switch (errno) {
  150. case ENOENT: {
  151. last_error = ERR_FILE_NOT_FOUND;
  152. } break;
  153. default: {
  154. last_error = ERR_FILE_CANT_OPEN;
  155. } break;
  156. }
  157. return last_error;
  158. }
  159. // Set close on exec to avoid leaking it to subprocesses.
  160. int fd = fileno(f);
  161. if (fd != -1) {
  162. int opts = fcntl(fd, F_GETFD);
  163. fcntl(fd, F_SETFD, opts | FD_CLOEXEC);
  164. }
  165. last_error = OK;
  166. flags = p_mode_flags;
  167. return OK;
  168. }
  169. void FileAccessUnix::_close() {
  170. if (!f) {
  171. return;
  172. }
  173. fclose(f);
  174. f = nullptr;
  175. if (close_notification_func) {
  176. close_notification_func(path, flags);
  177. }
  178. if (!save_path.is_empty()) {
  179. int rename_error = rename(path.utf8().get_data(), save_path.utf8().get_data());
  180. if (rename_error && close_fail_notify) {
  181. close_fail_notify(save_path);
  182. }
  183. save_path = "";
  184. ERR_FAIL_COND(rename_error != 0);
  185. }
  186. }
  187. bool FileAccessUnix::is_open() const {
  188. return (f != nullptr);
  189. }
  190. String FileAccessUnix::get_path() const {
  191. return path_src;
  192. }
  193. String FileAccessUnix::get_path_absolute() const {
  194. return path;
  195. }
  196. #if defined(TOOLS_ENABLED)
  197. String FileAccessUnix::get_real_path() const {
  198. char *resolved_path = ::realpath(path.utf8().get_data(), nullptr);
  199. if (!resolved_path) {
  200. return path;
  201. }
  202. String result;
  203. Error parse_ok = result.append_utf8(resolved_path);
  204. ::free(resolved_path);
  205. if (parse_ok != OK) {
  206. return path;
  207. }
  208. return result.simplify_path();
  209. }
  210. #endif
  211. void FileAccessUnix::seek(uint64_t p_position) {
  212. ERR_FAIL_NULL_MSG(f, "File must be opened before use.");
  213. if (fseeko(f, p_position, SEEK_SET)) {
  214. check_errors();
  215. }
  216. }
  217. void FileAccessUnix::seek_end(int64_t p_position) {
  218. ERR_FAIL_NULL_MSG(f, "File must be opened before use.");
  219. if (fseeko(f, p_position, SEEK_END)) {
  220. check_errors();
  221. }
  222. }
  223. uint64_t FileAccessUnix::get_position() const {
  224. ERR_FAIL_NULL_V_MSG(f, 0, "File must be opened before use.");
  225. int64_t pos = ftello(f);
  226. if (pos < 0) {
  227. check_errors();
  228. ERR_FAIL_V(0);
  229. }
  230. return pos;
  231. }
  232. uint64_t FileAccessUnix::get_length() const {
  233. ERR_FAIL_NULL_V_MSG(f, 0, "File must be opened before use.");
  234. int64_t pos = ftello(f);
  235. ERR_FAIL_COND_V(pos < 0, 0);
  236. ERR_FAIL_COND_V(fseeko(f, 0, SEEK_END), 0);
  237. int64_t size = ftello(f);
  238. ERR_FAIL_COND_V(size < 0, 0);
  239. ERR_FAIL_COND_V(fseeko(f, pos, SEEK_SET), 0);
  240. return size;
  241. }
  242. bool FileAccessUnix::eof_reached() const {
  243. return feof(f);
  244. }
  245. uint64_t FileAccessUnix::get_buffer(uint8_t *p_dst, uint64_t p_length) const {
  246. ERR_FAIL_NULL_V_MSG(f, -1, "File must be opened before use.");
  247. ERR_FAIL_COND_V(!p_dst && p_length > 0, -1);
  248. uint64_t read = fread(p_dst, 1, p_length, f);
  249. check_errors();
  250. return read;
  251. }
  252. Error FileAccessUnix::get_error() const {
  253. return last_error;
  254. }
  255. Error FileAccessUnix::resize(int64_t p_length) {
  256. ERR_FAIL_NULL_V_MSG(f, FAILED, "File must be opened before use.");
  257. int res = ::ftruncate(fileno(f), p_length);
  258. switch (res) {
  259. case 0:
  260. return OK;
  261. case EBADF:
  262. return ERR_FILE_CANT_OPEN;
  263. case EFBIG:
  264. return ERR_OUT_OF_MEMORY;
  265. case EINVAL:
  266. return ERR_INVALID_PARAMETER;
  267. default:
  268. return FAILED;
  269. }
  270. }
  271. void FileAccessUnix::flush() {
  272. ERR_FAIL_NULL_MSG(f, "File must be opened before use.");
  273. fflush(f);
  274. }
  275. bool FileAccessUnix::store_buffer(const uint8_t *p_src, uint64_t p_length) {
  276. ERR_FAIL_NULL_V_MSG(f, false, "File must be opened before use.");
  277. ERR_FAIL_COND_V(!p_src && p_length > 0, false);
  278. bool res = fwrite(p_src, 1, p_length, f) == p_length;
  279. check_errors(true);
  280. return res;
  281. }
  282. bool FileAccessUnix::file_exists(const String &p_path) {
  283. struct stat st = {};
  284. const CharString filename_utf8 = fix_path(p_path).utf8();
  285. // Does the name exist at all?
  286. if (stat(filename_utf8.get_data(), &st)) {
  287. return false;
  288. }
  289. // See if we have access to the file
  290. if (access(filename_utf8.get_data(), F_OK)) {
  291. return false;
  292. }
  293. // See if this is a regular file
  294. switch (st.st_mode & S_IFMT) {
  295. case S_IFLNK:
  296. case S_IFREG:
  297. return true;
  298. default:
  299. return false;
  300. }
  301. }
  302. uint64_t FileAccessUnix::_get_modified_time(const String &p_file) {
  303. String file = fix_path(p_file);
  304. struct stat st = {};
  305. int err = stat(file.utf8().get_data(), &st);
  306. if (!err) {
  307. uint64_t modified_time = 0;
  308. if ((st.st_mode & S_IFMT) == S_IFLNK || (st.st_mode & S_IFMT) == S_IFREG || (st.st_mode & S_IFDIR) == S_IFDIR) {
  309. modified_time = st.st_mtime;
  310. }
  311. #ifdef ANDROID_ENABLED
  312. // Workaround for GH-101007
  313. //FIXME: After saving, all timestamps (st_mtime, st_ctime, st_atime) are set to the same value.
  314. // After exporting or after some time, only 'modified_time' resets to a past timestamp.
  315. uint64_t created_time = st.st_ctime;
  316. if (modified_time < created_time) {
  317. modified_time = created_time;
  318. }
  319. #endif
  320. return modified_time;
  321. } else {
  322. return 0;
  323. }
  324. }
  325. uint64_t FileAccessUnix::_get_access_time(const String &p_file) {
  326. String file = fix_path(p_file);
  327. struct stat st = {};
  328. int err = stat(file.utf8().get_data(), &st);
  329. if (!err) {
  330. if ((st.st_mode & S_IFMT) == S_IFLNK || (st.st_mode & S_IFMT) == S_IFREG || (st.st_mode & S_IFDIR) == S_IFDIR) {
  331. return st.st_atime;
  332. }
  333. }
  334. ERR_FAIL_V_MSG(0, "Failed to get access time for: " + p_file + "");
  335. }
  336. int64_t FileAccessUnix::_get_size(const String &p_file) {
  337. String file = fix_path(p_file);
  338. struct stat st = {};
  339. int err = stat(file.utf8().get_data(), &st);
  340. if (!err) {
  341. if ((st.st_mode & S_IFMT) == S_IFLNK || (st.st_mode & S_IFMT) == S_IFREG) {
  342. return st.st_size;
  343. }
  344. }
  345. ERR_FAIL_V_MSG(-1, "Failed to get size for: " + p_file + "");
  346. }
  347. BitField<FileAccess::UnixPermissionFlags> FileAccessUnix::_get_unix_permissions(const String &p_file) {
  348. String file = fix_path(p_file);
  349. struct stat status = {};
  350. int err = stat(file.utf8().get_data(), &status);
  351. if (!err) {
  352. return status.st_mode & 0xFFF; //only permissions
  353. } else {
  354. ERR_FAIL_V_MSG(0, "Failed to get unix permissions for: " + p_file + ".");
  355. }
  356. }
  357. Error FileAccessUnix::_set_unix_permissions(const String &p_file, BitField<FileAccess::UnixPermissionFlags> p_permissions) {
  358. String file = fix_path(p_file);
  359. int err = chmod(file.utf8().get_data(), p_permissions);
  360. if (!err) {
  361. return OK;
  362. }
  363. return FAILED;
  364. }
  365. bool FileAccessUnix::_get_hidden_attribute(const String &p_file) {
  366. #if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__APPLE__)
  367. String file = fix_path(p_file);
  368. struct stat st = {};
  369. int err = stat(file.utf8().get_data(), &st);
  370. ERR_FAIL_COND_V_MSG(err, false, "Failed to get attributes for: " + p_file);
  371. return (st.st_flags & UF_HIDDEN);
  372. #else
  373. return false;
  374. #endif
  375. }
  376. Error FileAccessUnix::_set_hidden_attribute(const String &p_file, bool p_hidden) {
  377. #if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__APPLE__)
  378. String file = fix_path(p_file);
  379. struct stat st = {};
  380. int err = stat(file.utf8().get_data(), &st);
  381. ERR_FAIL_COND_V_MSG(err, FAILED, "Failed to get attributes for: " + p_file);
  382. if (p_hidden) {
  383. err = chflags(file.utf8().get_data(), st.st_flags | UF_HIDDEN);
  384. } else {
  385. err = chflags(file.utf8().get_data(), st.st_flags & ~UF_HIDDEN);
  386. }
  387. ERR_FAIL_COND_V_MSG(err, FAILED, "Failed to set attributes for: " + p_file);
  388. return OK;
  389. #else
  390. return ERR_UNAVAILABLE;
  391. #endif
  392. }
  393. bool FileAccessUnix::_get_read_only_attribute(const String &p_file) {
  394. #if defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__) || defined(__APPLE__)
  395. String file = fix_path(p_file);
  396. struct stat st = {};
  397. int err = stat(file.utf8().get_data(), &st);
  398. ERR_FAIL_COND_V_MSG(err, false, "Failed to get attributes for: " + p_file);
  399. return st.st_flags & UF_IMMUTABLE;
  400. #else
  401. return false;
  402. #endif
  403. }
  404. Error FileAccessUnix::_set_read_only_attribute(const String &p_file, bool p_ro) {
  405. #if defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__) || defined(__APPLE__)
  406. String file = fix_path(p_file);
  407. struct stat st = {};
  408. int err = stat(file.utf8().get_data(), &st);
  409. ERR_FAIL_COND_V_MSG(err, FAILED, "Failed to get attributes for: " + p_file);
  410. if (p_ro) {
  411. err = chflags(file.utf8().get_data(), st.st_flags | UF_IMMUTABLE);
  412. } else {
  413. err = chflags(file.utf8().get_data(), st.st_flags & ~UF_IMMUTABLE);
  414. }
  415. ERR_FAIL_COND_V_MSG(err, FAILED, "Failed to set attributes for: " + p_file);
  416. return OK;
  417. #else
  418. return ERR_UNAVAILABLE;
  419. #endif
  420. }
  421. void FileAccessUnix::close() {
  422. _close();
  423. }
  424. FileAccessUnix::CloseNotificationFunc FileAccessUnix::close_notification_func = nullptr;
  425. FileAccessUnix::~FileAccessUnix() {
  426. _close();
  427. }
  428. #endif // UNIX_ENABLED