git-commit-vandalism/refs
Patrick Steinhardt ce54672f9b refs: fix corruption by not correctly syncing packed-refs to disk
At GitLab we have recently received a report where a repository was left
with a corrupted `packed-refs` file after the node hard-crashed even
though `core.fsync=reference` was set. This is something that in theory
should not happen if we correctly did the atomic-rename dance to:

    1. Write the data into a temporary file.

    2. Synchronize the temporary file to disk.

    3. Rename the temporary file into place.

So if we crash in the middle of writing the `packed-refs` file we should
only ever see either the old or the new state of the file.

And while we do the dance when writing the `packed-refs` file, there is
indeed one gotcha: we use a `FILE *` stream to write the temporary file,
but don't flush it before synchronizing it to disk. As a consequence any
data that is still buffered will not get synchronized and a crash of the
machine may cause corruption.

Fix this bug by flushing the file stream before we fsync.

Signed-off-by: Patrick Steinhardt <ps@pks.im>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2022-12-25 16:18:12 +09:00
..
debug.c refs: centralize initialization of the base ref_store. 2021-12-22 13:51:38 -08:00
files-backend.c core.fsync: new option to harden references 2022-03-15 13:30:58 -07:00
iterator.c use CALLOC_ARRAY 2021-03-13 16:00:09 -08:00
packed-backend.c refs: fix corruption by not correctly syncing packed-refs to disk 2022-12-25 16:18:12 +09:00
packed-backend.h Merge branch 'ps/avoid-unnecessary-hook-invocation-with-packed-refs' 2022-02-18 13:53:27 -08:00
ref-cache.c Merge branch 'jt/no-abuse-alternate-odb-for-submodules' 2021-10-25 16:06:56 -07:00
ref-cache.h Merge branch 'jt/no-abuse-alternate-odb-for-submodules' 2021-10-25 16:06:56 -07:00
refs-internal.h Merge branch 'ps/avoid-unnecessary-hook-invocation-with-packed-refs' 2022-02-18 13:53:27 -08:00