2
0

file_access_unix.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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. #ifndef ANDROID_ENABLED
  37. #include <sys/statvfs.h>
  38. #endif
  39. #ifdef MSVC
  40. #define S_ISREG(m) ((m)&_S_IFREG)
  41. #endif
  42. #ifndef S_ISREG
  43. #define S_ISREG(m) ((m)&S_IFREG)
  44. #endif
  45. void FileAccessUnix::check_errors() const {
  46. ERR_FAIL_COND(!f);
  47. if (feof(f)) {
  48. last_error = ERR_FILE_EOF;
  49. }
  50. }
  51. Error FileAccessUnix::_open(const String &p_path, int p_mode_flags) {
  52. if (f)
  53. fclose(f);
  54. f = NULL;
  55. path = fix_path(p_path);
  56. //printf("opening %ls, %i\n", path.c_str(), Memory::get_static_mem_usage());
  57. ERR_FAIL_COND_V(f, ERR_ALREADY_IN_USE);
  58. const char *mode_string;
  59. if (p_mode_flags == READ)
  60. mode_string = "rb";
  61. else if (p_mode_flags == WRITE)
  62. mode_string = "wb";
  63. else if (p_mode_flags == READ_WRITE)
  64. mode_string = "rb+";
  65. else if (p_mode_flags == WRITE_READ)
  66. mode_string = "wb+";
  67. else
  68. return ERR_INVALID_PARAMETER;
  69. /* pretty much every implementation that uses fopen as primary
  70. backend (unix-compatible mostly) supports utf8 encoding */
  71. //printf("opening %s as %s\n", p_path.utf8().get_data(), path.utf8().get_data());
  72. struct stat st;
  73. if (stat(path.utf8().get_data(), &st) == 0) {
  74. if (!S_ISREG(st.st_mode))
  75. return ERR_FILE_CANT_OPEN;
  76. };
  77. if (is_backup_save_enabled() && p_mode_flags & WRITE && !(p_mode_flags & READ)) {
  78. save_path = path;
  79. path = path + ".tmp";
  80. //print_line("saving instead to "+path);
  81. }
  82. f = fopen(path.utf8().get_data(), mode_string);
  83. if (f == NULL) {
  84. last_error = ERR_FILE_CANT_OPEN;
  85. return ERR_FILE_CANT_OPEN;
  86. } else {
  87. last_error = OK;
  88. flags = p_mode_flags;
  89. return OK;
  90. }
  91. }
  92. void FileAccessUnix::close() {
  93. if (!f)
  94. return;
  95. fclose(f);
  96. f = NULL;
  97. if (close_notification_func) {
  98. close_notification_func(path, flags);
  99. }
  100. if (save_path != "") {
  101. //unlink(save_path.utf8().get_data());
  102. //print_line("renaming..");
  103. int rename_error = rename((save_path + ".tmp").utf8().get_data(), save_path.utf8().get_data());
  104. if (rename_error && close_fail_notify) {
  105. close_fail_notify(save_path);
  106. }
  107. save_path = "";
  108. ERR_FAIL_COND(rename_error != 0);
  109. }
  110. }
  111. bool FileAccessUnix::is_open() const {
  112. return (f != NULL);
  113. }
  114. void FileAccessUnix::seek(size_t p_position) {
  115. ERR_FAIL_COND(!f);
  116. last_error = OK;
  117. if (fseek(f, p_position, SEEK_SET))
  118. check_errors();
  119. }
  120. void FileAccessUnix::seek_end(int64_t p_position) {
  121. ERR_FAIL_COND(!f);
  122. if (fseek(f, p_position, SEEK_END))
  123. check_errors();
  124. }
  125. size_t FileAccessUnix::get_pos() const {
  126. size_t aux_position = 0;
  127. if (!(aux_position = ftell(f))) {
  128. check_errors();
  129. };
  130. return aux_position;
  131. }
  132. size_t FileAccessUnix::get_len() const {
  133. ERR_FAIL_COND_V(!f, 0);
  134. FileAccessUnix *fau = const_cast<FileAccessUnix *>(this);
  135. int pos = fau->get_pos();
  136. fau->seek_end();
  137. int size = fau->get_pos();
  138. fau->seek(pos);
  139. return size;
  140. }
  141. bool FileAccessUnix::eof_reached() const {
  142. return last_error == ERR_FILE_EOF;
  143. }
  144. uint8_t FileAccessUnix::get_8() const {
  145. ERR_FAIL_COND_V(!f, 0);
  146. uint8_t b;
  147. if (fread(&b, 1, 1, f) == 0) {
  148. check_errors();
  149. };
  150. return b;
  151. }
  152. int FileAccessUnix::get_buffer(uint8_t *p_dst, int p_length) const {
  153. ERR_FAIL_COND_V(!f, -1);
  154. int read = fread(p_dst, 1, p_length, f);
  155. check_errors();
  156. return read;
  157. };
  158. Error FileAccessUnix::get_error() const {
  159. return last_error;
  160. }
  161. void FileAccessUnix::store_8(uint8_t p_dest) {
  162. ERR_FAIL_COND(!f);
  163. fwrite(&p_dest, 1, 1, f);
  164. }
  165. bool FileAccessUnix::file_exists(const String &p_path) {
  166. FILE *g;
  167. //printf("opening file %s\n", p_fname.c_str());
  168. String filename = fix_path(p_path);
  169. g = fopen(filename.utf8().get_data(), "rb");
  170. if (g == NULL) {
  171. return false;
  172. } else {
  173. fclose(g);
  174. return true;
  175. }
  176. }
  177. uint64_t FileAccessUnix::_get_modified_time(const String &p_file) {
  178. String file = fix_path(p_file);
  179. struct stat flags;
  180. bool success = (stat(file.utf8().get_data(), &flags) == 0);
  181. if (success) {
  182. return flags.st_mtime;
  183. } else {
  184. print_line("Cannot open: " + p_file);
  185. ERR_FAIL_V(0);
  186. };
  187. }
  188. Error FileAccessUnix::_chmod(const String &p_path, int p_mod) {
  189. int err = chmod(p_path.utf8().get_data(), p_mod);
  190. if (!err) {
  191. return OK;
  192. }
  193. return FAILED;
  194. }
  195. FileAccess *FileAccessUnix::create_libc() {
  196. return memnew(FileAccessUnix);
  197. }
  198. CloseNotificationFunc FileAccessUnix::close_notification_func = NULL;
  199. FileAccessUnix::FileAccessUnix() {
  200. f = NULL;
  201. flags = 0;
  202. last_error = OK;
  203. }
  204. FileAccessUnix::~FileAccessUnix() {
  205. close();
  206. }
  207. #endif