summaryrefslogtreecommitdiff
path: root/src/shared
diff options
context:
space:
mode:
authorAlban Crequy <alban@endocode.com>2015-03-10 18:15:52 +0100
committerLennart Poettering <lennart@poettering.net>2015-03-10 18:23:46 +0100
commitf85ef957e647c5182acf5e64298f68e4b7fbfe8f (patch)
tree505151f058adc41e72d262d044cc2b0092a148aa /src/shared
parent27cc6f166bdebc0e698fb692993b801db2618866 (diff)
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.
Diffstat (limited to 'src/shared')
-rw-r--r--src/shared/copy.c12
-rw-r--r--src/shared/machine-image.c5
-rw-r--r--src/shared/machine-pool.c5
-rw-r--r--src/shared/util.c41
-rw-r--r--src/shared/util.h2
5 files changed, 58 insertions, 7 deletions
diff --git a/src/shared/copy.c b/src/shared/copy.c
index 0239a58066..f9ec6733be 100644
--- a/src/shared/copy.c
+++ b/src/shared/copy.c
@@ -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;
diff --git a/src/shared/machine-image.c b/src/shared/machine-image.c
index 552847e0f0..00337e7c9f 100644
--- a/src/shared/machine-image.c
+++ b/src/shared/machine-image.c
@@ -440,8 +440,9 @@ int image_rename(Image *i, const char *new_name) {
if (!nn)
return -ENOMEM;
- if (renameat2(AT_FDCWD, i->path, AT_FDCWD, new_path, RENAME_NOREPLACE) < 0)
- return -errno;
+ r = rename_noreplace(AT_FDCWD, i->path, AT_FDCWD, new_path);
+ if (r < 0)
+ return r;
/* Restore the immutable bit, if it was set before */
if (file_attr & FS_IMMUTABLE_FL)
diff --git a/src/shared/machine-pool.c b/src/shared/machine-pool.c
index e7671a3c5e..0fae623944 100644
--- a/src/shared/machine-pool.c
+++ b/src/shared/machine-pool.c
@@ -140,8 +140,9 @@ static int setup_machine_raw(uint64_t size, sd_bus_error *error) {
goto fail;
}
- if (renameat2(AT_FDCWD, tmp, AT_FDCWD, "/var/lib/machines.raw", RENAME_NOREPLACE) < 0) {
- r = sd_bus_error_set_errnof(error, errno, "Failed to move /var/lib/machines.raw into place: %m");
+ r = rename_noreplace(AT_FDCWD, tmp, AT_FDCWD, "/var/lib/machines.raw");
+ if (r < 0) {
+ sd_bus_error_set_errnof(error, r, "Failed to move /var/lib/machines.raw into place: %m");
goto fail;
}
diff --git a/src/shared/util.c b/src/shared/util.c
index b90e10ffb2..55de83023d 100644
--- a/src/shared/util.c
+++ b/src/shared/util.c
@@ -8122,3 +8122,44 @@ void cmsg_close_all(struct msghdr *mh) {
if (cmsg->cmsg_level == SOL_SOCKET && cmsg->cmsg_type == SCM_RIGHTS)
close_many((int*) CMSG_DATA(cmsg), (cmsg->cmsg_len - CMSG_LEN(0)) / sizeof(int));
}
+
+int rename_noreplace(int olddirfd, const char *oldpath, int newdirfd, const char *newpath) {
+ struct stat buf;
+ int ret;
+
+ ret = renameat2(olddirfd, oldpath, newdirfd, newpath, RENAME_NOREPLACE);
+ if (ret >= 0)
+ return 0;
+
+ /* Even though renameat2() exists since Linux 3.15, btrfs added
+ * support for it later. If it is not implemented, fallback to another
+ * method. */
+ if (errno != EINVAL)
+ return -errno;
+
+ /* The link()/unlink() fallback does not work on directories. But
+ * renameat() without RENAME_NOREPLACE gives the same semantics on
+ * directories, except when newpath is an *empty* directory. This is
+ * good enough. */
+ ret = fstatat(olddirfd, oldpath, &buf, AT_SYMLINK_NOFOLLOW);
+ if (ret >= 0 && S_ISDIR(buf.st_mode)) {
+ ret = renameat(olddirfd, oldpath, newdirfd, newpath);
+ return ret >= 0 ? 0 : -errno;
+ }
+
+ /* If it is not a directory, use the link()/unlink() fallback. */
+ ret = linkat(olddirfd, oldpath, newdirfd, newpath, 0);
+ if (ret < 0)
+ return -errno;
+
+ ret = unlinkat(olddirfd, oldpath, 0);
+ if (ret < 0) {
+ /* backup errno before the following unlinkat() alters it */
+ ret = errno;
+ (void) unlinkat(newdirfd, newpath, 0);
+ errno = ret;
+ return -errno;
+ }
+
+ return 0;
+}
diff --git a/src/shared/util.h b/src/shared/util.h
index d2da3e2895..d229e1e68c 100644
--- a/src/shared/util.h
+++ b/src/shared/util.h
@@ -1081,3 +1081,5 @@ void sigkill_wait(pid_t *pid);
int syslog_parse_priority(const char **p, int *priority, bool with_facility);
void cmsg_close_all(struct msghdr *mh);
+
+int rename_noreplace(int olddirfd, const char *oldpath, int newdirfd, const char *newpath);