file_access_unix.cpp 6.3 KB

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