Fun with Windows paths.
June 19, 2012 § 3 Comments
I am currently attempting to implement a JavaScript library to handle file system paths in a portable manner.
Right now, I am having lots of fun with Windows paths and I wanted to share a few tidbits.
Under Windows, a path name can look like:
- “\\?\drivename:” followed by backslash-separated components.
Such paths can be either relative or absolute.
In such paths, “.”, “..” and “/” are regular file names. - “\\.\drivename:” followed by backslash-separated components.
Such paths can be either relative or absolute.
In such paths, “.”, “..” and “/” are special names. - “\\?\UNC\servername” followed by backslash-separated components.
Such paths can only be absolute.
In such paths, “.”, “..” and “/” are regular file names. - “\\servername” followed by slash- or backslash- components.
Such paths can only be absolute.
In such paths, “.”, “..” and “/” are special names. - “drivename:” followed by slash- or backslash- components.
Such paths can be either relative or absolute.
In such paths, “.”, “..” and “/” are special names. - Just a series of slash- or backslash- components.
Such paths can be either relative or absolute.
In such paths, “.”, “..” and “/” are special names.
To simplify things further, depending on the version of Windows, a drive name can be:
- only one letter between A and Z;
- any sequence of letters between A an Z;
- something that looks like Volume{41AF5D4F-04CC-4D15-9389-734BD6F52A7E}.
Also
- if a path starts with “\\?\”, its length is limited to 32,767 chars;
- otherwise, its length is limited to 260 chars.
Also
- some names such as “LPT”, “COM”, etc. are reserved and cannot be used as file names;
- … unless your path starts with “\\”.
Also
- paths are case-insensitive;
- … except when they are case-sensitive because of the disk format;
- … except when they are case-sensitive because of something else.
Fortunately, the Windows APIs provides the following functions to simplify matters:
- PathCanonicalize (completely broken);
- GetFullPathName (broken);
- GetLongPathName (requires access permissions just to tell you if a path is well-formatted);
- UriCanonicalize (not sure what it does exactly, I haven’t tested it yet).
Of course, not all Windows API functions accept all schemes.
As you can imagine, I am having lots of fun.
Quick exercise given two paths A and B (either absolute or relative), how do you determine the path obtained by concatenating A and B?
If you are interested in following my progress, details are on bugzilla.
If you haven’t already, there are of course some good sources for how paths are represented on MSDN like http://msdn.microsoft.com/en-us/library/windows/desktop/aa365247%28v=vs.85%29.aspx
Yep, that’s my main source.
Haha, I’ve dealt with this exact issue for nsinstall.py. Windows paths are so broken it’s adorable,