Repository navigation
Description
The os namespace (to be renamed posix) is tackling the problem of providing a cross-platform abstraction over the operating system facilities from a, in my opinion, wrong angle. In this essay I will briefly explain why.
The aim here is having a single level of abstraction that works well enough both for direct consumers (users of std.os namespace) or indirect consumers (eg. using std.fs abstractions built on std.os) without leaking too many details about the underlying implementation. The current approach tries to clump everything under the posix name, a name that carries a heavy baggage of do's and dont's that other platforms (mainly Windows) may not agree with.
A few examples:
renamedoesn't follow the posix semantics on Windows, unless you have a very recent Windows 10 version andFILE_RENAME_FLAG_POSIX_SEMANTICSis used.openaccepts a well-defined set of options that's not even shared across all the posix-compatible and for Windows this means we're pinky-swearing to faithfully translate all of them into something equivalent. What if there's no direct equivalent? If you promise posix compatibility I'd expect something akin tomingw.- What we're interested in is getting a handle on a given file, with a given set of flags.
statdoesn't exist on Windows,GetFileAttributesExor equivalents are used and the resulting infos are chopped into a posixstatstructure. It's a clear case of square peg in a round hole as many fields are unix-specific.- What we're interested in is getting a basic set of infos about the file, platform-specific extra stuff may be retrieved/modified with a companion function.
preadv/pwritevare not available on Windows and, at the moment at least, you get a nice compile error trying to use them.- What we're interested in is a
{write,read}Manyfunction, implemented usingp{write,read}vorReadFileScatter/WriteFileGather. iovecis a posix thing, I just want to write/read a[][]u8!
- What we're interested in is a
copy_file_rangeis a Linux-only syscall with a fallback onpwrite/preadthat in turn falls back onReadFile/WriteFileon Windows.- IMO this doesn't even belong to
osas it can be safely implemented somewhere infs, callingcopy_file_rangeor other platform-specific APIs as needed. - It's not portable! It's not part of the posix standard!
copy_file_rangeis all about efficiency as it's done at the FS level, if the fallback path is triggered you get a disappointing read/write pair (not even a loop!)
- IMO this doesn't even belong to
The point is that we should aim at breaking free from the posix rules and write our own, a full-blown posix compatibility layer is not something that belongs to the stdlib. I see Rust took this very same approach (I'm looking at the filsystem-related part), their API surface is small and comprises all the common bits required by the users (external ones and ones working on the stdlib). A small note lets the user know what platform-specific method is used, but that's it.
The gist of this proposal is:
- No more posix compatibility layer
osbecomes the home of all the cross-platform native interactions with the OS. No fallbacks, no posix names, no posix guarantees.- No fallbacks really means no half-assed fallbacks, if something can be implemented across all the different platforms with a consistent set of characteristics it belongs to
os, otherwise down the platform-specific namespace it goes.- ✔️
writeManyimplemented withWriteFileGatherandpwritev - ❌
copy_file_rangecannot be implemented as zero-copy on Windows! (On other posix-compatible systemssendfilemay be used, but the gains are really small unless you're copying a lot of data (I'm using it to copy whole files in std: Make file copy ops use zero-copy mechanisms #6516))
- ✔️
- No fallbacks really means no half-assed fallbacks, if something can be implemented across all the different platforms with a consistent set of characteristics it belongs to
Thanks for watching.
Activity
kprotty commentedon Oct 7, 2020
a note on the
pread/pwritebit is thatWriteFileGatheron windows looks like it only supports page aligned userspace buffers + require null terminating its "iovec equivalent" structure. There exist general purpose vectored IO on windows for sockets using WSASend & friends but the generic "HANDLE" equivalent doesn't seem to be as flexible.Another point on vectored IO is that having the function take in a generic
iovec(which for windows sockets, could beWSABUF) would alleviate the os function from allocating its own in order to represent[][]u8as the layout of slices is currently both undefined or not matching with the layout of socketiovectypes, making casting to and from them probably not correct.I would like to propose taking the idea of restructuring
std.oseven further and to get rid of it entirely (or at least hide it from the user), similar to what the Rust stdlib does. Replace the exposed functions by having them in their own higher level structs that they act upon (File/Dir, Socket, Pipe, Process, Thread, Random, IoPoll, Memory, .etc) and not requiring everything to go through a posix-based API as that can restrict and otherwise prohibit certain functionality from non-posix systems like Windows:Make an
std.os.posixwhich acts like thestd.os.linuxor partially the currentstd.osbut only for posix systems while windows sticks to usingstd.os.windows. Then design the apis of the higher level structs based on the greatest common functionality for all stdlib supported OS instead of through the lens of posix only.ikskuh commentedon Oct 7, 2020
@kprotty i fully support this! Nothing more to add
katesuyu commentedon Oct 7, 2020
I completely agree with this issue. Even Rust isn't a great example of how to do things, although we do fairly well at avoiding the most glaring flaw of Rust's OS abstractions at the moment: not supporting widechar APIs for Windows. I'm not sure we do this everywhere but I've seen
ZandWfunctions enough to be fairly satisfied.Also, relying on the existence of a given compatibility layer on every platform, even if you restrict it to things you can currently shim over with reasonable precision, has proven to be a huge mistake for stabilized language ecosystems. For example, the introduction of WASI as a target has made the Rust standard library shim over a huge amount of details in a quite inefficient manner, because their APIs and API consumers are hard-dependent on absolute paths and/or being able to access files and directory paths without an existing directory handle. Zig mostly works with directory and file handles already, and supports iterating over WASI preopens instead of pretending paths like
/abcor./abcare even remotely coherent on WASI.In essence, although plenty of things can be reasonably supported in a cross-platform manner, you cannot commit yourself to such a specific standard like POSIX and expect it to work flawlessly everywhere and fit every use case.
std.osshould be native rather than trying to stretch itself over unnatural targets, and features elsewhere in the standard library that depend on OS-specific details should not promise features that are impossible to implement correctly for every target. They should be exposed, just not with the proposition that they will work anywhere. An perfect example of this isstd.fs.cwd(), which makes the correct choice by emitting a compile error on WASI instead of attempting to shim a concept that is wholly inapplicable to WASI.jayschwa commentedon Oct 7, 2020
I think the standard library has too much
if wasi, else if windows, else posixlogic sprinkled throughout it. I can't tell if this proposal would help or hurt that.My half-baked hot take: a namespace like
std.fsshouldn't know what an operating system is. Instead, it should provide a conservative interface for file-like objects that other namespaces (OS or otherwise) can satisfy.io.Readerandio.Writerare good (albeit simpler) examples of what I mean.This could be layered. If I'm writing a package that allows Zig to target "Jayschwa OS", I could choose to satisfy the
std.fsinterface directly. Or instead, I could choose to satisfy the lower-level POSIX interface (how BYOS kind of works now) and get its support forstd.fs(and probably other interfaces) for free.katesuyu commentedon Oct 7, 2020
@jayschwa I agree that
std.fsshould allow overriding the basic file-like objects directly, however I disagree that the switches are bad. Referring to specific concrete APIs, as appropriate, massively improves the readability and navigability of the codebase. Deferring to a duck-typed platform abstraction layer is the real issue. Switches avoid forcing readers of the code to sift through a heap of files to find how each platform implements the same (usually simple) function, and avoid code duplication when multiple platforms are handled by the same switch arm.Sticking the implementation of everything in separate platform-specific modules is what makes the Rust standard library (and many Rust crates that copy the standard library's organization) horrific to navigate, and the more layers of this you have, the more you end up like our present day
std.os(which has already fallen for this trap: seeconst systemand all the conditionalusingnamespacedeclarations).BYOS should be split up into more discretely implementable interfaces, but there should be no internal abstraction layer:
std.fsshould keep its switch statements, but allow its structures and functions to be overriden on a case-by-case basis. And specific APIs should not have implementations on platforms where this is impractical!Rocknest commentedon Oct 7, 2020
I like the idea of a zig-style posix layer that unifies error codes, checks if arguments are correct, takes care of version prefixed/extended/safe apis. This is must have until zig's own higher level apis provide all possible interactions with the os.
However POSIX as a fit-any-os apis is obsolete, existing "posix-compatible" oses continue to diverge from it, some posix abstraction prevent efficient software so most new oses are not compatible with posix.
I think
std.os.posixshould exist mostly how it is now, however without any fallbacks, so if i want to use a posix api i must check first if it exists:std.os.posix.has("name"). Maybe we should have a common place for fallbacks that std uses, namedstd.os.@"$internal$"(or other not trivially accessible name), for examplecopy_file_rangewould be moved there.squeek502 commentedon Oct 8, 2020
I've run into confusion regarding this as well, e.g. with
realpath(#4658 (comment)):(note that
fs.realpathis just an alias foros.realpathso the following applies toos.realpath)It's unclear to me what behavior is 'correct,' and currently it feels like both
std.fsandstd.osare not very clear about how/if they should handle platform-specific behavior.qgcarver commentedon Oct 8, 2020
If we go through with this proposal, are we making a decision on #1840 ?
Writing platform code for Windows is pretty shaky right now, and if we're renaming it to posix or not is related. #5037 #4426
Edit: to clarify, #1840 'prefers' ntdll, but #4426 is on shakier ground, no proposal has been accepted.
Rocknest commentedon Oct 8, 2020
@squeek502 in my opinion all platform dependant behaviour in std.fs and other high level apis is a bug. For example in the case of realpath, on windows it should be emulated with few more syscalls, however if some behaviour is unemulatable then it should return error.Unsupported.
ron-wolf commentedon Oct 10, 2020
I arrived here via the Zig English Telegram group. Here’s a related essay (found on Lobste.rs) about Go’s somewhat similar issues when compared with Rust: “Early Impressions of Go from a Rust Programmer”
lukewilson2002 commentedon Oct 21, 2020
IMO only common behavior which is known to function fine on most operating systems should be exposed in a high-level interface. There shouldn't be a need for an
error.Unsupportedwhen the high-level wrapper over OS functions implements interfaces to common OS functions likeFile.open(self, URI, mode),File.close(self)etc. OS equivalence should be chosen at compile time, to provide compile-time errors for impossible instructions.When a user (of the OS library) requires an OS-specific API, they can get it from that OS-specific library. Fits with Zig zen of communicating intent precisely and preferring compile errors opposed to runtime crashes.
andrewrk commentedon Oct 27, 2020
I generally support this. That said,
std.oscurrently has 5,657 lines of code, and it's useful logic that needs to exist somewhere. So I'm not sure what it would look like to slap an "accept" label on this and then start implementing it. But I would be in favor of and supportive of self-contained patches that start moving the standard library toward's @LemonBoy's vision, provided that we have clear upgrade paths for the existing use cases we support.@LemonBoy Here are some questions I have about your vision. And note these are not arguments against it; my intent is to help move your project forward.
execveand I don't care about Windows or other operating systems that don't support it? Currently that looks like callingstd.os.execve. In C, it looks like#include <unistd.h>and callingexecve. Same question for:pipe,waitpid,kevent, and a few more.std.time,std.fs,std.process,std.netwhich are the high level operating system abstractions? How will these be affected by this?Also just to clarify - nothing in the std lib is required to go through
std.osas a "lower level API". That was only ever done when it made sense in terms of code organization. It just happens to be a convenient place to put a bunch of abstractions.Can we come up with some clear bullet points which act as guidelines for creating patches towards the goal of this issue? Maybe an example patch that incrementally moves the std lib towards this?
(edit: also let's wait until 0.7.0 is tagged before starting to work on this, seems like a lot of breaking changes)
47 remaining items
goodbye posix.listen
goodbye posix.accept
std.posix: delete epoll APIs
goodbye posix.getsockname
std.posix: remove getsockopt, getsockoptError
goodbye posix.fstatat
goodbye posix.fork
goodbye posix.nanosleep
posix: remove send, sendto, sendmsg
std.posix: remove recvfrom, recvmsg
std: move memory locking and memory protection to process
andrewrk commentedon Mar 27, 2026
Migrated to https://codeberg.org/ziglang/zig/issues/31694