1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
/*
Appellation: atomic <module>
Created At: 2026.08.13:00:00:00
Contrib: @FL03
*/
//! Atomic write: tempfile -> fsync -> rename -> fsync(dir).
//!
//! The publication sequence is fixed: create a same-directory candidate with
//! `O_CREAT|O_EXCL`, write the canonical bytes plus one newline, flush and
//! fsync the file, rename it over the target, then fsync the parent directory.
//! Directory fsync is a POSIX durability boundary and fails explicitly on a
//! host that cannot provide it.
//!
//! No `std::process` anywhere in this module: `crates/core` may not depend on
//! it (decision 8, enforced by `boundaries.yml`'s process/argv grep), so the
//! temp filename's uniqueness comes from a wall-clock timestamp plus an
//! in-process atomic counter instead of a process id. That means two
//! *different OS processes* can independently compute the same
//! `(nanos, counter)` candidate and both call [`temp_path`] for the same
//! `target` at (effectively) the same nanosecond — the in-process counter
//! only rules out a same-process collision. [`create_temp_file_exclusive`]
//! is what turns that possible collision into a retry instead of silent
//! corruption: it opens each candidate with `O_CREAT|O_EXCL`
//! (`create_new(true)`), so a colliding candidate fails atomically with
//! `ErrorKind::AlreadyExists` rather than truncating whatever the other
//! writer already has open there.
use ;
use ;
use crate;
static ATOMIC_WRITE_SEQ: AtomicU64 = new;
/// Attempts before [`create_temp_file_exclusive`] gives up. Each attempt is
/// an independently generated `(nanos, counter)` candidate; a collision on
/// every one of a hundred of them would mean something structurally wrong
/// (a stuck clock, an exhausted counter) rather than an ordinary two-process
/// race, which is expected to resolve within one or two retries.
const MAX_TEMP_NAME_ATTEMPTS: u32 = 100;
/// A candidate path in `target`'s own directory. Same directory matters: a
/// rename is only atomic within one filesystem, and a tempfile placed
/// elsewhere (e.g. `$TMPDIR`) can land on a different one.
///
/// This is a *candidate*, not a guaranteed-unique name — see the module doc
/// comment. [`create_temp_file_exclusive`] is what actually enforces
/// uniqueness, by retrying with a fresh candidate on collision.
/// Exclusively create a tempfile from a sequence of candidate paths. Each candidate is
/// opened with `O_CREAT|O_EXCL` (`create_new(true)`), which atomically fails
/// with `ErrorKind::AlreadyExists` if the path already exists instead of
/// truncating whatever is there. On that specific error, a fresh candidate
/// is pulled from `next_candidate` and retried, bounded by
/// [`MAX_TEMP_NAME_ATTEMPTS`].
///
/// Parameterized over the candidate generator (rather than calling
/// [`temp_path`] directly) so a test can force a collision deterministically
/// — seed the first candidate with a path a fixture already created, then
/// hand back a genuinely free one — without depending on a real two-process
/// race or wall-clock timing.
///
/// Returns the path that was exclusively created and the open handle
/// positioned at its start, ready for writing.
///
/// # Errors
///
/// Returns [`Error::Unknown`] if a candidate fails to open for a reason
/// other than `AlreadyExists`, or if every one of
/// [`MAX_TEMP_NAME_ATTEMPTS`] candidates collided.
/// Write `contents` to `target` atomically with the pinned run-store
/// sequencing and bytes (a trailing `\n` after `contents`, which `contents`
/// itself should not already carry — [`crate::run::RunState::to_canonical_json`]
/// does not add one, for exactly this reason).
pub
/// Flush the directory entry that now points at the renamed file.
///
/// On POSIX the write is not durable until the DIRECTORY is flushed too, not
/// just the file's own bytes: the rename is metadata, and metadata lives in the
/// parent.
/// Windows has no equivalent step, and attempting one is not a no-op -- it is a
/// hard failure.
///
/// `std::fs::File::open` on a directory needs `FILE_FLAG_BACKUP_SEMANTICS`,
/// which `open` does not set, so every store returned
/// `Access is denied. (os error 5)` and no run could be persisted at all. Even
/// with the flag, `FlushFileBuffers` on a directory handle is not a supported
/// operation.
///
/// This is a deliberate platform difference, not a swallowed error. NTFS
/// journals metadata operations, so the rename that published `target` is
/// already recorded in the log by the time it returns; there is no separate
/// directory entry left unflushed the way there is on POSIX.
/// Exclusively create a tempfile near `target` (via `next_candidate` and
/// [`create_temp_file_exclusive`]), write `contents` + a trailing `\n` to
/// it, fsync it, then rename it onto `target`.
///
/// Split out from [`atomic_write`] so the collision-retry path is
/// unit-testable directly: a test supplies `next_candidate` and inspects
/// `target` afterward, without needing `atomic_write`'s directory-creation
/// or closing directory-fsync steps. `pub(super)` (rather than private) is
/// exactly that seam: visible to `run::tests::atomic_io`, invisible outside
/// the `run` module.
///
/// Cleanup on failure only ever targets the ONE tempfile this call itself
/// exclusively created (`tmp`, below) — never a path merely returned by
/// `next_candidate`, since a collision there means that path belongs to
/// another writer.
pub