Skip to content

Commit bbafe86

Browse files
committed
Merge pull request #109383 from bruvzg/link_back_save
[Unix] Replace symlink target, not the link itself when using backup save mode.
2 parents 9d7250f + 8b4e34c commit bbafe86

File tree

3 files changed

+29
-7
lines changed

3 files changed

+29
-7
lines changed

drivers/unix/dir_access_unix.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -437,7 +437,7 @@ Error DirAccessUnix::remove(String p_path) {
437437
}
438438

439439
struct stat flags = {};
440-
if ((lstat(p_path.utf8().get_data(), &flags) != 0)) {
440+
if (lstat(p_path.utf8().get_data(), &flags) != 0) {
441441
return FAILED;
442442
}
443443

@@ -467,7 +467,7 @@ bool DirAccessUnix::is_link(String p_file) {
467467
}
468468

469469
struct stat flags = {};
470-
if ((lstat(p_file.utf8().get_data(), &flags) != 0)) {
470+
if (lstat(p_file.utf8().get_data(), &flags) != 0) {
471471
return false;
472472
}
473473

@@ -484,8 +484,8 @@ String DirAccessUnix::read_link(String p_file) {
484484
p_file = p_file.left(-1);
485485
}
486486

487-
char buf[256];
488-
memset(buf, 0, 256);
487+
char buf[PATH_MAX];
488+
memset(buf, 0, PATH_MAX);
489489
ssize_t len = readlink(p_file.utf8().get_data(), buf, sizeof(buf));
490490
String link;
491491
if (len > 0) {

drivers/unix/file_access_unix.cpp

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,29 @@ Error FileAccessUnix::open_internal(const String &p_path, int p_mode_flags) {
117117
#endif
118118

119119
if (is_backup_save_enabled() && (p_mode_flags == WRITE)) {
120-
save_path = path;
120+
// Set save path to the symlink target, not the link itself.
121+
String link;
122+
bool is_link = false;
123+
{
124+
CharString cs = path.utf8();
125+
struct stat lst = {};
126+
if (lstat(cs.get_data(), &lst) == 0) {
127+
is_link = S_ISLNK(lst.st_mode);
128+
}
129+
if (is_link) {
130+
char buf[PATH_MAX];
131+
memset(buf, 0, PATH_MAX);
132+
ssize_t len = readlink(cs.get_data(), buf, sizeof(buf));
133+
if (len > 0) {
134+
link.append_utf8(buf, len);
135+
}
136+
if (!link.is_absolute_path()) {
137+
link = path.get_base_dir().path_join(link);
138+
}
139+
}
140+
}
141+
save_path = is_link ? link : path;
142+
121143
// Create a temporary file in the same directory as the target file.
122144
path = path + "-XXXXXX";
123145
CharString cs = path.utf8();

drivers/unix/os_unix.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1130,8 +1130,8 @@ String OS_Unix::get_user_data_dir(const String &p_user_dir) const {
11301130
String OS_Unix::get_executable_path() const {
11311131
#ifdef __linux__
11321132
//fix for running from a symlink
1133-
char buf[256];
1134-
memset(buf, 0, 256);
1133+
char buf[PATH_MAX];
1134+
memset(buf, 0, PATH_MAX);
11351135
ssize_t len = readlink("/proc/self/exe", buf, sizeof(buf));
11361136
String b;
11371137
if (len > 0) {

0 commit comments

Comments
 (0)