Skip to content

Commit 47d32af

Browse files
raalkmlgitster
authored andcommitted
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>
1 parent 123ed65 commit 47d32af

File tree

3 files changed

+45
-15
lines changed

3 files changed

+45
-15
lines changed

builtin-fsck.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -201,12 +201,16 @@ static void check_unreachable_object(struct object *obj)
201201
char *buf = read_sha1_file(obj->sha1,
202202
&type, &size);
203203
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));
205207
free(buf);
206208
}
207209
} else
208210
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));
210214
}
211215
return;
212216
}

builtin-merge.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -293,8 +293,10 @@ static void squash_message(void)
293293
pretty_print_commit(rev.commit_format, commit, &out, rev.abbrev,
294294
NULL, NULL, rev.date_mode, 0);
295295
}
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));
298300
strbuf_release(&out);
299301
}
300302

rerere.c

Lines changed: 35 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,19 @@ static int write_rr(struct string_list *rr, int out_fd)
7070
return 0;
7171
}
7272

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+
7386
static int handle_file(const char *path,
7487
unsigned char *sha1, const char *output)
7588
{
@@ -82,6 +95,7 @@ static int handle_file(const char *path,
8295
struct strbuf one = STRBUF_INIT, two = STRBUF_INIT;
8396
FILE *f = fopen(path, "r");
8497
FILE *out = NULL;
98+
int wrerror = 0;
8599

86100
if (!f)
87101
return error("Could not open %s", path);
@@ -118,11 +132,11 @@ static int handle_file(const char *path,
118132
hunk_no++;
119133
hunk = RR_CONTEXT;
120134
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);
126140
}
127141
if (sha1) {
128142
git_SHA1_Update(&ctx, one.buf ? one.buf : "",
@@ -139,7 +153,7 @@ static int handle_file(const char *path,
139153
else if (hunk == RR_SIDE_2)
140154
strbuf_addstr(&two, buf);
141155
else if (out)
142-
fputs(buf, out);
156+
ferr_puts(buf, out, &wrerror);
143157
continue;
144158
bad:
145159
hunk = 99; /* force error exit */
@@ -149,15 +163,21 @@ static int handle_file(const char *path,
149163
strbuf_release(&two);
150164

151165
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));
154172
if (sha1)
155173
git_SHA1_Final(sha1, &ctx);
156174
if (hunk != RR_CONTEXT) {
157175
if (output)
158176
unlink(output);
159177
return error("Could not parse conflict hunks in %s", path);
160178
}
179+
if (wrerror)
180+
return -1;
161181
return hunk_no;
162182
}
163183

@@ -200,9 +220,13 @@ static int merge(const char *name, const char *path)
200220
if (!ret) {
201221
FILE *f = fopen(path, "w");
202222
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));
206230
}
207231

208232
free(cur.ptr);

0 commit comments

Comments
 (0)