[sheepdog] [PATCH stable-0.6 2/3] util: consolidate atomic_create_and_write to be concurrency friendly

Hitoshi Mitake mitake.hitoshi at gmail.com
Sun Jul 14 18:35:15 CEST 2013


From: Liu Yuan <namei.unix at gmail.com>

Only one thread can call this function on the specified path.

Signed-off-by: Liu Yuan <namei.unix at gmail.com>
Signed-off-by: MORITA Kazutaka <morita.kazutaka at lab.ntt.co.jp>
---
 lib/util.c |   12 ++++++++----
 1 files changed, 8 insertions(+), 4 deletions(-)

diff --git a/lib/util.c b/lib/util.c
index 61a1cb5..253a0c5 100644
--- a/lib/util.c
+++ b/lib/util.c
@@ -459,23 +459,27 @@ int atomic_create_and_write(const char *path, char *buf, size_t len)
 
 	snprintf(tmp_path, PATH_MAX, "%s.tmp", path);
 
-	fd = open(tmp_path, O_WRONLY | O_CREAT | O_SYNC, sd_def_fmode);
+	fd = open(tmp_path, O_WRONLY | O_CREAT | O_SYNC | O_EXCL, sd_def_fmode);
 	if (fd < 0) {
-		sd_eprintf("failed to open temporal file, %m");
+		if (errno == EEXIST)
+			sd_dprintf("someone else is dealing with %s", tmp_path);
+		else
+			sd_eprintf("failed to open temporal file %s, %m",
+				   tmp_path);
 		ret = -1;
 		goto end;
 	}
 
 	ret = xwrite(fd, buf, len);
 	if (ret != len) {
-		sd_eprintf("failed to write, %m");
+		sd_eprintf("failed to write %s, %m", path);
 		ret = -1;
 		goto close_fd;
 	}
 
 	ret = rename(tmp_path, path);
 	if (ret < 0) {
-		sd_eprintf("failed to rename, %m");
+		sd_eprintf("failed to rename %s, %m", path);
 		ret = -1;
 	}
 
-- 
1.7.5.1




More information about the sheepdog mailing list