coreutils icon indicating copy to clipboard operation
coreutils copied to clipboard

truncate: eliminate duplicate stat() syscall

Open Jean-Christian-Cirstea opened this issue 4 weeks ago • 3 comments

When no reference file is given, truncate performs two stat() syscalls:

  1. The first one to retrieve the size of the file.
  2. The second one to check if the file if a pipe.

This commit fixes this issue by restructuring the code. The main change resides in the elimination of the initial match performed at the top level to select between different scenarios. Instead, the new implementation:

  1. Check if a reference file has been given. If so, retrieve its size.
  2. Determine the given mode. If no mode has been given, TruncateMode::Extend(0) is implied.
  3. Retrieve the size of the file, while checking if it is a pipe. This is the key point of this PR.
  4. Determine the truncate size based on the reference size and the mode. If no reference file has been provided, use the size of the file to be resized as a reference size.
  5. Finally, perform ftruncate() to resize the file.

Jean-Christian-Cirstea avatar Nov 30 '25 01:11 Jean-Christian-Cirstea

GNU testsuite comparison:

Skip an intermittent issue tests/tail/overlay-headers (fails in this run but passes in the 'main' branch)

github-actions[bot] avatar Nov 30 '25 08:11 github-actions[bot]

GNU testsuite comparison:

Skip an intermittent issue tests/tail/overlay-headers (fails in this run but passes in the 'main' branch)
Skipping an intermittent issue tests/misc/tee (passes in this run but fails in the 'main' branch)

github-actions[bot] avatar Nov 30 '25 10:11 github-actions[bot]

GNU testsuite comparison:

Skip an intermittent issue tests/tail/overlay-headers (fails in this run but passes in the 'main' branch)
Congrats! The gnu test tests/tty/tty-eof is no longer failing!

github-actions[bot] avatar Dec 06 '25 16:12 github-actions[bot]

@sylvestre Any outstanding issues with this PR?

Jean-Christian-Cirstea avatar Dec 19 '25 20:12 Jean-Christian-Cirstea