file_access_unix.cpp 8.8 KB

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