file_access_unix.cpp 12 KB

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