[sheepdog] [PATCH v2 3/4] util: add a new API atomic_create_and_write()
Hitoshi Mitake
mitake.hitoshi at lab.ntt.co.jp
Wed Apr 17 09:58:50 CEST 2013
This patch adds a new API atomic_create_and_write(). As the name
implies, the API creates and writes data to a file.
atomic_create_and_write() creates a file path.tmp (path is a
parameter) internally. During the initialization process of sheep,
every user of this API should detect and clean temporal files.
Signed-off-by: Hitoshi Mitake <mitake.hitoshi at lab.ntt.co.jp>
---
v2:
- don't panic() even if temporal files reamin
include/util.h | 1 +
lib/util.c | 35 +++++++++++++++++++++++++++++++++++
2 files changed, 36 insertions(+), 0 deletions(-)
diff --git a/include/util.h b/include/util.h
index 4274850..dbac48d 100644
--- a/include/util.h
+++ b/include/util.h
@@ -96,6 +96,7 @@ bool is_xattr_enabled(const char *path);
void trim_zero_sectors(void *buf, uint64_t *offset, uint32_t *len);
void untrim_zero_sectors(void *buf, uint64_t offset, uint32_t len,
uint32_t requested_len);
+int atomic_create_and_write(const char *path, char *buf, size_t len);
#ifdef assert
#undef assert
diff --git a/lib/util.c b/lib/util.c
index 8338bac..84277b3 100644
--- a/lib/util.c
+++ b/lib/util.c
@@ -22,6 +22,7 @@
#include <ctype.h>
#include <signal.h>
#include <sys/xattr.h>
+#include <fcntl.h>
#include "util.h"
#include "logger.h"
@@ -437,3 +438,37 @@ bool is_xattr_enabled(const char *path)
return !(ret == -1 && errno == ENOTSUP);
}
+
+int atomic_create_and_write(const char *path, char *buf, size_t len)
+{
+ int fd, ret;
+ char tmp_path[PATH_MAX];
+
+ snprintf(tmp_path, PATH_MAX, "%s.tmp", path);
+
+ fd = open(tmp_path, O_WRONLY | O_CREAT | O_SYNC, sd_def_fmode);
+ if (fd < 0) {
+ sd_eprintf("failed to temporal file for config, %m");
+ ret = -1;
+ goto end;
+ }
+
+ ret = xwrite(fd, buf, len);
+ if (ret != len) {
+ sd_eprintf("failed to write config data, %m");
+ ret = -1;
+ goto close_fd;
+ }
+
+ ret = rename(tmp_path, path);
+ if (ret < 0) {
+ sd_eprintf("failed to rename, %m");
+ ret = -1;
+ }
+
+close_fd:
+ close(fd);
+end:
+ return ret;
+}
+
--
1.7.2.5
More information about the sheepdog
mailing list