file_access_unix.cpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  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-2018 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2018 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_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. //print_line("saving instead to "+path);
  92. }
  93. f = fopen(path.utf8().get_data(), mode_string);
  94. if (f == NULL) {
  95. last_error = ERR_FILE_CANT_OPEN;
  96. return ERR_FILE_CANT_OPEN;
  97. } else {
  98. last_error = OK;
  99. flags = p_mode_flags;
  100. return OK;
  101. }
  102. }
  103. void FileAccessUnix::close() {
  104. if (!f)
  105. return;
  106. fclose(f);
  107. f = NULL;
  108. if (close_notification_func) {
  109. close_notification_func(path, flags);
  110. }
  111. if (save_path != "") {
  112. //unlink(save_path.utf8().get_data());
  113. //print_line("renaming...");
  114. int rename_error = rename((save_path + ".tmp").utf8().get_data(), save_path.utf8().get_data());
  115. if (rename_error && close_fail_notify) {
  116. close_fail_notify(save_path);
  117. }
  118. save_path = "";
  119. ERR_FAIL_COND(rename_error != 0);
  120. }
  121. }
  122. bool FileAccessUnix::is_open() const {
  123. return (f != NULL);
  124. }
  125. String FileAccessUnix::get_path() const {
  126. return path_src;
  127. }
  128. String FileAccessUnix::get_path_absolute() const {
  129. return path;
  130. }
  131. void FileAccessUnix::seek(size_t p_position) {
  132. ERR_FAIL_COND(!f);
  133. last_error = OK;
  134. if (fseek(f, p_position, SEEK_SET))
  135. check_errors();
  136. }
  137. void FileAccessUnix::seek_end(int64_t p_position) {
  138. ERR_FAIL_COND(!f);
  139. if (fseek(f, p_position, SEEK_END))
  140. check_errors();
  141. }
  142. size_t FileAccessUnix::get_position() const {
  143. ERR_FAIL_COND_V(!f, 0);
  144. long pos = ftell(f);
  145. if (pos < 0) {
  146. check_errors();
  147. ERR_FAIL_V(0);
  148. }
  149. return pos;
  150. }
  151. size_t FileAccessUnix::get_len() const {
  152. ERR_FAIL_COND_V(!f, 0);
  153. long pos = ftell(f);
  154. ERR_FAIL_COND_V(pos < 0, 0);
  155. ERR_FAIL_COND_V(fseek(f, 0, SEEK_END), 0);
  156. long size = ftell(f);
  157. ERR_FAIL_COND_V(size < 0, 0);
  158. ERR_FAIL_COND_V(fseek(f, pos, SEEK_SET), 0);
  159. return size;
  160. }
  161. bool FileAccessUnix::eof_reached() const {
  162. return last_error == ERR_FILE_EOF;
  163. }
  164. uint8_t FileAccessUnix::get_8() const {
  165. ERR_FAIL_COND_V(!f, 0);
  166. uint8_t b;
  167. if (fread(&b, 1, 1, f) == 0) {
  168. check_errors();
  169. b = '\0';
  170. }
  171. return b;
  172. }
  173. int FileAccessUnix::get_buffer(uint8_t *p_dst, int p_length) const {
  174. ERR_FAIL_COND_V(!f, -1);
  175. int read = fread(p_dst, 1, p_length, f);
  176. check_errors();
  177. return read;
  178. };
  179. Error FileAccessUnix::get_error() const {
  180. return last_error;
  181. }
  182. void FileAccessUnix::flush() {
  183. ERR_FAIL_COND(!f);
  184. fflush(f);
  185. }
  186. void FileAccessUnix::store_8(uint8_t p_dest) {
  187. ERR_FAIL_COND(!f);
  188. ERR_FAIL_COND(fwrite(&p_dest, 1, 1, f) != 1);
  189. }
  190. void FileAccessUnix::store_buffer(const uint8_t *p_src, int p_length) {
  191. ERR_FAIL_COND(!f);
  192. ERR_FAIL_COND(fwrite(p_src, 1, p_length, f) != p_length);
  193. }
  194. bool FileAccessUnix::file_exists(const String &p_path) {
  195. int err;
  196. struct stat st;
  197. String filename = fix_path(p_path);
  198. // Does the name exist at all?
  199. err = stat(filename.utf8().get_data(), &st);
  200. if (err)
  201. return false;
  202. #ifdef UNIX_ENABLED
  203. // See if we have access to the file
  204. if (access(filename.utf8().get_data(), F_OK))
  205. return false;
  206. #else
  207. if (_access(filename.utf8().get_data(), 4) == -1)
  208. return false;
  209. #endif
  210. // See if this is a regular file
  211. switch (st.st_mode & S_IFMT) {
  212. case S_IFLNK:
  213. case S_IFREG:
  214. return true;
  215. default:
  216. return false;
  217. }
  218. }
  219. uint64_t FileAccessUnix::_get_modified_time(const String &p_file) {
  220. String file = fix_path(p_file);
  221. struct stat flags;
  222. int err = stat(file.utf8().get_data(), &flags);
  223. if (!err) {
  224. return flags.st_mtime;
  225. } else {
  226. print_line("ERROR IN: " + p_file);
  227. ERR_FAIL_V(0);
  228. };
  229. }
  230. // NEW FUNCTION
  231. uint64_t FileAccessUnix::_get_creation_time(const String &p_file) {
  232. String file = fix_path(p_file);
  233. struct stat flags;
  234. int err = stat(file.utf8().get_data(), &flags);
  235. if (!err) {
  236. return flags.st_ctime;
  237. } else {
  238. print_line("ERROR IN: " + p_file);
  239. ERR_FAIL_V(0);
  240. };
  241. }
  242. Error FileAccessUnix::_chmod(const String &p_path, int p_mod) {
  243. int err = chmod(p_path.utf8().get_data(), p_mod);
  244. if (!err) {
  245. return OK;
  246. }
  247. return FAILED;
  248. }
  249. FileAccess *FileAccessUnix::create_libc() {
  250. return memnew(FileAccessUnix);
  251. }
  252. CloseNotificationFunc FileAccessUnix::close_notification_func = NULL;
  253. FileAccessUnix::FileAccessUnix() {
  254. f = NULL;
  255. flags = 0;
  256. last_error = OK;
  257. }
  258. FileAccessUnix::~FileAccessUnix() {
  259. close();
  260. }
  261. #endif