Commit graph

582 commits

Author SHA1 Message Date
Ryan Liptak
53e615b920
Merge pull request #25993 from squeek502/windows-paths
Teach `std.fs.path` about the wonderful world of Windows paths
2025-11-24 15:27:24 -08:00
Ryan Liptak
822f412424 fs.path: Fix on big-endian architectures, make PathType.isSep assume WTF-16 is LE
This commit flips usage of PathType.isSep from requiring the caller to convert to native to assuming the input is LE encoded, which is a breaking change. This makes usage a bit nicer, though, and moves the endian conversion work from runtime to comptime.
2025-11-21 22:26:58 -08:00
Ryan Liptak
59b8bed222 Teach fs.path about the wonderful world of Windows paths
Previously, fs.path handled a few of the Windows path types, but not all of them, and only a few of them correctly/consistently. This commit aims to make `std.fs.path` correct and consistent in handling all possible Win32 path types.

This commit also slightly nudges the codebase towards a separation of Win32 paths and NT paths, as NT paths are not actually distinguishable from Win32 paths from looking at their contents alone (i.e. `\Device\Foo` could be an NT path or a Win32 rooted path, no way to tell without external context). This commit formalizes `std.fs.path` being fully concerned with Win32 paths, and having no special detection/handling of NT paths.

Resources on Windows path types, and Win32 vs NT paths:

- https://googleprojectzero.blogspot.com/2016/02/the-definitive-guide-on-win32-to-nt.html
- https://chrisdenton.github.io/omnipath/Overview.html
- https://learn.microsoft.com/en-us/windows/win32/fileio/naming-a-file

API additions/changes/deprecations

- `std.os.windows.getWin32PathType` was added (it is analogous to `RtlDetermineDosPathNameType_U`), while `std.os.windows.getNamespacePrefix` and `std.os.windows.getUnprefixedPathType` were deleted. `getWin32PathType` forms the basis on which the updated `std.fs.path` functions operate.
- `std.fs.path.parsePath`, `std.fs.path.parsePathPosix`, and `std.fs.path.parsePathWindows` were added, while `std.fs.path.windowsParsePath` was deprecated. The new `parsePath` functions provide the "root" and the "kind" of a path, which is platform-specific. The now-deprecated `windowsParsePath` did not handle all possible path types, while the new `parsePathWindows` does.
- `std.fs.path.diskDesignator` has been deprecated in favor of `std.fs.path.parsePath`, and same deal with `diskDesignatorWindows` -> `parsePathWindows`
- `relativeWindows` is now a compile error when *not* targeting Windows, while `relativePosix` is now a compile error when targeting Windows. This is because those functions read/use the CWD path which will behave improperly when used from a system with different path semantics (e.g. calling `relativePosix` from a Windows system with a CWD like `C:\foo\bar` will give you a bogus result since that'd be treated as a single relative component when using POSIX semantics). This also allows `relativeWindows` to use Windows-specific APIs for getting the CWD and environment variables to cut down on allocations.
- `componentIterator`/`ComponentIterator.init` have been made infallible. These functions used to be able to error on UNC paths with an empty server component, and on paths that were assumed to be NT paths, but now:
  + We follow the lead of `RtlDetermineDosPathNameType_U`/`RtlGetFullPathName_U` in how it treats a UNC path with an empty server name (e.g. `\\\share`) and allow it, even if it'll be invalid at the time of usage
  + Now that `std.fs.path` assumes paths are Win32 paths and not NT paths, we don't have to worry about NT paths

Behavior changes

- `std.fs.path` generally: any combinations of mixed path separators for UNC paths are universally supported, e.g. `\/server/share`, `/\server\share`, `/\server/\\//share` are all seen as equivalent UNC paths
- `resolveWindows` handles all path types more appropriately/consistently.
  + `//` and `//foo` used to be treated as a relative path, but are now seen as UNC paths
  + If a rooted/drive-relative path cannot be resolved against anything more definite, the result will remain a rooted/drive-relative path.
  + I've created [a script to generate the results of a huge number of permutations of different path types](https://gist.github.com/squeek502/9eba7f19cad0d0d970ccafbc30f463bf) (the result of running the script is also included for anyone that'd like to vet the behavior).
- `dirnameWindows` now treats the drive-relative root as the dirname of a drive-relative path with a component, e.g. `dirname("C:foo")` is now `C:`, whereas before it would return null. `dirnameWindows` also handles local device paths appropriately now.
- `basenameWindows` now handles all path types more appropriately. The most notable change here is `//a` being treated as a partial UNC path now and therefore `basename` will return `""` for it, whereas before it would return `"a"`
- `relativeWindows` will now do its best to resolve against the most appropriate CWD for each path, e.g. relative for `D:foo` will look at the CWD to check if the drive letter matches, and if not, look at the special environment variable `=D:` to get the shell-defined CWD for that drive, and if that doesn't exist, then it'll resolve against `D:\`.

Implementation details

- `resolveWindows` previously looped through the paths twice to build up the relevant info before doing the actual resolution. Now, `resolveWindows` iterates backwards once and keeps track of which paths are actually relevant using a bit set, which also allows it to break from the loop when it's no longer possible for earlier paths to matter.
- A standalone test was added to test parts of `relativeWindows` since the CWD resolution logic depends on CWD information from the PEB and environment variables

Edge cases worth noting

- A strange piece of trivia that I found out while working on this is that it's technically possible to have a drive letter that it outside the intended A-Z range, or even outside the ASCII range entirely. Since we deal with both WTF-8 and WTF-16 paths, `path[0]`/`path[1]`/`path[2]` will not always refer to the same bits of information, so to get consistent behavior, some decision about how to deal with this edge case had to be made. I've made the choice to conform with how `RtlDetermineDosPathNameType_U` works, i.e. treat the first WTF-16 code unit as the drive letter. This means that when working with WTF-8, checking for drive-relative/drive-absolute paths is a bit more complicated. For more details, see the lengthy comment in `std.os.windows.getWin32PathType`
- `relativeWindows` will now almost always be able to return either a fully-qualified absolute path or a relative path, but there's one scenario where it may return a rooted path: when the CWD gotten from the PEB is not a drive-absolute or UNC path (if that's actually feasible/possible?). An alternative approach to this scenario might be to resolve against the `HOMEDRIVE` env var if available, and/or default to `C:\` as a last resort in order to guarantee the result of `relative` is never a rooted path.
- Partial UNC paths (e.g. `\\server` instead of `\\server\share`) are a bit awkward to handle, generally. Not entirely sure how best to handle them, so there may need to be another pass in the future to iron out any issues that arise. As of now the behavior is:
  + For `relative`, any part of a UNC disk designator is treated as the "root" and therefore isn't applicable for relative paths, e.g. calling `relative` with `\\server` and `\\server\share` will result in `\\server\share` rather than just `share` and if `relative` is called with `\\server\foo` and `\\server\bar` the result will be `\\server\bar` rather than `..\bar`
  + For `resolve`, any part of a UNC disk designator is also treated as the "root", but relative and rooted paths are still elligable for filling in missing portions of the disk designator, e.g. `resolve` with `\\server` and `foo` or `\foo` will result in `\\server\foo`

Fixes #25703
Closes #25702
2025-11-21 00:03:44 -08:00
rpkak
6b4f45f782 system specific errno 2025-11-20 15:03:23 -08:00
Benjamin Jurk
4b5351bc0d
update deprecated ArrayListUnmanaged usage (#25958) 2025-11-20 14:46:23 -08:00
Andrew Kelley
a9568ed296
Merge pull request #25898 from jacobly0/elfv2-progress
Elf2: more progress
2025-11-20 04:33:04 -08:00
Ryan Liptak
aa4332fb0e
Merge pull request #25539 from squeek502/windows-readlinkw
windows: Make readLinkW APIs output WTF-16, reduce stack usage of callers
2025-11-15 23:36:34 -08:00
Ryan Liptak
6aa3570cb0 windows: Make readLinkW APIs output WTF-16, reduce stack usage of callers
- Affects the following functions:
  + `std.fs.Dir.readLinkW`
  + `std.os.windows.ReadLink`
  + `std.os.windows.ntToWin32Namespace`
  + `std.posix.readlinkW`
  + `std.posix.readlinkatW`

Each of these functions (except `ntToWin32Namespace`) took WTF-16 as input and would output WTF-8, which makes optimal buffer re-use difficult at callsites and could force unnecessary WTF-16 <-> WTF-8 conversion during an intermediate step.

The functions have been updated to output WTF-16, and also allow for the path and the output to re-use the same buffer (i.e. in-place modification), which can reduce the stack usage at callsites. For example, all of `std.fs.Dir.readLink`/`readLinkZ`/`std.posix.readlink`/`readlinkZ`/`readlinkat`/`readlinkatZ` have had their stack usage reduced by one PathSpace struct (64 KiB) when targeting Windows.

The new `ntToWin32Namespace` takes an output buffer and returns a slice from that instead of returning a PathSpace, which is necessary to make the above possible.
2025-11-15 18:16:03 -08:00
Sam Bossley
1ebbdf8eef fix: add specific error set for SelectiveWalker iterator function 2025-11-15 05:00:14 -08:00
Alex Rønne Petersen
9ab7eec23e represent Mac Catalyst as aarch64-maccatalyst-none rather than aarch64-ios-macabi
Apple's own headers and tbd files prefer to think of Mac Catalyst as a distinct
OS target. Earlier, when DriverKit support was added to LLVM, it was represented
a distinct OS. So why Apple decided to only represent Mac Catalyst as an ABI in
the target triple is beyond me. But this isn't the first time they've ignored
established target triple norms (see: armv7k and aarch64_32) and it probably
won't be the last.

While doing this, I also audited all Darwin OS prongs throughout the codebase
and made sure they cover all the tags.
2025-11-14 11:33:35 +01:00
Jacob Young
8647e4d311 aarch64: cleanup register lock 2025-11-11 01:47:27 -05:00
Andrew Kelley
46f7e3ea9f std.Io.Threaded: add ioBasic which disables networking 2025-10-29 06:20:51 -07:00
Andrew Kelley
5c527a1854 std.Io.Threaded: implement fileStat for Windows 2025-10-29 06:20:51 -07:00
Andrew Kelley
2f26025690 std: fix build failure on wasm32-freestanding 2025-10-29 06:20:51 -07:00
Andrew Kelley
62c0496d0a std.Io.Threaded: implement dirOpenDir 2025-10-29 06:20:50 -07:00
Andrew Kelley
dc6a4f3bf1 std.Io: add dirMakePath and dirMakeOpenPath 2025-10-29 06:20:50 -07:00
Andrew Kelley
1c67607397 std.Io.Threaded: implement dirOpenFile for Windows 2025-10-29 06:20:50 -07:00
Andrew Kelley
b561d5f3fe std.Io.Threaded: implement dirCreateFile for Windows 2025-10-29 06:20:50 -07:00
Andrew Kelley
aa6e8eff40 std.Io.Threaded: implement dirAccess for Windows 2025-10-29 06:20:50 -07:00
Andrew Kelley
482343f2e2 std.Io.Threaded: implement dirStatPath for Windows 2025-10-29 06:20:50 -07:00
Andrew Kelley
10b1eef2d3 std: fix compilation errors on Windows 2025-10-29 06:20:50 -07:00
Andrew Kelley
21e195a1a9 std: move some windows path checking logic 2025-10-29 06:20:50 -07:00
Andrew Kelley
2d7d98da0c std.fs: use BadPathName rather than InvalidWtf8 on Windows 2025-10-29 06:20:50 -07:00
Andrew Kelley
81e7e9fdbb std.Io: add dirOpenDir and WASI impl 2025-10-29 06:20:50 -07:00
Andrew Kelley
da6b959f64 std.Io.Threaded: implement dirOpenFile for WASI 2025-10-29 06:20:50 -07:00
Andrew Kelley
d4215ffaa0 std.Io.Threaded: implement dirCreateFile for WASI 2025-10-29 06:20:50 -07:00
Andrew Kelley
f8ea00bd6d std.Io: add dirAccess 2025-10-29 06:20:50 -07:00
Andrew Kelley
3bf0ce65a5 fix miscellaneous compilation errors
- ILSEQ -> error.BadPathName
- implement dirStatPath for WASI
2025-10-29 06:20:50 -07:00
Andrew Kelley
7b1502f327 std: fix compilation errors on macos 2025-10-29 06:20:50 -07:00
Andrew Kelley
f7bbcb4a4b fix miscellaneous compilation failures 2025-10-29 06:20:50 -07:00
Andrew Kelley
c2d1a339da std.fs.File: begrudgingly add back deprecated APIs
I'm not ready to rework MachO linker file access at the moment.
2025-10-29 06:20:49 -07:00
Andrew Kelley
7d478114ec fix compilation errors introduced by rebasing 2025-10-29 06:20:49 -07:00
Andrew Kelley
b1733b7bce std.Io: implement dirOpenFile 2025-10-29 06:20:49 -07:00
Andrew Kelley
8a1e6c8c39 std.Io: implement dirStatPath 2025-10-29 06:20:49 -07:00
Andrew Kelley
750b1431bf std: fix some Io compilation errors 2025-10-29 06:20:49 -07:00
Andrew Kelley
ebcc6f166c std.Io: bring back Timestamp but also keep Clock.Timestamp
this feels better
2025-10-29 06:20:49 -07:00
Andrew Kelley
89412fda77 std.Io: implement fileStat 2025-10-29 06:20:48 -07:00
Andrew Kelley
47aa5a70a5 std: updating to std.Io interface
got the build runner compiling
2025-10-29 06:20:48 -07:00
Andrew Kelley
b428612a20 WIP: hack away at std.Io return flight 2025-10-29 06:20:48 -07:00
Andrew Kelley
774df26835 WIP: hack at std.Io on a plane 2025-10-29 06:20:48 -07:00
Andrew Kelley
00f26cb0a4 WIP land the std.Io interface
fix std lib compilation errors caused by introducing std.Io
2025-10-29 06:20:48 -07:00
Andrew Kelley
0e9280ef1a std.Io: extract Dir to separate file 2025-10-29 06:20:48 -07:00
Andrew Kelley
e7729a7b89 std: start moving fs.File to Io 2025-10-29 06:20:48 -07:00
Alex Rønne Petersen
dba1bf9353 remove all Oracle Solaris support
There is no straightforward way for the Zig team to access the Solaris system
headers; to do this, one has to create an Oracle account, accept their EULA to
download the installer ISO, and finally install it on a machine or VM. We do not
have to jump through hoops like this for any other OS that we support, and no
one on the team has expressed willingness to do it.

As a result, we cannot audit any Solaris contributions to std.c or other
similarly sensitive parts of the standard library. The best we would be able to
do is assume that Solaris and illumos are 100% compatible with no way to verify
that assumption. But at that point, the solaris and illumos OS tags would be
functionally identical anyway.

For Solaris especially, any contributions that involve APIs introduced after the
OS was made closed-source would also be inherently more risky than equivalent
contributions for other proprietary OSs due to the case of Google LLC v. Oracle
America, Inc., wherein Oracle clearly demonstrated its willingness to pursue
legal action against entities that merely copy API declarations.

Finally, Oracle laid off most of the Solaris team in 2017; the OS has been in
maintenance mode since, presumably to be retired completely sometime in the 2030s.

For these reasons, this commit removes all Oracle Solaris support.

Anyone who still wishes to use Zig on Solaris can try their luck by simply using
illumos instead of solaris in target triples - chances are it'll work. But there
will be no effort from the Zig team to support this use case; we recommend that
people move to illumos instead.
2025-10-27 07:35:38 -07:00
Techatrix
bd1e960bc0 fix std.fs.path.resolveWindows on UNC paths with mixed path separators 2025-10-26 02:18:11 +02:00
usebeforefree
62e3d46287 replaced https://simonsapin.github.io/wtf-8/ with https://wtf-8.codeberg.page/ 2025-10-10 23:53:00 +02:00
Ryan Liptak
d629a146f5 Dir.realpathW: remove redundant buffer/copy
This same change was applied in 69007f0961 but accidentally reverted in 7bf740ee71
2025-10-10 12:13:35 -07:00
Andrew Kelley
df712d0833 std.fs.File.Reader.seekTo: fix one more logical position bug 2025-10-09 11:59:53 -07:00
Andrew Kelley
529aa9f270
Merge pull request #25512 from ziglang/sendfile-fixes
std.Io: Writer and Reader bug fixes related to sendFile, delimiters, Limited, and seeking
2025-10-09 02:30:31 -07:00
Maciej 'vesim' Kuliński
e5a55f6144 std: std.fs.File fix sendFile with buffered data
fixes #25196

Co-authored-by: Andrew Kelley <andrew@ziglang.org>
2025-10-08 17:14:03 -07:00