file_access_unix.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  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(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. save_path = path;
  106. // Create a temporary file in the same directory as the target file.
  107. path = path + "-XXXXXX";
  108. CharString cs = path.utf8();
  109. int fd = mkstemp(cs.ptrw());
  110. if (fd == -1) {
  111. last_error = ERR_FILE_CANT_OPEN;
  112. return last_error;
  113. }
  114. fchmod(fd, 0644);
  115. path = String::utf8(cs.ptr());
  116. f = fdopen(fd, mode_string);
  117. if (f == nullptr) {
  118. // Delete temp file and close descriptor if open failed.
  119. ::unlink(cs.ptr());
  120. ::close(fd);
  121. last_error = ERR_FILE_CANT_OPEN;
  122. return last_error;
  123. }
  124. } else {
  125. f = fopen(path.utf8().get_data(), mode_string);
  126. }
  127. if (f == nullptr) {
  128. switch (errno) {
  129. case ENOENT: {
  130. last_error = ERR_FILE_NOT_FOUND;
  131. } break;
  132. default: {
  133. last_error = ERR_FILE_CANT_OPEN;
  134. } break;
  135. }
  136. return last_error;
  137. }
  138. // Set close on exec to avoid leaking it to subprocesses.
  139. int fd = fileno(f);
  140. if (fd != -1) {
  141. int opts = fcntl(fd, F_GETFD);
  142. fcntl(fd, F_SETFD, opts | FD_CLOEXEC);
  143. }
  144. last_error = OK;
  145. flags = p_mode_flags;
  146. return OK;
  147. }
  148. void FileAccessUnix::_close() {
  149. if (!f) {
  150. return;
  151. }
  152. fclose(f);
  153. f = nullptr;
  154. if (close_notification_func) {
  155. close_notification_func(path, flags);
  156. }
  157. if (!save_path.is_empty()) {
  158. int rename_error = rename(path.utf8().get_data(), save_path.utf8().get_data());
  159. if (rename_error && close_fail_notify) {
  160. close_fail_notify(save_path);
  161. }
  162. save_path = "";
  163. ERR_FAIL_COND(rename_error != 0);
  164. }
  165. }
  166. bool FileAccessUnix::is_open() const {
  167. return (f != nullptr);
  168. }
  169. String FileAccessUnix::get_path() const {
  170. return path_src;
  171. }
  172. String FileAccessUnix::get_path_absolute() const {
  173. return path;
  174. }
  175. #if defined(TOOLS_ENABLED)
  176. String FileAccessUnix::get_real_path() const {
  177. char *resolved_path = ::realpath(path.utf8().get_data(), nullptr);
  178. if (!resolved_path) {
  179. return path;
  180. }
  181. String result;
  182. Error parse_ok = result.parse_utf8(resolved_path);
  183. ::free(resolved_path);
  184. if (parse_ok != OK) {
  185. return path;
  186. }
  187. return result.simplify_path();
  188. }
  189. #endif
  190. void FileAccessUnix::seek(uint64_t p_position) {
  191. ERR_FAIL_NULL_MSG(f, "File must be opened before use.");
  192. if (fseeko(f, p_position, SEEK_SET)) {
  193. check_errors();
  194. }
  195. }
  196. void FileAccessUnix::seek_end(int64_t p_position) {
  197. ERR_FAIL_NULL_MSG(f, "File must be opened before use.");
  198. if (fseeko(f, p_position, SEEK_END)) {
  199. check_errors();
  200. }
  201. }
  202. uint64_t FileAccessUnix::get_position() const {
  203. ERR_FAIL_NULL_V_MSG(f, 0, "File must be opened before use.");
  204. int64_t pos = ftello(f);
  205. if (pos < 0) {
  206. check_errors();
  207. ERR_FAIL_V(0);
  208. }
  209. return pos;
  210. }
  211. uint64_t FileAccessUnix::get_length() const {
  212. ERR_FAIL_NULL_V_MSG(f, 0, "File must be opened before use.");
  213. int64_t pos = ftello(f);
  214. ERR_FAIL_COND_V(pos < 0, 0);
  215. ERR_FAIL_COND_V(fseeko(f, 0, SEEK_END), 0);
  216. int64_t size = ftello(f);
  217. ERR_FAIL_COND_V(size < 0, 0);
  218. ERR_FAIL_COND_V(fseeko(f, pos, SEEK_SET), 0);
  219. return size;
  220. }
  221. bool FileAccessUnix::eof_reached() const {
  222. return feof(f);
  223. }
  224. uint64_t FileAccessUnix::get_buffer(uint8_t *p_dst, uint64_t p_length) const {
  225. ERR_FAIL_NULL_V_MSG(f, -1, "File must be opened before use.");
  226. ERR_FAIL_COND_V(!p_dst && p_length > 0, -1);
  227. uint64_t read = fread(p_dst, 1, p_length, f);
  228. check_errors();
  229. return read;
  230. }
  231. Error FileAccessUnix::get_error() const {
  232. return last_error;
  233. }
  234. Error FileAccessUnix::resize(int64_t p_length) {
  235. ERR_FAIL_NULL_V_MSG(f, FAILED, "File must be opened before use.");
  236. int res = ::ftruncate(fileno(f), p_length);
  237. switch (res) {
  238. case 0:
  239. return OK;
  240. case EBADF:
  241. return ERR_FILE_CANT_OPEN;
  242. case EFBIG:
  243. return ERR_OUT_OF_MEMORY;
  244. case EINVAL:
  245. return ERR_INVALID_PARAMETER;
  246. default:
  247. return FAILED;
  248. }
  249. }
  250. void FileAccessUnix::flush() {
  251. ERR_FAIL_NULL_MSG(f, "File must be opened before use.");
  252. fflush(f);
  253. }
  254. bool FileAccessUnix::store_buffer(const uint8_t *p_src, uint64_t p_length) {
  255. ERR_FAIL_NULL_V_MSG(f, false, "File must be opened before use.");
  256. ERR_FAIL_COND_V(!p_src && p_length > 0, false);
  257. bool res = fwrite(p_src, 1, p_length, f) == p_length;
  258. check_errors(true);
  259. return res;
  260. }
  261. bool FileAccessUnix::file_exists(const String &p_path) {
  262. struct stat st = {};
  263. const CharString filename_utf8 = fix_path(p_path).utf8();
  264. // Does the name exist at all?
  265. if (stat(filename_utf8.get_data(), &st)) {
  266. return false;
  267. }
  268. // See if we have access to the file
  269. if (access(filename_utf8.get_data(), F_OK)) {
  270. return false;
  271. }
  272. // See if this is a regular file
  273. switch (st.st_mode & S_IFMT) {
  274. case S_IFLNK:
  275. case S_IFREG:
  276. return true;
  277. default:
  278. return false;
  279. }
  280. }
  281. uint64_t FileAccessUnix::_get_modified_time(const String &p_file) {
  282. String file = fix_path(p_file);
  283. struct stat status = {};
  284. int err = stat(file.utf8().get_data(), &status);
  285. if (!err) {
  286. return status.st_mtime;
  287. } else {
  288. return 0;
  289. }
  290. }
  291. BitField<FileAccess::UnixPermissionFlags> FileAccessUnix::_get_unix_permissions(const String &p_file) {
  292. String file = fix_path(p_file);
  293. struct stat status = {};
  294. int err = stat(file.utf8().get_data(), &status);
  295. if (!err) {
  296. return status.st_mode & 0xFFF; //only permissions
  297. } else {
  298. ERR_FAIL_V_MSG(0, "Failed to get unix permissions for: " + p_file + ".");
  299. }
  300. }
  301. Error FileAccessUnix::_set_unix_permissions(const String &p_file, BitField<FileAccess::UnixPermissionFlags> p_permissions) {
  302. String file = fix_path(p_file);
  303. int err = chmod(file.utf8().get_data(), p_permissions);
  304. if (!err) {
  305. return OK;
  306. }
  307. return FAILED;
  308. }
  309. bool FileAccessUnix::_get_hidden_attribute(const String &p_file) {
  310. #if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__APPLE__)
  311. String file = fix_path(p_file);
  312. struct stat st = {};
  313. int err = stat(file.utf8().get_data(), &st);
  314. ERR_FAIL_COND_V_MSG(err, false, "Failed to get attributes for: " + p_file);
  315. return (st.st_flags & UF_HIDDEN);
  316. #else
  317. return false;
  318. #endif
  319. }
  320. Error FileAccessUnix::_set_hidden_attribute(const String &p_file, bool p_hidden) {
  321. #if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__APPLE__)
  322. String file = fix_path(p_file);
  323. struct stat st = {};
  324. int err = stat(file.utf8().get_data(), &st);
  325. ERR_FAIL_COND_V_MSG(err, FAILED, "Failed to get attributes for: " + p_file);
  326. if (p_hidden) {
  327. err = chflags(file.utf8().get_data(), st.st_flags | UF_HIDDEN);
  328. } else {
  329. err = chflags(file.utf8().get_data(), st.st_flags & ~UF_HIDDEN);
  330. }
  331. ERR_FAIL_COND_V_MSG(err, FAILED, "Failed to set attributes for: " + p_file);
  332. return OK;
  333. #else
  334. return ERR_UNAVAILABLE;
  335. #endif
  336. }
  337. bool FileAccessUnix::_get_read_only_attribute(const String &p_file) {
  338. #if defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__) || defined(__APPLE__)
  339. String file = fix_path(p_file);
  340. struct stat st = {};
  341. int err = stat(file.utf8().get_data(), &st);
  342. ERR_FAIL_COND_V_MSG(err, false, "Failed to get attributes for: " + p_file);
  343. return st.st_flags & UF_IMMUTABLE;
  344. #else
  345. return false;
  346. #endif
  347. }
  348. Error FileAccessUnix::_set_read_only_attribute(const String &p_file, bool p_ro) {
  349. #if defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__) || defined(__APPLE__)
  350. String file = fix_path(p_file);
  351. struct stat st = {};
  352. int err = stat(file.utf8().get_data(), &st);
  353. ERR_FAIL_COND_V_MSG(err, FAILED, "Failed to get attributes for: " + p_file);
  354. if (p_ro) {
  355. err = chflags(file.utf8().get_data(), st.st_flags | UF_IMMUTABLE);
  356. } else {
  357. err = chflags(file.utf8().get_data(), st.st_flags & ~UF_IMMUTABLE);
  358. }
  359. ERR_FAIL_COND_V_MSG(err, FAILED, "Failed to set attributes for: " + p_file);
  360. return OK;
  361. #else
  362. return ERR_UNAVAILABLE;
  363. #endif
  364. }
  365. void FileAccessUnix::close() {
  366. _close();
  367. }
  368. FileAccessUnix::CloseNotificationFunc FileAccessUnix::close_notification_func = nullptr;
  369. FileAccessUnix::~FileAccessUnix() {
  370. _close();
  371. }
  372. #endif // UNIX_ENABLED