std: rework atomic file / temp file API #30686

Merged
andrewrk merged 6 commits from std.Io.File.Atomic into master 2026-01-06 05:32:19 +01:00
Owner

Builds upon #30703

Main motivation for this change was to move the call to std.crypto.random below the std.Io.VTable. Specifically, the one in std.Io.File.Atomic.init.

At the same time, I took the opportunity to integrate it with O_TMPFILE on Linux. I'd like to take the opportunity to complain about this API. First of all, it's almost very good. It gives the ability to create an ephemeral, unnamed file descriptor, which one can operate on freely until ready to materialize it onto the file system. If the process terminates before it gets around to doing that, the OS garbage collects the file, rather than leaving temporary, insecure trash around. Brilliant! Unfortunately, due to multiple bugs and a debilitating design limitation, the API is nearly useless.

First of all, O_TMPFILE is split across 2 bits on some architectures, and missing from another. Wtf? That's not a real problem though, moving on.

When using O_TMPFILE, how would you guess that openat() indicates that the file system does not support that operation? Perhaps with ENOSYS? Or OPNOTSUPP perchance? The all-singing, all-dancing EINVAL? Those would be two very reasonable guesses, and an acceptable third, however, wrong!! It returns either EISDIR or ENOENT. As a reminder, this is openat() we're talking about, so we very much need to know whether the path doesn't exist, or the temp file mechanism doesn't work.

Next up we have a missing API. linkat() doesn't support the AT_REPLACE flag even though there was a patch for it submitted nearly 10 years ago that was perfectly fine. Linus said it was OK, and then it just never got merged. Without this flag, O_TMPFILE cannot be used to atomically overwrite an existing file. This means if you want to do that you have to create a regular old non temp file with random numbers or something, and then use renameat().

So the only time that this O_TMPFILE trick actually does any good is if you want hard link semantics, i.e. you want error.PathAlreadyExists when the destination path already exists. Think about it, if you deleted the file to make room, then it wouldn't be atomic any more!

OK, rant over.

Anyway the upshot of this is, the moment any OS fixes their shitty APIs with respect to temporary files, we can change std.Io.Threaded accordingly, and all the Zig code that uses std.Io can remain unchanged and gain those benefits transparently.

Finally, this branch introduces std.Io.File.hardLink API, which only works on Linux, and is needed in order to materialize a O_TMPFILE file descriptor without replacement semantics.

Upgrade Guide

    var buffer: [1024]u8 = undefined;
    var atomic_file = try dest_dir.atomicFile(io, dest_path, .{
        .permissions = actual_permissions,
        .write_buffer = &buffer,
    });
    defer atomic_file.deinit();

    // do something with atomic_file.file_writer;

    try atomic_file.flush();
    try atomic_file.renameIntoPlace();

⬇️

    var atomic_file = try dest_dir.createFileAtomic(io, dest_path, .{
        .permissions = actual_permissions,
        .make_path = true,
        .replace = true,
    });
    defer atomic_file.deinit(io);

    var buffer: [1024]u8 = undefined; // Used only when direct fd-to-fd is not available.
    var file_writer = atomic_file.file.writer(io, &buffer);

    // do something with file_writer

    try file_writer.flush();
    try atomic_file.replace(io); // or set .replace = false above and call link() instead

Followup Work

Builds upon https://codeberg.org/ziglang/zig/pulls/30703 Main motivation for this change was to move the call to `std.crypto.random` below the `std.Io.VTable`. Specifically, the one in `std.Io.File.Atomic.init`. At the same time, I took the opportunity to integrate it with `O_TMPFILE` on Linux. I'd like to take the opportunity to complain about this API. First of all, it's almost very good. It gives the ability to create an ephemeral, unnamed file descriptor, which one can operate on freely until ready to materialize it onto the file system. If the process terminates before it gets around to doing that, the OS garbage collects the file, rather than leaving temporary, insecure trash around. Brilliant! Unfortunately, due to multiple bugs and a debilitating design limitation, the API is nearly useless. First of all, `O_TMPFILE` is split across 2 bits on some architectures, and missing from another. Wtf? That's not a real problem though, moving on. When using `O_TMPFILE`, how would you guess that `openat()` indicates that the file system does not support that operation? Perhaps with `ENOSYS`? Or `OPNOTSUPP` perchance? The all-singing, all-dancing `EINVAL`? Those would be two very reasonable guesses, and an acceptable third, however, wrong!! It returns either `EISDIR` or `ENOENT`. As a reminder, this is `openat()` we're talking about, so we very much need to know whether the path doesn't exist, or the temp file mechanism doesn't work. Next up we have a missing API. `linkat()` doesn't support the `AT_REPLACE` flag even though there was [a patch](https://patchwork.kernel.org/project/linux-fsdevel/patch/c823982d5b46ea888dc1fdf26c067a7aa0f3585f.1490103963.git.osandov@fb.com/) for it submitted nearly 10 years ago that was perfectly fine. Linus said it was OK, and then it just never got merged. Without this flag, `O_TMPFILE` cannot be used to atomically overwrite an existing file. This means if you want to do that you have to create a regular old non temp file with random numbers or something, and then use `renameat()`. So the only time that this `O_TMPFILE` trick actually does any good is if you want hard link semantics, i.e. you want `error.PathAlreadyExists` when the destination path already exists. Think about it, if you deleted the file to make room, then it wouldn't be atomic any more! OK, rant over. Anyway the upshot of this is, the moment any OS fixes their shitty APIs with respect to temporary files, we can change `std.Io.Threaded` accordingly, and all the Zig code that uses `std.Io` can remain unchanged and gain those benefits transparently. Finally, this branch introduces `std.Io.File.hardLink` API, which only works on Linux, and is needed in order to materialize a `O_TMPFILE` file descriptor without replacement semantics. ## Upgrade Guide ```zig var buffer: [1024]u8 = undefined; var atomic_file = try dest_dir.atomicFile(io, dest_path, .{ .permissions = actual_permissions, .write_buffer = &buffer, }); defer atomic_file.deinit(); // do something with atomic_file.file_writer; try atomic_file.flush(); try atomic_file.renameIntoPlace(); ``` ⬇️ ```zig var atomic_file = try dest_dir.createFileAtomic(io, dest_path, .{ .permissions = actual_permissions, .make_path = true, .replace = true, }); defer atomic_file.deinit(io); var buffer: [1024]u8 = undefined; // Used only when direct fd-to-fd is not available. var file_writer = atomic_file.file.writer(io, &buffer); // do something with file_writer try file_writer.flush(); try atomic_file.replace(io); // or set .replace = false above and call link() instead ``` ## Followup Work * https://codeberg.org/ziglang/zig/issues/30707
andrewrk force-pushed std.Io.File.Atomic from be13e2240d
Some checks failed
ci / aarch64-linux-debug (pull_request) Has been cancelled
ci / aarch64-macos-debug (pull_request) Has been cancelled
ci / aarch64-linux-release (pull_request) Has been cancelled
ci / x86_64-freebsd-release (pull_request) Has been cancelled
ci / x86_64-freebsd-debug (pull_request) Has been cancelled
ci / x86_64-linux-debug-llvm (pull_request) Has been cancelled
ci / x86_64-windows-debug (pull_request) Has been cancelled
ci / x86_64-windows-release (pull_request) Has been cancelled
ci / x86_64-linux-debug (pull_request) Has been cancelled
ci / x86_64-linux-release (pull_request) Has been cancelled
ci / aarch64-macos-release (pull_request) Has been cancelled
ci / loongarch64-linux-debug (pull_request) Has been cancelled
ci / loongarch64-linux-release (pull_request) Has been cancelled
ci / riscv64-linux-debug (pull_request) Has been cancelled
ci / riscv64-linux-release (pull_request) Has been cancelled
ci / s390x-linux-debug (pull_request) Has been cancelled
ci / s390x-linux-release (pull_request) Has been cancelled
to 86990cca88
Some checks failed
ci / aarch64-macos-release (pull_request) Has been cancelled
ci / loongarch64-linux-debug (pull_request) Has been cancelled
ci / loongarch64-linux-release (pull_request) Has been cancelled
ci / riscv64-linux-debug (pull_request) Has been cancelled
ci / riscv64-linux-release (pull_request) Has been cancelled
ci / s390x-linux-release (pull_request) Has been cancelled
ci / x86_64-freebsd-debug (pull_request) Has been cancelled
ci / x86_64-freebsd-release (pull_request) Has been cancelled
ci / x86_64-linux-debug (pull_request) Has been cancelled
ci / s390x-linux-debug (pull_request) Has been cancelled
ci / aarch64-macos-debug (pull_request) Has been cancelled
ci / x86_64-linux-debug-llvm (pull_request) Has been cancelled
ci / x86_64-windows-debug (pull_request) Has been cancelled
ci / x86_64-windows-release (pull_request) Has been cancelled
ci / aarch64-linux-debug (pull_request) Has been cancelled
ci / aarch64-linux-release (pull_request) Has been cancelled
ci / x86_64-linux-release (pull_request) Has been cancelled
2026-01-05 07:27:45 +01:00
Compare
andrewrk force-pushed std.Io.File.Atomic from f5a97f1f2f
Some checks failed
ci / aarch64-linux-release (pull_request) Has been cancelled
ci / aarch64-linux-debug (pull_request) Has been cancelled
ci / x86_64-linux-debug (pull_request) Has been cancelled
ci / aarch64-macos-release (pull_request) Has been cancelled
ci / aarch64-macos-debug (pull_request) Has been cancelled
ci / s390x-linux-debug (pull_request) Has been cancelled
ci / x86_64-freebsd-debug (pull_request) Has been cancelled
ci / x86_64-freebsd-release (pull_request) Has been cancelled
ci / x86_64-linux-release (pull_request) Has been cancelled
ci / x86_64-linux-debug-llvm (pull_request) Has been cancelled
ci / x86_64-windows-debug (pull_request) Has been cancelled
ci / x86_64-windows-release (pull_request) Has been cancelled
ci / loongarch64-linux-debug (pull_request) Has been cancelled
ci / loongarch64-linux-release (pull_request) Has been cancelled
ci / riscv64-linux-debug (pull_request) Has been cancelled
ci / riscv64-linux-release (pull_request) Has been cancelled
ci / s390x-linux-release (pull_request) Has been cancelled
to 2e573e36fa
Some checks failed
ci / loongarch64-linux-debug (pull_request) Has been cancelled
ci / loongarch64-linux-release (pull_request) Has been cancelled
ci / riscv64-linux-debug (pull_request) Has been cancelled
ci / riscv64-linux-release (pull_request) Has been cancelled
ci / s390x-linux-debug (pull_request) Has been cancelled
ci / aarch64-linux-release (pull_request) Has been cancelled
ci / aarch64-linux-debug (pull_request) Has been cancelled
ci / s390x-linux-release (pull_request) Has been cancelled
ci / x86_64-windows-debug (pull_request) Has been cancelled
ci / aarch64-macos-debug (pull_request) Has been cancelled
ci / aarch64-macos-release (pull_request) Has been cancelled
ci / x86_64-linux-debug-llvm (pull_request) Has been cancelled
ci / x86_64-windows-release (pull_request) Has been cancelled
ci / x86_64-linux-release (pull_request) Has been cancelled
ci / x86_64-linux-debug (pull_request) Has been cancelled
ci / x86_64-freebsd-debug (pull_request) Has been cancelled
ci / x86_64-freebsd-release (pull_request) Has been cancelled
2026-01-05 08:45:55 +01:00
Compare
andrewrk force-pushed std.Io.File.Atomic from 2e573e36fa
Some checks failed
ci / loongarch64-linux-debug (pull_request) Has been cancelled
ci / loongarch64-linux-release (pull_request) Has been cancelled
ci / riscv64-linux-debug (pull_request) Has been cancelled
ci / riscv64-linux-release (pull_request) Has been cancelled
ci / s390x-linux-debug (pull_request) Has been cancelled
ci / aarch64-linux-release (pull_request) Has been cancelled
ci / aarch64-linux-debug (pull_request) Has been cancelled
ci / s390x-linux-release (pull_request) Has been cancelled
ci / x86_64-windows-debug (pull_request) Has been cancelled
ci / aarch64-macos-debug (pull_request) Has been cancelled
ci / aarch64-macos-release (pull_request) Has been cancelled
ci / x86_64-linux-debug-llvm (pull_request) Has been cancelled
ci / x86_64-windows-release (pull_request) Has been cancelled
ci / x86_64-linux-release (pull_request) Has been cancelled
ci / x86_64-linux-debug (pull_request) Has been cancelled
ci / x86_64-freebsd-debug (pull_request) Has been cancelled
ci / x86_64-freebsd-release (pull_request) Has been cancelled
to 36044a0e81
Some checks failed
ci / x86_64-freebsd-release (pull_request) Successful in 35m10s
ci / x86_64-freebsd-debug (pull_request) Successful in 57m14s
ci / aarch64-macos-release (pull_request) Successful in 1h0m1s
ci / x86_64-linux-debug (pull_request) Successful in 1h13m28s
ci / aarch64-linux-release (pull_request) Successful in 1h30m27s
ci / x86_64-linux-release (pull_request) Successful in 1h57m36s
ci / aarch64-macos-debug (pull_request) Successful in 1h58m54s
ci / aarch64-linux-debug (pull_request) Successful in 2h32m25s
ci / s390x-linux-release (pull_request) Successful in 1h37m55s
ci / x86_64-linux-debug-llvm (pull_request) Successful in 2h42m38s
ci / s390x-linux-debug (pull_request) Successful in 4h7m30s
ci / loongarch64-linux-debug (pull_request) Failing after 2h50m20s
ci / loongarch64-linux-release (pull_request) Failing after 3h0m37s
ci / x86_64-windows-debug (pull_request) Failing after 48m38s
ci / x86_64-windows-release (pull_request) Failing after 55m31s
ci / riscv64-linux-debug (pull_request) Has been skipped
ci / riscv64-linux-release (pull_request) Has been skipped
2026-01-05 09:19:29 +01:00
Compare
andrewrk force-pushed std.Io.File.Atomic from 36044a0e81
Some checks failed
ci / x86_64-freebsd-release (pull_request) Successful in 35m10s
ci / x86_64-freebsd-debug (pull_request) Successful in 57m14s
ci / aarch64-macos-release (pull_request) Successful in 1h0m1s
ci / x86_64-linux-debug (pull_request) Successful in 1h13m28s
ci / aarch64-linux-release (pull_request) Successful in 1h30m27s
ci / x86_64-linux-release (pull_request) Successful in 1h57m36s
ci / aarch64-macos-debug (pull_request) Successful in 1h58m54s
ci / aarch64-linux-debug (pull_request) Successful in 2h32m25s
ci / s390x-linux-release (pull_request) Successful in 1h37m55s
ci / x86_64-linux-debug-llvm (pull_request) Successful in 2h42m38s
ci / s390x-linux-debug (pull_request) Successful in 4h7m30s
ci / loongarch64-linux-debug (pull_request) Failing after 2h50m20s
ci / loongarch64-linux-release (pull_request) Failing after 3h0m37s
ci / x86_64-windows-debug (pull_request) Failing after 48m38s
ci / x86_64-windows-release (pull_request) Failing after 55m31s
ci / riscv64-linux-debug (pull_request) Has been skipped
ci / riscv64-linux-release (pull_request) Has been skipped
to 7cf84284e0
Some checks failed
ci / x86_64-freebsd-release (pull_request) Successful in 53m41s
ci / x86_64-freebsd-debug (pull_request) Successful in 1h18m7s
ci / aarch64-linux-release (pull_request) Successful in 1h32m34s
ci / aarch64-linux-debug (pull_request) Successful in 2h24m22s
ci / aarch64-macos-release (pull_request) Successful in 53m10s
ci / aarch64-macos-debug (pull_request) Successful in 1h36m4s
ci / x86_64-linux-release (pull_request) Successful in 2h22m7s
ci / x86_64-linux-debug (pull_request) Successful in 1h20m30s
ci / x86_64-linux-debug-llvm (pull_request) Successful in 2h26m32s
ci / x86_64-windows-release (pull_request) Has been cancelled
ci / x86_64-windows-debug (pull_request) Has been cancelled
ci / loongarch64-linux-debug (pull_request) Has been cancelled
ci / loongarch64-linux-release (pull_request) Has been cancelled
ci / riscv64-linux-debug (pull_request) Has been cancelled
ci / riscv64-linux-release (pull_request) Has been cancelled
ci / s390x-linux-debug (pull_request) Has been cancelled
ci / s390x-linux-release (pull_request) Has been cancelled
2026-01-05 21:24:33 +01:00
Compare
andrewrk force-pushed std.Io.File.Atomic from 7cf84284e0
Some checks failed
ci / x86_64-freebsd-release (pull_request) Successful in 53m41s
ci / x86_64-freebsd-debug (pull_request) Successful in 1h18m7s
ci / aarch64-linux-release (pull_request) Successful in 1h32m34s
ci / aarch64-linux-debug (pull_request) Successful in 2h24m22s
ci / aarch64-macos-release (pull_request) Successful in 53m10s
ci / aarch64-macos-debug (pull_request) Successful in 1h36m4s
ci / x86_64-linux-release (pull_request) Successful in 2h22m7s
ci / x86_64-linux-debug (pull_request) Successful in 1h20m30s
ci / x86_64-linux-debug-llvm (pull_request) Successful in 2h26m32s
ci / x86_64-windows-release (pull_request) Has been cancelled
ci / x86_64-windows-debug (pull_request) Has been cancelled
ci / loongarch64-linux-debug (pull_request) Has been cancelled
ci / loongarch64-linux-release (pull_request) Has been cancelled
ci / riscv64-linux-debug (pull_request) Has been cancelled
ci / riscv64-linux-release (pull_request) Has been cancelled
ci / s390x-linux-debug (pull_request) Has been cancelled
ci / s390x-linux-release (pull_request) Has been cancelled
to 06130c5e61
Some checks failed
ci / aarch64-linux-release (pull_request) Successful in 1h32m22s
ci / x86_64-windows-release (pull_request) Successful in 54m48s
ci / x86_64-windows-debug (pull_request) Successful in 58m40s
ci / aarch64-macos-release (pull_request) Successful in 53m29s
ci / aarch64-linux-debug (pull_request) Successful in 2h24m55s
ci / aarch64-macos-debug (pull_request) Successful in 2h28m21s
ci / x86_64-linux-debug (pull_request) Successful in 1h4m12s
ci / x86_64-freebsd-release (pull_request) Successful in 53m59s
ci / x86_64-freebsd-debug (pull_request) Successful in 1h10m47s
ci / x86_64-linux-debug-llvm (pull_request) Successful in 3h3m10s
ci / x86_64-linux-release (pull_request) Successful in 2h59m39s
ci / s390x-linux-release (pull_request) Successful in 1h55m45s
ci / s390x-linux-debug (pull_request) Successful in 2h44m5s
ci / riscv64-linux-debug (pull_request) Has been skipped
ci / riscv64-linux-release (pull_request) Has been skipped
ci / loongarch64-linux-debug (pull_request) Failing after 2h49m16s
ci / loongarch64-linux-release (pull_request) Failing after 3h0m30s
2026-01-06 05:31:57 +01:00
Compare
andrewrk merged commit c906f7d2e7 into master 2026-01-06 05:32:19 +01:00
andrewrk deleted branch std.Io.File.Atomic 2026-01-06 05:32:20 +01:00
Sign in to join this conversation.
No reviewers
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
ziglang/zig!30686
No description provided.