From b4610f8fc01cdf781d93f26f34ca4138e6eca0b1 Mon Sep 17 00:00:00 2001 From: Sebastian Parborg Date: Wed, 21 Feb 2024 16:21:06 +0100 Subject: [PATCH] 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 --- source/blender/blenlib/intern/fileops_c.cc | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/source/blender/blenlib/intern/fileops_c.cc b/source/blender/blenlib/intern/fileops_c.cc index 0046d1af75c..01e0467e75f 100644 --- a/source/blender/blenlib/intern/fileops_c.cc +++ b/source/blender/blenlib/intern/fileops_c.cc @@ -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. */