file_access_windows.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. /*************************************************************************/
  2. /* file_access_windows.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2014 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. #ifdef WINDOWS_ENABLED
  30. #include <Windows.h>
  31. #include "Shlwapi.h"
  32. #include "file_access_windows.h"
  33. #include <sys/types.h>
  34. #include <sys/stat.h>
  35. #include <wchar.h>
  36. #include <tchar.h>
  37. #include "print_string.h"
  38. #ifdef _MSC_VER
  39. #define S_ISREG(m) ((m)&_S_IFREG)
  40. #endif
  41. void FileAccessWindows::check_errors() const {
  42. ERR_FAIL_COND(!f);
  43. if (feof(f)) {
  44. last_error=ERR_FILE_EOF;;
  45. }
  46. }
  47. Error FileAccessWindows::_open(const String& p_filename, int p_mode_flags) {
  48. String filename=fix_path(p_filename);
  49. if (f)
  50. close();
  51. const wchar_t* mode_string;
  52. if (p_mode_flags==READ)
  53. mode_string=L"rb";
  54. else if (p_mode_flags==WRITE)
  55. mode_string=L"wb";
  56. else if (p_mode_flags==READ_WRITE)
  57. mode_string=L"wb+";
  58. else
  59. return ERR_INVALID_PARAMETER;
  60. /* pretty much every implementation that uses fopen as primary
  61. backend supports utf8 encoding */
  62. struct _stat st;
  63. if (_wstat(filename.c_str(), &st) == 0) {
  64. if (!S_ISREG(st.st_mode))
  65. return ERR_FILE_CANT_OPEN;
  66. };
  67. if (is_backup_save_enabled() && p_mode_flags&WRITE && !(p_mode_flags&READ)) {
  68. save_path=filename;
  69. filename=filename+".tmp";
  70. //print_line("saving instead to "+path);
  71. }
  72. f=_wfopen(filename.c_str(), mode_string);
  73. if (f==NULL) {
  74. last_error=ERR_FILE_CANT_OPEN;
  75. return ERR_FILE_CANT_OPEN;
  76. } else {
  77. last_error=OK;
  78. flags=p_mode_flags;
  79. return OK;
  80. }
  81. }
  82. void FileAccessWindows::close() {
  83. if (!f)
  84. return;
  85. fclose(f);
  86. f = NULL;
  87. if (save_path!="") {
  88. //unlink(save_path.utf8().get_data());
  89. //print_line("renaming..");
  90. //_wunlink(save_path.c_str()); //unlink if exists
  91. //int rename_error = _wrename((save_path+".tmp").c_str(),save_path.c_str());
  92. bool rename_error;
  93. if (!PathFileExistsW(save_path.c_str())) {
  94. //creating new file
  95. rename_error = _wrename((save_path+".tmp").c_str(),save_path.c_str())!=0;
  96. } else {
  97. //atomic replace for existing file
  98. rename_error = !ReplaceFileW(save_path.c_str(), (save_path+".tmp").c_str(), NULL, 2|4, NULL, NULL);
  99. }
  100. save_path="";
  101. ERR_FAIL_COND( rename_error );
  102. }
  103. }
  104. bool FileAccessWindows::is_open() const {
  105. return (f!=NULL);
  106. }
  107. void FileAccessWindows::seek(size_t p_position) {
  108. ERR_FAIL_COND(!f);
  109. last_error=OK;
  110. if ( fseek(f,p_position,SEEK_SET) )
  111. check_errors();
  112. }
  113. void FileAccessWindows::seek_end(int64_t p_position) {
  114. ERR_FAIL_COND(!f);
  115. if ( fseek(f,p_position,SEEK_END) )
  116. check_errors();
  117. }
  118. size_t FileAccessWindows::get_pos() const {
  119. size_t aux_position=0;
  120. if ( !(aux_position = ftell(f)) ) {
  121. check_errors();
  122. };
  123. return aux_position;
  124. }
  125. size_t FileAccessWindows::get_len() const {
  126. ERR_FAIL_COND_V(!f,0);
  127. size_t pos = get_pos();
  128. fseek(f,0,SEEK_END);
  129. int size = get_pos();
  130. fseek(f, pos, SEEK_SET);
  131. return size;
  132. }
  133. bool FileAccessWindows::eof_reached() const {
  134. check_errors();
  135. return last_error==ERR_FILE_EOF;
  136. }
  137. uint8_t FileAccessWindows::get_8() const {
  138. ERR_FAIL_COND_V(!f,0);
  139. uint8_t b;
  140. if (fread(&b,1,1,f) == 0) {
  141. check_errors();
  142. };
  143. return b;
  144. }
  145. int FileAccessWindows::get_buffer(uint8_t *p_dst, int p_length) const {
  146. ERR_FAIL_COND_V(!f,-1);
  147. int read = fread(p_dst, 1,p_length, f);
  148. check_errors();
  149. return read;
  150. };
  151. Error FileAccessWindows::get_error() const {
  152. return last_error;
  153. }
  154. void FileAccessWindows::store_8(uint8_t p_dest) {
  155. ERR_FAIL_COND(!f);
  156. fwrite(&p_dest,1,1,f);
  157. }
  158. bool FileAccessWindows::file_exists(const String& p_name) {
  159. FILE *g;
  160. //printf("opening file %s\n", p_fname.c_str());
  161. String filename=fix_path(p_name);
  162. g=_wfopen(filename.c_str(),L"rb");
  163. if (g==NULL) {
  164. return false;
  165. } else {
  166. fclose(g);
  167. return true;
  168. }
  169. }
  170. uint64_t FileAccessWindows::_get_modified_time(const String& p_file) {
  171. String file=fix_path(p_file);
  172. if (file.ends_with("/") && file!="/")
  173. file=file.substr(0,file.length()-1);
  174. struct _stat st;
  175. int rv = _wstat(file.c_str(), &st);
  176. if (rv == 0) {
  177. return st.st_mtime;
  178. } else {
  179. print_line("no access to "+file );
  180. }
  181. ERR_FAIL_V(0);
  182. };
  183. FileAccessWindows::FileAccessWindows() {
  184. f=NULL;
  185. flags=0;
  186. last_error=OK;
  187. }
  188. FileAccessWindows::~FileAccessWindows() {
  189. close();
  190. }
  191. #endif