file_access_unix.cpp 7.4 KB

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