batpak 0.9.0

Event sourcing with causal graphs and caller-defined gates. Sync API, no async runtime.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
use crate::store::StoreError;
use std::fs::{File, Metadata, ReadDir};
use std::io::{self, Write};
use std::path::{Path, PathBuf};
use tempfile::NamedTempFile;

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) enum CowStrategyUsed {
    Reflink,
    Hardlink,
    DeepCopy,
}

pub(crate) fn reject_symlink_leaf(path: &Path, purpose: &str) -> Result<(), StoreError> {
    match std::fs::symlink_metadata(path) {
        Ok(meta) if meta.file_type().is_symlink() => Err(StoreError::Io(std::io::Error::new(
            std::io::ErrorKind::InvalidInput,
            format!(
                "refusing to write {purpose} through symlink {}",
                path.display()
            ),
        ))),
        Ok(_) | Err(_) => Ok(()),
    }
}

pub(crate) fn reject_cache_symlink_leaf(path: &Path) -> Result<(), StoreError> {
    match reject_symlink_leaf(path, "cache path") {
        Ok(()) => Ok(()),
        Err(StoreError::Io(error)) => Err(StoreError::CacheFailed(Box::new(error))),
        Err(error) => Err(error),
    }
}

/// Atomic write via the production default backend ([`RealFs`]).
///
/// A thin wrapper over [`write_file_atomically_with_fs`] that preserves the
/// standalone (fs-less) test forge (`checkpoint::tests`); every production
/// cold-start artifact write path (checkpoint / mmap-index / idempotency-store /
/// compaction marker) now drives the `_with_fs` variant with the store's
/// configured filesystem so a `SimFs` can tear the atomic publish. Test-only
/// because no production call site remains.
#[cfg(test)]
pub(crate) fn write_file_atomically(
    data_dir: &Path,
    final_path: &Path,
    purpose: &str,
    write: impl FnOnce(&mut File) -> Result<(), StoreError>,
) -> Result<(), StoreError> {
    write_file_atomically_with_fs(data_dir, final_path, purpose, write, &RealFs)
}

/// [`write_file_atomically`], routed through the supplied [`StoreFs`] backend.
///
/// A composite of already-routed primitives — the symlink-leaf guard, the
/// temp-file create ([`StoreFs::named_temp_in`]), and the atomic publish
/// ([`StoreFs::persist_temp_with_parent_sync`]) — so a fault-injecting backend
/// can tear the publish and a crash harness can observe a cold-start artifact
/// the store believed durable. The temp-contents fsync stays on the direct
/// `platform::sync` seam (the staging half), mirroring
/// [`super::super::hidden_ranges::write_cancelled_ranges`] and
/// [`super::super::delivery::cursor::Cursor::save_checkpoint_with_fs`]. `RealFs`
/// makes it byte-for-byte the production behavior.
pub(crate) fn write_file_atomically_with_fs(
    data_dir: &Path,
    final_path: &Path,
    purpose: &str,
    write: impl FnOnce(&mut File) -> Result<(), StoreError>,
    fs: &dyn StoreFs,
) -> Result<(), StoreError> {
    fs.reject_symlink_leaf(final_path, purpose)?;
    let tmp = fs.named_temp_in(data_dir)?;
    let mut file = tmp.reopen().map_err(StoreError::Io)?;
    write(&mut file)?;
    crate::store::platform::sync::sync_file_all_io(&file).map_err(StoreError::Io)?;
    drop(file);
    let admission = crate::store::platform::sync::admit_current_parent_dir_sync()?;
    fs.persist_temp_with_parent_sync(tmp, final_path, admission)
        .map_err(StoreError::Io)?;
    Ok(())
}

pub(crate) fn write_derivative_file_atomically(
    data_dir: &Path,
    final_path: &Path,
    purpose: &str,
    bytes: &[u8],
) -> io::Result<()> {
    match reject_symlink_leaf(final_path, purpose) {
        Ok(()) => {}
        Err(StoreError::Io(error)) => return Err(error),
        Err(error) => return Err(io::Error::other(error.to_string())),
    }
    let tmp = named_temp_in(data_dir)?;
    {
        let mut file = io::BufWriter::new(tmp.as_file());
        file.write_all(bytes)?;
        file.into_inner().map_err(|error| error.into_error())?;
    }
    tmp.persist(final_path).map_err(|error| error.error)?;
    Ok(())
}

pub(crate) fn create_new_file(path: &Path) -> Result<File, StoreError> {
    File::create_new(path).map_err(StoreError::Io)
}

pub(crate) fn open_file(path: &Path) -> io::Result<File> {
    File::open(path)
}

pub(crate) fn read(path: &Path) -> io::Result<Vec<u8>> {
    std::fs::read(path)
}

pub(crate) fn read_dir(path: &Path) -> io::Result<ReadDir> {
    std::fs::read_dir(path)
}

pub(crate) fn create_dir_all(path: &Path) -> io::Result<()> {
    std::fs::create_dir_all(path)
}

pub(crate) fn canonicalize(path: &Path) -> io::Result<PathBuf> {
    std::fs::canonicalize(path)
}

pub(crate) fn metadata(path: &Path) -> io::Result<Metadata> {
    std::fs::metadata(path)
}

pub(crate) fn symlink_metadata(path: &Path) -> io::Result<Metadata> {
    std::fs::symlink_metadata(path)
}

pub(crate) fn remove_file(path: &Path) -> io::Result<()> {
    std::fs::remove_file(path)
}

pub(crate) fn remove_file_if_present(path: &Path) -> io::Result<bool> {
    match remove_file(path) {
        Ok(()) => Ok(true),
        Err(error) if error.kind() == io::ErrorKind::NotFound => Ok(false),
        Err(error) => Err(error),
    }
}

pub(crate) fn remove_dir_all(path: &Path) -> io::Result<()> {
    std::fs::remove_dir_all(path)
}

pub(crate) fn remove_dir_all_if_present(path: &Path) -> io::Result<bool> {
    match remove_dir_all(path) {
        Ok(()) => Ok(true),
        Err(error) if error.kind() == io::ErrorKind::NotFound => Ok(false),
        Err(error) => Err(error),
    }
}

pub(crate) fn named_temp_in(dir: &Path) -> io::Result<NamedTempFile> {
    NamedTempFile::new_in(dir)
}

pub(crate) fn rename(from: &Path, to: &Path) -> io::Result<()> {
    std::fs::rename(from, to)
}

pub(crate) fn copy(from: &Path, to: &Path) -> io::Result<u64> {
    std::fs::copy(from, to)
}

/// Truncate (or extend) the file at `path` to exactly `len` bytes.
///
/// Used by the deterministic-simulation filesystem ([`super::super::sim::fs::SimFs`])
/// to model a crash: each tracked file is truncated to its last durable length,
/// discarding the write-but-unsynced tail. Lives here, under the platform
/// boundary, so the raw file-open + `set_len` target contact stays out of the
/// store-runtime code the structural gate guards.
#[cfg(feature = "dangerous-test-hooks")]
pub(crate) fn truncate_file_to(path: &Path, len: u64) -> io::Result<()> {
    let file = std::fs::OpenOptions::new().write(true).open(path)?;
    file.set_len(len)
}

pub(crate) fn hard_link(from: &Path, to: &Path) -> io::Result<()> {
    std::fs::hard_link(from, to)
}

pub(crate) fn reflink(from: &Path, to: &Path) -> io::Result<()> {
    reject_copy_source(from)?;
    remove_file_if_present(to)?;
    reflink_impl(from, to).inspect_err(|_| {
        drop(remove_file_if_present(to));
    })
}

pub(crate) fn cow_copy_file(
    from: &Path,
    to: &Path,
    preference: crate::store::CopyPreference,
) -> io::Result<CowStrategyUsed> {
    use crate::store::CopyPreference;
    let use_reflink = matches!(preference, CopyPreference::ReflinkThenHardlink);
    let use_hardlink = matches!(
        preference,
        CopyPreference::ReflinkThenHardlink | CopyPreference::HardlinkOnly
    );

    reject_copy_source(from)?;
    remove_file_if_present(to)?;

    if use_reflink {
        match reflink(from, to) {
            Ok(()) => return Ok(CowStrategyUsed::Reflink),
            Err(error) => {
                tracing::debug!(
                    source = %from.display(),
                    destination = %to.display(),
                    error = %error,
                    "reflink failed; falling back to next fork copy rung"
                );
                remove_file_if_present(to)?;
            }
        }
    }

    if use_hardlink {
        match hard_link(from, to) {
            Ok(()) => return Ok(CowStrategyUsed::Hardlink),
            Err(error) => {
                tracing::debug!(
                    source = %from.display(),
                    destination = %to.display(),
                    error = %error,
                    "hardlink failed; falling back to deep copy"
                );
                remove_file_if_present(to)?;
            }
        }
    }

    copy(from, to)?;
    Ok(CowStrategyUsed::DeepCopy)
}

fn reject_copy_source(path: &Path) -> io::Result<()> {
    let meta = std::fs::symlink_metadata(path)?;
    if meta.file_type().is_symlink() {
        return Err(io::Error::new(
            io::ErrorKind::InvalidInput,
            format!("refusing to copy symlink source {}", path.display()),
        ));
    }
    if !meta.is_file() {
        return Err(io::Error::new(
            io::ErrorKind::InvalidInput,
            format!("refusing to copy non-file source {}", path.display()),
        ));
    }
    Ok(())
}

#[cfg(target_os = "linux")]
fn reflink_impl(from: &Path, to: &Path) -> io::Result<()> {
    use std::os::fd::AsRawFd;

    const FICLONE: libc::c_ulong = 0x4004_9409;
    let source = File::open(from)?;
    let destination = File::create_new(to)?;
    // SAFETY: `source` and `destination` are live file descriptors opened by
    // this function. `FICLONE` does not retain pointers into Rust memory; it
    // asks the kernel to clone file data from `source` into `destination`.
    let result = unsafe { libc::ioctl(destination.as_raw_fd(), FICLONE, source.as_raw_fd()) };
    if result == 0 {
        Ok(())
    } else {
        Err(io::Error::last_os_error())
    }
}

#[cfg(target_os = "macos")]
fn reflink_impl(from: &Path, to: &Path) -> io::Result<()> {
    use std::ffi::CString;
    use std::os::unix::ffi::OsStrExt;

    let from = CString::new(from.as_os_str().as_bytes()).map_err(|_| {
        io::Error::new(
            io::ErrorKind::InvalidInput,
            "source path contains interior NUL",
        )
    })?;
    let to = CString::new(to.as_os_str().as_bytes()).map_err(|_| {
        io::Error::new(
            io::ErrorKind::InvalidInput,
            "destination path contains interior NUL",
        )
    })?;
    // SAFETY: the C strings are NUL-terminated and live for the duration of
    // the call. `clonefile` does not retain the pointers after returning.
    let result = unsafe { libc::clonefile(from.as_ptr(), to.as_ptr(), 0) };
    if result == 0 {
        Ok(())
    } else {
        Err(io::Error::last_os_error())
    }
}

#[cfg(not(any(target_os = "linux", target_os = "macos")))]
fn reflink_impl(_from: &Path, _to: &Path) -> io::Result<()> {
    Err(io::Error::new(
        io::ErrorKind::Unsupported,
        "reflink is not supported on this platform",
    ))
}

#[derive(Debug)]
pub(crate) enum PositionedReadError {
    Io(std::io::Error),
    ShortRead { bytes_read: usize },
}

pub(crate) fn read_exact_at(
    file: &mut File,
    offset: u64,
    buf: &mut [u8],
) -> Result<(), PositionedReadError> {
    #[cfg(unix)]
    {
        use std::os::unix::fs::FileExt;
        let mut total_read = 0;
        while total_read < buf.len() {
            let n = file
                .read_at(&mut buf[total_read..], offset + total_read as u64)
                .map_err(PositionedReadError::Io)?;
            if n == 0 {
                return Err(PositionedReadError::ShortRead {
                    bytes_read: total_read,
                });
            }
            total_read = total_read.saturating_add(n);
        }
        Ok(())
    }
    #[cfg(not(unix))]
    {
        use std::io::Read;
        use std::io::{Seek, SeekFrom};
        file.seek(SeekFrom::Start(offset))
            .map_err(PositionedReadError::Io)?;
        let mut total_read = 0;
        while total_read < buf.len() {
            let n = file
                .read(&mut buf[total_read..])
                .map_err(PositionedReadError::Io)?;
            if n == 0 {
                return Err(PositionedReadError::ShortRead {
                    bytes_read: total_read,
                });
            }
            total_read = total_read.saturating_add(n);
        }
        Ok(())
    }
}

// Platform free-function routing status (release row `STORE-PLATFORM-FS-ROUTING`,
// terminal FAIL-CLOSED boundary). The tail is now EMPTY: every store call site
// that reaches the target filesystem does so through [`StoreFs`]. The
// crash-sensitive atomic-rename / persist cluster (`rename`, `remove_file`,
// `named_temp_in`, `persist_temp_with_parent_sync`) was routed by W3; the
// positioned-read primitive (`read_exact_at`) is now routed too, so a `SimFs` can
// fault the active-segment frame read that, as a free fn, was unfaultable. The
// free fn itself STAYS here — it holds the `read_at` / `#[cfg(unix)]` /
// `FileExt` contact the `platform_boundary` gate confines to `platform/` —
// and `RealFs::read_exact_at` delegates straight to it. Asserted empty by
// `store_fs_fail_closed_boundary_lists_unrouted_tail_ops`.

/// Filesystem seam for production and (future) deterministic simulation.
///
/// Boundary: this is the narrow trait through which store code reaches the
/// target filesystem. Production routes through [`RealFs`], whose every method
/// is a byte-for-byte delegate to the existing `platform::fs::*` /
/// `platform::sync::*` free functions — the only observable difference from a
/// direct free-fn call is the indirection through a trait object. A later
/// gauntlet item installs a `SimFs` that fakes the filesystem on an in-memory
/// model for deterministic crash/fault tests; it is NOT built here. This trait
/// only introduces the seam so call sites that hold a `StoreConfig` stop
/// reaching `std::fs` (via the free fns) directly.
///
/// `Send + Sync` so it can live behind `Arc<dyn StoreFs>` on `StoreConfig` and
/// be shared across threads, mirroring the [`super::spawn::Spawn`] seam.
///
/// Scope note: routed subset of the platform fs/sync surface. The trait covers
/// directory iteration, directory creation, segment file creation, the sync
/// cluster, symlink/canonicalize guards, copy/metadata, the cow copy paths, and
/// (W3) the crash-sensitive atomic-rename / persist cluster — `rename`,
/// `remove_file`, `named_temp_in`, `persist_temp_with_parent_sync` — so the
/// compaction swap/rollback, visibility-range persist, and cursor-checkpoint
/// persist sequences are fault-injectable under [`super::super::sim::fs::SimFs`],
/// and the positioned read `read_exact_at`, so the active-segment frame read is
/// fault-injectable too. The routing tail is now empty: no store call site
/// reaches `platform::fs::*` directly (release row `STORE-PLATFORM-FS-ROUTING`).
pub(crate) trait StoreFs: Send + Sync {
    /// Iterate a directory's entries. Mirrors [`std::fs::read_dir`].
    fn read_dir(&self, path: &Path) -> io::Result<ReadDir>;

    /// Create a directory and all missing parents. Mirrors
    /// [`std::fs::create_dir_all`].
    fn create_dir_all(&self, path: &Path) -> io::Result<()>;

    /// Create-new the segment/data file at `path`, returning the open handle.
    ///
    /// `path` is the LOGICAL file path the caller will hold (e.g. a segment
    /// `.fbat`); a fault-injecting backend keys its durable-length model off it
    /// so a later [`StoreFs::crash`] can truncate the file to its last fsynced
    /// length. Mirrors [`create_new_file`].
    fn create_new_file(&self, path: &Path) -> Result<File, StoreError>;

    /// Fsync the contents of `file` (backing `path`) with `mode`.
    ///
    /// This is the per-event / per-rotation durability boundary. A backend may
    /// honor it (advancing the durable length recorded for `path`) or, under its
    /// seeded fault schedule, drop it (leaving the most recent bytes lost on the
    /// next [`StoreFs::crash`]). `path` lets the backend key its durable model.
    /// Mirrors [`super::sync::sync_file_with_mode`].
    fn sync_file_with_mode(
        &self,
        file: &File,
        path: &Path,
        mode: &crate::store::SyncMode,
    ) -> Result<(), StoreError>;

    /// Fsync the contents of `file` (backing `path`) unconditionally (`sync_all`
    /// semantics). Used on segment create where the header bytes must be durable
    /// before the directory entry. Mirrors [`super::sync::sync_file_all_io`].
    fn sync_file_all(&self, file: &File, path: &Path) -> io::Result<()>;

    /// Fsync the directory entry for `path`'s parent so a freshly-created file's
    /// name is durable. Mirrors [`super::sync::sync_parent_dir`].
    fn sync_parent_dir(&self, path: &Path) -> Result<(), StoreError>;

    /// Reject symlink leaf paths before writing. Mirrors [`reject_symlink_leaf`].
    fn reject_symlink_leaf(&self, path: &Path, purpose: &str) -> Result<(), StoreError>;

    /// Canonicalize a path. Mirrors [`canonicalize`].
    fn canonicalize(&self, path: &Path) -> io::Result<PathBuf>;

    /// Symlink-aware metadata. Mirrors [`symlink_metadata`].
    fn symlink_metadata(&self, path: &Path) -> io::Result<Metadata>;

    /// Copy-on-write file copy for fork. Mirrors [`cow_copy_file`].
    fn cow_copy_file(
        &self,
        from: &Path,
        to: &Path,
        preference: crate::store::CopyPreference,
    ) -> io::Result<CowStrategyUsed>;

    /// Deep file copy for snapshot. Mirrors [`copy`].
    fn copy(&self, from: &Path, to: &Path) -> io::Result<u64>;

    /// File metadata. Mirrors [`metadata`].
    fn metadata(&self, path: &Path) -> io::Result<Metadata>;

    /// Rename `from` to `to`. Mirrors [`rename`].
    ///
    /// Crash-sensitive: the single atomic swap/rollback point of compaction
    /// (relocate the merged source, then restore it on rollback). Routed so a
    /// fault-injecting backend can tear the swap and a crash harness can observe
    /// the half-applied rename.
    fn rename(&self, from: &Path, to: &Path) -> io::Result<()>;

    /// Remove the file at `path`. Mirrors [`remove_file`].
    ///
    /// Crash-sensitive: the post-swap reclaim of superseded segments after a
    /// successful compaction. Routed so the reclaim becomes fault-injectable.
    fn remove_file(&self, path: &Path) -> io::Result<()>;

    /// Remove the file at `path`, reporting whether it existed (`Ok(false)` when
    /// already absent). Mirrors [`remove_file_if_present`].
    ///
    /// Provided in terms of [`StoreFs::remove_file`] so a fault-injecting backend
    /// only interposes the one primitive; the not-found tolerance is identical on
    /// every backend.
    fn remove_file_if_present(&self, path: &Path) -> io::Result<bool> {
        match self.remove_file(path) {
            Ok(()) => Ok(true),
            Err(error) if error.kind() == io::ErrorKind::NotFound => Ok(false),
            Err(error) => Err(error),
        }
    }

    /// Create a uniquely-named temp file in `dir`. Mirrors [`named_temp_in`].
    ///
    /// The temp file is the staging half of an atomic publish: the caller writes
    /// + fsyncs it, then hands it to [`StoreFs::persist_temp_with_parent_sync`].
    fn named_temp_in(&self, dir: &Path) -> io::Result<NamedTempFile>;

    /// Persist `named_temp` as `final_path` with a parent-directory fsync.
    /// Mirrors [`super::sync::persist_temp_with_parent_sync`].
    ///
    /// Crash-sensitive: the atomic publish point for the visibility-range and
    /// cursor-checkpoint metadata files. Routed so a fault-injecting backend can
    /// drop the rename and a crash harness can observe a publish that the store
    /// believed durable.
    fn persist_temp_with_parent_sync(
        &self,
        named_temp: NamedTempFile,
        final_path: &Path,
        admission: super::sync::ParentDirSyncAdmission,
    ) -> io::Result<()>;

    /// Read exactly `buf.len()` bytes into `buf` starting at byte `offset` of
    /// `file`. Mirrors the [`read_exact_at`] free fn.
    ///
    /// This is the active-segment frame read (the FD/pread path;
    /// [`super::super::segment::scan::Reader`] holds the open handle). Routed so a
    /// fault-injecting backend can inject a positioned-read error or a short read
    /// where the free fn — which takes no fs handle — could not be intercepted.
    /// The `read_at` / `#[cfg(unix)]` / `FileExt` contact stays in the free fn
    /// under `platform/`; [`RealFs`] delegates straight to it.
    fn read_exact_at(
        &self,
        file: &mut File,
        offset: u64,
        buf: &mut [u8],
    ) -> Result<(), PositionedReadError>;
}

/// Production [`StoreFs`]: every method delegates to the existing
/// `platform::fs::*` free functions, so the default build behaves byte-for-byte
/// as it did before the seam was introduced.
#[derive(Debug, Default, Clone, Copy)]
pub(crate) struct RealFs;

impl StoreFs for RealFs {
    fn read_dir(&self, path: &Path) -> io::Result<ReadDir> {
        read_dir(path)
    }

    fn create_dir_all(&self, path: &Path) -> io::Result<()> {
        create_dir_all(path)
    }

    fn create_new_file(&self, path: &Path) -> Result<File, StoreError> {
        create_new_file(path)
    }

    fn sync_file_with_mode(
        &self,
        file: &File,
        _path: &Path,
        mode: &crate::store::SyncMode,
    ) -> Result<(), StoreError> {
        // RealFs ignores `path`: the real OS keys durability off the file handle.
        crate::store::platform::sync::sync_file_with_mode(file, mode)
    }

    fn sync_file_all(&self, file: &File, _path: &Path) -> io::Result<()> {
        crate::store::platform::sync::sync_file_all_io(file)
    }

    fn sync_parent_dir(&self, path: &Path) -> Result<(), StoreError> {
        crate::store::platform::sync::sync_parent_dir(path)
    }

    fn reject_symlink_leaf(&self, path: &Path, purpose: &str) -> Result<(), StoreError> {
        reject_symlink_leaf(path, purpose)
    }

    fn canonicalize(&self, path: &Path) -> io::Result<PathBuf> {
        canonicalize(path)
    }

    fn symlink_metadata(&self, path: &Path) -> io::Result<Metadata> {
        symlink_metadata(path)
    }

    fn cow_copy_file(
        &self,
        from: &Path,
        to: &Path,
        preference: crate::store::CopyPreference,
    ) -> io::Result<CowStrategyUsed> {
        cow_copy_file(from, to, preference)
    }

    fn copy(&self, from: &Path, to: &Path) -> io::Result<u64> {
        copy(from, to)
    }

    fn metadata(&self, path: &Path) -> io::Result<Metadata> {
        metadata(path)
    }

    fn rename(&self, from: &Path, to: &Path) -> io::Result<()> {
        rename(from, to)
    }

    fn remove_file(&self, path: &Path) -> io::Result<()> {
        remove_file(path)
    }

    fn named_temp_in(&self, dir: &Path) -> io::Result<NamedTempFile> {
        named_temp_in(dir)
    }

    fn persist_temp_with_parent_sync(
        &self,
        named_temp: NamedTempFile,
        final_path: &Path,
        admission: super::sync::ParentDirSyncAdmission,
    ) -> io::Result<()> {
        crate::store::platform::sync::persist_temp_with_parent_sync(
            named_temp, final_path, admission,
        )
    }

    fn read_exact_at(
        &self,
        file: &mut File,
        offset: u64,
        buf: &mut [u8],
    ) -> Result<(), PositionedReadError> {
        read_exact_at(file, offset, buf)
    }
}

#[cfg(all(test, any(target_os = "linux", target_os = "macos")))]
#[path = "fs_reflink_mutation_tests.rs"]
mod fs_reflink_mutation_tests;

#[cfg(test)]
#[path = "fs_tests.rs"]
mod tests;