Repository navigation
Implement a better Stat::stat(), Stat::lstat() for Windows - #819
Merged
Merged
Conversation
`stat()` as implemented in msvcrt.dll (used by mingw-w64) has a number
of flaws:
- `st_ino` is always 0
- other file attributes are queried from the parent directory instead of
the file itself, and so can be incorrect/stale, e.g. if a file is
modified via another hard link.
The implementation in the UCRT is slightly better -- it prefers to query
attributes from the file itself if possible, but it still doesn't
populate `st_ino` and can silently fall back to querying from the parent
directory and giving stale results.
I don't believe the msvcrt.dll source is available (you have to infer
its behaviour from e.g. Process Monitor) but the ucrtbase.dll source
code is available in the Windows 10 SDK.
Write an implementation of `stat`/`lstat` that sidesteps these issues by
using Win32 APIs to query file information. A few implementation notes:
- We do not fall back to querying the parent directory (as the UCRT
`stat()` implementation and [Python's `stat()`][1] do) if we can't get
a handle to the actual file. If we don't have `FILE_READ_ATTRIBUTES`
permissions, we fail with `EACCES` instead of returning
incomplete/stale results fetched from the parent directory.
- Windows-specific `Stat::file_attributes()` / `Stat::reparse_tag()`
added to provide the Win32 file attributes and file reparse tag.
- `lstat()` sets the `S_IFLNK` bit in `st_mode` for symlinks. On
Windows, there are other symlink-like things ("name surrogate reparse
points") such as directory junctions and mountpoints. To identify
these, the caller can check the reparse tag to identify a directory
junction/mountpoint.
- We use Python's mapping of Win32 error codes to C errno.
[1]: https://github.com/python/cpython/blob/6086ae7fd4aeb4089282189673f9bd0cc33abf9b/Modules/posixmodule.c#L1884
Member
|
I'm very happy that you care about making ccache work robustly on Windows. Thanks! At the same time I'm sad and surprised that so much Windows-specific code is needed due to bad portability layer implementations. |
Contributor
Author
I hear you. Perhaps this will improve if/when you're able to move ccache to C++17? Visual Studio's C standard library is pretty awful as we've seen, but its |
Closed
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.
stat()as implemented in msvcrt.dll (used by mingw-w64) has a number of flaws:st_inois always 0 (soStat::same_inode_asgives inaccurate results)The implementation in the UCRT is slightly better -- it prefers to query attributes from the file itself if possible, but it still doesn't populate
st_inoand can silently fall back to querying from the parent directory and giving stale results.I don't believe the msvcrt.dll source is available (you have to infer its behaviour from e.g. Process Monitor) but the ucrtbase.dll source code is available in the Windows 10 SDK.
Write an implementation of
stat/lstatthat sidesteps these issues by using Win32 APIs to query file information. A few implementation notes:stat()implementation and Python'sstat()do) if we can't get a handle to the actual file. If we don't haveFILE_READ_ATTRIBUTESpermissions, we fail withEACCESinstead of returning incomplete/stale results fetched from the parent directory.Stat::file_attributes()/Stat::reparse_tag()are added to provide the Win32 file attributes and file reparse tag.lstat()sets theS_IFLNKbit inst_modefor symlinks. On Windows, there are other symlink-like things ("name surrogate reparse points") such as directory junctions and mountpoints. To identify these, the caller can check the reparse tag to identify a directory junction/mountpoint.