file_access_unix.cpp 12 KB

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