Merge pull request #51430 from mhilbrunner/windows-files

This commit is contained in:
Rémi Verschelde 2021-08-10 09:55:18 +02:00 committed by GitHub
commit 4bfb49b8f6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -77,8 +77,8 @@ Error FileAccessWindows::_open(const String &p_path, int p_mode_flags) {
return ERR_INVALID_PARAMETER; return ERR_INVALID_PARAMETER;
} }
/* pretty much every implementation that uses fopen as primary /* Pretty much every implementation that uses fopen as primary
backend supports utf8 encoding */ backend supports utf8 encoding. */
struct _stat st; struct _stat st;
if (_wstat((LPCWSTR)(path.utf16().get_data()), &st) == 0) { if (_wstat((LPCWSTR)(path.utf16().get_data()), &st) == 0) {
@ -113,10 +113,10 @@ Error FileAccessWindows::_open(const String &p_path, int p_mode_flags) {
path = path + ".tmp"; path = path + ".tmp";
} }
errno_t errcode = _wfopen_s(&f, (LPCWSTR)(path.utf16().get_data()), mode_string); f = _wfsopen((LPCWSTR)(path.utf16().get_data()), mode_string, _SH_DENYNO);
if (f == nullptr) { if (f == nullptr) {
switch (errcode) { switch (errno) {
case ENOENT: { case ENOENT: {
last_error = ERR_FILE_NOT_FOUND; last_error = ERR_FILE_NOT_FOUND;
} break; } break;
@ -157,10 +157,10 @@ void FileAccessWindows::close() {
#else #else
if (!PathFileExistsW((LPCWSTR)(save_path.utf16().get_data()))) { if (!PathFileExistsW((LPCWSTR)(save_path.utf16().get_data()))) {
#endif #endif
//creating new file // Creating new file
rename_error = _wrename((LPCWSTR)((save_path + ".tmp").utf16().get_data()), (LPCWSTR)(save_path.utf16().get_data())) != 0; rename_error = _wrename((LPCWSTR)((save_path + ".tmp").utf16().get_data()), (LPCWSTR)(save_path.utf16().get_data())) != 0;
} else { } else {
//atomic replace for existing file // Atomic replace for existing file
rename_error = !ReplaceFileW((LPCWSTR)(save_path.utf16().get_data()), (LPCWSTR)((save_path + ".tmp").utf16().get_data()), nullptr, 2 | 4, nullptr, nullptr); rename_error = !ReplaceFileW((LPCWSTR)(save_path.utf16().get_data()), (LPCWSTR)((save_path + ".tmp").utf16().get_data()), nullptr, 2 | 4, nullptr, nullptr);
} }
if (rename_error) { if (rename_error) {
@ -205,6 +205,7 @@ void FileAccessWindows::seek(uint64_t p_position) {
void FileAccessWindows::seek_end(int64_t p_position) { void FileAccessWindows::seek_end(int64_t p_position) {
ERR_FAIL_COND(!f); ERR_FAIL_COND(!f);
if (_fseeki64(f, p_position, SEEK_END)) { if (_fseeki64(f, p_position, SEEK_END)) {
check_errors(); check_errors();
} }
@ -237,6 +238,7 @@ bool FileAccessWindows::eof_reached() const {
uint8_t FileAccessWindows::get_8() const { uint8_t FileAccessWindows::get_8() const {
ERR_FAIL_COND_V(!f, 0); ERR_FAIL_COND_V(!f, 0);
if (flags == READ_WRITE || flags == WRITE_READ) { if (flags == READ_WRITE || flags == WRITE_READ) {
if (prev_op == WRITE) { if (prev_op == WRITE) {
fflush(f); fflush(f);
@ -273,6 +275,7 @@ Error FileAccessWindows::get_error() const {
void FileAccessWindows::flush() { void FileAccessWindows::flush() {
ERR_FAIL_COND(!f); ERR_FAIL_COND(!f);
fflush(f); fflush(f);
if (prev_op == WRITE) { if (prev_op == WRITE) {
prev_op = 0; prev_op = 0;
@ -281,6 +284,7 @@ void FileAccessWindows::flush() {
void FileAccessWindows::store_8(uint8_t p_dest) { void FileAccessWindows::store_8(uint8_t p_dest) {
ERR_FAIL_COND(!f); ERR_FAIL_COND(!f);
if (flags == READ_WRITE || flags == WRITE_READ) { if (flags == READ_WRITE || flags == WRITE_READ) {
if (prev_op == READ) { if (prev_op == READ) {
if (last_error != ERR_FILE_EOF) { if (last_error != ERR_FILE_EOF) {
@ -295,6 +299,7 @@ void FileAccessWindows::store_8(uint8_t p_dest) {
void FileAccessWindows::store_buffer(const uint8_t *p_src, uint64_t p_length) { void FileAccessWindows::store_buffer(const uint8_t *p_src, uint64_t p_length) {
ERR_FAIL_COND(!f); ERR_FAIL_COND(!f);
ERR_FAIL_COND(!p_src && p_length > 0); ERR_FAIL_COND(!p_src && p_length > 0);
if (flags == READ_WRITE || flags == WRITE_READ) { if (flags == READ_WRITE || flags == WRITE_READ) {
if (prev_op == READ) { if (prev_op == READ) {
if (last_error != ERR_FILE_EOF) { if (last_error != ERR_FILE_EOF) {
@ -307,10 +312,8 @@ void FileAccessWindows::store_buffer(const uint8_t *p_src, uint64_t p_length) {
} }
bool FileAccessWindows::file_exists(const String &p_name) { bool FileAccessWindows::file_exists(const String &p_name) {
FILE *g;
//printf("opening file %s\n", p_fname.utf8().get_data());
String filename = fix_path(p_name); String filename = fix_path(p_name);
_wfopen_s(&g, (LPCWSTR)(filename.utf16().get_data()), L"rb"); FILE *g = _wfsopen((LPCWSTR)(filename.utf16().get_data()), L"rb", _SH_DENYNO);
if (g == nullptr) { if (g == nullptr) {
return false; return false;
} else { } else {