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 <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jeff King committed Mar 25, 2013 at 17:49 UTC d9c31e14d0aafdd45a382d01fcfd66c65a5f4b95
2 files changed +34 -7
entry.c
+9 -7
@@ -120,16 +120,18 @@ static int streaming_write_entry(struct cache_entry *ce, char *path,
120 const struct checkout *state, int to_tempfile,
121 int *fstat_done, struct stat *statbuf)
122 {
123 - int result = -1;
123 + int result = 0;
124 int fd;
125
126 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)
135 unlink(path);
136 return result;
137 }
t/t1060-object-corruption.sh
+25
@@ -24,6 +24,15 @@ test_expect_success 'setup corrupt repo' '
24 )
25 '
26
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 +
36 test_expect_success 'streaming a corrupt blob fails' '
37 (
38 cd bit-error &&
@@ -31,4 +40,20 @@ test_expect_success 'streaming a corrupt blob fails' '
40 )
41 '
42
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 +
59 test_done