Skip to content

Commit af3dcb0

Browse files
author
Edward Thomson
committed
refdb_fs: optionally fsync loose references
1 parent 5312621 commit af3dcb0

File tree

2 files changed

+40
-4
lines changed

2 files changed

+40
-4
lines changed

src/refdb_fs.c

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -736,7 +736,7 @@ static int reference_path_available(
736736

737737
static int loose_lock(git_filebuf *file, refdb_fs_backend *backend, const char *name)
738738
{
739-
int error;
739+
int error, filebuf_flags;
740740
git_buf ref_path = GIT_BUF_INIT;
741741

742742
assert(file && backend && name);
@@ -755,7 +755,11 @@ static int loose_lock(git_filebuf *file, refdb_fs_backend *backend, const char *
755755
if (git_buf_joinpath(&ref_path, backend->gitpath, name) < 0)
756756
return -1;
757757

758-
error = git_filebuf_open(file, ref_path.ptr, GIT_FILEBUF_FORCE, GIT_REFS_FILE_MODE);
758+
filebuf_flags = GIT_FILEBUF_FORCE;
759+
if (git_object__synchronized_writing)
760+
filebuf_flags |= GIT_FILEBUF_FSYNC;
761+
762+
error = git_filebuf_open(file, ref_path.ptr, filebuf_flags, GIT_REFS_FILE_MODE);
759763

760764
if (error == GIT_EDIRECTORY)
761765
giterr_set(GITERR_REFERENCE, "cannot lock ref '%s', there are refs beneath that folder", name);
@@ -1784,7 +1788,7 @@ static int refdb_reflog_fs__write(git_refdb_backend *_backend, git_reflog *reflo
17841788
/* Append to the reflog, must be called under reference lock */
17851789
static int reflog_append(refdb_fs_backend *backend, const git_reference *ref, const git_oid *old, const git_oid *new, const git_signature *who, const char *message)
17861790
{
1787-
int error, is_symbolic;
1791+
int error, is_symbolic, open_flags;
17881792
git_oid old_id = {{0}}, new_id = {{0}};
17891793
git_buf buf = GIT_BUF_INIT, path = GIT_BUF_INIT;
17901794
git_repository *repo = backend->repo;
@@ -1852,7 +1856,12 @@ static int reflog_append(refdb_fs_backend *backend, const git_reference *ref, co
18521856
goto cleanup;
18531857
}
18541858

1855-
error = git_futils_writebuffer(&buf, git_buf_cstr(&path), O_WRONLY|O_CREAT|O_APPEND, GIT_REFLOG_FILE_MODE);
1859+
open_flags = O_WRONLY | O_CREAT | O_APPEND;
1860+
1861+
if (git_object__synchronized_writing)
1862+
open_flags |= O_FSYNC;
1863+
1864+
error = git_futils_writebuffer(&buf, git_buf_cstr(&path), open_flags, GIT_REFLOG_FILE_MODE);
18561865

18571866
cleanup:
18581867
git_buf_free(&buf);

tests/refs/create.c

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ static git_repository *g_repo;
1313
void test_refs_create__initialize(void)
1414
{
1515
g_repo = cl_git_sandbox_init("testrepo");
16+
p_fsync__cnt = 0;
1617
}
1718

1819
void test_refs_create__cleanup(void)
@@ -21,6 +22,7 @@ void test_refs_create__cleanup(void)
2122

2223
cl_git_pass(git_libgit2_opts(GIT_OPT_ENABLE_STRICT_OBJECT_CREATION, 1));
2324
cl_git_pass(git_libgit2_opts(GIT_OPT_ENABLE_STRICT_SYMBOLIC_REF_CREATION, 1));
25+
cl_git_pass(git_libgit2_opts(GIT_OPT_ENABLE_SYNCHRONIZED_OBJECT_CREATION, 0));
2426
}
2527

2628
void test_refs_create__symbolic(void)
@@ -297,3 +299,28 @@ void test_refs_create__creating_a_loose_ref_with_invalid_windows_name(void)
297299

298300
test_win32_name("refs/heads/com1");
299301
}
302+
303+
void test_refs_create__does_not_fsync_by_default(void)
304+
{
305+
git_reference *ref = NULL;
306+
git_oid id;
307+
308+
git_oid_fromstr(&id, current_master_tip);
309+
cl_git_pass(git_reference_create(&ref, g_repo, "refs/heads/fsync_test", &id, 0, "log message"));
310+
git_reference_free(ref);
311+
cl_assert_equal_i(0, p_fsync__cnt);
312+
}
313+
314+
void test_refs_create__fsyncs_when_requested(void)
315+
{
316+
git_reference *ref = NULL;
317+
git_oid id;
318+
319+
cl_git_pass(git_libgit2_opts(GIT_OPT_ENABLE_SYNCHRONIZED_OBJECT_CREATION, 1));
320+
p_fsync__cnt = 0;
321+
322+
git_oid_fromstr(&id, current_master_tip);
323+
cl_git_pass(git_reference_create(&ref, g_repo, "refs/heads/fsync_test", &id, 0, "log message"));
324+
git_reference_free(ref);
325+
cl_assert_equal_i(2, p_fsync__cnt);
326+
}

0 commit comments

Comments
 (0)