file_access_unix.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. /*************************************************************************/
  2. /* file_access_unix.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2015 Juan Linietsky, Ariel Manzur. */
  9. /* */
  10. /* Permission is hereby granted, free of charge, to any person obtaining */
  11. /* a copy of this software and associated documentation files (the */
  12. /* "Software"), to deal in the Software without restriction, including */
  13. /* without limitation the rights to use, copy, modify, merge, publish, */
  14. /* distribute, sublicense, and/or sell copies of the Software, and to */
  15. /* permit persons to whom the Software is furnished to do so, subject to */
  16. /* the following conditions: */
  17. /* */
  18. /* The above copyright notice and this permission notice shall be */
  19. /* included in all copies or substantial portions of the Software. */
  20. /* */
  21. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  22. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  23. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  24. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  25. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  26. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  27. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  28. /*************************************************************************/
  29. #include "file_access_unix.h"
  30. #if defined(UNIX_ENABLED) || defined(LIBC_FILEIO_ENABLED)
  31. #include <sys/types.h>
  32. #include <sys/stat.h>
  33. #include "print_string.h"
  34. #include "core/os/os.h"
  35. #ifndef ANDROID_ENABLED
  36. #include <sys/statvfs.h>
  37. #endif
  38. #ifdef MSVC
  39. #define S_ISREG(m) ((m)&_S_IFREG)
  40. #endif
  41. #ifndef S_ISREG
  42. #define S_ISREG(m) ((m) & S_IFREG)
  43. #endif
  44. void FileAccessUnix::check_errors() const {
  45. ERR_FAIL_COND(!f);
  46. if (feof(f)) {
  47. last_error=ERR_FILE_EOF;
  48. }
  49. }
  50. Error FileAccessUnix::_open(const String& p_path, int p_mode_flags) {
  51. if (f)
  52. fclose(f);
  53. f=NULL;
  54. String path=fix_path(p_path);
  55. //printf("opening %ls, %i\n", path.c_str(), Memory::get_static_mem_usage());
  56. ERR_FAIL_COND_V(f,ERR_ALREADY_IN_USE);
  57. const char* mode_string;
  58. if (p_mode_flags==READ)
  59. mode_string="rb";
  60. else if (p_mode_flags==WRITE)
  61. mode_string="wb";
  62. else if (p_mode_flags==READ_WRITE)
  63. mode_string="rb+";
  64. else
  65. return ERR_INVALID_PARAMETER;
  66. /* pretty much every implementation that uses fopen as primary
  67. backend (unix-compatible mostly) supports utf8 encoding */
  68. //printf("opening %s as %s\n", p_path.utf8().get_data(), path.utf8().get_data());
  69. struct stat st;
  70. if (stat(path.utf8().get_data(),&st) == 0) {
  71. if (!S_ISREG(st.st_mode))
  72. return ERR_FILE_CANT_OPEN;
  73. };
  74. if (is_backup_save_enabled() && p_mode_flags&WRITE && !(p_mode_flags&READ)) {
  75. save_path=path;
  76. path=path+".tmp";
  77. //print_line("saving instead to "+path);
  78. }
  79. f=fopen(path.utf8().get_data(),mode_string);
  80. if (f==NULL) {
  81. last_error=ERR_FILE_CANT_OPEN;
  82. return ERR_FILE_CANT_OPEN;
  83. } else {
  84. last_error=OK;
  85. flags=p_mode_flags;
  86. return OK;
  87. }
  88. }
  89. void FileAccessUnix::close() {
  90. if (!f)
  91. return;
  92. fclose(f);
  93. f = NULL;
  94. if (save_path!="") {
  95. //unlink(save_path.utf8().get_data());
  96. //print_line("renaming..");
  97. int rename_error = rename((save_path+".tmp").utf8().get_data(),save_path.utf8().get_data());
  98. save_path="";
  99. ERR_FAIL_COND( rename_error != 0);
  100. }
  101. }
  102. bool FileAccessUnix::is_open() const{
  103. return (f!=NULL);
  104. }
  105. void FileAccessUnix::seek(size_t p_position) {
  106. ERR_FAIL_COND(!f);
  107. last_error=OK;
  108. if ( fseek(f,p_position,SEEK_SET) )
  109. check_errors();
  110. }
  111. void FileAccessUnix::seek_end(int64_t p_position) {
  112. ERR_FAIL_COND(!f);
  113. if ( fseek(f,p_position,SEEK_END) )
  114. check_errors();
  115. }
  116. size_t FileAccessUnix::get_pos() const{
  117. size_t aux_position=0;
  118. if ( !(aux_position = ftell(f)) ) {
  119. check_errors();
  120. };
  121. return aux_position;
  122. }
  123. size_t FileAccessUnix::get_len() const{
  124. ERR_FAIL_COND_V(!f,0);
  125. FileAccessUnix *fau = const_cast<FileAccessUnix*>(this);
  126. int pos = fau->get_pos();
  127. fau->seek_end();
  128. int size = fau->get_pos();
  129. fau->seek(pos);
  130. return size;
  131. }
  132. bool FileAccessUnix::eof_reached() const{
  133. return last_error==ERR_FILE_EOF;
  134. }
  135. uint8_t FileAccessUnix::get_8() const{
  136. ERR_FAIL_COND_V(!f,0);
  137. uint8_t b;
  138. if (fread(&b,1,1,f) == 0) {
  139. check_errors();
  140. };
  141. return b;
  142. }
  143. int FileAccessUnix::get_buffer(uint8_t *p_dst, int p_length) const {
  144. ERR_FAIL_COND_V(!f,-1);
  145. int read = fread(p_dst, 1, p_length, f);
  146. check_errors();
  147. return read;
  148. };
  149. Error FileAccessUnix::get_error() const{
  150. return last_error;
  151. }
  152. void FileAccessUnix::store_8(uint8_t p_dest) {
  153. ERR_FAIL_COND(!f);
  154. fwrite(&p_dest,1,1,f);
  155. }
  156. bool FileAccessUnix::file_exists(const String &p_path) {
  157. FILE *g;
  158. //printf("opening file %s\n", p_fname.c_str());
  159. String filename=fix_path(p_path);
  160. g=fopen(filename.utf8().get_data(),"rb");
  161. if (g==NULL) {
  162. return false;
  163. } else {
  164. fclose(g);
  165. return true;
  166. }
  167. }
  168. uint64_t FileAccessUnix::_get_modified_time(const String &p_file) {
  169. String file=fix_path(p_file);
  170. struct stat flags;
  171. bool success = (stat(file.utf8().get_data(),&flags)==0);
  172. if (success) {
  173. return flags.st_mtime;
  174. } else {
  175. print_line("ERROR IN: "+p_file);
  176. ERR_FAIL_V(0);
  177. };
  178. }
  179. FileAccess * FileAccessUnix::create_libc() {
  180. return memnew( FileAccessUnix );
  181. }
  182. FileAccessUnix::FileAccessUnix() {
  183. f=NULL;
  184. flags=0;
  185. last_error=OK;
  186. }
  187. FileAccessUnix::~FileAccessUnix() {
  188. close();
  189. }
  190. #endif