std: rework atomic file / temp file API #30686
No reviewers
Labels
No labels
abi/f32
abi/ilp32
abi/sf
accepted
arch/21k
arch/6502
arch/aarch64
arch/alpha
arch/amdgcn
arch/arc
arch/arc32
arch/arc64
arch/arm
arch/avr
arch/bfin
arch/bpf
arch/colossus
arch/cris
arch/csky
arch/dlx
arch/epiphany
arch/fr30
arch/frv
arch/hexagon
arch/hppa
arch/hppa64
arch/ia64
arch/kalimba
arch/kvx
arch/lanai
arch/lm32
arch/loongarch32
arch/loongarch64
arch/m32r
arch/m68k
arch/m88k
arch/mcore
arch/microblaze
arch/mips
arch/mips64
arch/mmix
arch/moxie
arch/mrisc32
arch/msp430
arch/nds32
arch/ns32k
arch/nvptx
arch/or1k
arch/powerpc
arch/powerpc64
arch/propeller
arch/riscv32
arch/riscv64
arch/rl78
arch/rx
arch/s390x
arch/sh
arch/sparc
arch/sparc64
arch/spirv
arch/spu
arch/tricore
arch/v850
arch/vax
arch/vc4
arch/ve
arch/wasm
arch/x86
arch/x86_64
arch/xcore
arch/xtensa
autodoc
backend/c
backend/llvm
backend/self-hosted
binutils
breaking
build system
debug info
docs
error message
frontend
fuzzing
incremental
lib/c
lib/compiler-rt
lib/cxx
lib/std
lib/tsan
lib/ubsan-rt
lib/unwind
linking
miscompilation
os/android
os/dragonfly
os/driverkit
os/emscripten
os/freebsd
os/fuchsia
os/haiku
os/hurd
os/illumos
os/ios
os/linux
os/maccatalyst
os/macos
os/managarm
os/netbsd
os/ohos
os/openbsd
os/plan9
os/redox
os/rtems
os/serenity
os/tvos
os/uefi
os/visionos
os/wasi
os/watchos
os/windows
proposal
release notes
testing
zig cc
zig fmt
zig reduce
bounty
bug
contributor-friendly
downstream
enhancement
infra
optimization
question
regression
upstream
use case
No milestone
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
ziglang/zig!30686
Loading…
Reference in a new issue
No description provided.
Delete branch "std.Io.File.Atomic"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Builds upon #30703
Main motivation for this change was to move the call to
std.crypto.randombelow thestd.Io.VTable. Specifically, the one instd.Io.File.Atomic.init.At the same time, I took the opportunity to integrate it with
O_TMPFILEon 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_TMPFILEis 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 thatopenat()indicates that the file system does not support that operation? Perhaps withENOSYS? OrOPNOTSUPPperchance? The all-singing, all-dancingEINVAL? Those would be two very reasonable guesses, and an acceptable third, however, wrong!! It returns eitherEISDIRorENOENT. As a reminder, this isopenat()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 theAT_REPLACEflag 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_TMPFILEcannot 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 userenameat().So the only time that this
O_TMPFILEtrick actually does any good is if you want hard link semantics, i.e. you wanterror.PathAlreadyExistswhen 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.Threadedaccordingly, and all the Zig code that usesstd.Iocan remain unchanged and gain those benefits transparently.Finally, this branch introduces
std.Io.File.hardLinkAPI, which only works on Linux, and is needed in order to materialize aO_TMPFILEfile descriptor without replacement semantics.Upgrade Guide
⬇️
Followup Work
be13e2240d86990cca88f5a97f1f2f2e573e36fa2e573e36fa36044a0e8136044a0e817cf84284e07cf84284e006130c5e61move_srcincremental test has started failing onloongarch64-linux#30748