bug#80513: du should fflush every now and then

Paul Eggert <[email protected]> Sat, 28 Feb 2026 19:16:57 -0800
Newsgroups gmane.comp.gnu.core-utils.bugs
Organization UCLA Computer Science Department
Message-ID <[email protected]>
A recently-installed coreutils change[1] had a good idea about avoiding 
locks, but the part that substituted ferror for fflush was problematic. 
That fflush had been in du.c for decades, and was put in because people 
want to run 'du' and see its output quickly as disk usage is discovered; 
they don't want to wait for a stdio output buffer to fill (or for du to 
exit) before seeing usage so far. Even with today's flash drives and 
faster file systems this can still be an issue, as hard drives are still 
in widespread use and network file systems can have nontrivial latency.

The du change differs from other recent changes involving ferror vs 
fflush in id and groups, as those programs don't have the relatively 
slow I/O that du does.

Simplest is to go back to using fflush, which I did by installing the 
attached.

A fancier approach I've seen other programs use, is to call fflush only 
if a second or so has passed since the last fflush. That might be 
overkill here, though.

[1]: 
https://cgit.git.savannah.gnu.org/cgit/coreutils.git/commit/?id=09a5449ff24d573224b3e857766e87a249f645d6
0001-du-fflush-after-outputting-a-line.patch (text/x-patch, 746 B)
From 69c0b63bea76ca643e7cbe46155f60a865782b35 Mon Sep 17 00:00:00 2001
From: Paul Eggert <[email protected]>
Date: Sat, 28 Feb 2026 19:07:52 -0800
Subject: [PATCH] du: fflush after outputting a line

* src/du.c (print_size): Resurrect the fflush call, since there
can be significant delay between output lines.
---
 src/du.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/src/du.c b/src/du.c
index a41ece904..bff1b6672 100644
--- a/src/du.c
+++ b/src/du.c
@@ -467,7 +467,7 @@ print_size (const struct duinfo *pdui, char const *string)
   putchar ('\t');
   fputs (string, stdout);
   putchar (opt_nul_terminate_output ? '\0' : '\n');
-  if (ferror (stdout))
+  if (fflush (stdout) < 0)
     write_error ();
 }
 
-- 
2.51.0