file_access_windows.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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-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. #ifdef WINDOWS_ENABLED
  30. #define WINVER 0x0500
  31. #include <windows.h>
  32. #include "shlwapi.h"
  33. #include "file_access_windows.h"
  34. #include <sys/types.h>
  35. #include <sys/stat.h>
  36. #include <wchar.h>
  37. #include <tchar.h>
  38. #include "print_string.h"
  39. #ifdef _MSC_VER
  40. #define S_ISREG(m) ((m)&_S_IFREG)
  41. #endif
  42. void FileAccessWindows::check_errors() const {
  43. ERR_FAIL_COND(!f);
  44. if (feof(f)) {
  45. last_error=ERR_FILE_EOF;;
  46. }
  47. }
  48. Error FileAccessWindows::_open(const String& p_filename, int p_mode_flags) {
  49. String filename=fix_path(p_filename);
  50. if (f)
  51. close();
  52. const wchar_t* mode_string;
  53. if (p_mode_flags==READ)
  54. mode_string=L"rb";
  55. else if (p_mode_flags==WRITE)
  56. mode_string=L"wb";
  57. else if (p_mode_flags==READ_WRITE)
  58. mode_string=L"rb+";
  59. else if (p_mode_flags==WRITE_READ)
  60. mode_string=L"wb+";
  61. else
  62. return ERR_INVALID_PARAMETER;
  63. /* pretty much every implementation that uses fopen as primary
  64. backend supports utf8 encoding */
  65. struct _stat st;
  66. if (_wstat(filename.c_str(), &st) == 0) {
  67. if (!S_ISREG(st.st_mode))
  68. return ERR_FILE_CANT_OPEN;
  69. };
  70. if (is_backup_save_enabled() && p_mode_flags&WRITE && !(p_mode_flags&READ)) {
  71. save_path=filename;
  72. filename=filename+".tmp";
  73. //print_line("saving instead to "+path);
  74. }
  75. f=_wfopen(filename.c_str(), mode_string);
  76. if (f==NULL) {
  77. last_error=ERR_FILE_CANT_OPEN;
  78. return ERR_FILE_CANT_OPEN;
  79. } else {
  80. last_error=OK;
  81. flags=p_mode_flags;
  82. return OK;
  83. }
  84. }
  85. void FileAccessWindows::close() {
  86. if (!f)
  87. return;
  88. fclose(f);
  89. f = NULL;
  90. if (save_path!="") {
  91. //unlink(save_path.utf8().get_data());
  92. //print_line("renaming..");
  93. //_wunlink(save_path.c_str()); //unlink if exists
  94. //int rename_error = _wrename((save_path+".tmp").c_str(),save_path.c_str());
  95. bool rename_error;
  96. if (!PathFileExistsW(save_path.c_str())) {
  97. //creating new file
  98. rename_error = _wrename((save_path+".tmp").c_str(),save_path.c_str())!=0;
  99. } else {
  100. //atomic replace for existing file
  101. rename_error = !ReplaceFileW(save_path.c_str(), (save_path+".tmp").c_str(), NULL, 2|4, NULL, NULL);
  102. }
  103. if (rename_error && close_fail_notify) {
  104. close_fail_notify(save_path);
  105. }
  106. save_path="";
  107. ERR_FAIL_COND( rename_error );
  108. }
  109. }
  110. bool FileAccessWindows::is_open() const {
  111. return (f!=NULL);
  112. }
  113. void FileAccessWindows::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 FileAccessWindows::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 FileAccessWindows::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 FileAccessWindows::get_len() const {
  132. ERR_FAIL_COND_V(!f,0);
  133. size_t pos = get_pos();
  134. fseek(f,0,SEEK_END);
  135. int size = get_pos();
  136. fseek(f, pos, SEEK_SET);
  137. return size;
  138. }
  139. bool FileAccessWindows::eof_reached() const {
  140. check_errors();
  141. return last_error==ERR_FILE_EOF;
  142. }
  143. uint8_t FileAccessWindows::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 FileAccessWindows::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 FileAccessWindows::get_error() const {
  158. return last_error;
  159. }
  160. void FileAccessWindows::store_8(uint8_t p_dest) {
  161. ERR_FAIL_COND(!f);
  162. fwrite(&p_dest,1,1,f);
  163. }
  164. bool FileAccessWindows::file_exists(const String& p_name) {
  165. FILE *g;
  166. //printf("opening file %s\n", p_fname.c_str());
  167. String filename=fix_path(p_name);
  168. g=_wfopen(filename.c_str(),L"rb");
  169. if (g==NULL) {
  170. return false;
  171. } else {
  172. fclose(g);
  173. return true;
  174. }
  175. }
  176. uint64_t FileAccessWindows::_get_modified_time(const String& p_file) {
  177. String file=fix_path(p_file);
  178. if (file.ends_with("/") && file!="/")
  179. file=file.substr(0,file.length()-1);
  180. struct _stat st;
  181. int rv = _wstat(file.c_str(), &st);
  182. if (rv == 0) {
  183. return st.st_mtime;
  184. } else {
  185. print_line("no access to "+file );
  186. }
  187. ERR_FAIL_V(0);
  188. };
  189. FileAccessWindows::FileAccessWindows() {
  190. f=NULL;
  191. flags=0;
  192. last_error=OK;
  193. }
  194. FileAccessWindows::~FileAccessWindows() {
  195. close();
  196. }
  197. #endif