Skip to main content

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