Skip to content

Commit fc77891

Browse files
author
Edward Thomson
committed
git_filebuf: optionally fsync when committing
1 parent a4b5ac6 commit fc77891

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

src/filebuf.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,9 @@ int git_filebuf_open_withsize(git_filebuf *file, const char *path, int flags, mo
291291
if (flags & GIT_FILEBUF_DO_NOT_BUFFER)
292292
file->do_not_buffer = true;
293293

294+
if (flags & GIT_FILEBUF_FSYNC)
295+
file->do_fsync = true;
296+
294297
file->buf_size = size;
295298
file->buf_pos = 0;
296299
file->fd = -1;
@@ -425,6 +428,11 @@ int git_filebuf_commit(git_filebuf *file)
425428

426429
file->fd_is_open = false;
427430

431+
if (file->do_fsync && p_fsync(file->fd) < 0) {
432+
giterr_set(GITERR_OS, "failed to fsync '%s'", file->path_lock);
433+
goto on_error;
434+
}
435+
428436
if (p_close(file->fd) < 0) {
429437
giterr_set(GITERR_OS, "failed to close file at '%s'", file->path_lock);
430438
goto on_error;

src/filebuf.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@
2020
#define GIT_FILEBUF_FORCE (1 << 3)
2121
#define GIT_FILEBUF_TEMPORARY (1 << 4)
2222
#define GIT_FILEBUF_DO_NOT_BUFFER (1 << 5)
23-
#define GIT_FILEBUF_DEFLATE_SHIFT (6)
23+
#define GIT_FILEBUF_FSYNC (1 << 6)
24+
#define GIT_FILEBUF_DEFLATE_SHIFT (7)
2425

2526
#define GIT_FILELOCK_EXTENSION ".lock\0"
2627
#define GIT_FILELOCK_EXTLENGTH 6
@@ -47,6 +48,7 @@ struct git_filebuf {
4748
bool created_lock;
4849
bool did_rename;
4950
bool do_not_buffer;
51+
bool do_fsync;
5052
int last_error;
5153
};
5254

0 commit comments

Comments
 (0)