tilo-content 0.5.2

Content-addressed binary storage for apps: the explicit, secondary alternative to versioned resources
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
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
//! Content in a local directory, sharded one level on the first two hex digits of the address so
//! no directory accumulates everything the machine has ever stored.

use std::collections::{BTreeMap, BTreeSet, HashMap};
use std::io::{Read, Write};
use std::path::{Path, PathBuf};
use std::sync::Mutex;
#[cfg(test)]
use std::sync::atomic::AtomicUsize;
use std::sync::atomic::{AtomicU64, Ordering};
use std::time::SystemTime;

use crate::memory::check_new_item;
use crate::{
    Collected, ContentBackend, ContentError, ContentLimits, ContentReader, ContentState, Stored,
    Verifying, valid_hash,
};

/// Content in `<root>/<hash[..2]>/<hash>`. Writes stream into a temporary file beside the target
/// and are renamed into place once complete and hashed, so a reader never sees a partial item
/// under an address that promises whole content; reads verify as they stream.
///
/// A durable store (the default) publishes an item behind a barrier: the item is flushed before
/// the rename, and the shard directory after it — and the root too when the write created the
/// shard — so an item `put` has returned for survives a power loss under its address.
/// [`DirBackend::regenerable`] stores skip every flush.
///
/// [`ContentBackend::has`] hashes a file once per file identity (inode, length, timestamps) per
/// backend: a file replaced or rewritten since is verified again. The quota total is kept in
/// memory between writes and rescanned only when another writer has written since (every writer
/// bumps `<root>/.content-store.gen` under the root lock), or before refusing a write, since files
/// removed behind the store's back make the kept total over-count.
pub struct DirBackend {
    root: PathBuf,
    /// Directories consulted, read-only, when content is not under `root` — where an earlier
    /// layout kept it.
    also_read: Vec<PathBuf>,
    limits: ContentLimits,
    durable: bool,
    state: Mutex<Index>,
    verified: Mutex<HashMap<PathBuf, FileSig>>,
    #[cfg(test)]
    probes: Probes,
}

#[derive(Default)]
struct Index {
    bytes: usize,
    entries: BTreeMap<String, usize>,
    /// The root's write generation this index was last brought up to date with, or `None` when it
    /// must be rescanned before it is trusted.
    generation: Option<u64>,
}

/// How many items of one batch are received at once: each waits on the disk,
/// so a folder of small files flushes a few in parallel.
const PUT_MANY_WORKERS: usize = 8;

/// Bumped under the root write lock by every writer after it changes the store, so a backend
/// whose kept index names the current generation knows nobody else has written since. Read and
/// written only under that lock; a missing or unreadable file means "rescan".
const GENERATION_FILE: &str = ".content-store.gen";

/// What identifies one version of a file without reading it: a file renamed over the item, or
/// rewritten in place, differs in at least one of these.
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
struct FileSig {
    len: u64,
    modified: Option<SystemTime>,
    #[cfg(unix)]
    dev: u64,
    #[cfg(unix)]
    ino: u64,
    #[cfg(unix)]
    ctime_ns: i128,
}

impl FileSig {
    fn of(meta: &std::fs::Metadata) -> Self {
        #[cfg(unix)]
        use std::os::unix::fs::MetadataExt;
        Self {
            len: meta.len(),
            modified: meta.modified().ok(),
            #[cfg(unix)]
            dev: meta.dev(),
            #[cfg(unix)]
            ino: meta.ino(),
            #[cfg(unix)]
            ctime_ns: i128::from(meta.ctime())
                .saturating_mul(1_000_000_000)
                .saturating_add(i128::from(meta.ctime_nsec())),
        }
    }
}

#[cfg(test)]
#[derive(Default)]
pub(crate) struct Probes {
    pub(crate) dir_syncs: AtomicUsize,
    pub(crate) file_syncs: AtomicUsize,
}

/// Held around every mutation and the quota scan before it. Separate from [`Index`]: two backends
/// (or two processes) on one root must not each admit a write from a stale local total.
type RootWriteLock = advisory_lock::FileLock;

static TEMP_COUNTER: AtomicU64 = AtomicU64::new(0);

impl DirBackend {
    pub fn new(root: impl Into<PathBuf>) -> Self {
        Self::with_limits(root, ContentLimits::default())
    }

    pub fn with_limits(root: impl Into<PathBuf>, limits: ContentLimits) -> Self {
        Self {
            root: root.into(),
            also_read: Vec::new(),
            limits,
            durable: true,
            state: Mutex::new(Index::default()),
            verified: Mutex::new(HashMap::new()),
            #[cfg(test)]
            probes: Probes::default(),
        }
    }

    #[cfg(test)]
    pub(crate) fn probes(&self) -> &Probes {
        &self.probes
    }

    #[must_use]
    pub fn reading_also(mut self, dir: impl Into<PathBuf>) -> Self {
        self.also_read.push(dir.into());
        self
    }

    /// Hold content its owner can recreate, such as a cache: writes are not flushed to disk, so
    /// a write never waits for the filesystem to flush every other dirty page first. After a
    /// crash an item may be missing or torn; reads verify it against its address, so a torn item
    /// reads as absent.
    #[must_use]
    pub fn regenerable(mut self) -> Self {
        self.durable = false;
        self
    }

    pub fn root(&self) -> &Path {
        &self.root
    }

    pub fn limits(&self) -> ContentLimits {
        self.limits
    }

    fn path_in(root: &Path, hash: &str) -> Option<PathBuf> {
        // The address reaches here from documents and peers: anything but plain hex is refused, since
        // a key with a separator in it would otherwise name a file outside the store.
        if !valid_hash(hash) {
            return None;
        }
        let hash = hash.to_ascii_lowercase();
        let prefix = hash.get(..2)?;
        Some(root.join(prefix).join(&hash))
    }

    fn existing(&self, hash: &str) -> Option<PathBuf> {
        std::iter::once(&self.root)
            .chain(&self.also_read)
            .filter_map(|root| Self::path_in(root, hash))
            .find(|path| path.is_file())
    }

    fn lock_root_for_write(&self) -> Result<RootWriteLock, ContentError> {
        std::fs::create_dir_all(&self.root)?;
        Ok(advisory_lock::FileLock::open(
            &self.root.join(".content-store.lock"),
            advisory_lock::LockMode::Exclusive,
        )?)
    }

    /// The root's write generation. Call only under the root write lock.
    fn read_generation(&self) -> Option<u64> {
        std::fs::read_to_string(self.root.join(GENERATION_FILE))
            .ok()?
            .trim()
            .parse()
            .ok()
    }

    /// Record that this backend changed the store, whose generation was `current` (as read under
    /// the root write lock still held), and bring `index` — already updated for the change — to
    /// the new generation.
    fn bump_generation(&self, index: &mut Index, current: Option<u64>) {
        let next = current.map_or_else(
            || tilo_clock::try_unix_now_ns().unwrap_or(1),
            |current| current.wrapping_add(1),
        );
        let path = self.root.join(GENERATION_FILE);
        if std::fs::write(&path, next.to_string()).is_ok() {
            index.generation = Some(next);
        } else {
            // Without a readable generation every backend rescans, which stays correct. A
            // generation left as it was would let another backend trust an index missing this
            // write.
            let _ = std::fs::remove_file(&path);
            index.generation = None;
        }
    }

    /// Bring `index` up to date for a write: trust it when it names the root's current
    /// generation, otherwise rescan. Call only under the root write lock. Returns the generation
    /// read and whether a scan ran.
    fn refresh_index(&self, index: &mut Index) -> Result<(Option<u64>, bool), ContentError> {
        let current = self.read_generation();
        if current.is_some() && index.generation == current {
            return Ok((current, false));
        }
        self.load_index(index)?;
        index.generation = current;
        Ok((current, true))
    }

    /// Flush `dir`'s entries to disk, for a durable store; a regenerable one never waits on it.
    fn sync_dir(&self, dir: &Path) -> Result<(), ContentError> {
        if !self.durable {
            return Ok(());
        }
        #[cfg(test)]
        self.probes.dir_syncs.fetch_add(1, Ordering::Relaxed);
        tilo_files::native::atomic::sync_directory(dir)?;
        Ok(())
    }

    fn load_index(&self, index: &mut Index) -> Result<(), ContentError> {
        index.generation = None;
        let shards = match std::fs::read_dir(&self.root) {
            Ok(shards) => shards,
            Err(error) if error.kind() == std::io::ErrorKind::NotFound => return Ok(()),
            Err(error) => return Err(error.into()),
        };
        let mut scanned = 0usize;
        let mut entries = BTreeMap::new();
        let mut total = 0usize;
        let mut count = || -> Result<(), ContentError> {
            scanned = scanned.saturating_add(1);
            if scanned > self.limits.max_scan_entries {
                return Err(ContentError::ScanLimitExceeded {
                    limit: self.limits.max_scan_entries,
                });
            }
            Ok(())
        };
        for shard in shards {
            let shard = shard?;
            count()?;
            let name = shard.file_name();
            let Some(prefix) = name.to_str() else {
                continue;
            };
            if prefix.len() != 2
                || !prefix.bytes().all(|b| b.is_ascii_hexdigit())
                || !shard.file_type().is_ok_and(|kind| kind.is_dir())
            {
                continue;
            }
            for item in std::fs::read_dir(shard.path())? {
                let item = item?;
                count()?;
                let name = item.file_name();
                let Some(hash) = name.to_str().filter(|hash| {
                    valid_hash(hash) && !hash.bytes().any(|b| b.is_ascii_uppercase())
                }) else {
                    continue;
                };
                if !item.file_type().is_ok_and(|kind| kind.is_file()) {
                    continue;
                }
                let len = usize::try_from(item.metadata()?.len()).map_err(|_| {
                    ContentError::TotalQuotaExceeded {
                        attempted: usize::MAX,
                        limit: self.limits.max_total_bytes,
                    }
                })?;
                total = total.saturating_add(len);
                entries.insert(hash.to_string(), len);
            }
        }
        index.bytes = total;
        index.entries = entries;
        Ok(())
    }

    /// Receive `reader` into a temporary file under the root, answering its
    /// temporary file, hash and length. `sync` flushes it before answering.
    fn receive(
        &self,
        reader: &mut dyn Read,
        sync: bool,
    ) -> Result<(PathBuf, String, usize), ContentError> {
        std::fs::create_dir_all(&self.root)?;
        let serial = TEMP_COUNTER.fetch_add(1, Ordering::Relaxed);
        let tmp = self
            .root
            .join(format!(".incoming-{}-{serial}", std::process::id()));
        let result = (|| -> Result<(String, usize), ContentError> {
            let mut file = std::fs::OpenOptions::new()
                .write(true)
                .create_new(true)
                .open(&tmp)?;
            let mut hasher = blake3::Hasher::new();
            let mut len = 0usize;
            let mut buf = vec![0u8; 64 * 1024];
            loop {
                let n = match reader.read(&mut buf) {
                    Ok(0) => break,
                    Ok(n) => n,
                    Err(error) if error.kind() == std::io::ErrorKind::Interrupted => continue,
                    Err(error) => return Err(error.into()),
                };
                len = len.saturating_add(n);
                if len > self.limits.max_item_bytes {
                    return Err(ContentError::ItemTooLarge {
                        actual: len,
                        limit: self.limits.max_item_bytes,
                    });
                }
                let chunk = buf.get(..n).unwrap_or_default();
                hasher.update(chunk);
                file.write_all(chunk)?;
            }
            if sync {
                file.sync_all()?;
                #[cfg(test)]
                self.probes.file_syncs.fetch_add(1, Ordering::Relaxed);
            }
            Ok((hasher.finalize().to_hex().to_string(), len))
        })();
        match result {
            Ok((hash, len)) => Ok((tmp, hash, len)),
            Err(error) => {
                let _ = std::fs::remove_file(&tmp);
                Err(error)
            }
        }
    }

    /// Whether `path` holds the content at `hash`, no larger than `limit`. A file this backend has
    /// already found intact, and which has not changed since, is not read again.
    fn intact(&self, path: &Path, hash: &str, limit: usize) -> bool {
        let Ok(meta) = std::fs::metadata(path) else {
            return false;
        };
        if !meta.is_file()
            || usize::try_from(meta.len())
                .ok()
                .is_none_or(|len| len > limit)
        {
            return false;
        }
        if self
            .verified
            .lock()
            .is_ok_and(|verified| verified.get(path) == Some(&FileSig::of(&meta)))
        {
            return true;
        }
        let Ok(file) = std::fs::File::open(path) else {
            return false;
        };
        // The identity of the file actually hashed, taken before reading it: a change while it
        // is read leaves a different identity behind, which the next call verifies afresh.
        let Ok(meta) = file.metadata() else {
            return false;
        };
        if usize::try_from(meta.len())
            .ok()
            .is_none_or(|len| len > limit)
        {
            return false;
        }
        let mut reader = Verifying::new(file, hash);
        if std::io::copy(&mut reader, &mut std::io::sink()).is_err() {
            return false;
        }
        self.remember_intact(path.to_path_buf(), FileSig::of(&meta));
        true
    }

    fn remember_intact(&self, path: PathBuf, sig: FileSig) {
        if let Ok(mut verified) = self.verified.lock() {
            verified.insert(path, sig);
        }
    }

    fn forget_intact(&self, path: &Path) {
        if let Ok(mut verified) = self.verified.lock() {
            verified.remove(path);
        }
    }

    /// Admit the received items of [`ContentBackend::put_many`] under one
    /// lock: the index, its generation and each shard directory are written
    /// once for the whole set instead of once per item.
    fn admit_received(
        &self,
        outcomes: &mut [Option<Result<Stored, ContentError>>],
        received: &[(usize, PathBuf, String, usize)],
        flush: BatchFlush,
    ) -> Result<(), String> {
        if received.is_empty() {
            return Ok(());
        }
        let mut index = self
            .state
            .lock()
            .map_err(|_| "content store lock poisoned".to_string())?;
        let root_lock = self
            .lock_root_for_write()
            .map_err(|error| error.to_string())?;
        let (generation, _) = self
            .refresh_index(&mut index)
            .map_err(|error| error.to_string())?;
        let mut shards: BTreeSet<PathBuf> = BTreeSet::new();
        let mut wrote = false;
        for (at, tmp, hash, len) in received {
            let stored = Stored {
                hash: hash.clone(),
                len: u64::try_from(*len).unwrap_or(u64::MAX),
            };
            let Some(path) = Self::path_in(&self.root, hash) else {
                let _ = std::fs::remove_file(tmp);
                note(outcomes, *at, Err(ContentError::InvalidHash(hash.clone())));
                continue;
            };
            if self.intact(&path, hash, usize::MAX) {
                self.written_now(&path);
                let _ = std::fs::remove_file(tmp);
                note(outcomes, *at, Ok(stored));
                continue;
            }
            if check_admission(&self.limits, &index, hash, *len).is_err() {
                // Files removed behind the store's back make the kept total
                // over-count: look again before refusing on it.
                self.load_index(&mut index)
                    .map_err(|error| error.to_string())?;
                if let Err(refusal) = check_admission(&self.limits, &index, hash, *len) {
                    let _ = std::fs::remove_file(tmp);
                    note(outcomes, *at, Err(refusal));
                    continue;
                }
            }
            let Some(dir) = path.parent() else {
                let _ = std::fs::remove_file(tmp);
                note(outcomes, *at, Err(ContentError::InvalidHash(hash.clone())));
                continue;
            };
            let dir = dir.to_path_buf();
            if !dir.is_dir() {
                if let Err(error) = std::fs::create_dir_all(&dir) {
                    let _ = std::fs::remove_file(tmp);
                    note(outcomes, *at, Err(error.into()));
                    continue;
                }
                if flush == BatchFlush::Each {
                    let _ = self.sync_dir(&self.root);
                }
            }
            if let Err(error) = std::fs::rename(tmp, &path) {
                let _ = std::fs::remove_file(tmp);
                note(outcomes, *at, Err(error.into()));
                continue;
            }
            if let Some(old_len) = index.entries.insert(hash.clone(), *len) {
                index.bytes = index.bytes.saturating_sub(old_len);
            }
            index.bytes = index.bytes.saturating_add(*len);
            match std::fs::metadata(&path) {
                Ok(meta) => self.remember_intact(path.clone(), FileSig::of(&meta)),
                Err(_) => self.forget_intact(&path),
            }
            shards.insert(dir);
            wrote = true;
            note(outcomes, *at, Ok(stored));
        }
        if wrote {
            self.bump_generation(&mut index, generation);
        }
        drop(root_lock);
        drop(index);
        if flush == BatchFlush::Each {
            self.sync_dirs(shards);
        }
        Ok(())
    }

    /// Flush everything this backend wrote under its root in one call: the batch's items, their
    /// renames and their directories. The platform flushes a filesystem; elsewhere the items are
    /// flushed one by one instead ([`BatchFlush::Each`]).
    #[cfg(target_os = "linux")]
    fn sync_store(&self) -> Result<(), String> {
        let root = std::fs::File::open(&self.root).map_err(|error| error.to_string())?;
        rustix::fs::syncfs(&root).map_err(|error| error.to_string())?;
        Ok(())
    }

    /// Flush the shard directories a batch was renamed into: a few at once,
    /// since a batch spreads over many shards and each flush waits on the
    /// disk.
    fn sync_dirs(&self, shards: BTreeSet<PathBuf>) {
        if shards.is_empty() || !self.durable {
            return;
        }
        let dirs: Vec<PathBuf> = shards.into_iter().collect();
        let workers = PUT_MANY_WORKERS.min(dirs.len()).max(1);
        let per = dirs.len().div_ceil(workers);
        std::thread::scope(|scope| {
            for chunk in dirs.chunks(per) {
                scope.spawn(move || {
                    for dir in chunk {
                        let _ = self.sync_dir(dir);
                    }
                });
            }
        });
    }
}

/// How a batch's items reach the disk.
#[derive(Clone, Copy, PartialEq, Eq)]
enum BatchFlush {
    /// The platform flushes the whole filesystem once, after the batch is in place: nothing is
    /// answered for until it returns.
    Whole,
    /// Each item is flushed as it is received, and the directories it was renamed into after:
    /// where the platform has no filesystem-wide flush.
    Each,
    /// Every flush is skipped: a store that can regenerate what it keeps.
    None,
}

/// Mark item `index`'s outcome of [`DirBackend::admit_received`].
fn note(
    outcomes: &mut [Option<Result<Stored, ContentError>>],
    index: usize,
    result: Result<Stored, ContentError>,
) {
    if let Some(outcome) = outcomes.get_mut(index) {
        *outcome = Some(result);
    }
}

impl ContentBackend for DirBackend {
    fn put(&self, reader: &mut dyn Read, _mime: &str) -> Result<Stored, ContentError> {
        // Flushed only once it is known to be new: content already here, as most of what a
        // warm start puts again, costs no wait on the disk.
        let (tmp, hash, len) = self.receive(reader, false)?;
        let stored = Stored {
            hash: hash.clone(),
            len: u64::try_from(len).unwrap_or(u64::MAX),
        };
        let admit = || -> Result<(), ContentError> {
            let Some(path) = Self::path_in(&self.root, &hash) else {
                return Err(ContentError::InvalidHash(hash.clone()));
            };
            let mut index = self
                .state
                .lock()
                .map_err(|_| ContentError::Io("content store lock poisoned".into()))?;
            let root_lock = self.lock_root_for_write()?;
            // Content already here is an idempotent write, even if a later configuration tightened
            // the limits. It is written again just now, as far as a collection that spares what
            // was written lately goes: under the lock, so no collection removes it in between.
            if self.intact(&path, &hash, usize::MAX) {
                self.written_now(&path);
                return Ok(());
            }
            // The kept index is authority for admission only while no other writer has written
            // since: every writer bumps the generation under this lock.
            let (generation, scanned) = self.refresh_index(&mut index)?;
            if let Err(refusal) = check_admission(&self.limits, &index, &hash, len) {
                // Files removed behind the store's back make a kept total over-count: look again
                // before refusing on it.
                if scanned {
                    return Err(refusal);
                }
                self.load_index(&mut index)?;
                index.generation = generation;
                check_admission(&self.limits, &index, &hash, len)?;
            }
            let Some(dir) = path.parent() else {
                return Err(ContentError::InvalidHash(hash.clone()));
            };
            let created_shard = !dir.is_dir();
            std::fs::create_dir_all(dir)?;
            if created_shard {
                self.sync_dir(&self.root)?;
            }
            if self.durable {
                // Write access, which some platforms need to flush a file.
                std::fs::OpenOptions::new()
                    .write(true)
                    .open(&tmp)?
                    .sync_all()?;
                #[cfg(test)]
                self.probes.file_syncs.fetch_add(1, Ordering::Relaxed);
            }
            std::fs::rename(&tmp, &path)?;
            // A file under this name that failed verification above was not retained content;
            // the rename replaced it, and with it the quota it held.
            if let Some(old_len) = index.entries.insert(hash.clone(), len) {
                index.bytes = index.bytes.saturating_sub(old_len);
            }
            index.bytes = index.bytes.saturating_add(len);
            self.bump_generation(&mut index, generation);
            match std::fs::metadata(&path) {
                Ok(meta) => self.remember_intact(path.clone(), FileSig::of(&meta)),
                Err(_) => self.forget_intact(&path),
            }
            self.sync_dir(dir)?;
            drop(root_lock);
            drop(index);
            Ok(())
        };
        let result = admit();
        let _ = std::fs::remove_file(&tmp);
        result.map(|()| stored)
    }

    /// Store `items` together: each is received and flushed as [`Self::put`]
    /// does, and the index, its generation and each shard directory are
    /// written once for the whole set instead of once per item.
    fn put_many(&self, items: &[(&[u8], &str)]) -> Vec<Result<Stored, ContentError>> {
        let started = tilo_clock::now();
        let mut outcomes: Vec<Option<Result<Stored, ContentError>>> =
            (0..items.len()).map(|_| None).collect();
        let mut received: Vec<(usize, PathBuf, String, usize)> = Vec::new();
        // Where the platform flushes a filesystem, the batch's items are written whole and
        // flushed together after they are renamed; elsewhere each item waits on its own flush.
        let flush = if self.durable {
            if cfg!(target_os = "linux") {
                BatchFlush::Whole
            } else {
                BatchFlush::Each
            }
        } else {
            BatchFlush::None
        };
        // The items are written a few at a time: each waits on the disk.
        let workers = PUT_MANY_WORKERS.min(items.len()).max(1);
        let per = items.len().div_ceil(workers);
        std::thread::scope(|scope| {
            let mut handles = Vec::new();
            for (part, chunk) in items.chunks(per).enumerate() {
                let start = part.saturating_mul(per);
                handles.push(scope.spawn(move || {
                    let mut done = Vec::new();
                    for (offset, (bytes, _mime)) in chunk.iter().enumerate() {
                        let index = start.saturating_add(offset);
                        let entry = self
                            .receive(&mut &bytes[..], flush == BatchFlush::Each)
                            .map_err(|error| (index, error));
                        done.push((index, entry));
                    }
                    done
                }));
            }
            for handle in handles {
                let Ok(done) = handle.join() else {
                    continue;
                };
                for (index, entry) in done {
                    match entry {
                        Ok((tmp, hash, len)) => received.push((index, tmp, hash, len)),
                        Err((index, error)) => note(&mut outcomes, index, Err(error)),
                    }
                }
            }
        });
        let received_ms = started.elapsed().as_millis();
        let admitted = self.admit_received(&mut outcomes, &received, flush);
        let admitted = if admitted.is_ok() && flush == BatchFlush::Whole {
            // Nothing is answered for until the store's filesystem has written the batch: its
            // items, their renames and their directories.
            #[cfg(target_os = "linux")]
            {
                self.sync_store()
            }
            #[cfg(not(target_os = "linux"))]
            {
                Ok(())
            }
        } else {
            admitted
        };
        tracing::debug!(
            items = items.len(),
            received_ms,
            admitted_ms = started.elapsed().as_millis().saturating_sub(received_ms),
            total_ms = started.elapsed().as_millis(),
            "content: a batch was written"
        );
        match admitted {
            Ok(()) => {}
            Err(why) => {
                for (index, tmp, _, _) in &received {
                    if outcomes.get(*index).is_some_and(Option::is_none) {
                        note(&mut outcomes, *index, Err(ContentError::Io(why.clone())));
                    }
                    let _ = std::fs::remove_file(tmp);
                }
            }
        }
        outcomes
            .into_iter()
            .map(|outcome| {
                outcome.unwrap_or_else(|| Err(ContentError::Io("no item was received".into())))
            })
            .collect()
    }

    fn open(&self, hash: &str) -> Result<Option<ContentReader>, ContentError> {
        if !valid_hash(hash) {
            return Err(ContentError::InvalidHash(hash.to_string()));
        }
        let Some(path) = self.existing(hash) else {
            return Ok(None);
        };
        let file = match std::fs::File::open(&path) {
            Ok(file) => file,
            Err(error) if error.kind() == std::io::ErrorKind::NotFound => return Ok(None),
            Err(error) => return Err(error.into()),
        };
        let declared = usize::try_from(file.metadata()?.len()).unwrap_or(usize::MAX);
        // Never stream an arbitrarily large file merely because a peer named its address.
        if declared > self.limits.max_item_bytes {
            return Ok(None);
        }
        let cap = u64::try_from(self.limits.max_item_bytes)
            .unwrap_or(u64::MAX)
            .saturating_add(1);
        Ok(Some(Box::new(Verifying::new(file.take(cap), hash))))
    }

    fn open_at(&self, hash: &str, offset: u64) -> Result<Option<ContentReader>, ContentError> {
        use std::io::{Seek as _, SeekFrom};
        if !valid_hash(hash) {
            return Err(ContentError::InvalidHash(hash.to_string()));
        }
        let Some(path) = self.existing(hash) else {
            return Ok(None);
        };
        let mut file = match std::fs::File::open(&path) {
            Ok(file) => file,
            Err(error) if error.kind() == std::io::ErrorKind::NotFound => return Ok(None),
            Err(error) => return Err(error.into()),
        };
        file.seek(SeekFrom::Start(offset))?;
        Ok(Some(Box::new(file)))
    }

    fn has(&self, hash: &str) -> bool {
        self.existing(hash)
            .is_some_and(|path| self.intact(&path, hash, self.limits.max_item_bytes))
    }

    fn size(&self, hash: &str) -> Option<u64> {
        let path = Self::path_in(&self.root, hash)?;
        std::fs::metadata(path).ok().map(|metadata| metadata.len())
    }

    fn state(&self, hash: &str) -> ContentState {
        if self.has(hash) {
            ContentState::Resolved
        } else {
            ContentState::Unavailable
        }
    }

    fn collect_unreferenced(&self, live: &BTreeSet<String>) -> Result<Collected, ContentError> {
        self.remove_where(|hash| !live.contains(hash))
    }

    fn release(&self, hash: &str) -> Result<Collected, ContentError> {
        self.remove_where(|kept| kept == hash)
    }

    fn max_item_bytes(&self) -> usize {
        self.limits.max_item_bytes
    }

    fn scratch_dir(&self) -> Option<PathBuf> {
        Some(self.root.clone())
    }
}

impl DirBackend {
    pub(crate) fn written_before(&self, hash: &str, before: SystemTime) -> bool {
        Self::path_in(&self.root, hash)
            .and_then(|path| std::fs::metadata(path).ok()?.modified().ok())
            .is_some_and(|written| written < before)
    }

    /// Remove every item not in `live` that was last written before `before`: what a store
    /// collects when an item may be referenced only after it is written (an upload a record
    /// names once it is kept). Writing an item that is already here counts as writing it.
    ///
    /// # Errors
    ///
    /// The store could not be read or changed.
    pub fn collect_unreferenced_before(
        &self,
        live: &BTreeSet<String>,
        before: SystemTime,
    ) -> Result<Collected, ContentError> {
        self.remove_where(|hash| !live.contains(hash) && self.written_before(hash, before))
    }

    fn written_now(&self, path: &Path) {
        let touched = std::fs::File::options()
            .append(true)
            .open(path)
            .and_then(|file| {
                file.set_modified(tilo_clock::to_std(tilo_clock::wall()))?;
                file.metadata()
            });
        match touched {
            Ok(meta) => self.remember_intact(path.to_path_buf(), FileSig::of(&meta)),
            Err(_) => self.forget_intact(path),
        }
    }

    fn remove_where(&self, gone: impl Fn(&str) -> bool) -> Result<Collected, ContentError> {
        let mut index = self
            .state
            .lock()
            .map_err(|_| ContentError::Io("content store lock poisoned".into()))?;
        let root_lock = self.lock_root_for_write()?;
        let generation = self.read_generation();
        self.load_index(&mut index)?;
        let stale: Vec<(String, usize)> = index
            .entries
            .iter()
            .filter(|(hash, _)| gone(hash))
            .map(|(hash, len)| (hash.clone(), *len))
            .collect();
        let mut collected = Collected::default();
        for (hash, len) in stale {
            let Some(path) = Self::path_in(&self.root, &hash) else {
                continue;
            };
            self.forget_intact(&path);
            match std::fs::remove_file(&path) {
                Ok(()) => {}
                Err(error) if error.kind() == std::io::ErrorKind::NotFound => {}
                Err(error) => {
                    // What was removed before the failure is a change other writers must see.
                    self.bump_generation(&mut index, generation);
                    return Err(error.into());
                }
            }
            index.entries.remove(&hash);
            index.bytes = index.bytes.saturating_sub(len);
            collected.bytes = collected
                .bytes
                .saturating_add(u64::try_from(len).unwrap_or(u64::MAX));
            collected.hashes.push(hash);
        }
        self.bump_generation(&mut index, generation);
        drop(root_lock);
        drop(index);
        Ok(collected)
    }
}

/// Admit `len` new bytes under `hash` against `index`'s total. A file already under the name that
/// failed verification is not retained content and must not count against the write that
/// replaces it.
fn check_admission(
    limits: &ContentLimits,
    index: &Index,
    hash: &str,
    len: usize,
) -> Result<(), ContentError> {
    let (bytes, entries) = match index.entries.get(hash) {
        Some(old_len) => (
            index.bytes.saturating_sub(*old_len),
            index.entries.len().saturating_sub(1),
        ),
        None => (index.bytes, index.entries.len()),
    };
    check_new_item(limits, bytes, entries, len)
}