Merge pull request #75074 from bruvzg/fix_unix_temp_files

Fix Unix temp file creations when using is_backup_save_enabled.
This commit is contained in:
Rémi Verschelde 2023-03-19 00:37:33 -07:00 committed by GitHub
commit 4d5f10fc3a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 17 additions and 5 deletions

View File

@ -102,13 +102,25 @@ Error FileAccessUnix::open_internal(const String &p_path, int p_mode_flags) {
save_path = path;
// Create a temporary file in the same directory as the target file.
path = path + "-XXXXXX";
if (!mkstemp(path.utf8().ptrw())) {
return ERR_FILE_CANT_OPEN;
CharString cs = path.utf8();
int fd = mkstemp(cs.ptrw());
if (fd == -1) {
last_error = ERR_FILE_CANT_OPEN;
return last_error;
}
path = path + ".tmp";
}
path = String::utf8(cs.ptr());
f = fopen(path.utf8().get_data(), mode_string);
f = fdopen(fd, mode_string);
if (f == nullptr) {
// Delete temp file and close descriptor if open failed.
::unlink(cs.ptr());
::close(fd);
last_error = ERR_FILE_CANT_OPEN;
return last_error;
}
} else {
f = fopen(path.utf8().get_data(), mode_string);
}
if (f == nullptr) {
switch (errno) {