Documentation
¶
Overview ¶
Package atomicfile writes a file through a sibling temp file and a rename, so a concurrent reader sees one whole version or another and never a torn one.
It is a leaf, and that is the point. This body used to live in internal/cclink beside the credentials file it was written for, which meant every package that needed an atomic write took a dependency on the package that reads and rewrites Claude Code's login. A switch that must never touch that login cannot prove it while importing the package that does, so the writer moved out and cclink keeps a wrapper.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ReadFile ¶ added in v0.31.0
ReadFile reads a file WriteFile replaces, retried per winerr.Retryable.
The replace is atomic for a reader on every platform, but on Windows a reader that opens the file at the instant another process renames over it gets a sharing violation rather than either version -- measured on CI, eight goroutines opening the store while one of them wrote it. A missing file is returned on the first try: winerr.Retryable does not name it, because it is an answer, not a transient.
func SwapRename ¶
SwapRename replaces the rename step for the duration of one test and returns a function that puts the original back.
It is exported for exactly one caller: internal/cclink's global-config test asserts that Claude Code's config lock is HELD at the moment of the rename, not merely around the call, and the rename now happens in this package. That assertion is the one that separates an implementation which read under the lock, released, and then wrote -- which would satisfy every other test in that file while racing Claude Code's own rewrite of the file on startup.
It is not safe for concurrent use and must not be called outside a test.
func TempPattern ¶
TempPattern returns the name of the sibling temp file WriteFile writes beside path, as a pattern.
The returned string is simultaneously an os.CreateTemp pattern and a filepath.Glob pattern — both read `*` as "the part that varies" — and that is the whole point of exporting it. A rename that never completed strands the temp file, and whatever collects those orphans has to name them; daemon.SweepStatusTemps is the one that does. Deriving its glob from here rather than spelling the suffix a second time is what stops the writer and the sweeper from drifting apart. They were two literals under a comment claiming they were one, nothing executed that claim, and changing the writer's left the whole suite green with the sweep collecting nothing.
It takes the full path rather than a base name so a caller cannot hand it the wrong half; only the base is used, so the result names one directory entry and never contains a separator.
The two readings part company for a base name holding a glob metacharacter — `*`, `?`, `[` — which os.CreateTemp takes literally and filepath.Match does not. Nothing sweeps such a file: the only sweeper names its files with constants, and the two callers that take a path from the user (`export --out`, `runway --out`) have no sweeper at all.
func WriteFile ¶
WriteFile writes data to path via a sibling temp file and a rename.
The temp file MUST be a sibling: a rename within one directory is atomic, so a concurrent reader sees either the whole old file or the whole new one. Writing to the system temp directory and moving across filesystems degrades to copy-then-unlink, where a reader can catch a half-written credential file.
The rename also changes the inode and advances the mtime, which is exactly what Claude Code's change probe watches, so a switch hot-reloads a running session with no restart.
On Windows an antivirus scanner or the search indexer may hold the target file open when the rename is attempted; that is retried per winerr.Retryable. If the temp file itself is still held after every retry, it can survive as an orphan — accepted here, since guarding against it needs a second retry loop for a rarer failure than the one this function already handles. An orphan is named by TempPattern, which is what a sweeper globs.
Types ¶
This section is empty.