Commit Graph

1872 Commits

Author SHA1 Message Date
Andrew Kelley 60e2ea0bfb windows: use ProcessPrng from bcryptprimitives.dll
rather than SystemFunction036 from advapi32. This has the advantage that
the code is loaded preemptively, preventing random numbers from
failing when they are needed for the first time on a system under heavy
load.
2026-01-05 00:36:11 -08:00
Andrew Kelley 75f417108c std.Io.Threaded: fix compilation on s390x, hexagon, or1k, m68k
Apparently the O_TMPFILE flag is split across two bits on these
architectures and missing on m68k.
2026-01-04 23:45:17 -08:00
Andrew Kelley fa315b1060 std.Io.Threaded: improve posix process creation
* cache /dev/null after opening
* make opening /dev/null cancelable
* avoid unreachable even when OS does something unexpected
2026-01-04 00:27:09 -08:00
Andrew Kelley c6b75b61b7 std: fix child processes on riscv32-linux 2026-01-04 00:27:08 -08:00
Andrew Kelley d97e4ca0d1 documentation should be descriptive not prescriptive 2026-01-04 00:27:08 -08:00
Andrew Kelley 60447ea97c std: fix windows compilation errors 2026-01-04 00:27:08 -08:00
Andrew Kelley 08447ca47e std.fs.path: make relative a pure function
Instead of querying the operating system for current working directory
and environment variables, this function now accepts those things as
inputs.
2026-01-04 00:27:08 -08:00
Andrew Kelley 32af0f6154 std: move child process APIs to std.Io
this gets the build runner compiling again on linux

this work is incomplete; it only moves code around so that environment
variables can be wrangled properly. a future commit will need to audit
the cancelation and error handling of this moved logic.
2026-01-04 00:27:08 -08:00
Matthew Lugg e57c557ad4 std.Io.Threaded: hugely improve Windows and NetBSD support
The most interesting thing here is the replacement of the pthread futex
implementation with an implementation based on thread park/unpark APIs.
Thread parking tends to be the primitive provided by systems which do
not have a futex primitive, such as NetBSD, so this implementation is
far more efficient than the pthread one. It is also useful on Windows,
where `RtlWaitOnAddress` is itself a userland implementation based on
thread park/unpark; we can implement it ourselves including support for
features which Windows' implementation lacks, such as cancelation and
waking a number of waiters with 1<n<infinity.

Compared to the pthread implementation, this thread-parking-based one
also supports full robust cancelation. Thread parking also turns out to
be useful for implementing `sleep`, so is now used for that on Windows
and NetBSD.

This commit also introduces proper cancelation support for most Windows
operations. The most notable omission right now is DNS lookups through
`GetAddrInfoEx`, just because they're a little more work due to having
a unique cancelation mechanism---but the machinery is all there, so I'll
finish gluing it together soon.

As of this commit, there are very few parts of `Io.Threaded` which do
not support full robust cancelation. The only ones which actually really
matter (because they could block for a prolonged period of time) are DNS
lookups on Windows (as discussed above) and futex waits on WASM.
2026-01-03 15:45:10 +00:00
Matthew Lugg 04226193cc Revert "Use mmap std.heap.page_allocator impl when compiling for Wasm + libc"
This reverts commit c9fa8e46df.

This commit was failing CI checks. This failure was unfortunately not
noticed before merge, due in part to the build runner bug fixed in the
last commit.
2026-01-03 14:52:17 +00:00
Carl Åstholm c9fa8e46df Use mmap std.heap.page_allocator impl when compiling for Wasm + libc
When linking libc, it should be the libc that manages the heap. The main
Wasm memory might have been configured as non-growable, which makes
`WasmAllocator` a poor default and causes the common `DebugAllocator`
use case fail with OOM errors unless the user uses `std_options` to
override the default page allocator. Additionally, on Emscripten,
growing Wasm memory without notifying the JS glue code will cause array
buffers to get detached and lead to spurious crashes.
2026-01-02 16:55:11 -08:00
Andrew Kelley c0809c9b68 std: add more timespec OMIT and NOW definitions 2025-12-27 11:08:56 -08:00
Andrew Kelley a7c9d11b28 std.Io: make file access time optional
Some filesystems, such as ZFS, do not report atime. It's pretty useless
in general, so make it an optional field in File.Stat.

Also take the opportunity to make setting timestamps API more flexible
and match the APIs widely available, which have UTIME_OMIT and UTIME_NOW
constants that can be independently set for both fields.

This is needed to handle smoothly the case when atime is null.
2025-12-27 10:40:24 -08:00
Andrew Kelley f160d5f979 std.Io.Threaded: implement file writing for Windows 2025-12-26 19:58:56 -08:00
Andrew Kelley fa79d34674 std: add changing cur dir back
There's a good argument to not have this in the std lib but it's more
work to remove it than to leave it in, and this branch is already
20,000+ lines changed.
2025-12-23 22:15:12 -08:00
Andrew Kelley 002d444964 std: fix Io.Dir.min_buffer_len on Linux 2025-12-23 22:15:11 -08:00
Ryan Liptak 1136d36362 windows: Remove BadPathName from error set of GetFinalPathNameByHandle
These validations are really only checking for kernel/mount manager
bugs, so an assert seems more appropriate.
2025-12-23 22:15:11 -08:00
Andrew Kelley 07c4ea2de0 all comments are notes, and use doc comments where possible 2025-12-23 22:15:11 -08:00
Ryan Liptak edcbd7ed2c os.windows: Add comment explaining why we don't need a separate EX definition for RENAME_INFORMATION 2025-12-23 22:15:10 -08:00
Andrew Kelley 651ff9f9ee std.Io.Threaded: implement dirHardLink 2025-12-23 22:15:10 -08:00
Andrew Kelley 8632a28ca9 std: add support for realpath on file handle
and rename OperationNotSupported to OperationUnsupported
2025-12-23 22:15:10 -08:00
Ryan Liptak f078c7138f std: Update/fix some usages/implementations of std.Io APIs 2025-12-23 22:15:10 -08:00
Andrew Kelley e205b13ffb std.os.linux.IoUring: update for new fs API
and split out tests into a separate file
2025-12-23 22:15:10 -08:00
Ryan Liptak d72983da44 File.OpenFlags: Add allow_directory and default it to true
This is one way of addressing/closing https://github.com/ziglang/zig/issues/16738

Previously, there was a mismatch between the default behaviors on Windows vs other platforms, where Windows was implicitly using .NON_DIRECTORY_FILE for its `openFile` implementation which caused `error.IsDir` when opening a directory, while on other platforms there is no equivalent flag for the `open` syscall. This meant that `openFile` on a path of a directory would fail on Windows but succeed on other platforms.

Adding `allow_directory` to `File.OpenFlags` serves two purposes:

1. It provides a cross-platform way to get the `.NON_DIRECTORY_FILE` behavior in the most efficient available way for the platform (on Windows, no extra syscalls are required, on other systems, an extra `fstat` is required)
2. It allows `statFile` to be implemented on top of `openFile` on Windows while still allowing `statFile` to work on directory paths. Before this commit, `statFile` on a directory path on Windows failed with `error.IsDir`

Note: The second purpose could have been addressed in different ways (bespoke call to NtCreateFile in the `statFile` implementation to avoid passing `NON_DIRECTORY_FILE`, or just never pass `NON_DIRECTORY_FILE` in the `openFile` implementation), so the first purpose is the more relevant/motivating force behind this change.

The default being `true` is intended to cut down on the number of syscalls as much as possible when using the default flags.
2025-12-23 22:15:10 -08:00
Andrew Kelley f82e7dfbc0 std.os.linux: move statx docs to proper location 2025-12-23 22:15:09 -08:00
Andrew Kelley 0e230993d5 std.Io.Dir: add setFilePermissions and setFileOwner 2025-12-23 22:15:09 -08:00
Andrew Kelley 314c906dba std.debug: simplify printLineFromFile 2025-12-23 22:15:08 -08:00
Andrew Kelley 9ccd68de0b std: move abort and exit from posix into process
and delete the unit tests that called fork()

no forking allowed in the std lib, including unit tests, except to implement child process spawning.
2025-12-23 22:15:08 -08:00
Andrew Kelley 9f4d40b1f9 update all stat() to stat(io) 2025-12-23 22:15:08 -08:00
Andrew Kelley 264d714321 update all openDir() sites to accept io instance 2025-12-23 22:15:08 -08:00
Andrew Kelley f53248a409 update all std.fs.cwd() to std.Io.Dir.cwd() 2025-12-23 22:15:08 -08:00
Andrew Kelley 8328de24f1 update all occurrences of openFile to receive an io instance 2025-12-23 22:15:08 -08:00
Andrew Kelley aafddc2ea1 update all occurrences of close() to close(io) 2025-12-23 22:15:07 -08:00
Andrew Kelley ba999d608c std: extract sendfile/copy_file_range from Io.File.Writer
and move it into std.Io.Threaded (below the VTable)
2025-12-23 22:15:07 -08:00
Andrew Kelley 814480db7c std: all File functions moved to std.Io 2025-12-23 22:15:07 -08:00
Stephen Gregoratto c3f2de5e51 Linux: Update syscall list for 6.17
The 6.17 kernel added[1] syscalls for getting/setting certain file flags
and attributes. It's meant to be a more extensible replacement for these
ioctl's:

- `FS_IOC_GETFLAGS`/`FS_IOC_SETFLAGS`.
- `FS_IOC_FSGETXATTR`/`FS_IOC_FSSETXATTR`.

The definitions of these calls are as follows:

```zig
const file_attr = extern struct {
    /// Extended flags that apply to this file. (get/set).
    xflags: u64,
    /// Preferred extent allocation size, in bytes. (get/set).
    extsize: u32,
    /// One of:
    /// - The number of data extents in this file.
    /// - If `FS_IOC_FSGETXATTRA` is set, the number of extended attribute events in the file.
    /// (get)
    nextents: u32,
    /// Project Identifier (get/set).
    projid: u32,
    /// Preferred extent allocation size for CoW operations, in bytes (get/set).
    cowextsize: u32,
};

// size=@sizeOf(file_attr)
fn file_getattr(dirfd: fd_t, path: [*:0], fattr: *file_attr, size: usize, at_flags: u32) {}
fn file_setattr(dirfd: fd_t, path: [*:0], fattr: *file_attr, size: usize, at_flags: u32) {}
```

Users need to set/check `xflags` with the `FS_XFLAG` flags defined in
`linux/fs.h`. `ioctl_xfs_fsgetxattr(2)` has more information about the
type of information one can retrieve.

[1]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=57fcb7d930d8f00f383e995aeebdcd2b416a187a
2025-12-24 13:10:38 +11:00
pyk b346090ed2 add PERF_EVENT_IOC_ID
The value is derived from the following:

```
std.os.linux.IOCTL.IOR('$', 7, u64);
```

Signed-off-by: pyk <pyk@noreply.codeberg.org>
2025-12-22 14:16:06 +01:00
Alex Rønne Petersen aa0249d74e Merge pull request 'std.ascii: rename indexOf functions to find' (#30101) from adria/zig:indexof-find into master
Reviewed-on: https://codeberg.org/ziglang/zig/pulls/30101
Reviewed-by: Andrew Kelley <andrewrk@noreply.codeberg.org>
Reviewed-by: mlugg <mlugg@noreply.codeberg.org>
2025-12-22 12:50:46 +01:00
Andrew Kelley 5fa7f13082 Merge pull request 'Windows: Avoid ever-expanding DeviceIoControl error set, handle NOT_A_REPARSE_POINT in ReadLink' (#30186) from squeek502/zig:win-deviceiocontrol into master
Reviewed-on: https://codeberg.org/ziglang/zig/pulls/30186
2025-12-19 18:50:27 +01:00
Alex Rønne Petersen a6c22c5edb std.os.linux.test: disable fallocate on mips64*-linux-*abin32
https://codeberg.org/ziglang/zig/issues/30220
2025-12-18 12:56:28 +01:00
Ryan Liptak 36cb5ea5f4 windows.ReadLink: handle NOT_A_REPARSE_POINT and add test 2025-12-14 07:12:38 -08:00
Ryan Liptak ea7512084b Make windows.DeviceIoControl return NTSTATUS instead of a Zig error
The number of possible errors in the theoretical error set of this function is very large, while each individual call to the function is only concerned with a (potentially small) subset of those errors that are specific to the control code being used. This commit makes the callers determine which statuses they are interested in to avoid an ever-ballooning error set and ever-growing switch cases at each call site that throw away most of those errors.
2025-12-14 07:12:38 -08:00
Stephen Gregoratto aec7bfb092 Linux: Dedupe generic decls
Commit #fc7a5f2 moved many of the `_t` types up a level, but didn't
remove them from arch_bits. Since `Stat` is gone, all but `time_t` can
be removed.
2025-12-14 01:41:47 +01:00
Stephen Gregoratto 6216922a9d Linux: Nuke Stat bits in favour of statx
Maintaining the POSIX `stat` bits for Zig is a pain. The order and
bit-length of members differ between all architectures, and int types
can be signed or unsigned. The libcs deal with this by introducing the
own version of `struct stat` and copying the kernel structure members to
it. In the case of glibc, they did it twice thanks to the largefile
transition!

In practice, the project needs to maintain three versions of `struct
stat`:
- What the kernel defines.
- What musl wants for `struct stat`.
- What glibc wants for `struct stat64`. Make sure to use `fstatat64`!

This isn't as simple as running `zig translate-c`. In #21440 I had to:
- Compile toolchains for each arch+glibc/musl combo.
- Create a test `fstat` program with/without `FILE_OFFSET_BITS=64`.
- Dump the value for `struct stat`.
- Stare at `std.os.linux`/`std.c` and cry.
- Add some missing padding.

The fact that so many target checks in the `linux` and `posix` tests
exist is most likely due to writing to padding bits and failing later.

The solution to this madness is `statx(2)`:
- It takes a single structure that is the same for all arches AND libcs.
- It uses a custom timestamp format, but it is 64-bit ready.
- It gives the same info as `fstatat(2)` and more!
- Unlike `fstatat(2)`, you can request a subset of the info required
  based on passing a mask.

It's so good that modern Linux arches (e.g. riscv) don't even implement
`stat`, with the libcs using a generic `struct stat` and copying from
`struct statx`.

Therefore, this commit rips out all the `stat` bits from `std.os.linux`
and `std.c`. `std.posix.Stat` is now `void`, and calling
`std.posix.*stat` is an compile-time error. A wrapper around `statx` has
been added to `std.os.linux`, and callers have been upgraded to use it.
Tests have also been updated to use `statx` where possible.

While I was here, I converted the mask and file attributes to be packed
struct bitfields. A nice side effect is checking that you actually
recieved the members you asked for via `Statx.mask`, which I have used
by adding `assert`s at specific callsites.
2025-12-14 01:41:47 +01:00
Stephen Gregoratto ff3fd950a7 Linux: Update Statx structure
Also removes the blank lines between members, and a comptime sizeOf
check.
2025-12-14 01:41:47 +01:00
Jacob Young c13857e504 windows: type safety improvements and more ntdll functions 2025-12-12 01:58:21 -05:00
mlugg dbb4c8d151 Merge pull request 'Remove things deprecated during the 0.15 release cycle' (#30018) from linus/zig:remove-deprecated-stuff into master
Reviewed-on: https://codeberg.org/ziglang/zig/pulls/30018
2025-12-06 08:51:15 +01:00
Adrià Arrufat 02c5f05e2f std: replace usages of std.mem.indexOf with std.mem.find 2025-12-05 14:31:27 +01:00
Andrew Kelley 54a84964f8 std.os.linux: SIG enum is non-exhaustive 2025-12-01 19:17:52 -08:00
Andrew Kelley 9e981c3ae5 std.os.linux: delete unnecessary @compileError
Without this, it already fails to compile with a sufficiently helpful
error message.
2025-12-01 19:17:52 -08:00