file_access_unix.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  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-2017 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2017 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 "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 = fix_path(p_path);
  60. //printf("opening %ls, %i\n", path.c_str(), Memory::get_static_mem_usage());
  61. ERR_FAIL_COND_V(f, ERR_ALREADY_IN_USE);
  62. const char *mode_string;
  63. if (p_mode_flags == READ)
  64. mode_string = "rb";
  65. else if (p_mode_flags == WRITE)
  66. mode_string = "wb";
  67. else if (p_mode_flags == READ_WRITE)
  68. mode_string = "rb+";
  69. else if (p_mode_flags == WRITE_READ)
  70. mode_string = "wb+";
  71. else
  72. return ERR_INVALID_PARAMETER;
  73. /* pretty much every implementation that uses fopen as primary
  74. backend (unix-compatible mostly) supports utf8 encoding */
  75. //printf("opening %s as %s\n", p_path.utf8().get_data(), path.utf8().get_data());
  76. struct stat st;
  77. int err = stat(path.utf8().get_data(), &st);
  78. if (!err) {
  79. switch (st.st_mode & S_IFMT) {
  80. case S_IFLNK:
  81. case S_IFREG:
  82. break;
  83. default:
  84. return ERR_FILE_CANT_OPEN;
  85. }
  86. }
  87. if (is_backup_save_enabled() && (p_mode_flags & WRITE) && !(p_mode_flags & READ)) {
  88. save_path = path;
  89. path = path + ".tmp";
  90. //print_line("saving instead to "+path);
  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. //unlink(save_path.utf8().get_data());
  112. //print_line("renaming..");
  113. int rename_error = rename((save_path + ".tmp").utf8().get_data(), save_path.utf8().get_data());
  114. if (rename_error && close_fail_notify) {
  115. close_fail_notify(save_path);
  116. }
  117. save_path = "";
  118. ERR_FAIL_COND(rename_error != 0);
  119. }
  120. }
  121. bool FileAccessUnix::is_open() const {
  122. return (f != NULL);
  123. }
  124. void FileAccessUnix::seek(size_t p_position) {
  125. ERR_FAIL_COND(!f);
  126. last_error = OK;
  127. if (fseek(f, p_position, SEEK_SET))
  128. check_errors();
  129. }
  130. void FileAccessUnix::seek_end(int64_t p_position) {
  131. ERR_FAIL_COND(!f);
  132. if (fseek(f, p_position, SEEK_END))
  133. check_errors();
  134. }
  135. size_t FileAccessUnix::get_position() const {
  136. ERR_FAIL_COND_V(!f, 0);
  137. int pos = ftell(f);
  138. if (pos < 0) {
  139. check_errors();
  140. ERR_FAIL_V(0);
  141. }
  142. return pos;
  143. }
  144. size_t FileAccessUnix::get_len() const {
  145. ERR_FAIL_COND_V(!f, 0);
  146. int pos = ftell(f);
  147. ERR_FAIL_COND_V(pos < 0, 0);
  148. ERR_FAIL_COND_V(fseek(f, 0, SEEK_END), 0);
  149. int size = ftell(f);
  150. ERR_FAIL_COND_V(size < 0, 0);
  151. ERR_FAIL_COND_V(fseek(f, pos, SEEK_SET), 0);
  152. return size;
  153. }
  154. bool FileAccessUnix::eof_reached() const {
  155. return last_error == ERR_FILE_EOF;
  156. }
  157. uint8_t FileAccessUnix::get_8() const {
  158. ERR_FAIL_COND_V(!f, 0);
  159. uint8_t b;
  160. if (fread(&b, 1, 1, f) == 0) {
  161. check_errors();
  162. b = '\0';
  163. }
  164. return b;
  165. }
  166. int FileAccessUnix::get_buffer(uint8_t *p_dst, int p_length) const {
  167. ERR_FAIL_COND_V(!f, -1);
  168. int read = fread(p_dst, 1, p_length, f);
  169. check_errors();
  170. return read;
  171. };
  172. Error FileAccessUnix::get_error() const {
  173. return last_error;
  174. }
  175. void FileAccessUnix::flush() {
  176. ERR_FAIL_COND(!f);
  177. fflush(f);
  178. }
  179. void FileAccessUnix::store_8(uint8_t p_dest) {
  180. ERR_FAIL_COND(!f);
  181. ERR_FAIL_COND(fwrite(&p_dest, 1, 1, f) != 1);
  182. }
  183. bool FileAccessUnix::file_exists(const String &p_path) {
  184. int err;
  185. struct stat st;
  186. String filename = fix_path(p_path);
  187. // Does the name exist at all?
  188. err = stat(filename.utf8().get_data(), &st);
  189. if (err)
  190. return false;
  191. #ifdef UNIX_ENABLED
  192. // See if we have access to the file
  193. if (access(filename.utf8().get_data(), F_OK))
  194. return false;
  195. #else
  196. if (_access(filename.utf8().get_data(), 4) == -1)
  197. return false;
  198. #endif
  199. // See if this is a regular file
  200. switch (st.st_mode & S_IFMT) {
  201. case S_IFLNK:
  202. case S_IFREG:
  203. return true;
  204. default:
  205. return false;
  206. }
  207. }
  208. uint64_t FileAccessUnix::_get_modified_time(const String &p_file) {
  209. String file = fix_path(p_file);
  210. struct stat flags;
  211. int err = stat(file.utf8().get_data(), &flags);
  212. if (!err) {
  213. return flags.st_mtime;
  214. } else {
  215. print_line("ERROR IN: " + p_file);
  216. ERR_FAIL_V(0);
  217. };
  218. }
  219. Error FileAccessUnix::_chmod(const String &p_path, int p_mod) {
  220. int err = chmod(p_path.utf8().get_data(), p_mod);
  221. if (!err) {
  222. return OK;
  223. }
  224. return FAILED;
  225. }
  226. FileAccess *FileAccessUnix::create_libc() {
  227. return memnew(FileAccessUnix);
  228. }
  229. CloseNotificationFunc FileAccessUnix::close_notification_func = NULL;
  230. FileAccessUnix::FileAccessUnix() {
  231. f = NULL;
  232. flags = 0;
  233. last_error = OK;
  234. }
  235. FileAccessUnix::~FileAccessUnix() {
  236. close();
  237. }
  238. #endif