]> git.cameronkatri.com Git - cgit.git/commitdiff
cache: flush stdio before restoring FDs
authorJohn Keeping <john@keeping.me.uk>
Mon, 24 Apr 2017 18:38:34 +0000 (19:38 +0100)
committerJohn Keeping <john@keeping.me.uk>
Tue, 3 Oct 2017 18:19:34 +0000 (19:19 +0100)
As described in commit 2efb59e (ui-patch: Flush stdout after outputting
data, 2014-06-11), we need to ensure that stdout is flushed before
restoring the file descriptor when writing to the cache.  It turns out
that it's not just ui-patch that is affected by this but also raw diff
which writes to stdout internally.

Let's avoid risking more places doing this by ensuring that stdout is
flushed after writing in fill_slot().

Signed-off-by: John Keeping <john@keeping.me.uk>
cache.c
ui-patch.c

diff --git a/cache.c b/cache.c
index 6736a017b2fce59608dac7318cbafac518045c3c..2ccdc4ebcefd44dc7a4a34d5455356da3fe9ee3e 100644 (file)
--- a/cache.c
+++ b/cache.c
@@ -224,6 +224,12 @@ static int fill_slot(struct cache_slot *slot)
        /* Generate cache content */
        slot->fn();
 
+       /* Make sure any buffered data is flushed to the file */
+       if (fflush(stdout)) {
+               close(tmp);
+               return errno;
+       }
+
        /* update stat info */
        if (fstat(slot->lock_fd, &slot->cache_st)) {
                close(tmp);
index 69aa4a83d9dcc366efaa1fdd4e2ad6a9760b1b61..8007a1181224b8360c85d1553c5045263d12acba 100644 (file)
@@ -92,6 +92,4 @@ void cgit_print_patch(const char *new_rev, const char *old_rev,
                log_tree_commit(&rev, commit);
                printf("-- \ncgit %s\n\n", cgit_version);
        }
-
-       fflush(stdout);
 }