Fix #116049, #117754: Renaming fails on linux with certain filesystems

Not all filesystems on linux supports the RENAME_NOREPLACE flag.
If we get a EINVAL return value, retry with a non atomic operation.

RENAME_NOREPLACE was introduced in 050d48edfc, so this is a regression
fix as well.

Pull Request: https://projects.blender.org/blender/blender/pulls/118571
This commit is contained in:
Sebastian Parborg
2024-02-21 16:21:06 +01:00
parent e5e5591c7e
commit b4610f8fc0

View File

@@ -480,7 +480,18 @@ int BLI_rename(const char *from, const char *to)
#elif defined(__GLIBC_PREREQ)
# if __GLIBC_PREREQ(2, 28)
/* Most common Linux cases. */
return renameat2(AT_FDCWD, from, AT_FDCWD, to, RENAME_NOREPLACE);
int ret = renameat2(AT_FDCWD, from, AT_FDCWD, to, RENAME_NOREPLACE);
if (ret < 0 && errno == EINVAL) {
/* Most likely a filesystem that doesn't support RENAME_NOREPLACE.
* (For example NFS, Samba, exFAT, NTFS, etc)
* Retry with a non atomic operation.
*/
if (BLI_exists(to)) {
return 1;
}
return rename(from, to);
}
return ret;
# endif
#else
/* At least all BSD's currently. */