Skip to content

Fix File.exists? for special devices on Windows#15587

Merged
straight-shoota merged 2 commits intocrystal-lang:masterfrom
HertzDevil:bug/windows-file-nul-exists
Mar 24, 2025
Merged

Fix File.exists? for special devices on Windows#15587
straight-shoota merged 2 commits intocrystal-lang:masterfrom
HertzDevil:bug/windows-file-nul-exists

Conversation

@HertzDevil
Copy link
Contributor

@HertzDevil HertzDevil commented Mar 22, 2025

Fixes #15019.

Note that File.realpath("nul") will now correctly return \\.\NUL. Support in Path will be added shortly.

@HertzDevil HertzDevil added kind:bug A bug in the code. Does not apply to documentation, specs, etc. platform:windows Windows support based on the MSVC toolchain / Win32 API topic:stdlib:files labels Mar 22, 2025
@HertzDevil HertzDevil moved this to Review in Windows Support Mar 22, 2025
@HertzDevil HertzDevil changed the title Fix File.exists?(File::NULL) on Windows Fix File.exists? for special devices on Windows Mar 22, 2025
@straight-shoota straight-shoota added this to the 1.16.0 milestone Mar 22, 2025
@straight-shoota straight-shoota merged commit f0fe486 into crystal-lang:master Mar 24, 2025
31 of 32 checks passed
@github-project-automation github-project-automation bot moved this from Review to Done in Windows Support Mar 24, 2025
@HertzDevil HertzDevil deleted the bug/windows-file-nul-exists branch March 24, 2025 09:43
straight-shoota pushed a commit that referenced this pull request Apr 18, 2025
There are [7 types of user-space paths on Win32](https://googleprojectzero.blogspot.com/2016/02/the-definitive-guide-on-win32-to-nt.html):

* Drive-absolute (`C:\foo`)
* Drive-relative (`C:foo`)
* Rooted (`\foo`)
* Relative (`foo`)
* UNC absolute (`\\server\share\foo`)
* Local device (`\\.\NUL`, `\\?\C:\foo`)
* Root local device (`\\.`, `\\?`)

Local device paths starting with exactly `\\?\` are also known as raw local device paths. Other than this prefix, all forward and backward slashes are interchangeable.

`Path` already recognizes the first 5 of them; this PR adds the remaining 2. As noted in #15587, the `\\.\` prefix appears when dealing with special devices, such as `\\.\NUL` and `\\.\COM15`, as well as named pipes. The `\\?\` prefix is useful when passing paths to Win32 functions that are already normalized, or if the path exceeds 260 UTF-16 code units (see also #13420).

This does not cover NT paths of the form `\??\...`. They appear in the reparse data area of Windows symbolic links.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

kind:bug A bug in the code. Does not apply to documentation, specs, etc. platform:windows Windows support based on the MSVC toolchain / Win32 API topic:stdlib:files

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

The file NUL does not exist on Windows

2 participants