Commit 5312621b by Edward Thomson

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.
parent 1c2c0ae2
...@@ -235,10 +235,16 @@ int git_futils_readbuffer(git_buf *buf, const char *path) ...@@ -235,10 +235,16 @@ int git_futils_readbuffer(git_buf *buf, const char *path)
int git_futils_writebuffer( int git_futils_writebuffer(
const git_buf *buf, const char *path, int flags, mode_t mode) 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) if (flags <= 0)
flags = O_CREAT | O_TRUNC | O_WRONLY; flags = O_CREAT | O_TRUNC | O_WRONLY;
if (!mode) if (!mode)
mode = GIT_FILEMODE_BLOB; mode = GIT_FILEMODE_BLOB;
...@@ -253,6 +259,12 @@ int git_futils_writebuffer( ...@@ -253,6 +259,12 @@ int git_futils_writebuffer(
return error; 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) if ((error = p_close(fd)) < 0)
giterr_set(GITERR_OS, "error while closing '%s'", path); giterr_set(GITERR_OS, "error while closing '%s'", path);
......
...@@ -25,6 +25,13 @@ extern int git_futils_readbuffer_updated( ...@@ -25,6 +25,13 @@ extern int git_futils_readbuffer_updated(
git_buf *obj, const char *path, git_oid *checksum, int *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); 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( extern int git_futils_writebuffer(
const git_buf *buf, const char *path, int open_flags, mode_t mode); const git_buf *buf, const char *path, int open_flags, mode_t mode);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment