Skip to content

Commit 59f9d2d

Browse files
pcloudsgitster
authored andcommitted
read-cache.c: move tempfile creation/cleanup out of write_shared_index
For one thing, we have more consistent cleanup procedure now and always keep errno intact. The real purpose is the ability to break out of write_locked_index() early when mks_tempfile() fails in the next patch. It's more awkward to do it if this mks_tempfile() is still inside write_shared_index(). Signed-off-by: Nguyễn Thái Ngọc Duy <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 7db2d08 commit 59f9d2d

File tree

1 file changed

+17
-16
lines changed

1 file changed

+17
-16
lines changed

read-cache.c

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2472,31 +2472,18 @@ static int clean_shared_index_files(const char *current_hex)
24722472
}
24732473

24742474
static int write_shared_index(struct index_state *istate,
2475-
struct lock_file *lock, unsigned flags)
2475+
struct tempfile **temp)
24762476
{
2477-
struct tempfile *real_temp;
2478-
struct tempfile **temp = &real_temp;
24792477
struct split_index *si = istate->split_index;
24802478
int ret;
24812479

2482-
real_temp = mks_tempfile(git_path("sharedindex_XXXXXX"));
2483-
if (!real_temp) {
2484-
hashclr(si->base_sha1);
2485-
return do_write_locked_index(istate, lock, flags);
2486-
}
2487-
temp = &real_temp;
24882480
move_cache_to_base_index(istate);
24892481
ret = do_write_index(si->base, *temp, 1);
2490-
if (ret) {
2491-
delete_tempfile(temp);
2482+
if (ret)
24922483
return ret;
2493-
}
24942484
ret = adjust_shared_perm(get_tempfile_path(*temp));
24952485
if (ret) {
2496-
int save_errno = errno;
24972486
error("cannot fix permission bits on %s", get_tempfile_path(*temp));
2498-
delete_tempfile(temp);
2499-
errno = save_errno;
25002487
return ret;
25012488
}
25022489
ret = rename_tempfile(temp,
@@ -2567,7 +2554,21 @@ int write_locked_index(struct index_state *istate, struct lock_file *lock,
25672554
new_shared_index = istate->cache_changed & SPLIT_INDEX_ORDERED;
25682555

25692556
if (new_shared_index) {
2570-
ret = write_shared_index(istate, lock, flags);
2557+
struct tempfile *temp;
2558+
int saved_errno;
2559+
2560+
temp = mks_tempfile(git_path("sharedindex_XXXXXX"));
2561+
if (!temp) {
2562+
hashclr(si->base_sha1);
2563+
ret = do_write_locked_index(istate, lock, flags);
2564+
} else
2565+
ret = write_shared_index(istate, &temp);
2566+
2567+
saved_errno = errno;
2568+
if (is_tempfile_active(temp))
2569+
delete_tempfile(&temp);
2570+
errno = saved_errno;
2571+
25712572
if (ret)
25722573
goto out;
25732574
}

0 commit comments

Comments
 (0)