[systemd-devel] [PATCH] [PATCH v3] util: add rename_noreplace

2015-03-10 Thread Alban Crequy
From: Alban Crequy al...@endocode.com

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.
---
 src/import/import-raw.c|  5 +++--
 src/import/import-tar.c|  5 +++--
 src/import/pull-raw.c  |  4 ++--
 src/import/pull-tar.c  |  5 +++--
 src/shared/copy.c  | 13 ++---
 src/shared/machine-image.c |  5 +++--
 src/shared/machine-pool.c  |  5 +++--
 src/shared/util.c  | 41 +
 src/shared/util.h  |  2 ++
 9 files changed, 70 insertions(+), 15 deletions(-)

diff --git a/src/import/import-raw.c b/src/import/import-raw.c
index 15e5eb2..c53b0e4 100644
--- a/src/import/import-raw.c
+++ b/src/import/import-raw.c
@@ -245,8 +245,9 @@ static int raw_import_finish(RawImport *i) {
 (void) rm_rf_dangerous(i-final_path, false, true, false);
 }
 
-if (renameat2(AT_FDCWD, i-temp_path, AT_FDCWD, i-final_path, 
RENAME_NOREPLACE)  0)
-return log_error_errno(errno, Failed to move image into 
place: %m);
+r = rename_noreplace(AT_FDCWD, i-temp_path, AT_FDCWD, i-final_path);
+if (r  0)
+return log_error_errno(r, Failed to move image into place: 
%m);
 
 free(i-temp_path);
 i-temp_path = NULL;
diff --git a/src/import/import-tar.c b/src/import/import-tar.c
index d5b6dad..65262d8 100644
--- a/src/import/import-tar.c
+++ b/src/import/import-tar.c
@@ -201,8 +201,9 @@ static int tar_import_finish(TarImport *i) {
 (void) rm_rf_dangerous(i-final_path, false, true, false);
 }
 
-if (renameat2(AT_FDCWD, i-temp_path, AT_FDCWD, i-final_path, 
RENAME_NOREPLACE)  0)
-return log_error_errno(errno, Failed to move image into 
place: %m);
+r = rename_noreplace(AT_FDCWD, i-temp_path, AT_FDCWD, i-final_path);
+if (r  0)
+return log_error_errno(r, Failed to move image into place: 
%m);
 
 free(i-temp_path);
 i-temp_path = NULL;
diff --git a/src/import/pull-raw.c b/src/import/pull-raw.c
index d1d77d5..988fc59 100644
--- a/src/import/pull-raw.c
+++ b/src/import/pull-raw.c
@@ -384,9 +384,9 @@ static void raw_pull_job_on_finished(PullJob *j) {
 if (r  0)
 goto finish;
 
-r = renameat2(AT_FDCWD, i-temp_path, AT_FDCWD, i-final_path, 
RENAME_NOREPLACE);
+r = rename_noreplace(AT_FDCWD, i-temp_path, AT_FDCWD, 
i-final_path);
 if (r  0) {
-r = log_error_errno(errno, Failed to move RAW file 
into place: %m);
+r = log_error_errno(r, Failed to move RAW file into 
place: %m);
 goto finish;
 }
 
diff --git a/src/import/pull-tar.c b/src/import/pull-tar.c
index 504642f..84ad571 100644
--- a/src/import/pull-tar.c
+++ b/src/import/pull-tar.c
@@ -281,8 +281,9 @@ static void tar_pull_job_on_finished(PullJob *j) {
 if (r  0)
 goto finish;
 
-if (renameat2(AT_FDCWD, i-temp_path, AT_FDCWD, i-final_path, 
RENAME_NOREPLACE)  0) {
-r = log_error_errno(errno, Failed to rename to final 
image name: %m);
+r = rename_noreplace(AT_FDCWD, i-temp_path, AT_FDCWD, 
i-final_path);
+if (r  0) {
+r = log_error_errno(r, Failed to rename to final 
image name: %m);
 goto finish;
 }
 
diff --git a/src/shared/copy.c b/src/shared/copy.c
index 0239a58..edbbb3c 100644
--- a/src/shared/copy.c
+++ b/src/shared/copy.c
@@ -404,9 +404,16 @@ 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 c6d2850..440a569 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;
 
  

Re: [systemd-devel] [PATCH] [PATCH v3] util: add rename_noreplace

2015-03-10 Thread Lennart Poettering
On Tue, 10.03.15 18:15, Alban Crequy (alban.cre...@gmail.com) wrote:

 From: Alban Crequy al...@endocode.com
 
 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.

Thanks! Applied. (With some minor fixes)

Lennart

-- 
Lennart Poettering, Red Hat
___
systemd-devel mailing list
systemd-devel@lists.freedesktop.org
http://lists.freedesktop.org/mailman/listinfo/systemd-devel