Skip to content

Commit 1229e1c

Browse files
author
Edward Thomson
committed
fsync parent directories when fsyncing
When fsync'ing files, fsync the parent directory in the case where we rename a file into place, or create a new file, to ensure that the directory entry is flushed correctly.
1 parent 5a747e0 commit 1229e1c

File tree

6 files changed

+63
-5
lines changed

6 files changed

+63
-5
lines changed

src/filebuf.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -445,6 +445,9 @@ int git_filebuf_commit(git_filebuf *file)
445445
goto on_error;
446446
}
447447

448+
if (file->do_fsync && git_futils_fsync_parent(file->path_original) < 0)
449+
goto on_error;
450+
448451
file->did_rename = true;
449452

450453
git_filebuf_cleanup(file);

src/fileops.c

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -265,8 +265,13 @@ int git_futils_writebuffer(
265265
return error;
266266
}
267267

268-
if ((error = p_close(fd)) < 0)
268+
if ((error = p_close(fd)) < 0) {
269269
giterr_set(GITERR_OS, "error while closing '%s'", path);
270+
return error;
271+
}
272+
273+
if (do_fsync && (flags & O_CREAT))
274+
error = git_futils_fsync_parent(path);
270275

271276
return error;
272277
}
@@ -1119,3 +1124,28 @@ void git_futils_filestamp_set_from_stat(
11191124
memset(stamp, 0, sizeof(*stamp));
11201125
}
11211126
}
1127+
1128+
int git_futils_fsync_dir(const char *path)
1129+
{
1130+
int fd, error = -1;
1131+
1132+
if ((fd = p_open(path, O_RDONLY)) < 0) {
1133+
giterr_set(GITERR_OS, "failed to open directory '%s' for fsync", path);
1134+
return -1;
1135+
}
1136+
1137+
if ((error = p_fsync(fd)) < 0)
1138+
giterr_set(GITERR_OS, "failed to fsync directory '%s'", path);
1139+
1140+
p_close(fd);
1141+
return error;
1142+
}
1143+
1144+
int git_futils_fsync_parent(const char *path)
1145+
{
1146+
char *parent = git_path_dirname(path);
1147+
int error = git_futils_fsync_dir(parent);
1148+
1149+
git__free(parent);
1150+
return error;
1151+
}

src/fileops.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -363,4 +363,22 @@ extern void git_futils_filestamp_set(
363363
extern void git_futils_filestamp_set_from_stat(
364364
git_futils_filestamp *stamp, struct stat *st);
365365

366+
/**
367+
* `fsync` the parent directory of the given path, if `fsync` is
368+
* supported for directories on this platform.
369+
*
370+
* @param path Path of the directory to sync.
371+
* @return 0 on success, -1 on error
372+
*/
373+
extern int git_futils_fsync_dir(const char *path);
374+
375+
/**
376+
* `fsync` the parent directory of the given path, if `fsync` is
377+
* supported for directories on this platform.
378+
*
379+
* @param path Path of the file whose parent directory should be synced.
380+
* @return 0 on success, -1 on error
381+
*/
382+
extern int git_futils_fsync_parent(const char *path);
383+
366384
#endif /* INCLUDE_fileops_h__ */

src/indexer.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1086,7 +1086,14 @@ int git_indexer_commit(git_indexer *idx, git_transfer_progress *stats)
10861086
goto on_error;
10871087

10881088
/* And don't forget to rename the packfile to its new place. */
1089-
p_rename(idx->pack->pack_name, git_buf_cstr(&filename));
1089+
if (p_rename(idx->pack->pack_name, git_buf_cstr(&filename)) < 0)
1090+
goto on_error;
1091+
1092+
/* And fsync the parent directory if we're asked to. */
1093+
if (git_object__synchronized_writing &&
1094+
git_futils_fsync_parent(git_buf_cstr(&filename)) < 0)
1095+
goto on_error;
1096+
10901097
idx->pack_committed = 1;
10911098

10921099
git_buf_free(&filename);

tests/pack/packbuilder.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ void test_pack_packbuilder__fsync_when_asked(void)
204204
p_fsync__cnt = 0;
205205
seed_packbuilder();
206206
git_packbuilder_write(_packbuilder, ".", 0666, NULL, NULL);
207-
cl_assert_equal_sz(2, p_fsync__cnt);
207+
cl_assert_equal_sz(4, p_fsync__cnt);
208208
}
209209

210210
static int foreach_cb(void *buf, size_t len, void *payload)

tests/refs/create.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -329,13 +329,13 @@ void test_refs_create__fsyncs_when_requested(void)
329329
git_oid_fromstr(&id, current_master_tip);
330330
cl_git_pass(git_reference_create(&ref, g_repo, "refs/heads/fsync_test", &id, 0, "log message"));
331331
git_reference_free(ref);
332-
cl_assert_equal_i(2, p_fsync__cnt);
332+
cl_assert_equal_i(4, p_fsync__cnt);
333333

334334
p_fsync__cnt = 0;
335335

336336
cl_git_pass(git_repository_refdb(&refdb, g_repo));
337337
cl_git_pass(git_refdb_compress(refdb));
338338
git_refdb_free(refdb);
339339

340-
cl_assert_equal_i(1, p_fsync__cnt);
340+
cl_assert_equal_i(2, p_fsync__cnt);
341341
}

0 commit comments

Comments
 (0)