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 <l.s.r@web.de>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
René Scharfe 2017-10-31 10:58:16 +01:00 committed by Junio C Hamano
parent 73646bfdcb
commit c8cee96e8a

View File

@ -2668,13 +2668,11 @@ leave_check:
static int rewrite_file(const char *path, const char *buf, size_t len) static int rewrite_file(const char *path, const char *buf, size_t len)
{ {
int rc = 0; int rc = 0;
int fd = open(path, O_WRONLY); int fd = open(path, O_WRONLY | O_TRUNC);
if (fd < 0) if (fd < 0)
return error_errno(_("could not open '%s' for writing"), path); return error_errno(_("could not open '%s' for writing"), path);
if (write_in_full(fd, buf, len) < 0) if (write_in_full(fd, buf, len) < 0)
rc = error_errno(_("could not write to '%s'"), path); 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); close(fd);
return rc; return rc;
} }