summaryrefslogtreecommitdiff
path: root/src/fileops.c
diff options
context:
space:
mode:
authorEdward Thomson <ethomson@github.com>2016-12-15 10:51:02 -0600
committerEdward Thomson <ethomson@github.com>2017-02-28 13:27:50 +0000
commit5312621bd4dc0ba38866db3311462139b2eb8e60 (patch)
treed7bc077d9ea43204aacb9ad89c0eda0e57710d93 /src/fileops.c
parent1c2c0ae2a4d09f5b0de3c4c091c5a93ab34baa46 (diff)
downloadlibgit2-5312621bd4dc0ba38866db3311462139b2eb8e60.tar.gz
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.
Diffstat (limited to 'src/fileops.c')
-rw-r--r--src/fileops.c14
1 files changed, 13 insertions, 1 deletions
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);