Commit 5312621bd4dc0ba38866db3311462139b2eb8e60

Edward Thomson 2016-12-15T10:51:02

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.

diff --git a/src/fileops.c b/src/fileops.c
index 57dea8f..ffa692e 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 65c96a6..9a6fc3f 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);