Commit graph

71 commits

Author SHA1 Message Date
Jacob Young
a28d57292f IoUring: update to new Io APIs 2026-02-09 10:47:21 -05:00
Andrew Kelley
cc442d24ab std.Io: move fileWriteStreaming to Operation
This serves as an example to contributors of how to move VTable
functions to becoming an Operation, thereby enabling Batch API and
timeouts.
2026-01-31 22:53:28 -08:00
Andrew Kelley
39a6d5d1c5 std.Io.File: add non-blocking flag
On Windows, we need to know ahead of time whether a file was opened in
synchronous mode or asynchronous mode. There may be advantages to
tracking this state for POSIX operating systems as well.
2026-01-30 22:03:14 -08:00
Jacob Young
2674acdb77 Io.Batch: implement alternate API 2026-01-30 22:03:14 -08:00
Andrew Kelley
8a80b54640 std: remove error.BrokenPipe from file reads, add error.EndOfStream
and make reading file streaming allowed to return 0 byte reads.
According to Microsoft documentation, on Windows it is possible to get
0-byte reads from pipes when 0-byte writes are made.
2026-01-30 22:03:14 -08:00
Andrew Kelley
20cadd60aa std.Io.File: introduce MultiReader
Concurrently read from multiple file streams, eliminating risk of
deadlocking.
2026-01-30 22:03:14 -08:00
Jacob Young
8146ccfecc Io: add ring to Batch API 2026-01-30 22:03:14 -08:00
Andrew Kelley
642f329ac9 std.Io: exploring a different batch API proposal 2026-01-30 22:03:14 -08:00
Andrew Kelley
05064e1281 std.Io: simplify operate function
- no timeout
- no n_wait
- infallible
2026-01-30 22:03:13 -08:00
Andrew Kelley
0a0ecc4fb1 std.Io: proof-of-concept "operations" API
This commit shows a proof-of-concept direction for std.Io.VTable to go,
which is to have general support for batching, timeouts, and
non-blocking.

I'm not sure if this is a good idea or not so I'm putting it up for
scrutiny.

This commit introduces `std.Io.operate`, `std.Io.Operation`, and
implements it experimentally for `FileReadStreaming`.

In `std.Io.Threaded`, the implementation is based on poll().

This commit shows how it can be used in `std.process.run` to collect
both stdout and stderr in a single-threaded program using
`std.Threaded.Io`.

It also demonstrates how to upgrade code that was previously using
`std.Io.poll` (*not* integrated with the interface!) using concurrency.
This may not be ideal since it makes the build runner no longer support
single-threaded mode. There is still a needed abstraction for
conveniently reading multiple File streams concurrently without
io.concurrent, but this commit demonstrates that such an API can be
built on top of the new `std.Io.operate` functionality.
2026-01-30 22:03:13 -08:00
Andrew Kelley
8827488fcd std: back out the flags field of Io.File
For now, let us refrain from putting the sync mode into the Io.File
struct, and document that to do concurrent batch operations, any Windows
file handles must be in asynchronous mode. The consequences for
violating this requirement is neither illegal behavior, nor an error,
but that concurrency is lost. In other words, deadlock might occur. This
prevents the addition of flags field.

partial revert of 2faf14200f58ee72ec3a13e894d765f59e6483a9
2026-01-30 22:03:13 -08:00
Andrew Kelley
cb7be96644 std.Io: give File a nonblocking bit on Windows
This tracks whether it is a file opened in synchronous mode, or
something that supports APC.

This will be needed in order to know whether concurrent batch operations
on the file should return error.ConcurrencyUnavailable, or use APC to
complete the batch.

This patch also switches to using NtCreateFile directly in
std.Io.Threaded for dirCreateFile, as well as NtReadFile for
fileReadStreaming, making it handle files opened in synchronous mode as
well as files opened in asynchronous mode.
2026-01-30 22:03:13 -08:00
Andrew Kelley
18c6abc0ba std: finish moving os.windows.ReadLink logic to Io.Threaded
- remove error.SharingViolation from all error sets since it has the
  same meaning as FileBusy
- add error.FileBusy to CreateFileAtomicError and ReadLinkError
- update dirReadLinkWindows to use NtCreateFile and NtFsControlFile and
  integrate with cancelation properly.
- move windows CTL_CODE constants to the proper namespace
- delete os.windows.ReadLink
2026-01-28 17:02:17 -08:00
Andrew Kelley
1badb2a840 std.Io.Threaded: dirCreateFileWindows uses NtCreateFile directly 2026-01-22 18:08:13 -08:00
Andrew Kelley
2b01764d86 std: fix handling of EBADF error code
In the context of read/write it's ambiguous, means file was opened
without read/write respectively.
2026-01-15 14:18:20 -08:00
Andrew Kelley
4821898432 std.Io.File.MemoryMap API tuning
- remove file_size parameter from MemoryMap.write
- remove requirement for mapping length to be aligned
- align allocated fallback memory
- add unit test for std.Io.Threaded.disable_memory_mapping = true
- add unit test for MemoryMap.setLength
2026-01-15 14:18:20 -08:00
Andrew Kelley
bed7bc37c4 std.File.MemoryMap updates
- change offset to u64
- make len non-optional
- make write take a file_size parameter
- std.Io.Threaded: introduce disable_memory_mapping flag to force it to
  take the fallback path.

Additionally:

- introduce BlockSize to File.Stat. On Windows, based on cached call to
  NtQuerySystemInformation. On unsupported OS's, set to 1.
- support File.NLink on Windows. this was available the whole time, we
  just didn't see the field at first.
- remove EBADF / INVALID_HANDLE from reading/writing file error sets
2026-01-15 14:18:20 -08:00
Andrew Kelley
a70e006157 std: add unit test for memory mapping 2026-01-15 14:18:20 -08:00
Andrew Kelley
5a7dc4b0fa std.Io: introduce File.MemoryMap
by defining the pointer contents to only be synchronized after explicit
sync points, makes it legal to have a fallback implementation based on
file operations while still supporting a handful of use cases for memory
mapping.

furthermore, it makes it legal for evented I/O implementations to use
evented file I/O for the sync points rather than memory mapping.

not yet done:
- implement checking the length when options.len is null
- some windows impl work
- some wasi impl work
- unit tests
- integration with compiler
2026-01-15 14:18:20 -08:00
Andrew Kelley
7e8c7e5696 std.Io: fix AT_SYMLINK_FOLLOW flags
hard linking has the backwards default, which uses a different flag
2026-01-07 11:03:37 -08:00
Andrew Kelley
ee574f665c std.Io.Dir: introduce renamePreserve and use it in File.Atomic.link
breaking change: the error for renaming over a non-empty directory now
returns error.DirNotEmpty rather than error.PathAlreadyExists.
2026-01-07 11:03:37 -08:00
Andrew Kelley
81bfd28974 std.Io.Dir: rework atomic file 2026-01-05 20:28:58 -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
28810f5e3d std.Io.File: add readStreaming
I resisted adding this because it's generally better to create a
File.Reader instead, but we do need a simple wrapper around the vtable
function, and there are use cases for it such as std.Progress.
2025-12-26 19:58:56 -08:00
Andrew Kelley
7c1236e267 std: different way of doing some options
to avoid dependency loops
2025-12-23 22:15:12 -08:00
Andrew Kelley
9158d3b281 std: remove ability to override Io.File.Permissions
This is causing another compiler dependency loop. I'll tackle these
overrides separately.
2025-12-23 22:15:12 -08:00
Andrew Kelley
3c2f5adf41 std: integrate Io.Threaded with environment variables
* std.option allows overriding the debug Io instance
* if the default is used, start code initializes environ and argv0

also fix some places that needed recancel(), thanks mlugg!

See #30562
2025-12-23 22:15:12 -08:00
Andrew Kelley
405db921dc std: fix compilation targeting WASI 2025-12-23 22:15:11 -08:00
Ryan Liptak
3431f45031 std: Enough fixes to get things compiling on Windows 2025-12-23 22:15:11 -08:00
Andrew Kelley
c1b9c46319 std.Io: introduce path_only to File.OpenFlags 2025-12-23 22:15:10 -08:00
Andrew Kelley
f27bd87ade std.Io.Threaded: allow length-0 file writes
At first I thought about keeping this as an assertion but I can see this
being useful if you already know how many bytes to read and you are
filling the end of the buffer.

This also more closely mirrors POSIX APIs.
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
Andrew Kelley
f3723b42e1 std.Io: add unimplemented hard link API to File and Dir 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
54e4a3456c link: update to new file system APIs 2025-12-23 22:15:09 -08:00
Andrew Kelley
68621afd2e std.tar: update fs API calls to take io argument 2025-12-23 22:15:09 -08:00
Andrew Kelley
03526c59d4 std.debug: fix printLineFromFile 2025-12-23 22:15:09 -08:00
Andrew Kelley
5b436d2c51 build_runner compiling again 2025-12-23 22:15:08 -08:00
Andrew Kelley
bee8005fe6 std.heap.DebugAllocator: never detect TTY config
instead, allow the user to set it as a field.

this fixes a bug where leak printing and error printing would run tty
config detection for stderr, and then emit a log, which is not necessary
going to print to stderr.

however, the nice defaults are gone; the user must explicitly assign the
tty_config field during initialization or else the logging will not have
color.

related: https://github.com/ziglang/zig/issues/24510
2025-12-23 22:15:08 -08:00
Andrew Kelley
4a53e5b0b4 fix a handful of compilation errors related to std.fs migration 2025-12-23 22:15:08 -08:00
Andrew Kelley
7f5bb118d4 std.Io: make all the close functions batched 2025-12-23 22:15:08 -08:00
Andrew Kelley
6f46570958 link.MachO: update parallel hasher to std.Io 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
9169983159 std.fs: migrate most of the API elsewhere 2025-12-23 22:15:08 -08:00
Andrew Kelley
877032ec6a std: reorganize realpath and process executable APIs 2025-12-23 22:15:08 -08:00
Andrew Kelley
72b76077ab std: fix some surface level compilation errors
And boldly remove preadv, pwritev, readv, writev, pread, pwrite from
std.posix.
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
Andrew Kelley
d1d2c37af2 std: all Dir functions moved to std.Io 2025-12-23 22:15:07 -08:00
Matthew Lugg
49f17237b0
compiler,std: remove a handful of redundant discards
I'm embarrassed to say I didn't realise until a few days ago that you
could omit the `_` capture for `if`/`while` on `E!void`...
2025-12-22 11:54:57 +00:00