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.hito...@lab.ntt.co.jp> --- include/util.h | 1 + lib/util.c | 39 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+), 0 deletions(-) diff --git a/include/util.h b/include/util.h index 38efb8b..9faa21b 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 aac0fa9..498fad8 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" @@ -434,3 +435,41 @@ 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); + if (!access(tmp_path, F_OK)) + panic("temporal file: %s exists, this implies invalid usage of" + " atomic_create_and_write() of sheepdog", tmp_path); + + fd = open(tmp_path, O_WRONLY | O_CREAT | O_SYNC, + S_IWUSR | S_IRUSR | S_IWGRP | S_IRGRP); + 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 -- sheepdog mailing list sheepdog@lists.wpkg.org http://lists.wpkg.org/mailman/listinfo/sheepdog