2
0

file_access_unix.cpp 8.7 KB

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