coreutils
coreutils copied to clipboard
truncate: eliminate duplicate stat() syscall
When no reference file is given, truncate performs two stat() syscalls:
- The first one to retrieve the size of the file.
- 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:
- Check if a reference file has been given. If so, retrieve its size.
- Determine the given mode. If no mode has been given,
TruncateMode::Extend(0)is implied. - Retrieve the size of the file, while checking if it is a pipe. This is the key point of this PR.
- 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.
- Finally, perform
ftruncate()to resize the file.
GNU testsuite comparison:
Skip an intermittent issue tests/tail/overlay-headers (fails in this run but passes in the 'main' branch)
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)
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!
@sylvestre Any outstanding issues with this PR?