From 5312621bd4dc0ba38866db3311462139b2eb8e60 Mon Sep 17 00:00:00 2001 From: Edward Thomson Date: Thu, 15 Dec 2016 10:51:02 -0600 Subject: [PATCH] git_futils_writebuffer: optionally fsync Add a custom `O_FSYNC` bit (if it's not been defined by the operating system`) so that `git_futils_writebuffer` can optionally do an `fsync` when it's done writing. We call `fsync` ourselves, even on systems that define `O_FSYNC` because its definition is no guarantee of its actual support. Mac, for instance, defines it but doesn't support it in an `open(2)` call. --- src/fileops.c | 14 +++++++++++++- src/fileops.h | 7 +++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/src/fileops.c b/src/fileops.c index 57dea8fce..ffa692eed 100644 --- a/src/fileops.c +++ b/src/fileops.c @@ -235,10 +235,16 @@ int git_futils_readbuffer(git_buf *buf, const char *path) int git_futils_writebuffer( const git_buf *buf, const char *path, int flags, mode_t mode) { - int fd, error = 0; + int fd, do_fsync = 0, error = 0; + + if ((flags & O_FSYNC) != 0) + do_fsync = 1; + + flags &= ~O_FSYNC; if (flags <= 0) flags = O_CREAT | O_TRUNC | O_WRONLY; + if (!mode) mode = GIT_FILEMODE_BLOB; @@ -253,6 +259,12 @@ int git_futils_writebuffer( return error; } + if (do_fsync && (error = p_fsync(fd)) < 0) { + giterr_set(GITERR_OS, "could not fsync '%s'", path); + p_close(fd); + return error; + } + if ((error = p_close(fd)) < 0) giterr_set(GITERR_OS, "error while closing '%s'", path); diff --git a/src/fileops.h b/src/fileops.h index 65c96a6f5..9a6fc3f6b 100644 --- a/src/fileops.h +++ b/src/fileops.h @@ -25,6 +25,13 @@ extern int git_futils_readbuffer_updated( git_buf *obj, const char *path, git_oid *checksum, int *updated); extern int git_futils_readbuffer_fd(git_buf *obj, git_file fd, size_t len); +/* Additional constants for `git_futils_writebuffer`'s `open_flags`. We + * support these internally and they will be removed before the `open` call. + */ +#ifndef O_FSYNC +# define O_FSYNC (1 << 31) +#endif + extern int git_futils_writebuffer( const git_buf *buf, const char *path, int open_flags, mode_t mode);