Commit Graph

286 Commits

Author SHA1 Message Date
Andrew Kelley 10e72a8cad std.Io.Threaded: implement fileSetTimestamps on WASI 2025-12-27 11:01:16 -08:00
Andrew Kelley dd98188ce0 std.Io.Threaded: mostly implement fileSetTimestamps for Windows
it's still missing the case of setting to now (which was also not
implemented before)
2025-12-27 10:52:17 -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 d8460910b4 std.Io.Threaded: init stderr_writer in streaming mode 2025-12-26 19:58:56 -08:00
Andrew Kelley f160d5f979 std.Io.Threaded: implement file writing for Windows 2025-12-26 19:58:56 -08:00
Ryan Liptak 58f0c8cfed Fix logic for detecting zero buffer capacity in fileReadStreamingWindows/fileReadPositionalWindows 2025-12-26 19:58:56 -08:00
Andrew Kelley eb29737d5d std.Io.Threaded: more efficient statx mask check 2025-12-26 19:58:56 -08:00
Andrew Kelley 6dcf951391 std.Io.Threaded: error instead of assert for statx
when statx fails to return some of the requested fields
2025-12-26 19:58:56 -08:00
Andrew Kelley 2e9415d8a0 std.Io.Threaded: fix missing @memcpy in realPathPosix for FreeBSD 2025-12-26 19:58:56 -08:00
Andrew Kelley 0dbcf97551 std.Io.Threaded: fix resource leak in dirRealPathFilePosix
it should unconditionally close the opened file descriptor, not only on
error.
2025-12-26 19:58:56 -08:00
Andrew Kelley dc1d27a646 std.Io.Threaded: more verbose TODO panic messages 2025-12-26 19:58:56 -08:00
Andrew Kelley 315bc10a0c std.Io.Threaded: dirRealPathFilePosix use libc realpath sometimes
When linking libc, and the directory is the cwd.

This avoids bugs on FreeBSD.
2025-12-24 12:57:18 -08:00
Andrew Kelley 6f4e93f0dd std.Io.Threaded: wasi does not support processExecutablePath 2025-12-24 00:55:27 -08:00
Andrew Kelley ace08ba642 std.Io.Threaded: prevent 0-byte file writes
these cause EINVAL on darwin and are generally a wasted syscall.

and remove a bogus error from File.Writer.Error.
2025-12-23 23:46:43 -08:00
Andrew Kelley a901e92413 std.Io.Threaded: implement process executable path on darwin 2025-12-23 23:31:47 -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 98e9716c08 std: fix compilation on NetBSD 2025-12-23 22:15:12 -08:00
Andrew Kelley bd6acbf7da std.Io: minor cleanups to futex and event
mainly avoid an unnecessary `@ptrCast`
2025-12-23 22:15:12 -08:00
Andrew Kelley e9da2783ce std.Io.Threaded: add a workaround for self-hosted wasm backend
Tracked by #30575
2025-12-23 22:15:12 -08:00
Andrew Kelley c98157a3c9 std.Io.Threaded: implement environment variable scanning 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 a29d79313a std.Io.Threaded: accept argv and environ on init
This is needed unfortunately for OpenBSD and Haiku for process
executable path.

I made it so that you can omit the options usually, but you get a
compile error if you omit the options on those targets.
2025-12-23 22:15:12 -08:00
Michael Dusan 60481b8223 std.Io: trim terminating NUL from exe path on BSDs 2025-12-23 22:15:12 -08:00
Andrew Kelley 1132e329d3 std.Io.Threaded: fix up processExecutablePath OpenBSD
* policy is to always handle EINTR from all syscalls
* assert the result of realpath
* don't ignore errors from realpath
* hard-code path separators for code simplicity
2025-12-23 22:15:11 -08:00
Michael Dusan 7014976d3d openbsd: avoid error.OperationUnsupported
- realPathPosix does not support OpenBSD
- change processExecutablePath OpenBSD section to use std.c.realpath
2025-12-23 22:15:11 -08:00
Michael Dusan 90bc371f56 openbsd: use Io.Dir.realPathFileAbsolute 2025-12-23 22:15:11 -08:00
Ryan Liptak bb788cd392 dirOpenFileWtf16: Disallow opening directories when requesting write permissions
This matches the behavior of POSIX
2025-12-23 22:15:11 -08:00
Ryan Liptak ad08117e9d Fix sizing of buffer/reservation size for dirReadWindows 2025-12-23 22:15:11 -08:00
Andrew Kelley 77d2ad8c92 std: consolidate all instances of std.Io.Threaded into a singleton
It's better to avoid references to this global variable, but, in the
cases where it's needed, such as in std.debug.print and collecting stack
traces, better to share the same instance.
2025-12-23 22:15:11 -08:00
Andrew Kelley 99f25bfc23 std.Io: implement directory reading for WASI 2025-12-23 22:15:11 -08:00
Andrew Kelley e41342af83 std.Io.Threaded: reinstate fchmodat fallback
Stephen Gregoratto (original author of the fallback) says:

I read through both libcs again to compare:

Musl:
- `stat` path. Check error.
- If path is a symlink, return `OPNOTSUPP`.
- `openat` path as `O_PATH|O_NOFOLLOW|O_CLOEXEC`. Return `OPNOTSUPP` if we got `ELOOP`.
- Build procfs filename.
- `stat` procfs file.
- If procfs file is a symlink, return `OPNOTSUPP`.
- close path fd.

Glibc:
- open path as `O_PATH|O_NOFOLLOW|O_CLOEXEC`.
- fstatat path fd.
- If path is a symlink, return `OPNOTSUPP`.
- Build procfs filename.
- chmod procfs filename. Return `OPNOTSUPP` if we got `ENOENT`.
- close path fd.

I prefer glibc since you open the path first, which avoids a possible
TOCTOU race.
2025-12-23 22:15:11 -08:00
Andrew Kelley 64de4a7371 std.Io.Threaded: remove handling of ECANCELED
The only known situation in which this occurs is when using musl with
the undocumented extension PTHREAD_CANCEL_MASKED, causing the next
syscall to return ECANCELED.

However zig std lib does not use this mechanism, even when targeting
musl libc because it would require each async task to be on a fresh
pthread.

For the same reason, if third party code were to cause ECANCELED to be
returned from any of these syscalls, it would cause subsequent tasks to
be incorrectly canceled since they cannot be rearmed.

Thus, this Io implementation cannot handle this error code correctly,
expecting never to receive it.
2025-12-23 22:15:11 -08:00
Andrew Kelley a8088306f6 std: rename other Dir "make" functions to "create" 2025-12-23 22:15:11 -08:00
Andrew Kelley 52ba2a4c72 std.Io.Threaded: don't assume dirents are aligned
Linux kernel seems to do it but qemu user mode seems not to.
2025-12-23 22:15:11 -08:00
Andrew Kelley 88110139fe std.Io.Threaded: fix NetBSD compilation 2025-12-23 22:15:11 -08:00
Andrew Kelley 4025af9c05 std.Io.Threaded: fix fchmodat on linux with libc 2025-12-23 22:15:11 -08:00
Andrew Kelley 3e624e17a4 std: fix compilation errors on FreeBSD 2025-12-23 22:15:11 -08:00
Andrew Kelley 405db921dc std: fix compilation targeting WASI 2025-12-23 22:15:11 -08:00
Andrew Kelley ac11b82738 std.Io.Threaded: fix splat buffer calculations 2025-12-23 22:15:11 -08:00
Andrew Kelley 002d444964 std: fix Io.Dir.min_buffer_len on Linux 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 47a2e8026f std.Io.Threaded: fix dirDeleteDir when not empty 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
Andrew Kelley 9bbb8e0d8e std.posix: make nlink_t on unsupported systems u0
instead of void
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 f3723b42e1 std.Io: add unimplemented hard link API to File and Dir 2025-12-23 22:15:10 -08:00
Andrew Kelley 446c145ca8 std.Io.Threaded: fix compilation errors on posix 2025-12-23 22:15:10 -08:00