file_access_unix.cpp 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  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. // Create a temporary file in the same directory as the target file.
  89. path = path + "-XXXXXX";
  90. CharString cs = path.utf8();
  91. int fd = mkstemp(cs.ptrw());
  92. if (fd == -1) {
  93. last_error = ERR_FILE_CANT_OPEN;
  94. return last_error;
  95. }
  96. fchmod(fd, 0666);
  97. path = String::utf8(cs.ptr());
  98. f = fdopen(fd, mode_string);
  99. if (f == nullptr) {
  100. // Delete temp file and close descriptor if open failed.
  101. ::unlink(cs.ptr());
  102. ::close(fd);
  103. last_error = ERR_FILE_CANT_OPEN;
  104. return last_error;
  105. }
  106. } else {
  107. f = fopen(path.utf8().get_data(), mode_string);
  108. }
  109. if (f == nullptr) {
  110. switch (errno) {
  111. case ENOENT: {
  112. last_error = ERR_FILE_NOT_FOUND;
  113. } break;
  114. default: {
  115. last_error = ERR_FILE_CANT_OPEN;
  116. } break;
  117. }
  118. return last_error;
  119. }
  120. // Set close on exec to avoid leaking it to subprocesses.
  121. int fd = fileno(f);
  122. if (fd != -1) {
  123. int opts = fcntl(fd, F_GETFD);
  124. fcntl(fd, F_SETFD, opts | FD_CLOEXEC);
  125. }
  126. last_error = OK;
  127. flags = p_mode_flags;
  128. return OK;
  129. }
  130. void FileAccessUnix::_close() {
  131. if (!f) {
  132. return;
  133. }
  134. fclose(f);
  135. f = nullptr;
  136. if (close_notification_func) {
  137. close_notification_func(path, flags);
  138. }
  139. if (!save_path.is_empty()) {
  140. int rename_error = rename(path.utf8().get_data(), save_path.utf8().get_data());
  141. if (rename_error && close_fail_notify) {
  142. close_fail_notify(save_path);
  143. }
  144. save_path = "";
  145. ERR_FAIL_COND(rename_error != 0);
  146. }
  147. }
  148. bool FileAccessUnix::is_open() const {
  149. return (f != nullptr);
  150. }
  151. String FileAccessUnix::get_path() const {
  152. return path_src;
  153. }
  154. String FileAccessUnix::get_path_absolute() const {
  155. return path;
  156. }
  157. void FileAccessUnix::seek(uint64_t p_position) {
  158. ERR_FAIL_COND_MSG(!f, "File must be opened before use.");
  159. last_error = OK;
  160. if (fseeko(f, p_position, SEEK_SET)) {
  161. check_errors();
  162. }
  163. }
  164. void FileAccessUnix::seek_end(int64_t p_position) {
  165. ERR_FAIL_COND_MSG(!f, "File must be opened before use.");
  166. if (fseeko(f, p_position, SEEK_END)) {
  167. check_errors();
  168. }
  169. }
  170. uint64_t FileAccessUnix::get_position() const {
  171. ERR_FAIL_COND_V_MSG(!f, 0, "File must be opened before use.");
  172. int64_t pos = ftello(f);
  173. if (pos < 0) {
  174. check_errors();
  175. ERR_FAIL_V(0);
  176. }
  177. return pos;
  178. }
  179. uint64_t FileAccessUnix::get_length() const {
  180. ERR_FAIL_COND_V_MSG(!f, 0, "File must be opened before use.");
  181. int64_t pos = ftello(f);
  182. ERR_FAIL_COND_V(pos < 0, 0);
  183. ERR_FAIL_COND_V(fseeko(f, 0, SEEK_END), 0);
  184. int64_t size = ftello(f);
  185. ERR_FAIL_COND_V(size < 0, 0);
  186. ERR_FAIL_COND_V(fseeko(f, pos, SEEK_SET), 0);
  187. return size;
  188. }
  189. bool FileAccessUnix::eof_reached() const {
  190. return last_error == ERR_FILE_EOF;
  191. }
  192. uint8_t FileAccessUnix::get_8() const {
  193. ERR_FAIL_COND_V_MSG(!f, 0, "File must be opened before use.");
  194. uint8_t b;
  195. if (fread(&b, 1, 1, f) == 0) {
  196. check_errors();
  197. b = '\0';
  198. }
  199. return b;
  200. }
  201. uint64_t FileAccessUnix::get_buffer(uint8_t *p_dst, uint64_t p_length) const {
  202. ERR_FAIL_COND_V(!p_dst && p_length > 0, -1);
  203. ERR_FAIL_COND_V_MSG(!f, -1, "File must be opened before use.");
  204. uint64_t read = fread(p_dst, 1, p_length, f);
  205. check_errors();
  206. return read;
  207. }
  208. Error FileAccessUnix::get_error() const {
  209. return last_error;
  210. }
  211. void FileAccessUnix::flush() {
  212. ERR_FAIL_COND_MSG(!f, "File must be opened before use.");
  213. fflush(f);
  214. }
  215. void FileAccessUnix::store_8(uint8_t p_dest) {
  216. ERR_FAIL_COND_MSG(!f, "File must be opened before use.");
  217. ERR_FAIL_COND(fwrite(&p_dest, 1, 1, f) != 1);
  218. }
  219. void FileAccessUnix::store_buffer(const uint8_t *p_src, uint64_t p_length) {
  220. ERR_FAIL_COND_MSG(!f, "File must be opened before use.");
  221. ERR_FAIL_COND(!p_src && p_length > 0);
  222. ERR_FAIL_COND(fwrite(p_src, 1, p_length, f) != p_length);
  223. }
  224. bool FileAccessUnix::file_exists(const String &p_path) {
  225. int err;
  226. struct stat st = {};
  227. String filename = fix_path(p_path);
  228. // Does the name exist at all?
  229. err = stat(filename.utf8().get_data(), &st);
  230. if (err) {
  231. return false;
  232. }
  233. #ifdef UNIX_ENABLED
  234. // See if we have access to the file
  235. if (access(filename.utf8().get_data(), F_OK)) {
  236. return false;
  237. }
  238. #else
  239. if (_access(filename.utf8().get_data(), 4) == -1) {
  240. return false;
  241. }
  242. #endif
  243. // See if this is a regular file
  244. switch (st.st_mode & S_IFMT) {
  245. case S_IFLNK:
  246. case S_IFREG:
  247. return true;
  248. default:
  249. return false;
  250. }
  251. }
  252. uint64_t FileAccessUnix::_get_modified_time(const String &p_file) {
  253. String file = fix_path(p_file);
  254. struct stat status = {};
  255. int err = stat(file.utf8().get_data(), &status);
  256. if (!err) {
  257. return status.st_mtime;
  258. } else {
  259. print_verbose("Failed to get modified time for: " + p_file + "");
  260. return 0;
  261. }
  262. }
  263. uint32_t FileAccessUnix::_get_unix_permissions(const String &p_file) {
  264. String file = fix_path(p_file);
  265. struct stat status = {};
  266. int err = stat(file.utf8().get_data(), &status);
  267. if (!err) {
  268. return status.st_mode & 0x7FF; //only permissions
  269. } else {
  270. ERR_FAIL_V_MSG(0, "Failed to get unix permissions for: " + p_file + ".");
  271. }
  272. }
  273. Error FileAccessUnix::_set_unix_permissions(const String &p_file, uint32_t p_permissions) {
  274. String file = fix_path(p_file);
  275. int err = chmod(file.utf8().get_data(), p_permissions);
  276. if (!err) {
  277. return OK;
  278. }
  279. return FAILED;
  280. }
  281. void FileAccessUnix::close() {
  282. _close();
  283. }
  284. CloseNotificationFunc FileAccessUnix::close_notification_func = nullptr;
  285. FileAccessUnix::~FileAccessUnix() {
  286. _close();
  287. }
  288. #endif // UNIX_ENABLED