Skip to main content

heddle_fs_prims/
fs_atomic.rs

1// SPDX-License-Identifier: Apache-2.0
2use std::{
3    fs::{self, File, OpenOptions},
4    io::{self, Write},
5    path::{Path, PathBuf},
6    sync::{
7        Arc, Mutex, OnceLock,
8        atomic::{AtomicU64, Ordering},
9    },
10    time::{SystemTime, UNIX_EPOCH},
11};
12
13#[derive(Default)]
14struct CloneDurabilityStats {
15    barriers: AtomicU64,
16    skipped: AtomicU64,
17}
18
19#[derive(Clone)]
20struct CloneDurabilityEntry {
21    root: PathBuf,
22    stats: Arc<CloneDurabilityStats>,
23}
24
25fn clone_durability_entries() -> &'static Mutex<Vec<CloneDurabilityEntry>> {
26    static ENTRIES: OnceLock<Mutex<Vec<CloneDurabilityEntry>>> = OnceLock::new();
27    ENTRIES.get_or_init(|| Mutex::new(Vec::new()))
28}
29
30fn deferred_clone_stats(path: &Path) -> Option<Arc<CloneDurabilityStats>> {
31    clone_durability_entries()
32        .lock()
33        .ok()?
34        .iter()
35        .rev()
36        .find(|entry| path.starts_with(&entry.root))
37        .map(|entry| Arc::clone(&entry.stats))
38}
39
40pub fn clone_write_is_deferred(path: &Path) -> bool {
41    deferred_clone_stats(path).is_some()
42}
43
44pub fn record_deferred_clone_barrier(path: &Path) {
45    if let Some(stats) = deferred_clone_stats(path) {
46        stats.skipped.fetch_add(1, Ordering::Relaxed);
47    }
48}
49
50/// Path-scoped durability suppression for reconstructible clone writes.
51///
52/// The caller must first persist a clone-intent marker outside this scope and
53/// must call [`commit`](Self::commit) only after hash-verifying the fetched
54/// closure. Writes outside `root` retain their ordinary per-operation fsyncs.
55pub struct CloneDurabilityBatch {
56    root: PathBuf,
57    stats: Arc<CloneDurabilityStats>,
58}
59
60impl CloneDurabilityBatch {
61    pub fn begin(root: impl AsRef<Path>) -> Self {
62        let root = root.as_ref().to_path_buf();
63        let stats = Arc::new(CloneDurabilityStats::default());
64        clone_durability_entries()
65            .lock()
66            .unwrap_or_else(std::sync::PoisonError::into_inner)
67            .push(CloneDurabilityEntry {
68                root: root.clone(),
69                stats: Arc::clone(&stats),
70            });
71        Self { root, stats }
72    }
73
74    /// Flush every dirty file and directory on the destination filesystem in
75    /// one kernel barrier. Refs remain unpublished while this runs.
76    pub fn commit(&self) -> io::Result<()> {
77        sync_filesystem(&self.root)?;
78        self.stats.barriers.fetch_add(1, Ordering::Relaxed);
79        Ok(())
80    }
81
82    pub fn barrier_count(&self) -> u64 {
83        self.stats.barriers.load(Ordering::Relaxed)
84    }
85
86    pub fn skipped_barrier_count(&self) -> u64 {
87        self.stats.skipped.load(Ordering::Relaxed)
88    }
89}
90
91impl Drop for CloneDurabilityBatch {
92    fn drop(&mut self) {
93        let mut entries = clone_durability_entries()
94            .lock()
95            .unwrap_or_else(std::sync::PoisonError::into_inner);
96        if let Some(index) = entries
97            .iter()
98            .rposition(|entry| entry.root == self.root && Arc::ptr_eq(&entry.stats, &self.stats))
99        {
100            entries.remove(index);
101        }
102    }
103}
104
105#[derive(Clone, Copy)]
106enum AtomicWriteKind {
107    Normal,
108    Secret,
109}
110
111impl AtomicWriteKind {
112    fn open_tmp(self, tmp: &Path) -> io::Result<File> {
113        let mut options = OpenOptions::new();
114        options.create_new(true).write(true);
115
116        #[cfg(unix)]
117        if matches!(self, Self::Secret) {
118            use std::os::unix::fs::OpenOptionsExt;
119            options.mode(0o600);
120        }
121
122        options.open(tmp)
123    }
124
125    fn enforce_before_write(self, file: &File) -> io::Result<()> {
126        match self {
127            Self::Normal => Ok(()),
128            Self::Secret => enforce_secret_permissions_before_write(file),
129        }
130    }
131}
132
133#[cfg(unix)]
134fn enforce_secret_permissions_before_write(file: &File) -> io::Result<()> {
135    use std::os::unix::fs::PermissionsExt;
136
137    file.set_permissions(fs::Permissions::from_mode(0o600))?;
138    let mode = file.metadata()?.permissions().mode() & 0o777;
139    if mode != 0o600 {
140        return Err(io::Error::new(
141            io::ErrorKind::PermissionDenied,
142            format!("secret temp file permissions are {mode:o}, expected 600"),
143        ));
144    }
145    Ok(())
146}
147
148#[cfg(not(unix))]
149fn enforce_secret_permissions_before_write(_file: &File) -> io::Result<()> {
150    // Non-Unix platforms do not expose POSIX mode bits through
151    // OpenOptions. The secret variant still uses the same create-new,
152    // write-fsync-rename discipline, but cannot verify a 0600 mode.
153    Ok(())
154}
155
156static TEMP_PATH_COUNTER: AtomicU64 = AtomicU64::new(0);
157
158/// POSIX `ENOSPC`. Identical on Linux and macOS. Windows surfaces disk-full
159/// as `ERROR_DISK_FULL` (112) or `ERROR_HANDLE_DISK_FULL` (39); we cover
160/// those by also checking `ErrorKind::StorageFull` (stable as of 1.83) and
161/// the older `ErrorKind::Other` "no space" message text as a fallback.
162const ENOSPC: i32 = 28;
163
164/// POSIX `ENOTEMPTY`. Linux=39, macOS/BSD=66. Windows surfaces this as
165/// `ERROR_DIR_NOT_EMPTY` (145). `ErrorKind::DirectoryNotEmpty` covers the
166/// portable case, but the raw codes are the canonical signal — Rust may
167/// still surface raw OS errors for paths the kernel reports unusually.
168const ENOTEMPTY_LINUX: i32 = 39;
169const ENOTEMPTY_MACOS: i32 = 66;
170const ENOTEMPTY_WINDOWS: i32 = 145;
171
172/// POSIX `EACCES`. Same code on Linux and macOS. `ErrorKind::PermissionDenied`
173/// covers Windows `ERROR_ACCESS_DENIED` (5) too.
174const EACCES: i32 = 13;
175
176/// POSIX `ENOENT`. Same code on Linux and macOS. `ErrorKind::NotFound` covers
177/// Windows `ERROR_FILE_NOT_FOUND` (2) and `ERROR_PATH_NOT_FOUND` (3).
178const ENOENT: i32 = 2;
179
180/// POSIX `EROFS`. Linux=30, macOS=30. `ErrorKind::ReadOnlyFilesystem` is
181/// the portable variant (stable as of 1.83).
182const EROFS: i32 = 30;
183
184/// POSIX `EXDEV` ("cross-device link"). Linux=18, macOS=18.
185/// `ErrorKind::CrossesDevices` is the portable variant (stable as of 1.83).
186const EXDEV: i32 = 18;
187
188/// Returns true when an `io::Error` indicates the filesystem is out of
189/// space. Centralised here because it's the same predicate used by
190/// `write_file_atomic` (the inner helper) and by the higher-level
191/// `cmd_snapshot` recovery path that prints the actionable message.
192pub fn is_out_of_space(err: &io::Error) -> bool {
193    if err.raw_os_error() == Some(ENOSPC) {
194        return true;
195    }
196    // `ErrorKind::StorageFull` is the portable kind. It maps to ENOSPC
197    // on Unix and the Windows disk-full codes. Available since Rust
198    // 1.83; the workspace MSRV is well past that.
199    if err.kind() == io::ErrorKind::StorageFull {
200        return true;
201    }
202    // `write_all` translates a short write into `WriteZero`. On a full
203    // disk, kernel can return a short write rather than ENOSPC outright
204    // (especially over network filesystems), so a `WriteZero` we couldn't
205    // otherwise classify is treated as out-of-space — overly inclusive
206    // here is safer than missing the signal.
207    if err.kind() == io::ErrorKind::WriteZero {
208        return true;
209    }
210    false
211}
212
213/// Returns true when an `io::Error` indicates a directory could not be
214/// removed because it still contained entries. The apply planner only removes
215/// tracked descendants; when tracked content is removed and the parent
216/// directory still holds untracked or explicitly ignored siblings, `remove_dir`
217/// returns this signal. We need both `ErrorKind::DirectoryNotEmpty` and the raw
218/// codes — Linux=39, macOS/BSD=66, Windows=145 — because Rust does not
219/// always translate every kernel surface into the portable `ErrorKind`.
220pub fn is_directory_not_empty(err: &io::Error) -> bool {
221    if err.kind() == io::ErrorKind::DirectoryNotEmpty {
222        return true;
223    }
224    matches!(
225        err.raw_os_error(),
226        Some(ENOTEMPTY_LINUX) | Some(ENOTEMPTY_MACOS) | Some(ENOTEMPTY_WINDOWS)
227    )
228}
229
230/// Returns true when an `io::Error` indicates the operation was denied
231/// for permissions reasons (`EACCES` on Unix, `ERROR_ACCESS_DENIED` on
232/// Windows). The portable `ErrorKind::PermissionDenied` covers most
233/// surfaces; the raw `EACCES` check handles oddball platforms that
234/// surface the OS code without translating to the portable kind.
235pub fn is_permission_denied(err: &io::Error) -> bool {
236    if err.kind() == io::ErrorKind::PermissionDenied {
237        return true;
238    }
239    err.raw_os_error() == Some(EACCES)
240}
241
242/// Returns true when an `io::Error` indicates the path referenced by an
243/// operation does not exist (`ENOENT` on Unix, `ERROR_FILE_NOT_FOUND` /
244/// `ERROR_PATH_NOT_FOUND` on Windows). Use this *only* at call sites
245/// where the operation expected the path to exist — the predicate alone
246/// can't distinguish "I expected this" from "I checked optionally".
247pub fn is_not_found(err: &io::Error) -> bool {
248    if err.kind() == io::ErrorKind::NotFound {
249        return true;
250    }
251    err.raw_os_error() == Some(ENOENT)
252}
253
254/// Returns true when an `io::Error` indicates the underlying filesystem
255/// is mounted read-only (`EROFS` on Unix). The portable
256/// `ErrorKind::ReadOnlyFilesystem` is preferred when present; we also
257/// match the raw OS code because some platforms (notably older macOS
258/// surfaces and certain remote filesystems) do not always translate.
259pub fn is_read_only_filesystem(err: &io::Error) -> bool {
260    if err.kind() == io::ErrorKind::ReadOnlyFilesystem {
261        return true;
262    }
263    err.raw_os_error() == Some(EROFS)
264}
265
266/// Returns true when an `io::Error` indicates a `rename` (or other
267/// link-style operation) attempted to bridge two filesystems (`EXDEV`).
268/// This is what trips when `temp_path` lands on a different mount than
269/// the destination — typically because `TMPDIR` is on a different volume,
270/// or the parent directory itself is a bind mount. We match both the
271/// portable `ErrorKind::CrossesDevices` and the raw `EXDEV` code.
272pub fn is_cross_device_link(err: &io::Error) -> bool {
273    if err.kind() == io::ErrorKind::CrossesDevices {
274        return true;
275    }
276    err.raw_os_error() == Some(EXDEV)
277}
278
279pub fn temp_path(path: &Path) -> PathBuf {
280    let parent = path.parent().unwrap_or_else(|| Path::new("."));
281    let file_name = path
282        .file_name()
283        .and_then(|s| s.to_str())
284        .filter(|s| !s.is_empty())
285        .unwrap_or("heddle-tmp");
286    let unique = SystemTime::now()
287        .duration_since(UNIX_EPOCH)
288        .map(|d| d.as_nanos())
289        .unwrap_or(0);
290    let counter = TEMP_PATH_COUNTER.fetch_add(1, Ordering::Relaxed);
291    let pid = std::process::id();
292    parent.join(format!(".{file_name}.tmp-{pid}-{unique}-{counter}"))
293}
294
295/// Kick a file's dirty page cache into background writeback WITHOUT waiting
296/// for it or issuing a device flush. Best-effort: any error is ignored, since
297/// the caller's subsequent `fsync` is what actually guarantees durability —
298/// this only *starts* the I/O early so many files' writeback overlaps instead
299/// of each `fsync` flushing its file synchronously from scratch.
300///
301/// Linux-only (`sync_file_range`); a no-op elsewhere, where the batched-fsync
302/// pass in [`stage_temp_files_durable`] simply runs without the overlap.
303#[cfg(target_os = "linux")]
304fn kick_writeback(file: &File) {
305    use std::os::unix::io::AsRawFd;
306    // SYNC_FILE_RANGE_WRITE = 2: initiate writeback of dirty pages in the
307    // given range (0..0 = whole file) without blocking. No barrier, no error
308    // path — a failure just means the later `sync_all` does the work.
309    const SYNC_FILE_RANGE_WRITE: libc::c_uint = 2;
310    unsafe {
311        libc::sync_file_range(file.as_raw_fd(), 0, 0, SYNC_FILE_RANGE_WRITE);
312    }
313}
314
315#[cfg(not(target_os = "linux"))]
316fn kick_writeback(_file: &File) {}
317
318/// Write many temp files with a single overlapped-writeback durability pass.
319///
320/// For each `(temp_path, bytes)`: create the temp file and write its contents,
321/// then start its page-cache writeback in the background ([`kick_writeback`]).
322/// After every file is written, `fsync` each one. On return, every temp file's
323/// data is on stable storage — the SAME guarantee as writing + `fsync`-ing each
324/// file individually — but the writeback I/O overlaps instead of serializing
325/// one synchronous `fsync` barrier per file.
326///
327/// This is the bulk-ref hot path (`heddle adopt` of N branches publishes N ref
328/// files in one batch): the per-file `write → fsync` loop paid ~N serial fsync
329/// barriers (~2.3s for 800 refs on a local SSD); overlapping the writeback
330/// collapses that to ~0.1s with no change to the durability contract. Callers
331/// still `rename` each temp into place and `fsync` the parent directory to make
332/// the renames durable.
333///
334/// The temp files' parent directories must already exist. On the first write
335/// error the partial temp files are left for the caller's rollback/cleanup to
336/// remove (they are uniquely named and never renamed into place).
337pub fn stage_temp_files_durable(files: &[(PathBuf, Vec<u8>)]) -> io::Result<()> {
338    let mut handles: Vec<File> = Vec::with_capacity(files.len());
339    for (temp_path, bytes) in files {
340        let mut file = File::create(temp_path).map_err(|err| enrich_write_error(temp_path, err))?;
341        file.write_all(bytes)
342            .map_err(|err| enrich_write_error(temp_path, err))?;
343        kick_writeback(&file);
344        handles.push(file);
345    }
346    // Barrier pass: by now most files' writeback is already in flight (or done),
347    // so each `sync_all` blocks only on the tail, not a cold synchronous flush.
348    for (file, (temp_path, _)) in handles.iter().zip(files) {
349        sync_file(file, temp_path).map_err(|err| enrich_write_error(temp_path, err))?;
350    }
351    Ok(())
352}
353
354/// fsync the directory inode so a preceding `rename` is durable across
355/// crashes. POSIX-only — on Windows this is a no-op.
356///
357/// On Linux/macOS, after an `fsync(file)` + `rename(tmp, dest)` the
358/// rename itself still needs to be made durable, which requires
359/// `fsync(parent_dir)` (open parent for read, `sync_all`). Without it
360/// a crash between the rename and the next directory writeback can
361/// leave the destination dirent missing even though the file's data is
362/// on disk.
363///
364/// Windows directories don't support this pattern. `CreateFileW` with
365/// `GENERIC_READ` against a directory returns `ERROR_ACCESS_DENIED`
366/// unless the caller passes `FILE_FLAG_BACKUP_SEMANTICS`, and even
367/// then `FlushFileBuffers` on a directory handle is undefined — NTFS
368/// reports access-denied. Directory metadata durability on Windows is
369/// handled by the NTFS log; there is no userspace knob equivalent to
370/// `fsync(dirfd)`, and standard ecosystem crates (`tempfile`,
371/// `atomicwrites`) treat the directory sync as a Unix-only concern.
372///
373/// Returning `Ok(())` on Windows matches that consensus and fixes
374/// heddle#105 (`Repository::init_default` panicking with
375/// `PermissionDenied` on every `write_file_atomic` of an oplog or
376/// state file under a Windows tempdir).
377#[cfg(windows)]
378pub fn sync_directory(_path: &Path) -> io::Result<()> {
379    Ok(())
380}
381
382#[cfg(not(windows))]
383pub fn sync_directory(path: &Path) -> io::Result<()> {
384    if let Some(stats) = deferred_clone_stats(path) {
385        stats.skipped.fetch_add(1, Ordering::Relaxed);
386        return Ok(());
387    }
388    let dir = OpenOptions::new().read(true).open(path)?;
389    dir.sync_all()
390}
391
392/// Sync one file unless it belongs to an active clone durability batch.
393pub fn sync_file(file: &File, path: &Path) -> io::Result<()> {
394    if let Some(stats) = deferred_clone_stats(path) {
395        stats.skipped.fetch_add(1, Ordering::Relaxed);
396        return Ok(());
397    }
398    file.sync_all()
399}
400
401pub fn sync_file_data(file: &File, path: &Path) -> io::Result<()> {
402    if let Some(stats) = deferred_clone_stats(path) {
403        stats.skipped.fetch_add(1, Ordering::Relaxed);
404        return Ok(());
405    }
406    file.sync_data()
407}
408
409#[cfg(any(target_os = "linux", target_os = "android"))]
410fn sync_filesystem(path: &Path) -> io::Result<()> {
411    use std::os::fd::AsRawFd;
412
413    let file = OpenOptions::new().read(true).open(path)?;
414    // SAFETY: `file` owns a live descriptor for the duration of the call.
415    if unsafe { libc::syncfs(file.as_raw_fd()) } == 0 {
416        Ok(())
417    } else {
418        Err(io::Error::last_os_error())
419    }
420}
421
422#[cfg(all(unix, not(any(target_os = "linux", target_os = "android"))))]
423fn sync_filesystem(_path: &Path) -> io::Result<()> {
424    // `syncfs(2)` is Linux-specific. `sync(2)` is the closest portable Unix
425    // whole-filesystem commit primitive and avoids a barrier per clone object.
426    unsafe { libc::sync() };
427    Ok(())
428}
429
430#[cfg(windows)]
431fn sync_filesystem(path: &Path) -> io::Result<()> {
432    // Windows exposes no non-privileged `syncfs` equivalent. Keep one logical
433    // clone commit phase, flushing every clone file only after verification;
434    // directory metadata is covered by NTFS journaling (see `sync_directory`).
435    for entry in fs::read_dir(path)? {
436        let entry = entry?;
437        let file_type = entry.file_type()?;
438        if file_type.is_dir() {
439            sync_filesystem(&entry.path())?;
440        } else if file_type.is_file() {
441            OpenOptions::new()
442                .read(true)
443                .open(entry.path())?
444                .sync_all()?;
445        }
446    }
447    Ok(())
448}
449
450/// Collect missing path components (deepest-first) and the deepest pre-existing
451/// parent that will hold the first new dirent. Used by durable dir creators so
452/// post-create fsync covers every new link without weakening create semantics.
453fn plan_missing_dirs(path: &Path) -> (Vec<PathBuf>, Option<PathBuf>) {
454    // Walk from `path` upward until we hit an existing directory (or run out of
455    // parents). `missing[0]` is the leaf; `missing.last()` is the shallowest new dir.
456    let mut missing: Vec<PathBuf> = Vec::new();
457    {
458        let mut cur = path;
459        loop {
460            match fs::metadata(cur) {
461                Ok(meta) if meta.is_dir() => break,
462                Ok(_) => {
463                    // Exists but is not a directory. Fall through to the create
464                    // call so the error matches the platform/create helper.
465                    break;
466                }
467                Err(e) if e.kind() == io::ErrorKind::NotFound => {
468                    missing.push(cur.to_path_buf());
469                    match cur.parent() {
470                        // `Path::new("a").parent()` is `Some("")` for a single
471                        // relative component — treat empty as cwd (`.`).
472                        Some(parent) if parent.as_os_str().is_empty() => break,
473                        // Root is its own parent (`"/".parent() == Some("/")`).
474                        Some(parent) if parent != cur => cur = parent,
475                        _ => break,
476                    }
477                }
478                // Permission / IO errors walking ancestors: let the create
479                // helper surface a consistent failure for the full path.
480                Err(_) => break,
481            }
482        }
483    }
484
485    let deepest_existing = missing
486        .last()
487        .and_then(|shallowest| match shallowest.parent() {
488            Some(parent) if parent.as_os_str().is_empty() => Some(PathBuf::from(".")),
489            Some(parent) => Some(parent.to_path_buf()),
490            None => None,
491        });
492
493    (missing, deepest_existing)
494}
495
496/// Fsync newly created directories deepest-first, then the deepest pre-existing
497/// parent so each new child dirent is durable. No-op when nothing was created.
498fn sync_new_dirents(missing: &[PathBuf], deepest_existing: Option<&Path>) -> io::Result<()> {
499    if missing.is_empty() {
500        return Ok(());
501    }
502    for dir in missing {
503        sync_directory(dir)?;
504    }
505    // Fsync the deepest pre-existing parent so the first new child dirent
506    // (the grandparent→shard link in the classic `blobs/ab/` case) is durable.
507    if let Some(existing) = deepest_existing {
508        sync_directory(existing)?;
509    }
510    Ok(())
511}
512
513/// Create a directory and any missing ancestors, making new dirents crash-durable.
514///
515/// Bare [`fs::create_dir_all`] only ensures the directories exist in the live
516/// filesystem view. After the first write into a newly created shard
517/// (e.g. `blobs/ab/…`), [`write_file_atomic`] fsyncs the shard directory so
518/// the *file* dirent is durable — but the grandparent that holds the new
519/// shard dirent may never be fsynced. A crash can then drop the entire new
520/// shard tree despite per-file durability (GAP_MAP L6).
521///
522/// This helper:
523/// 1. creates missing ancestor directories (same end state as `create_dir_all`);
524/// 2. fsyncs each newly created directory, deepest-first;
525/// 3. fsyncs the deepest pre-existing parent so the new child dirent is durable.
526///
527/// On Windows, directory fsync is a no-op (see [`sync_directory`]); creation
528/// still proceeds. Cost is once per new path segment (typically once per
529/// object-store shard).
530pub fn create_dir_all_durable(path: &Path) -> io::Result<()> {
531    let (missing, deepest_existing) = plan_missing_dirs(path);
532    fs::create_dir_all(path)?;
533    sync_new_dirents(&missing, deepest_existing.as_deref())
534}
535
536/// Wrap an `io::Error` raised while writing `path` so that ENOSPC carries
537/// an actionable message naming the path. Non-ENOSPC errors pass through
538/// unchanged. The wrapped error's `raw_os_error()` still returns 28, and
539/// [`is_out_of_space`] still detects it — callers (e.g. `cmd_snapshot`)
540/// rely on this for stable exit-code mapping.
541///
542/// Thin wrapper over [`enrich_fs_error`] for the historical "writing"
543/// call sites. New code should prefer `enrich_fs_error(path, "writing", err)`
544/// directly so the operation name is explicit at the call site.
545fn enrich_write_error(path: &Path, err: io::Error) -> io::Error {
546    enrich_fs_error(path, "writing", err)
547}
548
549/// Wrap an `io::Error` produced by a filesystem operation against `path`
550/// with a heddle-context message naming both the operation and the path.
551///
552/// The mapping covers the cases users actually hit and the messages we
553/// promise from heddle's CLI surface:
554/// - **ENOTEMPTY** — usually `remove_dir` against a directory that still
555///   holds untracked or explicitly ignored content, such as build output.
556///   The high-level fix is to leave the directory in place, but when the
557///   error does surface (e.g. a path the planner *did* expect to remove),
558///   the message names the path so the user can investigate.
559/// - **EACCES** — naming the path and the action ("removing", "writing",
560///   "renaming") is enough for the user to inspect mode bits.
561/// - **ENOENT** — caller-driven: only enriched when the operation
562///   expected the path to exist (so optional reads like a missing index
563///   pass through unchanged via the `is_not_found` predicate).
564/// - **EROFS** — points the user at the filesystem mount, not at heddle.
565/// - **EXDEV** — points the user at the temp path / mount mismatch.
566/// - **ENOSPC** — same actionable disk-full message the snapshot path
567///   already relies on.
568///
569/// `op` is a verb in the present-progressive ("writing", "removing",
570/// "renaming", "creating") so the resulting message reads naturally:
571///   `"could not remove `<path>` because it contains content..."`.
572///
573/// The wrapped error preserves `raw_os_error()` (callers still classify
574/// disk-full via [`is_out_of_space`]) and exposes the original `io::Error`
575/// through the `Error::source` chain (so `RUST_BACKTRACE=1` and
576/// `anyhow`'s chain printer still surface the OS error).
577pub fn enrich_fs_error(path: &Path, op: &'static str, err: io::Error) -> io::Error {
578    if is_out_of_space(&err) {
579        let msg = format!(
580            "out of disk space {op} {}: free disk space and re-run the command — your working tree is unchanged",
581            path.display()
582        );
583        return io::Error::new(
584            io::ErrorKind::StorageFull,
585            EnrichedFsError { msg, source: err },
586        );
587    }
588    if is_directory_not_empty(&err) {
589        let msg = format!(
590            "could not remove directory `{}` because it contains content (heddle-ignored or otherwise) — leaving in place",
591            path.display()
592        );
593        return io::Error::new(
594            io::ErrorKind::DirectoryNotEmpty,
595            EnrichedFsError { msg, source: err },
596        );
597    }
598    if is_read_only_filesystem(&err) {
599        let msg = format!(
600            "filesystem is read-only — `{}` cannot be modified",
601            path.display()
602        );
603        return io::Error::new(
604            io::ErrorKind::ReadOnlyFilesystem,
605            EnrichedFsError { msg, source: err },
606        );
607    }
608    if is_permission_denied(&err) {
609        let msg = format!(
610            "permission denied {op} `{}` — check filesystem permissions",
611            path.display()
612        );
613        return io::Error::new(
614            io::ErrorKind::PermissionDenied,
615            EnrichedFsError { msg, source: err },
616        );
617    }
618    if is_not_found(&err) {
619        let msg = format!("could not find `{}` for {op}", path.display());
620        return io::Error::new(
621            io::ErrorKind::NotFound,
622            EnrichedFsError { msg, source: err },
623        );
624    }
625    if is_cross_device_link(&err) {
626        let msg = format!(
627            "cannot rename across filesystems — temp file for `{}` lives on a different mount; set TMPDIR to the same filesystem as the destination",
628            path.display()
629        );
630        return io::Error::new(
631            io::ErrorKind::CrossesDevices,
632            EnrichedFsError { msg, source: err },
633        );
634    }
635    err
636}
637
638/// Wrap an `EXDEV` error from `fs::rename` with both the source temp path
639/// and the destination — the user needs both to understand which mount
640/// boundary the rename tripped on. Other error kinds delegate to
641/// [`enrich_fs_error`] using the destination as the principal path.
642pub fn enrich_rename_error(src: &Path, dst: &Path, err: io::Error) -> io::Error {
643    if is_cross_device_link(&err) {
644        let msg = format!(
645            "cannot rename across filesystems — temp file at `{}` cannot be renamed to `{}`; set TMPDIR to the same filesystem as the destination",
646            src.display(),
647            dst.display()
648        );
649        return io::Error::new(
650            io::ErrorKind::CrossesDevices,
651            EnrichedFsError { msg, source: err },
652        );
653    }
654    enrich_fs_error(dst, "renaming", err)
655}
656
657#[derive(Debug)]
658struct EnrichedFsError {
659    msg: String,
660    source: io::Error,
661}
662
663impl std::fmt::Display for EnrichedFsError {
664    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
665        f.write_str(&self.msg)
666    }
667}
668
669impl std::error::Error for EnrichedFsError {
670    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
671        Some(&self.source)
672    }
673}
674
675pub struct StagedAtomicWrite {
676    path: PathBuf,
677    parent: PathBuf,
678    tmp: PathBuf,
679    pending: bool,
680}
681
682impl StagedAtomicWrite {
683    pub fn publish(mut self) -> io::Result<()> {
684        fs::rename(&self.tmp, &self.path)
685            .map_err(|error| enrich_rename_error(&self.tmp, &self.path, error))?;
686        self.pending = false;
687        sync_directory(&self.parent)
688            .map_err(|error| enrich_fs_error(&self.parent, "syncing", error))
689    }
690}
691
692impl Drop for StagedAtomicWrite {
693    fn drop(&mut self) {
694        if self.pending {
695            let _ = fs::remove_file(&self.tmp);
696        }
697    }
698}
699
700fn stage_file_atomic_impl(
701    path: &Path,
702    bytes: &[u8],
703    kind: AtomicWriteKind,
704    before_write: impl FnOnce(&File, &Path) -> io::Result<()>,
705) -> io::Result<StagedAtomicWrite> {
706    let parent = path.parent().unwrap_or_else(|| Path::new("."));
707    create_dir_all_durable(parent).map_err(|e| enrich_fs_error(parent, "creating", e))?;
708
709    let tmp = temp_path(path);
710    let inner = (|| -> io::Result<()> {
711        let mut file = kind.open_tmp(&tmp)?;
712        kind.enforce_before_write(&file)?;
713        before_write(&file, &tmp)?;
714        file.write_all(bytes)?;
715        sync_file(&file, &tmp)?;
716        Ok(())
717    })();
718
719    if let Err(err) = inner {
720        // Best-effort cleanup. On ENOSPC the tempfile may itself be the
721        // cause of the disk pressure; removing it gives the user back
722        // some slack before they re-run.
723        let _ = fs::remove_file(&tmp);
724        return Err(enrich_write_error(path, err));
725    }
726
727    Ok(StagedAtomicWrite {
728        path: path.to_path_buf(),
729        parent: parent.to_path_buf(),
730        tmp,
731        pending: true,
732    })
733}
734
735fn write_file_atomic_impl(
736    path: &Path,
737    bytes: &[u8],
738    kind: AtomicWriteKind,
739    before_write: impl FnOnce(&File, &Path) -> io::Result<()>,
740) -> io::Result<()> {
741    stage_file_atomic_impl(path, bytes, kind, before_write)?.publish()
742}
743
744pub fn write_file_atomic(path: &Path, bytes: &[u8]) -> io::Result<()> {
745    write_file_atomic_impl(path, bytes, AtomicWriteKind::Normal, |_, _| Ok(()))
746}
747
748/// Atomically publish reconstructible bytes without forcing them to stable
749/// storage. The final path is never observed partially written, but a crash may
750/// lose this write. Callers must have an independent durable source from which
751/// the file can be rebuilt.
752pub fn write_file_atomic_reconstructible(path: &Path, bytes: &[u8]) -> io::Result<()> {
753    let parent = path.parent().unwrap_or_else(|| Path::new("."));
754    fs::create_dir_all(parent).map_err(|error| enrich_fs_error(parent, "creating", error))?;
755    let tmp = temp_path(path);
756    let result = (|| -> io::Result<()> {
757        let mut file = AtomicWriteKind::Normal.open_tmp(&tmp)?;
758        file.write_all(bytes)?;
759        file.flush()?;
760        drop(file);
761        fs::rename(&tmp, path).map_err(|error| enrich_rename_error(&tmp, path, error))
762    })();
763    if let Err(error) = result {
764        let _ = fs::remove_file(&tmp);
765        return Err(enrich_write_error(path, error));
766    }
767    Ok(())
768}
769
770/// Create a directory tree with owner-only permissions on Unix (`0o700`),
771/// making newly created dirents crash-durable (same fsync chain as
772/// [`create_dir_all_durable`]).
773///
774/// Used for `.heddle` / `~/.heddle` trees that hold credentials, keys, and
775/// repository secrets. On Unix, missing ancestors are created with mode
776/// `0o700` and then fsynced deepest-first, plus the deepest pre-existing
777/// parent. On non-Unix platforms this falls back to durable
778/// [`create_dir_all_durable`] (no portable POSIX mode API). Existing
779/// directories are left as-is (creation-time privacy; callers that need to
780/// tighten existing modes should do so explicitly).
781pub fn create_private_dir_all(path: &Path) -> io::Result<()> {
782    #[cfg(unix)]
783    {
784        use std::os::unix::fs::DirBuilderExt;
785        let (missing, deepest_existing) = plan_missing_dirs(path);
786        let mut builder = fs::DirBuilder::new();
787        builder.recursive(true).mode(0o700);
788        match builder.create(path) {
789            Ok(()) => {}
790            Err(e) if e.kind() == io::ErrorKind::AlreadyExists => {}
791            Err(e) => return Err(e),
792        }
793        sync_new_dirents(&missing, deepest_existing.as_deref())
794    }
795    #[cfg(not(unix))]
796    {
797        // No portable POSIX mode API — same durable create chain as public dirs.
798        create_dir_all_durable(path)
799    }
800}
801
802/// Atomically write secret material without ever creating a group/world
803/// readable temporary file.
804///
805/// On Unix the temp inode is created with `OpenOptions::mode(0o600)` before
806/// any bytes are written, then the open file descriptor is enforced to exact
807/// `0600` before the payload is written. Permission failures are hard errors
808/// and the temp file is removed best-effort. On non-Unix platforms there is no
809/// portable POSIX mode API, so this uses the normal create-new temp file,
810/// fsync, and rename sequence.
811pub fn write_file_atomic_secret(path: &Path, bytes: &[u8]) -> io::Result<()> {
812    write_file_atomic_impl(path, bytes, AtomicWriteKind::Secret, |_, _| Ok(()))
813}
814
815pub fn stage_file_atomic_secret(path: &Path, bytes: &[u8]) -> io::Result<StagedAtomicWrite> {
816    stage_file_atomic_impl(path, bytes, AtomicWriteKind::Secret, |_, _| Ok(()))
817}
818
819/// Publish an existing on-disk file at `src` to `dst` with the same
820/// crash-consistency contract as [`write_file_atomic`]:
821///
822/// 1. `fsync` the source so its data blocks are stable before any directory
823///    entry is updated (`rename` moves a dirent; it does not re-write bytes).
824/// 2. `rename(src, dst)` when both paths share a filesystem — atomic dirent
825///    publish.
826/// 3. On `EXDEV`, stream-copy into a *same-directory* temp, `fsync` the temp,
827///    `rename` over `dst`, then remove `src`. Never write the final path in
828///    place: a crash mid-copy must not leave a torn content-addressed object
829///    under its final name.
830/// 4. `fsync` the destination parent so the new dirent is durable.
831///
832/// If `dst` already exists and rename reports `AlreadyExists` (Windows;
833/// POSIX `rename` replaces files), the source is removed and `Ok(())` is
834/// returned — content-addressed install idempotency.
835///
836/// Non-`EXDEV` rename failures propagate. Callers must not silently fall
837/// through to a raw in-place copy on unrelated errors (the previous
838/// streaming-pack install path did exactly that).
839/// Fsync an existing regular file's data blocks.
840///
841/// On Windows, `FlushFileBuffers` requires write access — a read-only
842/// `File::open` + `sync_all` returns `ERROR_ACCESS_DENIED` (code 5). Open
843/// with write so pack install / L8 journal publish works under Windows
844/// tempdirs (projfs smoke fixtures).
845fn fsync_file_data(path: &Path) -> io::Result<()> {
846    let file = OpenOptions::new()
847        .read(true)
848        .write(true)
849        .open(path)
850        .map_err(|e| enrich_fs_error(path, "opening", e))?;
851    sync_file(&file, path).map_err(|e| enrich_fs_error(path, "syncing", e))
852}
853
854pub fn publish_file_durable(src: &Path, dst: &Path) -> io::Result<()> {
855    let parent = dst.parent().unwrap_or_else(|| Path::new("."));
856    create_dir_all_durable(parent).map_err(|e| enrich_fs_error(parent, "creating", e))?;
857
858    // Data-block durability before publishing the dirent. Required even on
859    // the same-filesystem rename path: StreamingPackBuilder (and similar
860    // staged writers) only `flush` buffered writers; without this fsync a
861    // crash after rename can lose the published object.
862    fsync_file_data(src)?;
863
864    match fs::rename(src, dst) {
865        Ok(()) => {}
866        Err(e) if e.kind() == io::ErrorKind::AlreadyExists => {
867            // Content-addressed install: destination already present.
868            let _ = fs::remove_file(src);
869        }
870        Err(e) if is_cross_device_link(&e) => {
871            publish_file_via_copy_durable(src, dst)?;
872        }
873        Err(e) => return Err(enrich_rename_error(src, dst, e)),
874    }
875
876    sync_directory(parent).map_err(|e| enrich_fs_error(parent, "syncing", e))
877}
878
879/// Cross-device publish path: copy to a same-dir temp, fsync, rename over
880/// `dst`. Exposed to unit tests so the no-torn-final-path contract is
881/// exercised without needing a real multi-mount layout.
882fn publish_file_via_copy_durable(src: &Path, dst: &Path) -> io::Result<()> {
883    let parent = dst.parent().unwrap_or_else(|| Path::new("."));
884    create_dir_all_durable(parent).map_err(|e| enrich_fs_error(parent, "creating", e))?;
885
886    let tmp = temp_path(dst);
887    let result = (|| -> io::Result<()> {
888        fs::copy(src, &tmp).map_err(|e| enrich_fs_error(&tmp, "writing", e))?;
889        fsync_file_data(&tmp)?;
890        fs::rename(&tmp, dst).map_err(|e| enrich_rename_error(&tmp, dst, e))?;
891        let _ = fs::remove_file(src);
892        Ok(())
893    })();
894    if result.is_err() {
895        let _ = fs::remove_file(&tmp);
896    }
897    result
898}
899
900#[cfg(test)]
901mod tests {
902    use super::*;
903
904    fn enospc_io_error() -> io::Error {
905        io::Error::from_raw_os_error(ENOSPC)
906    }
907
908    #[test]
909    fn is_out_of_space_detects_enospc_raw() {
910        assert!(is_out_of_space(&enospc_io_error()));
911    }
912
913    #[test]
914    fn is_out_of_space_detects_storage_full_kind() {
915        let err = io::Error::new(io::ErrorKind::StorageFull, "mock disk full");
916        assert!(is_out_of_space(&err));
917    }
918
919    #[test]
920    fn is_out_of_space_detects_write_zero() {
921        let err = io::Error::new(io::ErrorKind::WriteZero, "short write");
922        assert!(is_out_of_space(&err));
923    }
924
925    #[test]
926    fn is_out_of_space_rejects_unrelated_errors() {
927        assert!(!is_out_of_space(&io::Error::new(
928            io::ErrorKind::NotFound,
929            "missing"
930        )));
931        assert!(!is_out_of_space(&io::Error::new(
932            io::ErrorKind::PermissionDenied,
933            "nope"
934        )));
935        assert!(!is_out_of_space(&io::Error::other("generic")));
936    }
937
938    #[test]
939    fn is_directory_not_empty_detects_kind() {
940        let err = io::Error::new(io::ErrorKind::DirectoryNotEmpty, "still has children");
941        assert!(is_directory_not_empty(&err));
942    }
943
944    #[test]
945    fn is_directory_not_empty_detects_raw_codes() {
946        for code in [ENOTEMPTY_LINUX, ENOTEMPTY_MACOS, ENOTEMPTY_WINDOWS] {
947            assert!(
948                is_directory_not_empty(&io::Error::from_raw_os_error(code)),
949                "expected raw OS error {code} to classify as ENOTEMPTY"
950            );
951        }
952    }
953
954    #[test]
955    fn is_directory_not_empty_rejects_unrelated() {
956        assert!(!is_directory_not_empty(&io::Error::new(
957            io::ErrorKind::NotFound,
958            "missing"
959        )));
960        assert!(!is_directory_not_empty(&enospc_io_error()));
961    }
962
963    #[test]
964    fn is_permission_denied_detects_kind_and_raw() {
965        assert!(is_permission_denied(&io::Error::new(
966            io::ErrorKind::PermissionDenied,
967            "nope"
968        )));
969        assert!(is_permission_denied(&io::Error::from_raw_os_error(EACCES)));
970    }
971
972    #[test]
973    fn is_not_found_detects_kind_and_raw() {
974        assert!(is_not_found(&io::Error::new(
975            io::ErrorKind::NotFound,
976            "missing"
977        )));
978        assert!(is_not_found(&io::Error::from_raw_os_error(ENOENT)));
979    }
980
981    #[test]
982    fn is_read_only_filesystem_detects_raw() {
983        assert!(is_read_only_filesystem(&io::Error::from_raw_os_error(
984            EROFS
985        )));
986    }
987
988    #[test]
989    fn is_cross_device_link_detects_raw() {
990        assert!(is_cross_device_link(&io::Error::from_raw_os_error(EXDEV)));
991    }
992
993    #[test]
994    fn enrich_fs_error_passes_through_unclassified() {
995        let path = Path::new("/tmp/example");
996        let original = io::Error::other("weird");
997        let wrapped = enrich_fs_error(path, "writing", original);
998        // Unclassified errors are returned untouched.
999        assert_eq!(wrapped.kind(), io::ErrorKind::Other);
1000        assert_eq!(wrapped.to_string(), "weird");
1001    }
1002
1003    #[test]
1004    fn enrich_fs_error_wraps_enospc_with_path_and_recovery_hint() {
1005        let path = Path::new("/repo/.heddle/state/abc.bin");
1006        let wrapped = enrich_fs_error(path, "writing", enospc_io_error());
1007
1008        // Stable kind so the CLI exit-code mapper finds it.
1009        assert_eq!(wrapped.kind(), io::ErrorKind::StorageFull);
1010        // Message names the failure, the path, and the recovery.
1011        let msg = wrapped.to_string();
1012        assert!(
1013            msg.contains("out of disk space"),
1014            "missing failure name: {msg}"
1015        );
1016        assert!(
1017            msg.contains("/repo/.heddle/state/abc.bin"),
1018            "missing path: {msg}"
1019        );
1020        assert!(
1021            msg.contains("free disk space") && msg.contains("re-run"),
1022            "missing recovery hint: {msg}"
1023        );
1024        assert!(
1025            msg.contains("working tree is unchanged"),
1026            "missing reassurance: {msg}"
1027        );
1028        // Source chain preserved so callers that walk `source()` (e.g.
1029        // anyhow's chain printer) can still see the original ENOSPC.
1030        let src = std::error::Error::source(&wrapped as &dyn std::error::Error)
1031            .or_else(|| wrapped.get_ref().and_then(|e| e.source()))
1032            .expect("source preserved");
1033        assert!(src.to_string().to_lowercase().contains("space"));
1034    }
1035
1036    #[test]
1037    fn enrich_fs_error_wraps_enotempty_with_directory_message() {
1038        let path = Path::new("/repo/web");
1039        let wrapped = enrich_fs_error(
1040            path,
1041            "removing",
1042            io::Error::from_raw_os_error(ENOTEMPTY_MACOS),
1043        );
1044        assert_eq!(wrapped.kind(), io::ErrorKind::DirectoryNotEmpty);
1045        let msg = wrapped.to_string();
1046        assert!(
1047            msg.contains("could not remove directory"),
1048            "missing action: {msg}"
1049        );
1050        assert!(msg.contains("/repo/web"), "missing path: {msg}");
1051        assert!(
1052            msg.contains("heddle-ignored"),
1053            "missing heddle-ignored hint: {msg}"
1054        );
1055        assert!(
1056            msg.contains("leaving in place"),
1057            "missing reassurance: {msg}"
1058        );
1059        // raw_os_error() does NOT round-trip — `io::Error::new(kind, source)`
1060        // synthesizes a new error whose `raw_os_error()` is None — but the
1061        // source chain still exposes the original OS code for callers that
1062        // walk it.
1063        let src = wrapped.get_ref().and_then(|e| e.source()).expect("source");
1064        let original = src
1065            .downcast_ref::<io::Error>()
1066            .expect("original io::Error preserved");
1067        assert_eq!(original.raw_os_error(), Some(ENOTEMPTY_MACOS));
1068    }
1069
1070    #[test]
1071    fn enrich_fs_error_wraps_eacces_with_op_and_path() {
1072        let path = Path::new("/repo/.heddle/state/index.bin");
1073        let wrapped = enrich_fs_error(path, "writing", io::Error::from_raw_os_error(EACCES));
1074        assert_eq!(wrapped.kind(), io::ErrorKind::PermissionDenied);
1075        let msg = wrapped.to_string();
1076        assert!(msg.starts_with("permission denied writing"), "msg: {msg}");
1077        assert!(msg.contains("/repo/.heddle/state/index.bin"), "msg: {msg}");
1078        assert!(msg.contains("check filesystem permissions"), "msg: {msg}");
1079    }
1080
1081    #[test]
1082    fn enrich_fs_error_wraps_enoent_with_op_and_path() {
1083        let path = Path::new("/repo/.heddle");
1084        let wrapped = enrich_fs_error(path, "opening", io::Error::from_raw_os_error(ENOENT));
1085        assert_eq!(wrapped.kind(), io::ErrorKind::NotFound);
1086        let msg = wrapped.to_string();
1087        assert!(msg.contains("could not find"), "missing action: {msg}");
1088        assert!(msg.contains("/repo/.heddle"), "missing path: {msg}");
1089        assert!(msg.contains("for opening"), "missing op: {msg}");
1090    }
1091
1092    #[test]
1093    fn enrich_fs_error_wraps_erofs_with_path() {
1094        let path = Path::new("/mnt/readonly/.heddle/state/index.bin");
1095        let wrapped = enrich_fs_error(path, "writing", io::Error::from_raw_os_error(EROFS));
1096        assert_eq!(wrapped.kind(), io::ErrorKind::ReadOnlyFilesystem);
1097        let msg = wrapped.to_string();
1098        assert!(msg.contains("filesystem is read-only"), "msg: {msg}");
1099        assert!(
1100            msg.contains("/mnt/readonly/.heddle/state/index.bin"),
1101            "msg: {msg}"
1102        );
1103        assert!(msg.contains("cannot be modified"), "msg: {msg}");
1104    }
1105
1106    #[test]
1107    fn enrich_rename_error_wraps_exdev_with_src_and_dst() {
1108        let src = Path::new("/tmp-mount/.x.tmp-1234");
1109        let dst = Path::new("/repo/.heddle/state/index.bin");
1110        let wrapped = enrich_rename_error(src, dst, io::Error::from_raw_os_error(EXDEV));
1111        assert_eq!(wrapped.kind(), io::ErrorKind::CrossesDevices);
1112        let msg = wrapped.to_string();
1113        assert!(
1114            msg.contains("cannot rename across filesystems"),
1115            "msg: {msg}"
1116        );
1117        assert!(msg.contains("/tmp-mount/.x.tmp-1234"), "missing src: {msg}");
1118        assert!(
1119            msg.contains("/repo/.heddle/state/index.bin"),
1120            "missing dst: {msg}"
1121        );
1122        assert!(msg.contains("TMPDIR"), "missing recovery hint: {msg}");
1123    }
1124
1125    #[test]
1126    fn enrich_rename_error_falls_through_to_generic_for_other_kinds() {
1127        let src = Path::new("/tmp/.x.tmp");
1128        let dst = Path::new("/repo/file");
1129        let wrapped = enrich_rename_error(src, dst, io::Error::from_raw_os_error(EACCES));
1130        // Non-EXDEV rename failures get the generic `enrich_fs_error`
1131        // treatment, which preserves the dst path and the "renaming" op.
1132        assert_eq!(wrapped.kind(), io::ErrorKind::PermissionDenied);
1133        let msg = wrapped.to_string();
1134        assert!(msg.starts_with("permission denied renaming"), "msg: {msg}");
1135        assert!(msg.contains("/repo/file"), "missing dst: {msg}");
1136    }
1137
1138    #[test]
1139    fn enrich_write_error_passes_through_non_enospc_unclassified() {
1140        // The historical helper now delegates to `enrich_fs_error`, so a
1141        // generic Other error still passes through unchanged.
1142        let path = Path::new("/tmp/example");
1143        let original = io::Error::other("weird");
1144        let wrapped = enrich_write_error(path, original);
1145        assert_eq!(wrapped.kind(), io::ErrorKind::Other);
1146        assert_eq!(wrapped.to_string(), "weird");
1147    }
1148
1149    #[test]
1150    fn write_file_atomic_round_trip() {
1151        let dir = tempfile::TempDir::new().unwrap();
1152        let target = dir.path().join("nested/under/here/file.bin");
1153        write_file_atomic(&target, b"hello").unwrap();
1154        assert_eq!(fs::read(&target).unwrap(), b"hello");
1155    }
1156
1157    #[test]
1158    fn stage_temp_files_durable_writes_every_file_verbatim() {
1159        // The bulk-ref hot path stages N temp files in one overlapped-writeback
1160        // pass. Every file must land with its exact bytes — the batching is a
1161        // durability/perf optimization, never a content one.
1162        let dir = tempfile::TempDir::new().unwrap();
1163        let files: Vec<(PathBuf, Vec<u8>)> = (0..50)
1164            .map(|i| {
1165                (
1166                    dir.path().join(format!("ref-{i}.tmp")),
1167                    format!("change-id-{i}\n").into_bytes(),
1168                )
1169            })
1170            .collect();
1171
1172        stage_temp_files_durable(&files).unwrap();
1173
1174        for (path, bytes) in &files {
1175            assert_eq!(&fs::read(path).unwrap(), bytes, "mismatch at {path:?}");
1176        }
1177    }
1178
1179    #[test]
1180    fn stage_temp_files_durable_empty_batch_is_ok() {
1181        // A publish with no new-content plans (e.g. a pure delete batch) hands
1182        // an empty slice; it must be a clean no-op, not an error.
1183        stage_temp_files_durable(&[]).unwrap();
1184    }
1185
1186    #[test]
1187    fn stage_temp_files_durable_errors_when_parent_missing() {
1188        // The helper does NOT create parent directories (callers pre-create
1189        // them via `alloc_temp_path`); a missing parent surfaces as an error
1190        // rather than silently dropping the write.
1191        let dir = tempfile::TempDir::new().unwrap();
1192        let files = vec![(dir.path().join("does/not/exist/ref.tmp"), b"x".to_vec())];
1193        assert!(stage_temp_files_durable(&files).is_err());
1194    }
1195
1196    #[cfg(unix)]
1197    #[test]
1198    fn create_private_dir_all_sets_0700() {
1199        use std::os::unix::fs::PermissionsExt;
1200
1201        let dir = tempfile::TempDir::new().unwrap();
1202        let target = dir.path().join("nested/private");
1203        create_private_dir_all(&target).expect("create private dir");
1204        let mode = fs::metadata(&target).unwrap().permissions().mode() & 0o777;
1205        assert_eq!(mode, 0o700, "new private dir must be 0700, got {mode:o}");
1206        // Intermediate ancestors created by the recursive private create must
1207        // also be owner-only (DirBuilder mode applies to each new segment).
1208        let mid_mode = fs::metadata(dir.path().join("nested"))
1209            .unwrap()
1210            .permissions()
1211            .mode()
1212            & 0o777;
1213        assert_eq!(
1214            mid_mode, 0o700,
1215            "intermediate private ancestor must be 0700"
1216        );
1217        // Idempotent after durable create: re-run is success and modes stick.
1218        create_private_dir_all(&target).expect("idempotent private create");
1219        let mode_again = fs::metadata(&target).unwrap().permissions().mode() & 0o777;
1220        assert_eq!(mode_again, 0o700);
1221    }
1222
1223    #[cfg(unix)]
1224    #[test]
1225    fn write_file_atomic_secret_is_0600_before_write_and_after_rename() {
1226        use std::os::unix::fs::PermissionsExt;
1227
1228        let dir = tempfile::TempDir::new().unwrap();
1229        let target = dir.path().join("nested/secret.txt");
1230        let mut observed_tmp_mode = None;
1231
1232        write_file_atomic_impl(&target, b"secret", AtomicWriteKind::Secret, |file, tmp| {
1233            let fd_mode = file.metadata()?.permissions().mode() & 0o777;
1234            let path_mode = fs::metadata(tmp)?.permissions().mode() & 0o777;
1235            observed_tmp_mode = Some((fd_mode, path_mode));
1236            Ok(())
1237        })
1238        .unwrap();
1239
1240        assert_eq!(observed_tmp_mode, Some((0o600, 0o600)));
1241        let final_mode = fs::metadata(&target).unwrap().permissions().mode() & 0o777;
1242        assert_eq!(final_mode, 0o600);
1243        assert_eq!(fs::read(&target).unwrap(), b"secret");
1244    }
1245
1246    #[test]
1247    fn write_file_atomic_secret_cleans_up_when_pre_write_check_fails() {
1248        let dir = tempfile::TempDir::new().unwrap();
1249        let target = dir.path().join("secret.txt");
1250        let mut tmp_path = None;
1251
1252        let err = write_file_atomic_impl(&target, b"secret", AtomicWriteKind::Secret, |_, tmp| {
1253            tmp_path = Some(tmp.to_path_buf());
1254            Err(io::Error::new(
1255                io::ErrorKind::PermissionDenied,
1256                "injected permission failure",
1257            ))
1258        })
1259        .expect_err("permission failure should propagate");
1260
1261        assert!(is_permission_denied(&err), "unexpected error: {err}");
1262        assert!(!target.exists(), "secret write must not publish target");
1263        let tmp = tmp_path.expect("pre-write hook observed temp path");
1264        assert!(!tmp.exists(), "failed secret write should remove temp file");
1265    }
1266
1267    #[test]
1268    fn staged_secret_is_unpublished_until_publish() {
1269        let dir = tempfile::TempDir::new().unwrap();
1270        let target = dir.path().join("secret.txt");
1271        let staged = stage_file_atomic_secret(&target, b"secret").unwrap();
1272
1273        assert!(!target.exists());
1274        staged.publish().unwrap();
1275        assert_eq!(fs::read(target).unwrap(), b"secret");
1276    }
1277
1278    #[test]
1279    fn dropping_staged_secret_removes_temporary_file() {
1280        let dir = tempfile::TempDir::new().unwrap();
1281        let target = dir.path().join("secret.txt");
1282        let staged = stage_file_atomic_secret(&target, b"secret").unwrap();
1283        drop(staged);
1284
1285        assert!(!target.exists());
1286        assert_eq!(fs::read_dir(dir.path()).unwrap().count(), 0);
1287    }
1288
1289    /// Regression for heddle#105: `sync_directory` must succeed on any
1290    /// writable directory. The original implementation called
1291    /// `OpenOptions::new().read(true).open(dir)` + `sync_all()`, which
1292    /// fails on Windows with `ERROR_ACCESS_DENIED` (5) because Windows
1293    /// directory handles require `FILE_FLAG_BACKUP_SEMANTICS` and
1294    /// `FlushFileBuffers` on a directory handle is not a supported
1295    /// operation. The failure cascaded through `write_file_atomic` into
1296    /// `Repository::init_default`, breaking `heddle init` on Windows.
1297    #[test]
1298    fn sync_directory_succeeds_on_writable_tempdir() {
1299        let dir = tempfile::TempDir::new().unwrap();
1300        sync_directory(dir.path()).expect("sync_directory on writable tempdir");
1301    }
1302
1303    /// Regression for heddle#105: full `write_file_atomic` round-trip
1304    /// against a freshly-created nested directory must not surface
1305    /// `PermissionDenied`. The previous failure mode was the
1306    /// `sync_directory(parent)` call at the end of `write_file_atomic`.
1307    #[test]
1308    fn write_file_atomic_does_not_permission_deny_on_parent_sync() {
1309        let dir = tempfile::TempDir::new().unwrap();
1310        let target = dir.path().join("oplog/oplog.bin");
1311        let result = write_file_atomic(&target, b"hello");
1312        if let Err(e) = &result {
1313            assert!(
1314                !is_permission_denied(e),
1315                "write_file_atomic surfaced PermissionDenied on a writable \
1316                 tempdir (heddle#105): {e}"
1317            );
1318        }
1319        result.expect("write_file_atomic");
1320    }
1321
1322    #[test]
1323    fn publish_file_durable_renames_and_removes_source() {
1324        let dir = tempfile::TempDir::new().unwrap();
1325        let src = dir.path().join("staged.pack");
1326        let dst = dir.path().join("objects/packs/final.pack");
1327        fs::write(&src, b"pack-bytes").unwrap();
1328
1329        publish_file_durable(&src, &dst).unwrap();
1330
1331        assert!(!src.exists(), "source must be consumed by publish");
1332        assert_eq!(fs::read(&dst).unwrap(), b"pack-bytes");
1333    }
1334
1335    /// Windows: FlushFileBuffers needs write access; read-only open +
1336    /// sync_all fails with ERROR_ACCESS_DENIED and broke L8 pack install /
1337    /// projfs fixture setup under tempdirs.
1338    #[test]
1339    fn publish_file_durable_syncs_source_without_permission_deny() {
1340        let dir = tempfile::TempDir::new().unwrap();
1341        let src = dir.path().join("staged.bin");
1342        let dst = dir.path().join("final.bin");
1343        fs::write(&src, b"need-fsync-before-rename").unwrap();
1344        let result = publish_file_durable(&src, &dst);
1345        if let Err(e) = &result {
1346            assert!(
1347                !is_permission_denied(e),
1348                "publish_file_durable PermissionDenied on source fsync: {e}"
1349            );
1350        }
1351        result.expect("publish_file_durable");
1352        assert_eq!(fs::read(&dst).unwrap(), b"need-fsync-before-rename");
1353    }
1354
1355    #[test]
1356    fn publish_file_via_copy_durable_never_writes_final_path_directly() {
1357        // Regression for streaming pack install: the EXDEV fallback used
1358        // `fs::copy(src, dst)` straight into the content-addressed final
1359        // path. A crash mid-copy left a torn pack under its BLAKE3 name
1360        // (readers treat that name as authoritative). The durable path
1361        // must land bytes at a temp sibling first, then rename.
1362        let dir = tempfile::TempDir::new().unwrap();
1363        let src = dir.path().join("staged.pack");
1364        let dst = dir.path().join("final.pack");
1365        // Pre-existing destination simulates a previous torn install that
1366        // a naive in-place copy would non-atomically overwrite.
1367        fs::write(&dst, b"TORN-OLD-CONTENT!!!!!!!!!!!!!").unwrap();
1368        fs::write(&src, b"complete-new-pack-bytes").unwrap();
1369
1370        publish_file_via_copy_durable(&src, &dst).unwrap();
1371
1372        assert!(!src.exists(), "source must be removed after copy publish");
1373        assert_eq!(fs::read(&dst).unwrap(), b"complete-new-pack-bytes");
1374        // No leftover temps in the destination directory.
1375        let leftovers: Vec<_> = fs::read_dir(dir.path())
1376            .unwrap()
1377            .filter_map(|e| e.ok())
1378            .map(|e| e.file_name().to_string_lossy().into_owned())
1379            .filter(|name| name.contains(".tmp-"))
1380            .collect();
1381        assert!(
1382            leftovers.is_empty(),
1383            "durable copy must not leave temp siblings: {leftovers:?}"
1384        );
1385    }
1386
1387    #[test]
1388    fn publish_file_via_copy_durable_cleans_temp_when_rename_cannot_publish() {
1389        // If the final rename cannot complete, the temp sibling must be
1390        // removed so a crash/retry path doesn't accumulate junk — and the
1391        // pre-existing destination must be left untouched (atomic replace
1392        // failed → old bytes still authoritative).
1393        let dir = tempfile::TempDir::new().unwrap();
1394        let src = dir.path().join("staged.pack");
1395        let dst_dir = dir.path().join("final.pack");
1396        fs::write(&src, b"new-bytes").unwrap();
1397        // Make `dst` a directory so `rename(temp, dst)` fails (EISDIR /
1398        // ERROR_ACCESS_DENIED class). The copy-into-temp step succeeds;
1399        // only the publish rename fails.
1400        fs::create_dir(&dst_dir).unwrap();
1401
1402        let err = publish_file_via_copy_durable(&src, &dst_dir).expect_err("rename over dir");
1403        assert!(
1404            err.kind() == io::ErrorKind::AlreadyExists
1405                || err.raw_os_error().is_some()
1406                || is_permission_denied(&err)
1407                || err.kind() == io::ErrorKind::Other
1408                || err.kind() == io::ErrorKind::IsADirectory
1409                || err.kind() == io::ErrorKind::DirectoryNotEmpty,
1410            "unexpected error kind for rename-over-dir: {err:?}"
1411        );
1412        assert!(src.exists(), "failed publish must leave source intact");
1413        assert!(dst_dir.is_dir(), "destination directory must be untouched");
1414        let leftovers: Vec<_> = fs::read_dir(dir.path())
1415            .unwrap()
1416            .filter_map(|e| e.ok())
1417            .map(|e| e.file_name().to_string_lossy().into_owned())
1418            .filter(|name| name.contains(".tmp-"))
1419            .collect();
1420        assert!(
1421            leftovers.is_empty(),
1422            "failed publish must clean temp siblings: {leftovers:?}"
1423        );
1424    }
1425
1426    #[test]
1427    fn publish_file_durable_propagates_non_exdev_rename_failures() {
1428        // The previous install_pack_files_streaming path treated *any*
1429        // rename failure as "try fs::copy into the final path". A
1430        // permission / type error must surface, not be laundered into a
1431        // second write attempt against the content-addressed name.
1432        let dir = tempfile::TempDir::new().unwrap();
1433        let src = dir.path().join("staged.pack");
1434        let dst = dir.path().join("final.pack");
1435        fs::write(&src, b"pack-bytes").unwrap();
1436        fs::create_dir(&dst).unwrap();
1437
1438        let err = publish_file_durable(&src, &dst).expect_err("rename over directory");
1439        assert!(
1440            !is_cross_device_link(&err),
1441            "failure must not be misclassified as EXDEV: {err}"
1442        );
1443        // Source remains for the caller to retry / clean up.
1444        assert!(src.exists());
1445    }
1446
1447    /// GAP_MAP L6: nested shard directories must be creatable via the durable
1448    /// helper. We cannot observe fsync from userspace, but we can assert the
1449    /// end state matches `create_dir_all` (full nested path exists as dirs).
1450    #[test]
1451    fn create_dir_all_durable_creates_nested_path() {
1452        let dir = tempfile::TempDir::new().unwrap();
1453        // Classic object-store shard layout: grandparent holds the new shard
1454        // dirent (`ab`), parent is the shard itself.
1455        let shard = dir.path().join("blobs/ab");
1456        create_dir_all_durable(&shard).expect("create nested shard path");
1457        assert!(shard.is_dir(), "leaf shard directory must exist");
1458        assert!(
1459            dir.path().join("blobs").is_dir(),
1460            "intermediate grandparent must exist"
1461        );
1462        // Idempotent: re-running against an existing tree is a no-op success.
1463        create_dir_all_durable(&shard).expect("idempotent durable create");
1464        assert!(shard.is_dir());
1465    }
1466
1467    /// GAP_MAP L6: `write_file_atomic` must still round-trip when the full
1468    /// parent chain is missing — it now goes through `create_dir_all_durable`
1469    /// instead of bare `create_dir_all`.
1470    #[test]
1471    fn write_file_atomic_creates_missing_shard_parents() {
1472        let dir = tempfile::TempDir::new().unwrap();
1473        let target = dir.path().join("blobs/ab/object.bin");
1474        write_file_atomic(&target, b"shard-bytes").unwrap();
1475        assert_eq!(fs::read(&target).unwrap(), b"shard-bytes");
1476        assert!(dir.path().join("blobs/ab").is_dir());
1477    }
1478
1479    /// Existing parent chain: durable create must not fail or alter contents.
1480    #[test]
1481    fn create_dir_all_durable_ok_when_path_already_exists() {
1482        let dir = tempfile::TempDir::new().unwrap();
1483        let nested = dir.path().join("already/there");
1484        fs::create_dir_all(&nested).unwrap();
1485        create_dir_all_durable(&nested).expect("existing dir");
1486        assert!(nested.is_dir());
1487    }
1488}