summaryrefslogtreecommitdiff
path: root/src
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
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')
-rw-r--r--src/fileops.c14
-rw-r--r--src/fileops.h7
2 files changed, 20 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);
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);