refdb_fs: optionally fsync packed refs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
diff --git a/src/refdb_fs.c b/src/refdb_fs.c
index 11ff968..fac5ba5 100644
--- a/src/refdb_fs.c
+++ b/src/refdb_fs.c
@@ -994,15 +994,18 @@ static int packed_write(refdb_fs_backend *backend)
{
git_sortedcache *refcache = backend->refcache;
git_filebuf pack_file = GIT_FILEBUF_INIT;
- int error;
+ int error, open_flags = 0;
size_t i;
/* lock the cache to updates while we do this */
if ((error = git_sortedcache_wlock(refcache)) < 0)
return error;
+ if (git_object__synchronized_writing)
+ open_flags = GIT_FILEBUF_FSYNC;
+
/* Open the file! */
- if ((error = git_filebuf_open(&pack_file, git_sortedcache_path(refcache), 0, GIT_PACKEDREFS_FILE_MODE)) < 0)
+ if ((error = git_filebuf_open(&pack_file, git_sortedcache_path(refcache), open_flags, GIT_PACKEDREFS_FILE_MODE)) < 0)
goto fail;
/* Packfiles have a header... apparently
diff --git a/tests/refs/create.c b/tests/refs/create.c
index 1189c47..5c46fb3 100644
--- a/tests/refs/create.c
+++ b/tests/refs/create.c
@@ -303,17 +303,24 @@ void test_refs_create__creating_a_loose_ref_with_invalid_windows_name(void)
void test_refs_create__does_not_fsync_by_default(void)
{
git_reference *ref = NULL;
+ git_refdb *refdb;
git_oid id;
git_oid_fromstr(&id, current_master_tip);
cl_git_pass(git_reference_create(&ref, g_repo, "refs/heads/fsync_test", &id, 0, "log message"));
git_reference_free(ref);
+
+ cl_git_pass(git_repository_refdb(&refdb, g_repo));
+ cl_git_pass(git_refdb_compress(refdb));
+ git_refdb_free(refdb);
+
cl_assert_equal_i(0, p_fsync__cnt);
}
void test_refs_create__fsyncs_when_requested(void)
{
git_reference *ref = NULL;
+ git_refdb *refdb;
git_oid id;
cl_git_pass(git_libgit2_opts(GIT_OPT_ENABLE_SYNCHRONIZED_OBJECT_CREATION, 1));
@@ -323,4 +330,12 @@ void test_refs_create__fsyncs_when_requested(void)
cl_git_pass(git_reference_create(&ref, g_repo, "refs/heads/fsync_test", &id, 0, "log message"));
git_reference_free(ref);
cl_assert_equal_i(2, p_fsync__cnt);
+
+ p_fsync__cnt = 0;
+
+ cl_git_pass(git_repository_refdb(&refdb, g_repo));
+ cl_git_pass(git_refdb_compress(refdb));
+ git_refdb_free(refdb);
+
+ cl_assert_equal_i(1, p_fsync__cnt);
}