sequencer: use O_TRUNC to truncate files
authorRené Scharfe <l.s.r@web.de>
Tue, 31 Oct 2017 09:58:16 +0000 (10:58 +0100)
committerJunio C Hamano <gitster@pobox.com>
Wed, 1 Nov 2017 01:53:19 +0000 (10:53 +0900)
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>
sequencer.c
index 17360eb38afa05253fac3013af227ceac6b23d94..f93b60f615ca390f16f80f42e13f04dd7d3afacc 100644 (file)
@@ -2668,13 +2668,11 @@ int check_todo_list(void)
 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;
 }