mirror of
https://github.com/allankoechke/rust.git
synced 2026-12-05 17:36:20 +00:00
Windows: add fallback if `canonicalize` fails This attempts a partial workaround for issues such as: https://github.com/rust-lang/rust/issues/59392, https://github.com/rust-lang/rust/issues/79449, https://github.com/rust-lang/rust/issues/59107, https://github.com/rust-lang/rust/issues/54875, https://github.com/rust-lang/rust/issues/52440, https://github.com/rust-lang/rust/issues/52377, https://github.com/rust-lang/rust/issues/48249, https://github.com/rust-lang/rust/issues/74327, https://github.com/rust-lang/rust/issues/55812 This may require a bit of explanation depending on how familiar you are with Windows paths, I'll try to keep it brief. The short version is that the above issues are cases where third party devices don't integrate with the system sufficiently so Windows isn't aware of the canonical drive for a particular path, causing our `canonicalize` function to fail. This PR works around it by manually search for the drive letter that corresponds to the root of the path. This only works in cases where there is a drive letter assigned but that is the majority of cases. It won't work when the device is only mounted to a directory in another filesystem or isn't mounted at all. To explain the implementation of this PR you should be aware that Windows on Windows NT is more like WINE on Linux then many people realise. You have a kernel (NT) and then you have an implementation of the Win32 APIs on top (this is why `kernel32.dll` is nothing to do with the real kernel, it's like an implementation of Win95's kernel API on top of another OS). Admittedly the boundaries have become fuzzier over the years but there still remains a clear distinction between the Win32 API and the NT kernel API in many places. Paths are one place where this distinction is made clear. You have the familiar Win32 paths like `C:\path\to\file` that date back to the time of DOS. And then you have the low-level NT kernel paths that look like `\Device\HarddiskVolume6\path\to\file` (which aren't really meant to be user-visible). To bridge the gap, the NT namespace has a special `??` directory containing mappings (i.e. symlinks) from drives like `C:` to paths like `\Device\HarddiskVolume6`. In that way translating between Win32 and NT paths is made simpler as you can replace `C:` with `\??\C:` and it'll get resolved to the right path and vice versa (the actual translation from win32 to NT is more complicated but I've already spent too many words on this). So back to canonicalisation. Resolving the canonical NT path should always succeed. The problem comes when mapping that to a Win32 drive path. When the drive is managed by the system then when resolving paths it knows which drive to pick. However, if the drive mapping is added manually then it doesn't. So the way to workaround this is to manually look at the drive mappings and see which one is the root of the NT path we have.