libtea-client 0.1.9

Independent async Rust client for the CycloneDX Transparency Exchange API.
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
//! Opt-in persistent HTTP cache (`docs/cache.md`).
//!
//! *Experimental until 0.9.* Enabled with [`crate::ClientBuilder::cache_dir`] or
//! [`crate::ClientBuilder::cache`]. Off by default.
//!
//! What it is: a per-user directory of response bodies, used to send
//! conditional `GET`s (`ETag` / `Last-Modified`) and, with
//! [`crate::ClientBuilder::offline`], to answer without the network.
//!
//! What it is not: a trust boundary. An entry is checked for truncation and
//! corruption (length and SHA-256), but anyone who can write the directory can
//! write a consistent entry. So the directory is created private (mode 0700,
//! files 0600 on Unix), a symbolic link or a directory open to other users is
//! refused, artifact bytes are checked against their declared checksums every
//! time they are used, and an entry that fails any check is treated as absent
//! and removed. Writes are atomic (temporary file, then rename) but are not
//! `fsync`ed; a crash can lose an entry, never leave a half-written one in use.

use std::io::Write;
use std::path::{Path, PathBuf};
use std::sync::atomic::{AtomicU64, Ordering};
use std::sync::Arc;
use std::time::{Duration, SystemTime, UNIX_EPOCH};

use libtea_core::escape_control_chars;
use libtea_core::http_cache::{
    decode_entry, encode_entry, is_cache_key, CacheEntryMeta, EntryBuilder,
};
use tokio::io::AsyncWriteExt;

use crate::error::ClientError;

/// Largest metadata block plus framing an entry file adds to its body.
const FRAMING_SLACK: u64 = 80 * 1024;
const ENTRY_DIR: &str = "v1";

/// Cache settings. Defaults are conservative; every limit is a ceiling.
///
/// *Experimental.*
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub struct CacheConfig {
    /// The cache directory. Created private if missing.
    pub dir: PathBuf,
    /// Total bytes of entries kept; the oldest are removed past this.
    pub max_total_bytes: u64,
    /// Largest body stored. A larger response is simply not cached.
    pub max_entry_bytes: u64,
    /// Oldest an entry may be, counted from its last successful revalidation.
    /// An older entry is treated as absent, offline mode included.
    pub max_age: Duration,
    /// Also store responses to requests that carried credentials, and
    /// `Cache-Control: private` responses. Off by default.
    pub authenticated: bool,
}

impl CacheConfig {
    /// Defaults: 512 MiB total, 64 MiB per entry, 7 days, no authenticated
    /// responses.
    #[must_use]
    pub fn new(dir: impl Into<PathBuf>) -> Self {
        Self {
            dir: dir.into(),
            max_total_bytes: 512 * 1024 * 1024,
            max_entry_bytes: 64 * 1024 * 1024,
            max_age: Duration::from_secs(7 * 24 * 60 * 60),
            authenticated: false,
        }
    }

    /// Set [`Self::max_total_bytes`].
    #[must_use]
    pub fn with_max_total_bytes(mut self, value: u64) -> Self {
        self.max_total_bytes = value;
        self
    }

    /// Set [`Self::max_entry_bytes`].
    #[must_use]
    pub fn with_max_entry_bytes(mut self, value: u64) -> Self {
        self.max_entry_bytes = value;
        self
    }

    /// Set [`Self::max_age`].
    #[must_use]
    pub fn with_max_age(mut self, value: Duration) -> Self {
        self.max_age = value;
        self
    }

    /// Set [`Self::authenticated`].
    #[must_use]
    pub fn with_authenticated(mut self, value: bool) -> Self {
        self.authenticated = value;
        self
    }
}

pub(crate) fn now_secs() -> u64 {
    SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .map_or(0, |elapsed| elapsed.as_secs())
}

fn io_error(what: &str, err: &std::io::Error) -> ClientError {
    ClientError::Io(escape_control_chars(&format!("{what}: {}", err.kind())))
}

fn refuse(path: &Path, why: &str) -> ClientError {
    ClientError::Io(escape_control_chars(&format!(
        "cache directory {}: {why}",
        path.display()
    )))
}

/// Check (and optionally create) a directory the cache will use. Returns
/// whether it exists. Refuses a symbolic link, a non-directory, and on Unix a
/// directory that group or others can access.
fn private_dir(path: &Path, create: bool) -> Result<bool, ClientError> {
    match std::fs::symlink_metadata(path) {
        Ok(meta) if meta.file_type().is_symlink() => {
            Err(refuse(path, "is a symbolic link; refusing to use it"))
        }
        Ok(meta) if !meta.is_dir() => Err(refuse(path, "is not a directory")),
        Ok(meta) => {
            #[cfg(unix)]
            {
                use std::os::unix::fs::{MetadataExt, PermissionsExt};
                // A private directory that someone else owns is theirs to
                // change; refuse it. The reference uid is that of a file this
                // process has just created, so no uid lookup is needed.
                if !owner_matches(meta.uid(), current_uid(Some(path))?) {
                    return Err(refuse(
                        path,
                        "is owned by another user; use a directory you own",
                    ));
                }
                let mode = meta.permissions().mode() & 0o777;
                if mode & 0o077 != 0 {
                    return Err(refuse(
                        path,
                        &format!(
                            "is accessible by other users (mode {mode:o}); run chmod 700 on it"
                        ),
                    ));
                }
            }
            #[cfg(not(unix))]
            let _ = meta;
            Ok(true)
        }
        Err(err) if err.kind() == std::io::ErrorKind::NotFound && create => {
            let mut builder = std::fs::DirBuilder::new();
            builder.recursive(true);
            #[cfg(unix)]
            {
                use std::os::unix::fs::DirBuilderExt;
                builder.mode(0o700);
            }
            builder
                .create(path)
                .map_err(|e| io_error("creating the cache directory", &e))?;
            // Look again: what we created must be what we meant to create.
            private_dir(path, false)
        }
        Err(err) if err.kind() == std::io::ErrorKind::NotFound => Ok(false),
        Err(err) => Err(io_error("inspecting the cache directory", &err)),
    }
}

/// Whether a directory owned by `dir_uid` belongs to the user running the
/// process, whose files get `own_uid`.
#[cfg(unix)]
pub(crate) fn owner_matches(dir_uid: u32, own_uid: u32) -> bool {
    dir_uid == own_uid
}

/// The uid this process creates files as: the owner of a file created now,
/// in the temporary directory or, if that is not writable, in `fallback`.
#[cfg(unix)]
pub(crate) fn current_uid(fallback: Option<&Path>) -> Result<u32, ClientError> {
    use std::os::unix::fs::MetadataExt;
    static COUNTER: AtomicU64 = AtomicU64::new(0);
    let name = format!(
        ".libtea-uid-{}-{}",
        std::process::id(),
        COUNTER.fetch_add(1, Ordering::Relaxed)
    );
    let mut last = None;
    for dir in std::iter::once(std::env::temp_dir()).chain(fallback.map(Path::to_path_buf)) {
        let probe = dir.join(&name);
        match open_private(&probe) {
            Ok(file) => {
                let uid = file
                    .metadata()
                    .map_err(|e| io_error("checking directory ownership", &e))?
                    .uid();
                drop(file);
                let _ = std::fs::remove_file(&probe);
                return Ok(uid);
            }
            Err(err) => last = Some(err),
        }
    }
    Err(match last {
        Some(err) => io_error("checking directory ownership", &err),
        None => ClientError::Io("checking directory ownership".into()),
    })
}

pub(crate) struct Cache {
    pub(crate) config: CacheConfig,
    entries: PathBuf,
    pub(crate) offline: bool,
}

/// Numbers temporary files. One per process, not per [`Cache`]: several clients
/// in one process can share a directory, and a per-cache counter gave each of
/// them the same names.
static TEMP_COUNTER: AtomicU64 = AtomicU64::new(0);

impl Cache {
    pub(crate) fn open(config: CacheConfig, offline: bool) -> Result<Self, ClientError> {
        private_dir(&config.dir, true)?;
        let entries = config.dir.join(ENTRY_DIR);
        private_dir(&entries, true)?;
        Ok(Self {
            config,
            entries,
            offline,
        })
    }

    fn entry_path(&self, key: &str) -> PathBuf {
        debug_assert!(is_cache_key(key));
        self.entries.join(format!("{key}.entry"))
    }

    fn temp_path(&self, key: &str) -> PathBuf {
        self.entries.join(format!(
            ".{key}.{}-{}.tmp",
            std::process::id(),
            TEMP_COUNTER.fetch_add(1, Ordering::Relaxed)
        ))
    }

    /// Read and check one entry; anything wrong removes it and returns `None`.
    pub(crate) fn load_blocking(&self, key: &str) -> Option<(CacheEntryMeta, Vec<u8>)> {
        if !is_cache_key(key) {
            return None;
        }
        let path = self.entry_path(key);
        let meta = std::fs::symlink_metadata(&path).ok()?;
        let remove = |path: &Path| {
            let _ = std::fs::remove_file(path);
        };
        if !meta.file_type().is_file() || meta.len() > self.config.max_entry_bytes + FRAMING_SLACK {
            remove(&path);
            return None;
        }
        let bytes = read_regular(&path, self.config.max_entry_bytes + FRAMING_SLACK).ok()?;
        let max_body = usize::try_from(self.config.max_entry_bytes).unwrap_or(usize::MAX);
        match decode_entry(&bytes, key, max_body) {
            Ok((entry, body)) => {
                let now = now_secs();
                let age = now.saturating_sub(entry.validated_at);
                if entry.validated_at > now.saturating_add(24 * 60 * 60)
                    || age > self.config.max_age.as_secs()
                {
                    remove(&path);
                    return None;
                }
                Some((entry, body))
            }
            Err(error) => {
                tracing::debug!(%error, "cache entry refused and removed");
                remove(&path);
                None
            }
        }
    }

    pub(crate) async fn load(self: &Arc<Self>, key: &str) -> Option<(CacheEntryMeta, Vec<u8>)> {
        let cache = Arc::clone(self);
        let key = key.to_owned();
        tokio::task::spawn_blocking(move || cache.load_blocking(&key))
            .await
            .ok()
            .flatten()
    }

    /// Store one entry atomically. Never fails the caller: a problem is logged.
    pub(crate) fn store_blocking(&self, key: &str, meta: CacheEntryMeta, body: &[u8]) {
        if !is_cache_key(key) || body.len() as u64 > self.config.max_entry_bytes {
            return;
        }
        let Ok(bytes) = encode_entry(meta, body) else {
            return;
        };
        match write_entry_file(&self.temp_path(key), &bytes, &self.entry_path(key)) {
            Ok(()) => self.prune_blocking(),
            Err(error) => tracing::debug!(kind = ?error.kind(), "cache entry not written"),
        }
    }

    pub(crate) async fn store(self: &Arc<Self>, key: &str, meta: CacheEntryMeta, body: Vec<u8>) {
        let cache = Arc::clone(self);
        let key = key.to_owned();
        let _ = tokio::task::spawn_blocking(move || cache.store_blocking(&key, meta, &body)).await;
    }

    pub(crate) fn remove_blocking(&self, key: &str) {
        if is_cache_key(key) {
            let _ = std::fs::remove_file(self.entry_path(key));
        }
    }

    pub(crate) async fn remove(self: &Arc<Self>, key: &str) {
        let cache = Arc::clone(self);
        let key = key.to_owned();
        let _ = tokio::task::spawn_blocking(move || cache.remove_blocking(&key)).await;
    }

    /// Drop the oldest entries until the total is within the limit, and stale
    /// temporary files left by a crash.
    pub(crate) fn prune_blocking(&self) {
        let Ok(dir) = std::fs::read_dir(&self.entries) else {
            return;
        };
        let now = SystemTime::now();
        let mut entries: Vec<(SystemTime, u64, PathBuf)> = Vec::new();
        let mut total = 0u64;
        for item in dir.flatten() {
            let path = item.path();
            let Ok(meta) = std::fs::symlink_metadata(&path) else {
                continue;
            };
            let name = item.file_name();
            let name = name.to_string_lossy();
            if is_cache_temp(&name) {
                let old = meta
                    .modified()
                    .ok()
                    .and_then(|m| now.duration_since(m).ok())
                    .is_some_and(|age| age > Duration::from_secs(3600));
                if old {
                    let _ = std::fs::remove_file(&path);
                }
                continue;
            }
            if !meta.file_type().is_file() || !name.ends_with(".entry") {
                continue;
            }
            total = total.saturating_add(meta.len());
            entries.push((meta.modified().unwrap_or(UNIX_EPOCH), meta.len(), path));
        }
        if total <= self.config.max_total_bytes {
            return;
        }
        entries.sort_by(|a, b| a.0.cmp(&b.0).then_with(|| a.2.cmp(&b.2)));
        for (_, size, path) in entries {
            if total <= self.config.max_total_bytes {
                break;
            }
            if std::fs::remove_file(&path).is_ok() {
                total = total.saturating_sub(size);
            }
        }
    }

    /// Begin writing an entry whose body is still arriving. `None` when the
    /// temporary file cannot be created.
    pub(crate) async fn start_tee(self: &Arc<Self>, key: &str) -> Option<CacheTee> {
        if !is_cache_key(key) {
            return None;
        }
        let tmp = self.temp_path(key);
        let mut options = tokio::fs::OpenOptions::new();
        options.write(true).create_new(true);
        #[cfg(unix)]
        options.mode(0o600);
        let mut file = options.open(&tmp).await.ok()?;
        if file.write_all(EntryBuilder::prefix()).await.is_err() {
            let _ = std::fs::remove_file(&tmp);
            return None;
        }
        Some(CacheTee {
            cache: Arc::clone(self),
            key: key.to_owned(),
            file: Some(file),
            tmp,
            builder: EntryBuilder::new(),
        })
    }
}

/// Open `path` only if it is a regular file, and keep that true while opening.
///
/// The path is inspected with `lstat`, opened, and inspected again; the opened
/// file must be the regular file the path names both times (same device and
/// inode). A symbolic link swapped in between the checks is refused instead of
/// followed, which `lstat`-then-read alone would not do. `between` runs after
/// the first inspection and exists so a test can make that swap happen.
#[cfg(unix)]
fn open_regular_with(path: &Path, between: impl FnOnce()) -> std::io::Result<std::fs::File> {
    use std::os::unix::fs::MetadataExt;
    let invalid =
        |what: &str| std::io::Error::new(std::io::ErrorKind::InvalidInput, what.to_owned());
    let before = std::fs::symlink_metadata(path)?;
    if !before.file_type().is_file() {
        return Err(invalid("not a regular file"));
    }
    between();
    let file = std::fs::File::open(path)?;
    let opened = file.metadata()?;
    let after = std::fs::symlink_metadata(path)?;
    if !after.file_type().is_file()
        || !opened.is_file()
        || (opened.dev(), opened.ino()) != (after.dev(), after.ino())
        || (opened.dev(), opened.ino()) != (before.dev(), before.ino())
    {
        return Err(invalid("the path changed while it was opened"));
    }
    Ok(file)
}

/// Read at most `limit` bytes of a regular file (see [`open_regular_with`]);
/// a longer file is an error.
fn read_regular(path: &Path, limit: u64) -> std::io::Result<Vec<u8>> {
    use std::io::Read;
    #[cfg(unix)]
    let file = open_regular_with(path, || {})?;
    #[cfg(not(unix))]
    let file = {
        if !std::fs::symlink_metadata(path)?.file_type().is_file() {
            return Err(std::io::Error::new(
                std::io::ErrorKind::InvalidInput,
                "not a regular file",
            ));
        }
        std::fs::File::open(path)?
    };
    let mut bytes = Vec::new();
    file.take(limit + 1).read_to_end(&mut bytes)?;
    if bytes.len() as u64 > limit {
        return Err(std::io::Error::new(
            std::io::ErrorKind::InvalidData,
            "entry is larger than the limit",
        ));
    }
    Ok(bytes)
}

/// Write `bytes` to the new file `tmp`, then rename it to `dest`. On failure
/// `tmp` is removed only if this call created it: a file already at that path
/// belongs to someone else.
fn write_entry_file(tmp: &Path, bytes: &[u8], dest: &Path) -> std::io::Result<()> {
    let mut file = open_private(tmp)?;
    let result = file.write_all(bytes).and_then(|()| {
        drop(file);
        std::fs::rename(tmp, dest)
    });
    if result.is_err() {
        let _ = std::fs::remove_file(tmp);
    }
    result
}

fn open_private(path: &Path) -> std::io::Result<std::fs::File> {
    let mut options = std::fs::OpenOptions::new();
    options.write(true).create_new(true);
    #[cfg(unix)]
    {
        use std::os::unix::fs::OpenOptionsExt;
        options.mode(0o600);
    }
    options.open(path)
}

/// An entry being written while the body streams past. The file is removed
/// unless [`CacheTee::finish`] commits it, so a failed or cancelled download
/// leaves nothing behind.
pub(crate) struct CacheTee {
    cache: Arc<Cache>,
    key: String,
    file: Option<tokio::fs::File>,
    tmp: PathBuf,
    builder: EntryBuilder,
}

impl CacheTee {
    /// Add body bytes. Returns `false` once the entry is abandoned (too large
    /// or a write failed); later calls are no-ops.
    pub(crate) async fn write(&mut self, chunk: &[u8]) -> bool {
        let Some(file) = self.file.as_mut() else {
            return false;
        };
        if self.builder.len().saturating_add(chunk.len() as u64) > self.cache.config.max_entry_bytes
            || file.write_all(chunk).await.is_err()
        {
            self.abandon();
            return false;
        }
        self.builder.update(chunk);
        true
    }

    fn abandon(&mut self) {
        self.file = None;
        let _ = std::fs::remove_file(&self.tmp);
    }

    /// Commit the entry. Call only after the body passed every check.
    pub(crate) async fn finish(mut self, meta: CacheEntryMeta) {
        let Some(mut file) = self.file.take() else {
            return;
        };
        let builder = std::mem::take(&mut self.builder);
        let Ok(trailer) = builder.trailer(meta) else {
            self.abandon();
            return;
        };
        if file.write_all(&trailer).await.is_err() || file.flush().await.is_err() {
            drop(file);
            let _ = std::fs::remove_file(&self.tmp);
            return;
        }
        drop(file);
        let cache = Arc::clone(&self.cache);
        let tmp = self.tmp.clone();
        let final_path = cache.entry_path(&self.key);
        let committed = tokio::task::spawn_blocking(move || {
            let ok = std::fs::rename(&tmp, final_path).is_ok();
            if ok {
                cache.prune_blocking();
            } else {
                let _ = std::fs::remove_file(&tmp);
            }
            ok
        })
        .await
        .unwrap_or(false);
        if committed {
            // The temporary file no longer exists; nothing for Drop to remove.
            self.tmp = PathBuf::new();
        }
    }
}

impl Drop for CacheTee {
    fn drop(&mut self) {
        if !self.tmp.as_os_str().is_empty() {
            let _ = std::fs::remove_file(&self.tmp);
        }
    }
}

/// What `libtea cache stat` reports.
///
/// *Experimental.*
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub struct CacheStats {
    /// The directory inspected.
    pub dir: PathBuf,
    /// Whether it exists.
    pub exists: bool,
    /// Entry files.
    pub entries: u64,
    /// Bytes in entry files.
    pub bytes: u64,
    /// Files, directories and links that are not entries.
    pub foreign: u64,
    /// Seconds since the oldest entry file was written (by file time).
    pub oldest_age_secs: Option<u64>,
    /// Seconds since the newest entry file was written (by file time).
    pub newest_age_secs: Option<u64>,
}

fn entry_files(dir: &Path) -> Result<Option<(PathBuf, std::fs::ReadDir)>, ClientError> {
    if !private_dir(dir, false)? {
        return Ok(None);
    }
    let entries = dir.join(ENTRY_DIR);
    if !private_dir(&entries, false)? {
        return Ok(None);
    }
    let read =
        std::fs::read_dir(&entries).map_err(|e| io_error("reading the cache directory", &e))?;
    Ok(Some((entries, read)))
}

/// Summarise a cache directory without changing it.
///
/// # Errors
///
/// Returns an error when the directory is a symbolic link, is open to other
/// users, or cannot be read. A directory that does not exist is not an error.
pub fn cache_stat(dir: &Path) -> Result<CacheStats, ClientError> {
    let mut stats = CacheStats {
        dir: dir.to_path_buf(),
        exists: false,
        entries: 0,
        bytes: 0,
        foreign: 0,
        oldest_age_secs: None,
        newest_age_secs: None,
    };
    let Some((_, read)) = entry_files(dir)? else {
        return Ok(stats);
    };
    stats.exists = true;
    let now = SystemTime::now();
    for item in read.flatten() {
        let name = item.file_name();
        let name = name.to_string_lossy().into_owned();
        let Ok(meta) = std::fs::symlink_metadata(item.path()) else {
            continue;
        };
        let key = name.strip_suffix(".entry").unwrap_or("");
        if meta.file_type().is_file() && is_cache_key(key) {
            stats.entries += 1;
            stats.bytes = stats.bytes.saturating_add(meta.len());
            if let Some(age) = meta
                .modified()
                .ok()
                .and_then(|m| now.duration_since(m).ok())
            {
                let age = age.as_secs();
                stats.oldest_age_secs = Some(stats.oldest_age_secs.map_or(age, |o| o.max(age)));
                stats.newest_age_secs = Some(stats.newest_age_secs.map_or(age, |n| n.min(age)));
            }
        } else if !name.ends_with(".tmp") {
            stats.foreign += 1;
        }
    }
    Ok(stats)
}

/// A temporary file the cache itself writes: `.<64 hex key>.<pid>-<n>.tmp`.
/// Nothing else is a cache temporary file, whatever its name ends in.
fn is_cache_temp(name: &str) -> bool {
    let Some(rest) = name.strip_prefix('.') else {
        return false;
    };
    let Some(rest) = rest.strip_suffix(".tmp") else {
        return false;
    };
    let Some((key, tail)) = rest.split_once('.') else {
        return false;
    };
    is_cache_key(key)
        && tail.split_once('-').is_some_and(|(pid, n)| {
            !pid.is_empty()
                && !n.is_empty()
                && pid.bytes().all(|b| b.is_ascii_digit())
                && n.bytes().all(|b| b.is_ascii_digit())
        })
}

/// Remove every cache entry (and stray temporary files) and return how many
/// files were removed. Only files this cache writes are touched: names that are
/// a cache key plus `.entry`, and `*.tmp`. Other files, and the directories
/// themselves, are left alone. A symbolic link with an entry's name is removed
/// as a link; its target is never followed.
///
/// # Errors
///
/// Returns an error when the directory is a symbolic link, is open to other
/// users, or cannot be read.
pub fn cache_clear(dir: &Path) -> Result<u64, ClientError> {
    let Some((entries, read)) = entry_files(dir)? else {
        return Ok(0);
    };
    let mut removed = 0;
    for item in read.flatten() {
        let name = item.file_name();
        let name = name.to_string_lossy().into_owned();
        let is_entry = name.strip_suffix(".entry").is_some_and(is_cache_key);
        let is_temp = is_cache_temp(&name);
        if !(is_entry || is_temp) {
            continue;
        }
        let path = entries.join(&name);
        let Ok(meta) = std::fs::symlink_metadata(&path) else {
            continue;
        };
        if (meta.file_type().is_file() || meta.file_type().is_symlink())
            && std::fs::remove_file(&path).is_ok()
        {
            removed += 1;
        }
    }
    Ok(removed)
}

#[cfg(test)]
mod temp_file_tests {
    use super::*;

    fn scratch(tag: &str) -> PathBuf {
        let dir =
            std::env::temp_dir().join(format!("libtea-cache-tmp-{tag}-{}", std::process::id()));
        let _ = std::fs::remove_dir_all(&dir);
        std::fs::create_dir_all(&dir).unwrap();
        dir
    }

    #[test]
    fn two_caches_on_one_directory_never_share_a_temporary_name() {
        let dir = scratch("names");
        // The cache creates its own directory, private; `scratch` is not.
        let cache_dir = dir.join("cache");
        let first = Cache::open(CacheConfig::new(&cache_dir), false).unwrap();
        let second = Cache::open(CacheConfig::new(&cache_dir), false).unwrap();
        let key = "0".repeat(64);
        let names: std::collections::BTreeSet<PathBuf> = (0..4)
            .flat_map(|_| [first.temp_path(&key), second.temp_path(&key)])
            .collect();
        assert_eq!(names.len(), 8);
        let _ = std::fs::remove_dir_all(&dir);
    }

    #[test]
    fn a_temporary_file_someone_else_created_is_left_alone() {
        let dir = scratch("theirs");
        let tmp = dir.join("theirs.tmp");
        let dest = dir.join("entry");
        std::fs::write(&tmp, b"theirs").unwrap();
        let error = write_entry_file(&tmp, b"mine", &dest).unwrap_err();
        assert_eq!(error.kind(), std::io::ErrorKind::AlreadyExists);
        assert_eq!(std::fs::read(&tmp).unwrap(), b"theirs");
        assert!(!dest.exists());
        let _ = std::fs::remove_dir_all(&dir);
    }

    #[test]
    fn a_failed_rename_removes_only_its_own_temporary_file() {
        let dir = scratch("mine");
        let tmp = dir.join("mine.tmp");
        // A destination inside a missing directory: the rename fails.
        let dest = dir.join("missing").join("entry");
        assert!(write_entry_file(&tmp, b"mine", &dest).is_err());
        assert!(!tmp.exists());
        let _ = std::fs::remove_dir_all(&dir);
    }
}

#[cfg(all(test, unix))]
mod tests {
    use super::*;

    fn scratch(tag: &str) -> PathBuf {
        let dir =
            std::env::temp_dir().join(format!("libtea-cache-unit-{tag}-{}", std::process::id()));
        let _ = std::fs::remove_dir_all(&dir);
        std::fs::create_dir_all(&dir).unwrap();
        dir
    }

    #[test]
    fn ownership_is_compared_with_the_uid_of_a_file_just_created() {
        let dir = scratch("uid");
        let mine = current_uid(None).unwrap();
        let probe = dir.join("probe");
        std::fs::write(&probe, b"x").unwrap();
        use std::os::unix::fs::MetadataExt;
        assert_eq!(std::fs::metadata(&probe).unwrap().uid(), mine);
        assert_eq!(std::fs::metadata(&dir).unwrap().uid(), mine);
        assert!(owner_matches(mine, mine));
        assert!(
            !owner_matches(mine.wrapping_add(1), mine),
            "another uid must not match"
        );
        assert!(!owner_matches(0, 501) && !owner_matches(501, 0));
        let _ = std::fs::remove_dir_all(dir);
    }

    #[test]
    fn a_regular_file_opens_and_a_symlink_never_does() {
        let dir = scratch("nofollow");
        let real = dir.join("real");
        std::fs::write(&real, b"entry").unwrap();
        assert_eq!(read_regular(&real, 100).unwrap(), b"entry");
        let link = dir.join("link");
        std::os::unix::fs::symlink(&real, &link).unwrap();
        assert!(open_regular_with(&link, || {}).is_err());
        assert!(read_regular(&link, 100).is_err());
        assert!(
            read_regular(&dir, 100).is_err(),
            "a directory is not an entry"
        );
        assert!(read_regular(&real, 3).is_err(), "over the limit");
        let _ = std::fs::remove_dir_all(dir);
    }

    #[test]
    fn a_symlink_swapped_in_after_the_check_is_refused_not_followed() {
        let dir = scratch("swap");
        let entry = dir.join("entry");
        let secret = dir.join("secret");
        std::fs::write(&entry, b"genuine").unwrap();
        std::fs::write(&secret, b"must not be read").unwrap();
        // Between the first lstat and the open, replace the file by a link.
        let swapped = open_regular_with(&entry, || {
            std::fs::remove_file(&entry).unwrap();
            std::os::unix::fs::symlink(&secret, &entry).unwrap();
        });
        assert!(swapped.is_err(), "the swapped-in link was followed");
        // And a plain replacement by another regular file is noticed too.
        // The original stays alive under a second name, so the replacement
        // cannot be given the same inode number (a freed inode is reused at
        // once on Linux, which would make the two indistinguishable).
        let entry2 = dir.join("entry2");
        let kept = dir.join("entry2-kept");
        std::fs::write(&entry2, b"first").unwrap();
        std::fs::hard_link(&entry2, &kept).unwrap();
        let replaced = open_regular_with(&entry2, || {
            std::fs::remove_file(&entry2).unwrap();
            std::fs::write(&entry2, b"second").unwrap();
        });
        assert!(
            replaced.is_err(),
            "a different file than the one inspected was accepted"
        );
        let _ = std::fs::remove_dir_all(dir);
    }

    #[test]
    fn only_the_caches_own_temporary_files_are_temporary_files() {
        let key = "a".repeat(64);
        assert!(is_cache_temp(&format!(".{key}.123-4.tmp")));
        for other in [
            ".mine.tmp",
            ".notes.libtea.tmp",
            "build.tmp",
            &format!(".{key}.tmp"),
            &format!(".{key}.x-4.tmp"),
            &format!(".{key}.12-.tmp"),
            &format!(".{key}.12-4.tmp.bak"),
            &format!(".{}.1-2.tmp", "g".repeat(64)),
            &format!(".{}.1-2.tmp", "a".repeat(63)),
            &format!("{key}.1-2.tmp"),
        ] {
            assert!(!is_cache_temp(other), "{other}");
        }
    }
}