Make some of fwrite/fclose/write/close failures visible
So that full filesystem conditions or permissions problems won't go unnoticed. Signed-off-by: Alex Riesen <raa.lkml@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Alex Riesen committed
Dec 5, 2008 at 01:35 UTC
47d32af2338418275ab6b53a737952af22289d97
3 files changed
+45
-15
builtin-fsck.c
+6
-2
@@ -201,12 +201,16 @@ static void check_unreachable_object(struct object *obj)
201
char *buf = read_sha1_file(obj->sha1,
202
&type, &size);
203
if (buf) {
204
- fwrite(buf, size, 1, f);
204
+ if (fwrite(buf, size, 1, f) != 1)
205
+ die("Could not write %s: %s",
206
+ filename, strerror(errno));
207
free(buf);
208
}
209
} else
210
fprintf(f, "%s\n", sha1_to_hex(obj->sha1));
209
- fclose(f);
211
+ if (fclose(f))
212
+ die("Could not finish %s: %s",
213
+ filename, strerror(errno));
214
}
215
return;
216
}
builtin-merge.c
+4
-2
@@ -293,8 +293,10 @@ static void squash_message(void)
293
pretty_print_commit(rev.commit_format, commit, &out, rev.abbrev,
294
NULL, NULL, rev.date_mode, 0);
295
}
296
- write(fd, out.buf, out.len);
297
- close(fd);
296
+ if (write(fd, out.buf, out.len) < 0)
297
+ die("Writing SQUASH_MSG: %s", strerror(errno));
298
+ if (close(fd))
299
+ die("Finishing SQUASH_MSG: %s", strerror(errno));
300
strbuf_release(&out);
301
}
302
rerere.c
+35
-11
@@ -70,6 +70,19 @@ static int write_rr(struct string_list *rr, int out_fd)
70
return 0;
71
}
72
73
+static void ferr_write(const void *p, size_t count, FILE *fp, int *err)
74
+{
75
+ if (!count || *err)
76
+ return;
77
+ if (fwrite(p, count, 1, fp) != 1)
78
+ *err = errno;
79
+}
80
+
81
+static inline void ferr_puts(const char *s, FILE *fp, int *err)
82
+{
83
+ ferr_write(s, strlen(s), fp, err);
84
+}
85
+
86
static int handle_file(const char *path,
87
unsigned char *sha1, const char *output)
88
{
@@ -82,6 +95,7 @@ static int handle_file(const char *path,
95
struct strbuf one = STRBUF_INIT, two = STRBUF_INIT;
96
FILE *f = fopen(path, "r");
97
FILE *out = NULL;
98
+ int wrerror = 0;
99
100
if (!f)
101
return error("Could not open %s", path);
@@ -118,11 +132,11 @@ static int handle_file(const char *path,
132
hunk_no++;
133
hunk = RR_CONTEXT;
134
if (out) {
121
- fputs("<<<<<<<\n", out);
122
- fwrite(one.buf, one.len, 1, out);
123
- fputs("=======\n", out);
124
- fwrite(two.buf, two.len, 1, out);
125
- fputs(">>>>>>>\n", out);
135
+ ferr_puts("<<<<<<<\n", out, &wrerror);
136
+ ferr_write(one.buf, one.len, out, &wrerror);
137
+ ferr_puts("=======\n", out, &wrerror);
138
+ ferr_write(two.buf, two.len, out, &wrerror);
139
+ ferr_puts(">>>>>>>\n", out, &wrerror);
140
}
141
if (sha1) {
142
git_SHA1_Update(&ctx, one.buf ? one.buf : "",
@@ -139,7 +153,7 @@ static int handle_file(const char *path,
153
else if (hunk == RR_SIDE_2)
154
strbuf_addstr(&two, buf);
155
else if (out)
142
- fputs(buf, out);
156
+ ferr_puts(buf, out, &wrerror);
157
continue;
158
bad:
159
hunk = 99; /* force error exit */
@@ -149,8 +163,12 @@ static int handle_file(const char *path,
163
strbuf_release(&two);
164
165
fclose(f);
152
- if (out)
153
- fclose(out);
166
+ if (wrerror)
167
+ error("There were errors while writing %s (%s)",
168
+ path, strerror(wrerror));
169
+ if (out && fclose(out))
170
+ wrerror = error("Failed to flush %s: %s",
171
+ path, strerror(errno));
172
if (sha1)
173
git_SHA1_Final(sha1, &ctx);
174
if (hunk != RR_CONTEXT) {
@@ -158,6 +176,8 @@ static int handle_file(const char *path,
176
unlink(output);
177
return error("Could not parse conflict hunks in %s", path);
178
}
179
+ if (wrerror)
180
+ return -1;
181
return hunk_no;
182
}
183
@@ -200,9 +220,13 @@ static int merge(const char *name, const char *path)
220
if (!ret) {
221
FILE *f = fopen(path, "w");
222
if (!f)
203
- return error("Could not write to %s", path);
204
- fwrite(result.ptr, result.size, 1, f);
205
- fclose(f);
223
+ return error("Could not open %s: %s", path,
224
+ strerror(errno));
225
+ if (fwrite(result.ptr, result.size, 1, f) != 1)
226
+ error("Could not write %s: %s", path, strerror(errno));
227
+ if (fclose(f))
228
+ return error("Writing %s failed: %s", path,
229
+ strerror(errno));
230
}
231
232
free(cur.ptr);