util: add rename_noreplace

renameat2() exists since Linux 3.15 but btrfs support for the flag
RENAME_NOREPLACE was added later.

This patch implements a fallback when renameat2() returns EINVAL.
EINVAL is the error returned when the filesystem does not support one of
the flags.
This commit is contained in:
Alban Crequy
2015-03-10 18:23:46 +01:00
committed by Lennart Poettering
parent 27cc6f166b
commit f85ef957e6
9 changed files with 69 additions and 15 deletions
+9 -3
View File
@@ -404,9 +404,15 @@ int copy_file_atomic(const char *from, const char *to, mode_t mode, bool replace
if (r < 0)
return r;
if (renameat2(AT_FDCWD, t, AT_FDCWD, to, replace ? 0 : RENAME_NOREPLACE) < 0) {
unlink_noerrno(t);
return -errno;
if (replace) {
r = renameat(AT_FDCWD, t, AT_FDCWD, to);
if (r < 0)
r = -errno;
} else
r = rename_noreplace(AT_FDCWD, t, AT_FDCWD, to);
if (r < 0) {
(void) unlink_noerrno(t);
return r;
}
return 0;