detect close failure on just-written file handles

I audited git for potential undetected write failures.
In the cases fixed below, the diagnostics I add mimic the diagnostics
used in surrounding code, even when that means not reporting
the precise strerror(errno) cause of the error.

Signed-off-by: Jim Meyering <jim@meyering.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
Jim Meyering 2007-06-24 21:20:41 +02:00 committed by Junio C Hamano
parent 2275d50211
commit 91c8d5905c
4 changed files with 8 additions and 7 deletions

View File

@ -40,7 +40,8 @@ static int copy_file(const char *dst, const char *src, int mode)
return fdo; return fdo;
} }
status = copy_fd(fdi, fdo); status = copy_fd(fdi, fdo);
close(fdo); if (close(fdo) != 0)
return error("%s: write error: %s", dst, strerror(errno));
if (!status && adjust_shared_perm(dst)) if (!status && adjust_shared_perm(dst))
return -1; return -1;

View File

@ -57,7 +57,8 @@ static int write_rr(struct path_list *rr, int out_fd)
write_in_full(out_fd, path, length) != length) write_in_full(out_fd, path, length) != length)
die("unable to write rerere record"); die("unable to write rerere record");
} }
close(out_fd); if (close(out_fd) != 0)
die("unable to write rerere record");
return commit_lock_file(&write_lock); return commit_lock_file(&write_lock);
} }

View File

@ -634,7 +634,8 @@ static void final(const char *final_pack_name, const char *curr_pack_name,
write_or_die(keep_fd, keep_msg, keep_msg_len); write_or_die(keep_fd, keep_msg, keep_msg_len);
write_or_die(keep_fd, "\n", 1); write_or_die(keep_fd, "\n", 1);
} }
close(keep_fd); if (close(keep_fd) != 0)
die("cannot write keep file");
report = "keep"; report = "keep";
} }
} }

6
refs.c
View File

@ -1106,8 +1106,7 @@ static int log_ref_write(const char *ref_name, const unsigned char *old_sha1,
len += sprintf(logrec + len - 1, "\t%.*s\n", msglen, msg) - 1; len += sprintf(logrec + len - 1, "\t%.*s\n", msglen, msg) - 1;
written = len <= maxlen ? write_in_full(logfd, logrec, len) : -1; written = len <= maxlen ? write_in_full(logfd, logrec, len) : -1;
free(logrec); free(logrec);
close(logfd); if (close(logfd) != 0 || written != len)
if (written != len)
return error("Unable to append to %s", log_file); return error("Unable to append to %s", log_file);
return 0; return 0;
} }
@ -1204,8 +1203,7 @@ int create_symref(const char *ref_target, const char *refs_heads_master,
goto error_free_return; goto error_free_return;
} }
written = write_in_full(fd, ref, len); written = write_in_full(fd, ref, len);
close(fd); if (close(fd) != 0 || written != len) {
if (written != len) {
error("Unable to write to %s", lockpath); error("Unable to write to %s", lockpath);
goto error_unlink_return; goto error_unlink_return;
} }