Merge pull request #27589 from Chaosus/fix_file

Fix File switching between reading and writing, opened with READ_WRITE on Windows
This commit is contained in:
Rémi Verschelde 2019-04-08 12:12:19 +02:00 committed by GitHub
commit d65cbddc8d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 34 additions and 0 deletions

View file

@ -197,12 +197,14 @@ void FileAccessWindows::seek(size_t p_position) {
last_error = OK; last_error = OK;
if (fseek(f, p_position, SEEK_SET)) if (fseek(f, p_position, SEEK_SET))
check_errors(); check_errors();
prev_op = 0;
} }
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 (fseek(f, p_position, SEEK_END)) if (fseek(f, p_position, SEEK_END))
check_errors(); check_errors();
prev_op = 0;
} }
size_t FileAccessWindows::get_position() const { size_t FileAccessWindows::get_position() const {
@ -234,6 +236,12 @@ 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 (prev_op == WRITE) {
fflush(f);
}
prev_op = READ;
}
uint8_t b; uint8_t b;
if (fread(&b, 1, 1, f) == 0) { if (fread(&b, 1, 1, f) == 0) {
check_errors(); check_errors();
@ -246,6 +254,12 @@ uint8_t FileAccessWindows::get_8() const {
int FileAccessWindows::get_buffer(uint8_t *p_dst, int p_length) const { int FileAccessWindows::get_buffer(uint8_t *p_dst, int p_length) const {
ERR_FAIL_COND_V(!f, -1); ERR_FAIL_COND_V(!f, -1);
if (flags == READ_WRITE || flags == WRITE_READ) {
if (prev_op == WRITE) {
fflush(f);
}
prev_op = READ;
}
int read = fread(p_dst, 1, p_length, f); int read = fread(p_dst, 1, p_length, f);
check_errors(); check_errors();
return read; return read;
@ -260,16 +274,34 @@ void FileAccessWindows::flush() {
ERR_FAIL_COND(!f); ERR_FAIL_COND(!f);
fflush(f); fflush(f);
if (prev_op == WRITE)
prev_op = 0;
} }
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 (prev_op == READ) {
if (last_error != ERR_FILE_EOF) {
fseek(f, 0, SEEK_CUR);
}
}
prev_op = WRITE;
}
fwrite(&p_dest, 1, 1, f); fwrite(&p_dest, 1, 1, f);
} }
void FileAccessWindows::store_buffer(const uint8_t *p_src, int p_length) { void FileAccessWindows::store_buffer(const uint8_t *p_src, int p_length) {
ERR_FAIL_COND(!f); ERR_FAIL_COND(!f);
if (flags == READ_WRITE || flags == WRITE_READ) {
if (prev_op == READ) {
if (last_error != ERR_FILE_EOF) {
fseek(f, 0, SEEK_CUR);
}
}
prev_op = WRITE;
}
ERR_FAIL_COND(fwrite(p_src, 1, p_length, f) != p_length); ERR_FAIL_COND(fwrite(p_src, 1, p_length, f) != p_length);
} }
@ -310,6 +342,7 @@ uint64_t FileAccessWindows::_get_modified_time(const String &p_file) {
FileAccessWindows::FileAccessWindows() : FileAccessWindows::FileAccessWindows() :
f(NULL), f(NULL),
flags(0), flags(0),
prev_op(0),
last_error(OK) { last_error(OK) {
} }
FileAccessWindows::~FileAccessWindows() { FileAccessWindows::~FileAccessWindows() {

View file

@ -47,6 +47,7 @@ class FileAccessWindows : public FileAccess {
FILE *f; FILE *f;
int flags; int flags;
void check_errors() const; void check_errors() const;
mutable int prev_op;
mutable Error last_error; mutable Error last_error;
String path; String path;
String path_src; String path_src;