Skip to content

Commit 1c2c0ae

Browse files
author
Edward Thomson
committed
packbuilder: honor git_object__synchronized_writing
Honor `git_object__synchronized_writing` when creating a packfile and corresponding index.
1 parent e6ed0d2 commit 1c2c0ae

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

src/indexer.c

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include "oid.h"
1818
#include "oidmap.h"
1919
#include "zstream.h"
20+
#include "object.h"
2021

2122
extern git_mutex git__mwindow_mutex;
2223

@@ -989,7 +990,9 @@ int git_indexer_commit(git_indexer *idx, git_transfer_progress *stats)
989990
return -1;
990991

991992
if (git_filebuf_open(&index_file, filename.ptr,
992-
GIT_FILEBUF_HASH_CONTENTS, idx->mode) < 0)
993+
GIT_FILEBUF_HASH_CONTENTS |
994+
(git_object__synchronized_writing ? GIT_FILEBUF_FSYNC : 0),
995+
idx->mode) < 0)
993996
goto on_error;
994997

995998
/* Write out the header */
@@ -1066,6 +1069,11 @@ int git_indexer_commit(git_indexer *idx, git_transfer_progress *stats)
10661069
return -1;
10671070
}
10681071

1072+
if (git_object__synchronized_writing && p_fsync(idx->pack->mwf.fd) < 0) {
1073+
giterr_set(GITERR_OS, "failed to fsync packfile");
1074+
goto on_error;
1075+
}
1076+
10691077
/* We need to close the descriptor here so Windows doesn't choke on commit_at */
10701078
if (p_close(idx->pack->mwf.fd) < 0) {
10711079
giterr_set(GITERR_OS, "failed to close packfile");

tests/pack/packbuilder.c

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,16 @@ void test_pack_packbuilder__initialize(void)
2323
cl_git_pass(git_vector_init(&_commits, 0, NULL));
2424
_commits_is_initialized = 1;
2525
memset(&_stats, 0, sizeof(_stats));
26+
p_fsync__cnt = 0;
2627
}
2728

2829
void test_pack_packbuilder__cleanup(void)
2930
{
3031
git_oid *o;
3132
unsigned int i;
3233

34+
cl_git_pass(git_libgit2_opts(GIT_OPT_ENABLE_SYNCHRONIZED_OBJECT_CREATION, 0));
35+
3336
if (_commits_is_initialized) {
3437
_commits_is_initialized = 0;
3538
git_vector_foreach(&_commits, i, o) {
@@ -188,6 +191,22 @@ void test_pack_packbuilder__permissions_readwrite(void)
188191
test_write_pack_permission(0666, 0666);
189192
}
190193

194+
void test_pack_packbuilder__does_not_fsync_by_default(void)
195+
{
196+
seed_packbuilder();
197+
git_packbuilder_write(_packbuilder, ".", 0666, NULL, NULL);
198+
cl_assert_equal_sz(0, p_fsync__cnt);
199+
}
200+
201+
void test_pack_packbuilder__fsync_when_asked(void)
202+
{
203+
cl_git_pass(git_libgit2_opts(GIT_OPT_ENABLE_SYNCHRONIZED_OBJECT_CREATION, 1));
204+
p_fsync__cnt = 0;
205+
seed_packbuilder();
206+
git_packbuilder_write(_packbuilder, ".", 0666, NULL, NULL);
207+
cl_assert_equal_sz(2, p_fsync__cnt);
208+
}
209+
191210
static int foreach_cb(void *buf, size_t len, void *payload)
192211
{
193212
git_indexer *idx = (git_indexer *) payload;

0 commit comments

Comments
 (0)