aft-store 0.68.1

On-disk index formats for Agent Foundational Toolkit
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
//! Durable flushes shared by the index store and AFT's other file writers.
//!
//! macOS uses fsync by default. Opt-in full drive flushes fall back to fsync on
//! mounts without F_FULLFSYNC support; other errors remain fatal.

use std::fs::File;
use std::io;
use std::path::Path;
use std::sync::atomic::{AtomicBool, Ordering};

static MACOS_FULL_SYNC: AtomicBool = AtomicBool::new(false);

/// Select full drive flushes for subsequent macOS syncs. Other platforms ignore it.
pub fn set_macos_full_sync(enabled: bool) {
    MACOS_FULL_SYNC.store(enabled, Ordering::Relaxed);
}

/// The requested macOS policy, even on other platforms. This read is lock-free.
pub fn macos_full_sync() -> bool {
    MACOS_FULL_SYNC.load(Ordering::Relaxed)
}

/// The active sync policy, for runtime diagnostics.
pub fn mode() -> &'static str {
    if cfg!(target_os = "macos") && macos_full_sync() {
        "full_sync"
    } else {
        "fsync"
    }
}

/// Flush file contents and metadata, using the configured macOS sync policy.
pub fn sync_all(file: &File, path: &Path) -> io::Result<()> {
    #[cfg(target_os = "macos")]
    {
        macos::sync_all(file, path)
    }
    #[cfg(not(target_os = "macos"))]
    {
        let _ = path;
        file.sync_all()
    }
}

/// Flush from a panic hook without entering the logger or its warning-set lock.
/// Unsupported full syncs are still counted and fall back to fsync, but do not
/// warn or mark the device as warned by an ordinary caller.
pub fn sync_all_in_panic(file: &File, path: &Path) -> io::Result<()> {
    #[cfg(target_os = "macos")]
    {
        macos::sync_all_inner(file, path, false)
    }
    #[cfg(not(target_os = "macos"))]
    {
        let _ = path;
        file.sync_all()
    }
}

/// Flush file contents, using the configured macOS policy and native semantics elsewhere.
pub fn sync_data(file: &File, path: &Path) -> io::Result<()> {
    #[cfg(target_os = "macos")]
    {
        // Rust uses F_FULLFSYNC for both sync_all and sync_data on macOS.
        macos::sync_all(file, path)
    }
    #[cfg(not(target_os = "macos"))]
    {
        let _ = path;
        file.sync_data()
    }
}

/// Number of unsupported full-sync calls that attempted the fsync fallback.
pub fn full_sync_fallbacks() -> u64 {
    #[cfg(target_os = "macos")]
    {
        macos::DIAGNOSTICS
            .fallbacks
            .load(std::sync::atomic::Ordering::Relaxed)
    }
    #[cfg(not(target_os = "macos"))]
    {
        0
    }
}

#[cfg(target_os = "macos")]
mod macos {
    use super::*;
    use std::collections::HashSet;
    use std::os::fd::AsRawFd;
    use std::os::unix::fs::MetadataExt;
    use std::sync::atomic::AtomicU64;
    use std::sync::{LazyLock, Mutex};

    #[derive(Default)]
    pub(super) struct Diagnostics {
        pub(super) fallbacks: AtomicU64,
        warned_devices: LazyLock<Mutex<HashSet<u64>>>,
    }

    pub(super) static DIAGNOSTICS: Diagnostics = Diagnostics {
        fallbacks: AtomicU64::new(0),
        warned_devices: LazyLock::new(Mutex::default),
    };

    pub(super) fn sync_all(file: &File, path: &Path) -> io::Result<()> {
        sync_all_inner(file, path, true)
    }

    pub(super) fn sync_all_inner(file: &File, path: &Path, warn: bool) -> io::Result<()> {
        sync_all_with(
            file,
            path,
            &DIAGNOSTICS,
            &MACOS_FULL_SYNC,
            || {
                // SAFETY: the borrowed File keeps its descriptor open.
                retry(|| unsafe { libc::fcntl(file.as_raw_fd(), libc::F_FULLFSYNC) })
            },
            || fsync(file),
            warn,
        )
    }

    fn fsync(file: &File) -> io::Result<()> {
        // SAFETY: the borrowed File keeps its descriptor open.
        retry(|| unsafe { libc::fsync(file.as_raw_fd()) })
    }

    fn retry(mut syscall: impl FnMut() -> libc::c_int) -> io::Result<()> {
        loop {
            if syscall() == 0 {
                return Ok(());
            }
            let error = io::Error::last_os_error();
            if error.kind() != io::ErrorKind::Interrupted {
                return Err(error);
            }
        }
    }

    // Inject the switch and syscalls so tests can change policy without racing
    // other file writers, while keeping production's fallback and diagnostics.
    fn sync_all_with(
        file: &File,
        path: &Path,
        diagnostics: &Diagnostics,
        full_sync_enabled: &AtomicBool,
        full_sync: impl FnOnce() -> io::Result<()>,
        fallback: impl FnOnce() -> io::Result<()>,
        warn: bool,
    ) -> io::Result<()> {
        if !full_sync_enabled.load(Ordering::Relaxed) {
            return fallback();
        }
        match full_sync() {
            Ok(()) => Ok(()),
            Err(error)
                if error.raw_os_error().is_some_and(|errno| {
                    [libc::ENOTTY, libc::ENOTSUP, libc::EINVAL].contains(&errno)
                }) =>
            {
                diagnostics.fallbacks.fetch_add(1, Ordering::Relaxed);
                // Device lookup is diagnostic only; it must not prevent fsync.
                if warn && let Ok(metadata) = file.metadata() {
                    let device = metadata.dev();
                    let first = diagnostics
                        .warned_devices
                        .lock()
                        .unwrap_or_else(|error| error.into_inner())
                        .insert(device);
                    if first {
                        log::warn!(
                            "F_FULLFSYNC unsupported; falling back to fsync: path={} errno={} device={device}",
                            path.display(),
                            error.raw_os_error().expect("matched an errno"),
                        );
                    }
                }
                fallback()
            }
            Err(error) => Err(error),
        }
    }

    #[cfg(test)]
    mod tests {
        use super::*;
        use std::cell::{Cell, RefCell};
        use std::sync::Once;

        struct TestLogger;
        static LOGGER: TestLogger = TestLogger;
        static INIT: Once = Once::new();
        thread_local! {
            static WARNINGS: RefCell<Vec<String>> = const { RefCell::new(Vec::new()) };
        }

        impl log::Log for TestLogger {
            fn enabled(&self, metadata: &log::Metadata<'_>) -> bool {
                metadata.level() == log::Level::Warn
            }

            fn log(&self, record: &log::Record<'_>) {
                if self.enabled(record.metadata()) {
                    WARNINGS.with(|warnings| warnings.borrow_mut().push(record.args().to_string()));
                }
            }

            fn flush(&self) {}
        }

        fn diagnostics() -> Diagnostics {
            INIT.call_once(|| {
                log::set_logger(&LOGGER).expect("install test logger");
                log::set_max_level(log::LevelFilter::Warn);
            });
            WARNINGS.with(|warnings| warnings.borrow_mut().clear());
            Diagnostics::default()
        }

        fn injected(
            file: &File,
            path: &Path,
            diagnostics: &Diagnostics,
            errno: i32,
            fsyncs: &Cell<usize>,
        ) -> io::Result<()> {
            sync_all_with(
                file,
                path,
                diagnostics,
                &AtomicBool::new(true),
                || Err(io::Error::from_raw_os_error(errno)),
                || {
                    fsyncs.set(fsyncs.get() + 1);
                    fsync(file)
                },
                true,
            )
        }

        #[test]
        fn full_sync_enotty_falls_back_and_counts() {
            let diagnostics = diagnostics();
            let file = tempfile::NamedTempFile::new().unwrap();
            let fsyncs = Cell::new(0);
            injected(
                file.as_file(),
                file.path(),
                &diagnostics,
                libc::ENOTTY,
                &fsyncs,
            )
            .unwrap();
            assert_eq!(fsyncs.get(), 1);
            assert_eq!(diagnostics.fallbacks.load(Ordering::Relaxed), 1);
            WARNINGS.with(|warnings| {
                let warnings = warnings.borrow();
                assert_eq!(warnings.len(), 1);
                assert!(warnings[0].contains(&file.path().display().to_string()));
                assert!(warnings[0].contains(&format!("errno={}", libc::ENOTTY)));
            });
        }

        #[test]
        fn full_sync_enotsup_and_einval_fall_back_for_directories() {
            let diagnostics = diagnostics();
            let dir = tempfile::tempdir().unwrap();
            let file = File::open(dir.path()).unwrap();
            let fsyncs = Cell::new(0);
            for errno in [libc::ENOTSUP, libc::EINVAL] {
                injected(&file, dir.path(), &diagnostics, errno, &fsyncs).unwrap();
            }
            assert_eq!(fsyncs.get(), 2);
            assert_eq!(diagnostics.fallbacks.load(Ordering::Relaxed), 2);
        }

        #[test]
        fn full_sync_eio_still_fails_without_fallback() {
            let diagnostics = diagnostics();
            let file = tempfile::NamedTempFile::new().unwrap();
            let fsyncs = Cell::new(0);
            let error = injected(
                file.as_file(),
                file.path(),
                &diagnostics,
                libc::EIO,
                &fsyncs,
            )
            .unwrap_err();
            assert_eq!(error.raw_os_error(), Some(libc::EIO));
            assert_eq!(fsyncs.get(), 0);
            assert_eq!(diagnostics.fallbacks.load(Ordering::Relaxed), 0);
            WARNINGS.with(|warnings| assert!(warnings.borrow().is_empty()));
        }

        #[test]
        fn full_sync_second_fallback_on_same_device_warns_once() {
            let diagnostics = diagnostics();
            let first = tempfile::NamedTempFile::new().unwrap();
            let second = tempfile::NamedTempFile::new().unwrap();
            assert_eq!(
                first.as_file().metadata().unwrap().dev(),
                second.as_file().metadata().unwrap().dev()
            );
            let fsyncs = Cell::new(0);
            for file in [&first, &second] {
                injected(
                    file.as_file(),
                    file.path(),
                    &diagnostics,
                    libc::ENOTTY,
                    &fsyncs,
                )
                .unwrap();
            }
            assert_eq!(fsyncs.get(), 2);
            assert_eq!(diagnostics.fallbacks.load(Ordering::Relaxed), 2);
            WARNINGS.with(|warnings| assert_eq!(warnings.borrow().len(), 1));
        }

        #[test]
        fn full_sync_fallback_failure_is_propagated() {
            let diagnostics = diagnostics();
            let file = tempfile::NamedTempFile::new().unwrap();
            let error = sync_all_with(
                file.as_file(),
                file.path(),
                &diagnostics,
                &AtomicBool::new(true),
                || Err(io::Error::from_raw_os_error(libc::ENOTTY)),
                || Err(io::Error::from_raw_os_error(libc::EIO)),
                true,
            )
            .unwrap_err();
            assert_eq!(error.raw_os_error(), Some(libc::EIO));
            assert_eq!(diagnostics.fallbacks.load(Ordering::Relaxed), 1);
        }

        #[test]
        fn full_sync_success_does_not_fall_back() {
            let diagnostics = diagnostics();
            let file = tempfile::NamedTempFile::new().unwrap();
            sync_all_with(
                file.as_file(),
                file.path(),
                &diagnostics,
                &AtomicBool::new(true),
                || Ok(()),
                || panic!("successful full sync must not fall back"),
                true,
            )
            .unwrap();
            assert_eq!(diagnostics.fallbacks.load(Ordering::Relaxed), 0);
            WARNINGS.with(|warnings| assert!(warnings.borrow().is_empty()));
        }

        #[test]
        fn full_sync_panic_fallback_skips_warning_lock() {
            let diagnostics = diagnostics();
            let file = tempfile::NamedTempFile::new().unwrap();
            let fsyncs = Cell::new(0);
            let warned = diagnostics.warned_devices.lock().unwrap();
            sync_all_with(
                file.as_file(),
                file.path(),
                &diagnostics,
                &AtomicBool::new(true),
                || Err(io::Error::from_raw_os_error(libc::ENOTTY)),
                || {
                    fsyncs.set(fsyncs.get() + 1);
                    fsync(file.as_file())
                },
                false,
            )
            .unwrap();
            assert_eq!(fsyncs.get(), 1);
            assert_eq!(diagnostics.fallbacks.load(Ordering::Relaxed), 1);
            assert!(warned.is_empty());
            WARNINGS.with(|warnings| assert!(warnings.borrow().is_empty()));
        }

        #[test]
        fn sync_policy_defaults_to_fsync_and_changes_on_the_next_call() {
            let diagnostics = diagnostics();
            let file = tempfile::NamedTempFile::new().unwrap();
            assert!(!macos_full_sync());
            assert_eq!(mode(), "fsync");
            let full_syncs = Cell::new(0);
            let fsyncs = Cell::new(0);
            let sync = || {
                sync_all_with(
                    file.as_file(),
                    file.path(),
                    &diagnostics,
                    &MACOS_FULL_SYNC,
                    || {
                        full_syncs.set(full_syncs.get() + 1);
                        Ok(())
                    },
                    || {
                        fsyncs.set(fsyncs.get() + 1);
                        Ok(())
                    },
                    true,
                )
                .unwrap();
            };
            sync();
            assert_eq!((full_syncs.get(), fsyncs.get()), (0, 1));
            set_macos_full_sync(true);
            assert_eq!(mode(), "full_sync");
            sync();
            assert_eq!((full_syncs.get(), fsyncs.get()), (1, 1));
            set_macos_full_sync(false);
            assert_eq!(mode(), "fsync");
            sync();
            assert_eq!((full_syncs.get(), fsyncs.get()), (1, 2));
            assert_eq!(diagnostics.fallbacks.load(Ordering::Relaxed), 0);
            WARNINGS.with(|warnings| assert!(warnings.borrow().is_empty()));
        }
    }
}