file_access_unix.cpp 8.3 KB

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