Skip to content

Commit 1c89be1

Browse files
committed
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()
2 parents 01e0c53 + 9360ec0 commit 1c89be1

File tree

1 file changed

+16
-29
lines changed

1 file changed

+16
-29
lines changed

sequencer.c

Lines changed: 16 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2670,6 +2670,19 @@ int check_todo_list(void)
26702670
return res;
26712671
}
26722672

2673+
static int rewrite_file(const char *path, const char *buf, size_t len)
2674+
{
2675+
int rc = 0;
2676+
int fd = open(path, O_WRONLY | O_TRUNC);
2677+
if (fd < 0)
2678+
return error_errno(_("could not open '%s' for writing"), path);
2679+
if (write_in_full(fd, buf, len) < 0)
2680+
rc = error_errno(_("could not write to '%s'"), path);
2681+
if (close(fd) && !rc)
2682+
rc = error_errno(_("could not close '%s'"), path);
2683+
return rc;
2684+
}
2685+
26732686
/* skip picking commits whose parents are unchanged */
26742687
int skip_unnecessary_picks(void)
26752688
{
@@ -2742,29 +2755,11 @@ int skip_unnecessary_picks(void)
27422755
}
27432756
close(fd);
27442757

2745-
fd = open(rebase_path_todo(), O_WRONLY, 0666);
2746-
if (fd < 0) {
2747-
error_errno(_("could not open '%s' for writing"),
2748-
rebase_path_todo());
2749-
todo_list_release(&todo_list);
2750-
return -1;
2751-
}
2752-
if (write_in_full(fd, todo_list.buf.buf + offset,
2753-
todo_list.buf.len - offset) < 0) {
2754-
error_errno(_("could not write to '%s'"),
2755-
rebase_path_todo());
2756-
close(fd);
2757-
todo_list_release(&todo_list);
2758-
return -1;
2759-
}
2760-
if (ftruncate(fd, todo_list.buf.len - offset) < 0) {
2761-
error_errno(_("could not truncate '%s'"),
2762-
rebase_path_todo());
2758+
if (rewrite_file(rebase_path_todo(), todo_list.buf.buf + offset,
2759+
todo_list.buf.len - offset) < 0) {
27632760
todo_list_release(&todo_list);
2764-
close(fd);
27652761
return -1;
27662762
}
2767-
close(fd);
27682763

27692764
todo_list.current = i;
27702765
if (is_fixup(peek_command(&todo_list, 0)))
@@ -2949,15 +2944,7 @@ int rearrange_squash(void)
29492944
}
29502945
}
29512946

2952-
fd = open(todo_file, O_WRONLY);
2953-
if (fd < 0)
2954-
res = error_errno(_("could not open '%s'"), todo_file);
2955-
else if (write(fd, buf.buf, buf.len) < 0)
2956-
res = error_errno(_("could not write to '%s'"), todo_file);
2957-
else if (ftruncate(fd, buf.len) < 0)
2958-
res = error_errno(_("could not truncate '%s'"),
2959-
todo_file);
2960-
close(fd);
2947+
res = rewrite_file(todo_file, buf.buf, buf.len);
29612948
strbuf_release(&buf);
29622949
}
29632950

0 commit comments

Comments
 (0)