[sheepdog] [PATCH] util: consolidate atomic_create_and_write to be concurrency friendly

Liu Yuan namei.unix at gmail.com
Thu Jun 20 05:00:35 CEST 2013


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

Signed-off-by: Liu Yuan <namei.unix at gmail.com>
---
 lib/util.c |    7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)

diff --git a/lib/util.c b/lib/util.c
index 61a1cb5..fff546e 100644
--- a/lib/util.c
+++ b/lib/util.c
@@ -459,8 +459,13 @@ 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) {
+		if (errno == EEXIST) {
+			sd_dprintf("someone else is dealing with %s", path);
+			ret = 0;
+			goto end;
+		}
 		sd_eprintf("failed to open temporal file, %m");
 		ret = -1;
 		goto end;
-- 
1.7.9.5




More information about the sheepdog mailing list