file_access_unix.cpp 8.5 KB

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