file_access_unix.cpp 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. /*************************************************************************/
  2. /* file_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 "file_access_unix.h"
  31. #if defined(UNIX_ENABLED) || defined(LIBC_FILEIO_ENABLED)
  32. #include "core/os/os.h"
  33. #include "core/string/print_string.h"
  34. #include <sys/stat.h>
  35. #include <sys/types.h>
  36. #include <errno.h>
  37. #if defined(UNIX_ENABLED)
  38. #include <unistd.h>
  39. #endif
  40. #ifndef ANDROID_ENABLED
  41. #include <sys/statvfs.h>
  42. #endif
  43. #ifdef MSVC
  44. #define S_ISREG(m) ((m)&_S_IFREG)
  45. #include <io.h>
  46. #endif
  47. #ifndef S_ISREG
  48. #define S_ISREG(m) ((m)&S_IFREG)
  49. #endif
  50. #ifndef NO_FCNTL
  51. #include <fcntl.h>
  52. #else
  53. #include <sys/ioctl.h>
  54. #endif
  55. void FileAccessUnix::check_errors() const {
  56. ERR_FAIL_COND_MSG(!f, "File must be opened before use.");
  57. if (feof(f)) {
  58. last_error = ERR_FILE_EOF;
  59. }
  60. }
  61. Error FileAccessUnix::open_internal(const String &p_path, int p_mode_flags) {
  62. _close();
  63. path_src = p_path;
  64. path = fix_path(p_path);
  65. //printf("opening %s, %i\n", path.utf8().get_data(), Memory::get_static_mem_usage());
  66. ERR_FAIL_COND_V_MSG(f, ERR_ALREADY_IN_USE, "File is already in use.");
  67. const char *mode_string;
  68. if (p_mode_flags == READ) {
  69. mode_string = "rb";
  70. } else if (p_mode_flags == WRITE) {
  71. mode_string = "wb";
  72. } else if (p_mode_flags == READ_WRITE) {
  73. mode_string = "rb+";
  74. } else if (p_mode_flags == WRITE_READ) {
  75. mode_string = "wb+";
  76. } else {
  77. return ERR_INVALID_PARAMETER;
  78. }
  79. /* pretty much every implementation that uses fopen as primary
  80. backend (unix-compatible mostly) supports utf8 encoding */
  81. //printf("opening %s as %s\n", p_path.utf8().get_data(), path.utf8().get_data());
  82. struct stat st;
  83. int err = stat(path.utf8().get_data(), &st);
  84. if (!err) {
  85. switch (st.st_mode & S_IFMT) {
  86. case S_IFLNK:
  87. case S_IFREG:
  88. break;
  89. default:
  90. return ERR_FILE_CANT_OPEN;
  91. }
  92. }
  93. if (is_backup_save_enabled() && (p_mode_flags == WRITE)) {
  94. save_path = path;
  95. path = path + ".tmp";
  96. }
  97. f = fopen(path.utf8().get_data(), mode_string);
  98. if (f == nullptr) {
  99. switch (errno) {
  100. case ENOENT: {
  101. last_error = ERR_FILE_NOT_FOUND;
  102. } break;
  103. default: {
  104. last_error = ERR_FILE_CANT_OPEN;
  105. } break;
  106. }
  107. return last_error;
  108. }
  109. // Set close on exec to avoid leaking it to subprocesses.
  110. int fd = fileno(f);
  111. if (fd != -1) {
  112. #if defined(NO_FCNTL)
  113. unsigned long par = 0;
  114. ioctl(fd, FIOCLEX, &par);
  115. #else
  116. int opts = fcntl(fd, F_GETFD);
  117. fcntl(fd, F_SETFD, opts | FD_CLOEXEC);
  118. #endif
  119. }
  120. last_error = OK;
  121. flags = p_mode_flags;
  122. return OK;
  123. }
  124. void FileAccessUnix::_close() {
  125. if (!f) {
  126. return;
  127. }
  128. fclose(f);
  129. f = nullptr;
  130. if (close_notification_func) {
  131. close_notification_func(path, flags);
  132. }
  133. if (!save_path.is_empty()) {
  134. int rename_error = rename((save_path + ".tmp").utf8().get_data(), save_path.utf8().get_data());
  135. if (rename_error && close_fail_notify) {
  136. close_fail_notify(save_path);
  137. }
  138. save_path = "";
  139. ERR_FAIL_COND(rename_error != 0);
  140. }
  141. }
  142. bool FileAccessUnix::is_open() const {
  143. return (f != nullptr);
  144. }
  145. String FileAccessUnix::get_path() const {
  146. return path_src;
  147. }
  148. String FileAccessUnix::get_path_absolute() const {
  149. return path;
  150. }
  151. void FileAccessUnix::seek(uint64_t p_position) {
  152. ERR_FAIL_COND_MSG(!f, "File must be opened before use.");
  153. last_error = OK;
  154. if (fseeko(f, p_position, SEEK_SET)) {
  155. check_errors();
  156. }
  157. }
  158. void FileAccessUnix::seek_end(int64_t p_position) {
  159. ERR_FAIL_COND_MSG(!f, "File must be opened before use.");
  160. if (fseeko(f, p_position, SEEK_END)) {
  161. check_errors();
  162. }
  163. }
  164. uint64_t FileAccessUnix::get_position() const {
  165. ERR_FAIL_COND_V_MSG(!f, 0, "File must be opened before use.");
  166. int64_t pos = ftello(f);
  167. if (pos < 0) {
  168. check_errors();
  169. ERR_FAIL_V(0);
  170. }
  171. return pos;
  172. }
  173. uint64_t FileAccessUnix::get_length() const {
  174. ERR_FAIL_COND_V_MSG(!f, 0, "File must be opened before use.");
  175. int64_t pos = ftello(f);
  176. ERR_FAIL_COND_V(pos < 0, 0);
  177. ERR_FAIL_COND_V(fseeko(f, 0, SEEK_END), 0);
  178. int64_t size = ftello(f);
  179. ERR_FAIL_COND_V(size < 0, 0);
  180. ERR_FAIL_COND_V(fseeko(f, pos, SEEK_SET), 0);
  181. return size;
  182. }
  183. bool FileAccessUnix::eof_reached() const {
  184. return last_error == ERR_FILE_EOF;
  185. }
  186. uint8_t FileAccessUnix::get_8() const {
  187. ERR_FAIL_COND_V_MSG(!f, 0, "File must be opened before use.");
  188. uint8_t b;
  189. if (fread(&b, 1, 1, f) == 0) {
  190. check_errors();
  191. b = '\0';
  192. }
  193. return b;
  194. }
  195. uint64_t FileAccessUnix::get_buffer(uint8_t *p_dst, uint64_t p_length) const {
  196. ERR_FAIL_COND_V(!p_dst && p_length > 0, -1);
  197. ERR_FAIL_COND_V_MSG(!f, -1, "File must be opened before use.");
  198. uint64_t read = fread(p_dst, 1, p_length, f);
  199. check_errors();
  200. return read;
  201. }
  202. Error FileAccessUnix::get_error() const {
  203. return last_error;
  204. }
  205. void FileAccessUnix::flush() {
  206. ERR_FAIL_COND_MSG(!f, "File must be opened before use.");
  207. fflush(f);
  208. }
  209. void FileAccessUnix::store_8(uint8_t p_dest) {
  210. ERR_FAIL_COND_MSG(!f, "File must be opened before use.");
  211. ERR_FAIL_COND(fwrite(&p_dest, 1, 1, f) != 1);
  212. }
  213. void FileAccessUnix::store_buffer(const uint8_t *p_src, uint64_t p_length) {
  214. ERR_FAIL_COND_MSG(!f, "File must be opened before use.");
  215. ERR_FAIL_COND(!p_src && p_length > 0);
  216. ERR_FAIL_COND(fwrite(p_src, 1, p_length, f) != p_length);
  217. }
  218. bool FileAccessUnix::file_exists(const String &p_path) {
  219. int err;
  220. struct stat st;
  221. String filename = fix_path(p_path);
  222. // Does the name exist at all?
  223. err = stat(filename.utf8().get_data(), &st);
  224. if (err) {
  225. return false;
  226. }
  227. #ifdef UNIX_ENABLED
  228. // See if we have access to the file
  229. if (access(filename.utf8().get_data(), F_OK)) {
  230. return false;
  231. }
  232. #else
  233. if (_access(filename.utf8().get_data(), 4) == -1) {
  234. return false;
  235. }
  236. #endif
  237. // See if this is a regular file
  238. switch (st.st_mode & S_IFMT) {
  239. case S_IFLNK:
  240. case S_IFREG:
  241. return true;
  242. default:
  243. return false;
  244. }
  245. }
  246. uint64_t FileAccessUnix::_get_modified_time(const String &p_file) {
  247. String file = fix_path(p_file);
  248. struct stat flags;
  249. int err = stat(file.utf8().get_data(), &flags);
  250. if (!err) {
  251. return flags.st_mtime;
  252. } else {
  253. print_verbose("Failed to get modified time for: " + p_file + "");
  254. return 0;
  255. }
  256. }
  257. uint32_t FileAccessUnix::_get_unix_permissions(const String &p_file) {
  258. String file = fix_path(p_file);
  259. struct stat flags;
  260. int err = stat(file.utf8().get_data(), &flags);
  261. if (!err) {
  262. return flags.st_mode & 0x7FF; //only permissions
  263. } else {
  264. ERR_FAIL_V_MSG(0, "Failed to get unix permissions for: " + p_file + ".");
  265. }
  266. }
  267. Error FileAccessUnix::_set_unix_permissions(const String &p_file, uint32_t p_permissions) {
  268. String file = fix_path(p_file);
  269. int err = chmod(file.utf8().get_data(), p_permissions);
  270. if (!err) {
  271. return OK;
  272. }
  273. return FAILED;
  274. }
  275. CloseNotificationFunc FileAccessUnix::close_notification_func = nullptr;
  276. FileAccessUnix::~FileAccessUnix() {
  277. _close();
  278. }
  279. #endif // UNIX_ENABLED || LIBC_FILEIO_ENABLED