diff options
author | Junio C Hamano <gitster@pobox.com> | 2017-11-21 14:05:33 +0900 |
---|---|---|
committer | Junio C Hamano <gitster@pobox.com> | 2017-11-21 14:05:33 +0900 |
commit | 1c89be1db2463c970b3b51e576d6f5f5d33c9bfe (patch) | |
tree | 217a546c5a168712bfe4ad48b0e138518c2994e5 | |
parent | 01e0c53c739854fda6d601a5a49ef1ccacd156fc (diff) | |
parent | 9360ec0002369f3194cc5ac75ec50dab4979c988 (diff) | |
download | git-1c89be1db2463c970b3b51e576d6f5f5d33c9bfe.tar.gz |
Merge branch 'rs/sequencer-rewrite-file-cleanup' into maint
Code cleanup.
* rs/sequencer-rewrite-file-cleanup:
sequencer.c: check return value of close() in rewrite_file()
sequencer: use O_TRUNC to truncate files
sequencer: factor out rewrite_file()
-rw-r--r-- | sequencer.c | 45 |
1 files changed, 16 insertions, 29 deletions
diff --git a/sequencer.c b/sequencer.c index 332a383b03..2882d53120 100644 --- a/sequencer.c +++ b/sequencer.c @@ -2670,6 +2670,19 @@ leave_check: return res; } +static int rewrite_file(const char *path, const char *buf, size_t len) +{ + int rc = 0; + int fd = open(path, O_WRONLY | O_TRUNC); + if (fd < 0) + return error_errno(_("could not open '%s' for writing"), path); + if (write_in_full(fd, buf, len) < 0) + rc = error_errno(_("could not write to '%s'"), path); + if (close(fd) && !rc) + rc = error_errno(_("could not close '%s'"), path); + return rc; +} + /* skip picking commits whose parents are unchanged */ int skip_unnecessary_picks(void) { @@ -2742,29 +2755,11 @@ int skip_unnecessary_picks(void) } close(fd); - fd = open(rebase_path_todo(), O_WRONLY, 0666); - if (fd < 0) { - error_errno(_("could not open '%s' for writing"), - rebase_path_todo()); - todo_list_release(&todo_list); - return -1; - } - if (write_in_full(fd, todo_list.buf.buf + offset, - todo_list.buf.len - offset) < 0) { - error_errno(_("could not write to '%s'"), - rebase_path_todo()); - close(fd); - todo_list_release(&todo_list); - return -1; - } - if (ftruncate(fd, todo_list.buf.len - offset) < 0) { - error_errno(_("could not truncate '%s'"), - rebase_path_todo()); + if (rewrite_file(rebase_path_todo(), todo_list.buf.buf + offset, + todo_list.buf.len - offset) < 0) { todo_list_release(&todo_list); - close(fd); return -1; } - close(fd); todo_list.current = i; if (is_fixup(peek_command(&todo_list, 0))) @@ -2949,15 +2944,7 @@ int rearrange_squash(void) } } - fd = open(todo_file, O_WRONLY); - if (fd < 0) - res = error_errno(_("could not open '%s'"), todo_file); - else if (write(fd, buf.buf, buf.len) < 0) - res = error_errno(_("could not write to '%s'"), todo_file); - else if (ftruncate(fd, buf.len) < 0) - res = error_errno(_("could not truncate '%s'"), - todo_file); - close(fd); + res = rewrite_file(todo_file, buf.buf, buf.len); strbuf_release(&buf); } |