Repository navigation
programs: don't write the output through a symbolic link - #4906
Merged
Merged
Conversation
The output file was opened with O_CREAT|O_TRUNC, which follows a symbolic link. stat() fails on a dangling link, so it was not seen as an existing file: zstd created its target and wrote the output there, even without -f. A new output file is now created with O_EXCL, which fails on any symbolic link, even a dangling one, including a link that appears after the existence check or after an existing file was removed. A dangling link now counts as an existing file: it is not overwritten without -f, and -f replaces the link, as it already did for a link to an existing file. Existing files that are not regular files, such as /dev/null, /dev/stdout or pipes, are still opened as they are. They are no longer truncated, and they are refused if they turn out to be regular files once opened. A regular file that could not be removed is still truncated in place.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
zstdopened its output withO_CREAT|O_TRUNC, which follows a symbolic link.stat()fails on a dangling link, so a dangling link at the output path was not seen as an existing file:zstdcreated its target and wrote the output there, even without-f.New output files are now created with
O_EXCL, as gzip, bzip2 and xz do.O_EXCLfails on any symbolic link, dangling or not, including a link that appears after the existence check or after-fremoved an existing file. A dangling link now counts as an existing file: it is not overwritten without-f, and-freplaces the link, as it already did for a link to an existing file.Outputs that exist and are not regular files (
/dev/null,/dev/stdout,/dev/fd/N, pipes) are opened as before, but withoutO_TRUNC, and they are refused if they turn out to be regular files once opened. A regular file that could not be removed is still truncated in place. Windows is unchanged.This supersedes #4640, thanks @metsw24-max. Its approach breaks some legitimate outputs:
O_NOFOLLOWrejects-o /dev/stdoutand-o /dev/fd/N,O_EXCLwithout-frejects-o /dev/nulland pipes, and truncating in place instead of removing the file writes through hard links.Tests: a new case in
playTests.sh, which fails without the fix. Thefile-stattraces are updated for the newUTIL_stat()andUTIL_isLink()calls.