Skip to content

Commit d9c31e1

Browse files
peffgitster
authored andcommitted
streaming_write_entry: propagate streaming errors
When we are streaming an index blob to disk, we store the error from stream_blob_to_fd in the "result" variable, and then immediately overwrite that with the return value of "close". That means we catch errors on close (e.g., problems committing the file to disk), but miss anything which happened before then. We can fix this by using bitwise-OR to accumulate errors in our result variable. While we're here, we can also simplify the error handling with an early return, which makes it easier to see under which circumstances we need to clean up. Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 7b6257b commit d9c31e1

File tree

2 files changed

+34
-7
lines changed

2 files changed

+34
-7
lines changed

entry.c

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -120,16 +120,18 @@ static int streaming_write_entry(struct cache_entry *ce, char *path,
120120
const struct checkout *state, int to_tempfile,
121121
int *fstat_done, struct stat *statbuf)
122122
{
123-
int result = -1;
123+
int result = 0;
124124
int fd;
125125

126126
fd = open_output_fd(path, ce, to_tempfile);
127-
if (0 <= fd) {
128-
result = stream_blob_to_fd(fd, ce->sha1, filter, 1);
129-
*fstat_done = fstat_output(fd, state, statbuf);
130-
result = close(fd);
131-
}
132-
if (result && 0 <= fd)
127+
if (fd < 0)
128+
return -1;
129+
130+
result |= stream_blob_to_fd(fd, ce->sha1, filter, 1);
131+
*fstat_done = fstat_output(fd, state, statbuf);
132+
result |= close(fd);
133+
134+
if (result)
133135
unlink(path);
134136
return result;
135137
}

t/t1060-object-corruption.sh

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,36 @@ test_expect_success 'setup corrupt repo' '
2424
)
2525
'
2626

27+
test_expect_success 'setup repo with missing object' '
28+
git init missing &&
29+
(
30+
cd missing &&
31+
test_commit content &&
32+
rm -f "$(obj_to_file HEAD:content.t)"
33+
)
34+
'
35+
2736
test_expect_success 'streaming a corrupt blob fails' '
2837
(
2938
cd bit-error &&
3039
test_must_fail git cat-file blob HEAD:content.t
3140
)
3241
'
3342

43+
test_expect_success 'read-tree -u detects bit-errors in blobs' '
44+
(
45+
cd bit-error &&
46+
rm -f content.t &&
47+
test_must_fail git read-tree --reset -u HEAD
48+
)
49+
'
50+
51+
test_expect_success 'read-tree -u detects missing objects' '
52+
(
53+
cd missing &&
54+
rm -f content.t &&
55+
test_must_fail git read-tree --reset -u HEAD
56+
)
57+
'
58+
3459
test_done

0 commit comments

Comments
 (0)