summaryrefslogtreecommitdiffstats
path: root/drivers/unix/file_access_unix.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/unix/file_access_unix.cpp')
-rw-r--r--drivers/unix/file_access_unix.cpp99
1 files changed, 60 insertions, 39 deletions
diff --git a/drivers/unix/file_access_unix.cpp b/drivers/unix/file_access_unix.cpp
index 0b80fb1491..e9affe41af 100644
--- a/drivers/unix/file_access_unix.cpp
+++ b/drivers/unix/file_access_unix.cpp
@@ -1,32 +1,32 @@
-/*************************************************************************/
-/* file_access_unix.cpp */
-/*************************************************************************/
-/* This file is part of: */
-/* GODOT ENGINE */
-/* https://godotengine.org */
-/*************************************************************************/
-/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
-/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
-/* */
-/* Permission is hereby granted, free of charge, to any person obtaining */
-/* a copy of this software and associated documentation files (the */
-/* "Software"), to deal in the Software without restriction, including */
-/* without limitation the rights to use, copy, modify, merge, publish, */
-/* distribute, sublicense, and/or sell copies of the Software, and to */
-/* permit persons to whom the Software is furnished to do so, subject to */
-/* the following conditions: */
-/* */
-/* The above copyright notice and this permission notice shall be */
-/* included in all copies or substantial portions of the Software. */
-/* */
-/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
-/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
-/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
-/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
-/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
-/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
-/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
-/*************************************************************************/
+/**************************************************************************/
+/* file_access_unix.cpp */
+/**************************************************************************/
+/* This file is part of: */
+/* GODOT ENGINE */
+/* https://godotengine.org */
+/**************************************************************************/
+/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
+/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
+/* */
+/* Permission is hereby granted, free of charge, to any person obtaining */
+/* a copy of this software and associated documentation files (the */
+/* "Software"), to deal in the Software without restriction, including */
+/* without limitation the rights to use, copy, modify, merge, publish, */
+/* distribute, sublicense, and/or sell copies of the Software, and to */
+/* permit persons to whom the Software is furnished to do so, subject to */
+/* the following conditions: */
+/* */
+/* The above copyright notice and this permission notice shall be */
+/* included in all copies or substantial portions of the Software. */
+/* */
+/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
+/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
+/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
+/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
+/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
+/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
+/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
+/**************************************************************************/
#include "file_access_unix.h"
@@ -100,11 +100,28 @@ Error FileAccessUnix::open_internal(const String &p_path, int p_mode_flags) {
if (is_backup_save_enabled() && (p_mode_flags == WRITE)) {
save_path = path;
- path = path + ".tmp";
+ // Create a temporary file in the same directory as the target file.
+ path = path + "-XXXXXX";
+ CharString cs = path.utf8();
+ int fd = mkstemp(cs.ptrw());
+ if (fd == -1) {
+ last_error = ERR_FILE_CANT_OPEN;
+ return last_error;
+ }
+ path = String::utf8(cs.ptr());
+
+ 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);
}
- f = fopen(path.utf8().get_data(), mode_string);
-
if (f == nullptr) {
switch (errno) {
case ENOENT: {
@@ -143,7 +160,7 @@ void FileAccessUnix::_close() {
}
if (!save_path.is_empty()) {
- int rename_error = rename((save_path + ".tmp").utf8().get_data(), save_path.utf8().get_data());
+ int rename_error = rename(path.utf8().get_data(), save_path.utf8().get_data());
if (rename_error && close_fail_notify) {
close_fail_notify(save_path);
@@ -284,11 +301,11 @@ bool FileAccessUnix::file_exists(const String &p_path) {
uint64_t FileAccessUnix::_get_modified_time(const String &p_file) {
String file = fix_path(p_file);
- struct stat flags = {};
- int err = stat(file.utf8().get_data(), &flags);
+ struct stat status = {};
+ int err = stat(file.utf8().get_data(), &status);
if (!err) {
- return flags.st_mtime;
+ return status.st_mtime;
} else {
print_verbose("Failed to get modified time for: " + p_file + "");
return 0;
@@ -297,11 +314,11 @@ uint64_t FileAccessUnix::_get_modified_time(const String &p_file) {
uint32_t FileAccessUnix::_get_unix_permissions(const String &p_file) {
String file = fix_path(p_file);
- struct stat flags = {};
- int err = stat(file.utf8().get_data(), &flags);
+ struct stat status = {};
+ int err = stat(file.utf8().get_data(), &status);
if (!err) {
- return flags.st_mode & 0x7FF; //only permissions
+ return status.st_mode & 0x7FF; //only permissions
} else {
ERR_FAIL_V_MSG(0, "Failed to get unix permissions for: " + p_file + ".");
}
@@ -318,6 +335,10 @@ Error FileAccessUnix::_set_unix_permissions(const String &p_file, uint32_t p_per
return FAILED;
}
+void FileAccessUnix::close() {
+ _close();
+}
+
CloseNotificationFunc FileAccessUnix::close_notification_func = nullptr;
FileAccessUnix::~FileAccessUnix() {