Skip to content

Commit ba95e96

Browse files
neerajsi-msftgitster
authored andcommitted
core.fsync: new option to harden the index
This commit introduces the new ability for the user to harden the index. In the event of a system crash, the index must be durable for the user to actually find a file that has been added to the repo and then deleted from the working tree. We use the presence of the COMMIT_LOCK flag and absence of the alternate_index_output as a proxy for determining whether we're updating the persistent index of the repo or some temporary index. We don't sync these temporary indexes. Signed-off-by: Neeraj Singh <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 844a8ad commit ba95e96

File tree

3 files changed

+15
-6
lines changed

3 files changed

+15
-6
lines changed

cache.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1004,6 +1004,7 @@ enum fsync_component {
10041004
FSYNC_COMPONENT_PACK = 1 << 1,
10051005
FSYNC_COMPONENT_PACK_METADATA = 1 << 2,
10061006
FSYNC_COMPONENT_COMMIT_GRAPH = 1 << 3,
1007+
FSYNC_COMPONENT_INDEX = 1 << 4,
10071008
};
10081009

10091010
#define FSYNC_COMPONENTS_DEFAULT (FSYNC_COMPONENT_PACK | \

config.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1331,6 +1331,7 @@ static const struct fsync_component_name {
13311331
{ "pack", FSYNC_COMPONENT_PACK },
13321332
{ "pack-metadata", FSYNC_COMPONENT_PACK_METADATA },
13331333
{ "commit-graph", FSYNC_COMPONENT_COMMIT_GRAPH },
1334+
{ "index", FSYNC_COMPONENT_INDEX },
13341335
};
13351336

13361337
static enum fsync_component parse_fsync_components(const char *var, const char *string)

read-cache.c

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2842,7 +2842,7 @@ static int record_ieot(void)
28422842
* rely on it.
28432843
*/
28442844
static int do_write_index(struct index_state *istate, struct tempfile *tempfile,
2845-
int strip_extensions)
2845+
int strip_extensions, unsigned flags)
28462846
{
28472847
uint64_t start = getnanotime();
28482848
struct hashfile *f;
@@ -2856,6 +2856,7 @@ static int do_write_index(struct index_state *istate, struct tempfile *tempfile,
28562856
struct strbuf previous_name_buf = STRBUF_INIT, *previous_name;
28572857
int drop_cache_tree = istate->drop_cache_tree;
28582858
off_t offset;
2859+
int csum_fsync_flag;
28592860
int ieot_entries = 1;
28602861
struct index_entry_offset_table *ieot = NULL;
28612862
int nr, nr_threads;
@@ -3089,7 +3090,13 @@ static int do_write_index(struct index_state *istate, struct tempfile *tempfile,
30893090
return -1;
30903091
}
30913092

3092-
finalize_hashfile(f, istate->oid.hash, FSYNC_COMPONENT_NONE, CSUM_HASH_IN_STREAM);
3093+
csum_fsync_flag = 0;
3094+
if (!alternate_index_output && (flags & COMMIT_LOCK))
3095+
csum_fsync_flag = CSUM_FSYNC;
3096+
3097+
finalize_hashfile(f, istate->oid.hash, FSYNC_COMPONENT_INDEX,
3098+
CSUM_HASH_IN_STREAM | csum_fsync_flag);
3099+
30933100
if (close_tempfile_gently(tempfile)) {
30943101
error(_("could not close '%s'"), get_tempfile_path(tempfile));
30953102
return -1;
@@ -3144,7 +3151,7 @@ static int do_write_locked_index(struct index_state *istate, struct lock_file *l
31443151
*/
31453152
trace2_region_enter_printf("index", "do_write_index", the_repository,
31463153
"%s", get_lock_file_path(lock));
3147-
ret = do_write_index(istate, lock->tempfile, 0);
3154+
ret = do_write_index(istate, lock->tempfile, 0, flags);
31483155
trace2_region_leave_printf("index", "do_write_index", the_repository,
31493156
"%s", get_lock_file_path(lock));
31503157

@@ -3238,7 +3245,7 @@ static int clean_shared_index_files(const char *current_hex)
32383245
}
32393246

32403247
static int write_shared_index(struct index_state *istate,
3241-
struct tempfile **temp)
3248+
struct tempfile **temp, unsigned flags)
32423249
{
32433250
struct split_index *si = istate->split_index;
32443251
int ret, was_full = !istate->sparse_index;
@@ -3248,7 +3255,7 @@ static int write_shared_index(struct index_state *istate,
32483255

32493256
trace2_region_enter_printf("index", "shared/do_write_index",
32503257
the_repository, "%s", get_tempfile_path(*temp));
3251-
ret = do_write_index(si->base, *temp, 1);
3258+
ret = do_write_index(si->base, *temp, 1, flags);
32523259
trace2_region_leave_printf("index", "shared/do_write_index",
32533260
the_repository, "%s", get_tempfile_path(*temp));
32543261

@@ -3357,7 +3364,7 @@ int write_locked_index(struct index_state *istate, struct lock_file *lock,
33573364
ret = do_write_locked_index(istate, lock, flags);
33583365
goto out;
33593366
}
3360-
ret = write_shared_index(istate, &temp);
3367+
ret = write_shared_index(istate, &temp, flags);
33613368

33623369
saved_errno = errno;
33633370
if (is_tempfile_active(temp))

0 commit comments

Comments
 (0)