From 73646bfdcb38114a4d9242a02fc180a9877e01bf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Scharfe?= Date: Tue, 31 Oct 2017 10:54:21 +0100 Subject: sequencer: factor out rewrite_file() Reduce code duplication by extracting a function for rewriting an existing file. Signed-off-by: Rene Scharfe Signed-off-by: Junio C Hamano diff --git a/sequencer.c b/sequencer.c index f2a10cc..17360eb 100644 --- a/sequencer.c +++ b/sequencer.c @@ -2665,6 +2665,20 @@ 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); + 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 (!rc && ftruncate(fd, len) < 0) + rc = error_errno(_("could not truncate '%s'"), path); + close(fd); + return rc; +} + /* skip picking commits whose parents are unchanged */ int skip_unnecessary_picks(void) { @@ -2737,29 +2751,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()); + if (rewrite_file(rebase_path_todo(), todo_list.buf.buf + offset, + todo_list.buf.len - offset) < 0) { 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()); - todo_list_release(&todo_list); - close(fd); - return -1; - } - close(fd); todo_list.current = i; if (is_fixup(peek_command(&todo_list, 0))) @@ -2944,15 +2940,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); } -- cgit v0.10.2-6-g49f6 From c8cee96e8a8de2e9c6f12bfac8295f1b9ee29112 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Scharfe?= Date: Tue, 31 Oct 2017 10:58:16 +0100 Subject: sequencer: use O_TRUNC to truncate files Cut off any previous content of the file to be rewritten by passing the flag O_TRUNC to open(2) instead of calling ftruncate(2) at the end. That's easier and shorter. Signed-off-by: Rene Scharfe Signed-off-by: Junio C Hamano diff --git a/sequencer.c b/sequencer.c index 17360eb..f93b60f 100644 --- a/sequencer.c +++ b/sequencer.c @@ -2668,13 +2668,11 @@ leave_check: static int rewrite_file(const char *path, const char *buf, size_t len) { int rc = 0; - int fd = open(path, O_WRONLY); + 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 (!rc && ftruncate(fd, len) < 0) - rc = error_errno(_("could not truncate '%s'"), path); close(fd); return rc; } -- cgit v0.10.2-6-g49f6 From 9360ec0002369f3194cc5ac75ec50dab4979c988 Mon Sep 17 00:00:00 2001 From: Simon Ruderich Date: Wed, 1 Nov 2017 15:45:42 +0100 Subject: sequencer.c: check return value of close() in rewrite_file() MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Not checking close(2) can hide errors as not all errors are reported during the write(2). Signed-off-by: Simon Ruderich Reviewed-by: René Scharfe Signed-off-by: Junio C Hamano diff --git a/sequencer.c b/sequencer.c index f93b60f..e0cc2f7 100644 --- a/sequencer.c +++ b/sequencer.c @@ -2673,7 +2673,8 @@ static int rewrite_file(const char *path, const char *buf, size_t len) 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); - close(fd); + if (close(fd) && !rc) + rc = error_errno(_("could not close '%s'"), path); return rc; } -- cgit v0.10.2-6-g49f6