link-assistant-router 1.6.0

Link.Assistant.Router — Claude MAX OAuth proxy and token gateway for Anthropic APIs
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
//! Transactional, edit-aware client configuration repair.

use std::fmt::Write as _;
use std::fs;
use std::path::{Path, PathBuf};

use serde::{Deserialize, Serialize};
use sha2::{Digest as _, Sha256};

use super::{
    ClientConfigAnalysis, ClientError, ClientKind, ClientManager, ManagedCredential,
    OwnershipState, RouterModel, SetupResult,
};

#[cfg(test)]
thread_local! {
    static FAIL_AFTER_WRITE: std::cell::Cell<Option<&'static str>> = const { std::cell::Cell::new(None) };
}

#[cfg(test)]
fn transaction_checkpoint(stage: &'static str) -> Result<(), ClientError> {
    if FAIL_AFTER_WRITE.get() == Some(stage) {
        return Err(ClientError::message(format!(
            "injected transaction failure after {stage}"
        )));
    }
    Ok(())
}

#[cfg(not(test))]
#[inline]
#[allow(clippy::unnecessary_wraps)]
const fn transaction_checkpoint(_stage: &'static str) -> Result<(), ClientError> {
    Ok(())
}

/// A secret-free repair preview. Constructing it performs no writes or I/O to
/// the selected Router.
#[derive(Clone, Debug, Serialize)]
pub struct RepairPlan {
    pub client: ClientKind,
    pub state: OwnershipState,
    pub conflicts: Vec<String>,
    pub changes: Vec<PathBuf>,
    pub action: &'static str,
}

/// Result of a committed local repair transaction.
#[derive(Clone, Debug, Serialize)]
pub struct RepairResult {
    pub client: ClientKind,
    pub before: OwnershipState,
    pub after: OwnershipState,
    pub changed: bool,
    pub restart_required: bool,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub backup_id: Option<String>,
}

#[derive(Clone, Debug, Deserialize, Serialize)]
struct SnapshotManifest {
    version: u8,
    id: String,
    client: String,
    entries: Vec<SnapshotEntry>,
}

#[derive(Clone, Debug, Deserialize, Serialize)]
struct SnapshotEntry {
    path: PathBuf,
    existed: bool,
    #[serde(default)]
    mode: Option<u32>,
    #[serde(default)]
    before_sha256: Option<String>,
    #[serde(default)]
    after_sha256: Option<String>,
    #[serde(default)]
    backup: Option<String>,
}

impl ClientManager {
    /// Whether the complete managed configuration already targets this public
    /// router origin. This check intentionally does not read or return the
    /// credential value, so callers can avoid minting a replacement before
    /// deciding that an identical setup is a no-op.
    pub(crate) fn managed_target_matches(
        &self,
        client: ClientKind,
        base_url: &str,
    ) -> Result<bool, ClientError> {
        if self.analyze(client)?.state != OwnershipState::ManagedIntact {
            return Ok(false);
        }
        let Some(metadata) = self.credential_metadata(client)? else {
            return Ok(false);
        };
        let Some(recorded) = metadata.router else {
            return Ok(false);
        };
        let requested = crate::managed_server::canonical_server_origin(base_url)
            .map_err(|error| ClientError::message(error.to_string()))?;
        let recorded = crate::managed_server::canonical_server_origin(&recorded)
            .map_err(|error| ClientError::message(error.to_string()))?;
        let current_hash = config_digest(&self.config_path(client))?;
        Ok(requested == recorded && metadata.config_sha256.as_deref() == Some(&current_hash))
    }

    /// Describe the exact local files repair is allowed to reconcile.
    pub fn repair_plan(&self, client: ClientKind) -> Result<RepairPlan, ClientError> {
        let analysis = self.analyze(client)?;
        Ok(RepairPlan {
            client,
            state: analysis.state,
            conflicts: analysis.conflicts,
            changes: self.repair_paths(client),
            action: if analysis.state == OwnershipState::ManagedIntact {
                "none"
            } else if client.setup_limitation().is_some() {
                "unsupported"
            } else {
                "reconcile"
            },
        })
    }

    /// Reconcile all managed files as one recoverable transaction.
    #[allow(dead_code)]
    pub(crate) fn apply_repair(
        &self,
        client: ClientKind,
        base_url: &str,
        token: &str,
        credential: &ManagedCredential,
        models: &[RouterModel],
    ) -> Result<RepairResult, ClientError> {
        self.apply_repair_with_codex_backend(client, base_url, token, credential, models, None)
    }

    pub(crate) fn apply_repair_with_codex_backend(
        &self,
        client: ClientKind,
        base_url: &str,
        token: &str,
        credential: &ManagedCredential,
        models: &[RouterModel],
        codex_backend_base_url: Option<&str>,
    ) -> Result<RepairResult, ClientError> {
        crate::durable_file::with_exclusive_lock(&self.repair_lock_path(client), || {
            self.apply_repair_locked(
                client,
                base_url,
                token,
                credential,
                models,
                codex_backend_base_url,
            )
        })
    }

    /// Merge ordinary persistent setup as one transaction while preserving
    /// its permissive merge semantics and user-facing backup.
    #[allow(dead_code)]
    pub(crate) fn apply_setup_transaction(
        &self,
        client: ClientKind,
        base_url: &str,
        token: &str,
        credential: &ManagedCredential,
        models: &[RouterModel],
    ) -> Result<SetupResult, ClientError> {
        self.apply_setup_transaction_with_codex_backend(
            client, base_url, token, credential, models, None,
        )
    }

    pub(crate) fn apply_setup_transaction_with_codex_backend(
        &self,
        client: ClientKind,
        base_url: &str,
        token: &str,
        credential: &ManagedCredential,
        models: &[RouterModel],
        codex_backend_base_url: Option<&str>,
    ) -> Result<SetupResult, ClientError> {
        crate::durable_file::with_exclusive_lock(&self.repair_lock_path(client), || {
            let before = self.analyze(client)?;
            let snapshot = self.capture_snapshot(client, &before)?;
            let mut setup_backup = None;
            let result: Result<SetupResult, ClientError> = (|| {
                snapshot.verify_before()?;
                let setup = self.setup_with_codex_backend(
                    client,
                    base_url,
                    models,
                    codex_backend_base_url,
                )?;
                setup_backup.clone_from(&setup.backup);
                transaction_checkpoint("config")?;
                snapshot.verify_before_path(&self.environment_path(client))?;
                self.write_environment(client, base_url, token)?;
                transaction_checkpoint("environment")?;
                snapshot.verify_before_path(&self.credential_metadata_path(client))?;
                let mut credential = credential.clone();
                credential.config_sha256 = Some(config_digest(&self.config_path(client))?);
                self.write_credential_metadata(client, &credential)?;
                transaction_checkpoint("metadata")?;
                crate::client_global::update_post_configure_hash(
                    &self.config_path(client),
                    self.ownership_marker_path(client).as_deref(),
                )
                .map_err(|error| ClientError::message(error.to_string()))?;
                transaction_checkpoint("undo-state")?;
                Ok(setup)
            })();
            match result {
                Ok(setup) => {
                    if let Err(error) = fs::remove_dir_all(&snapshot.root) {
                        tracing::warn!(
                            path = %snapshot.root.display(),
                            %error,
                            "setup committed but its owner-only transaction snapshot remains"
                        );
                    }
                    Ok(setup)
                }
                Err(error) => {
                    let backup_error = setup_backup
                        .as_deref()
                        .map(remove_if_present)
                        .transpose()
                        .err();
                    let rollback_error = snapshot.restore(false).err();
                    let cleanup_error = fs::remove_dir_all(&snapshot.root).err();
                    let mut message = error.to_string();
                    if let Some(backup) = backup_error {
                        let _ = write!(message, "; setup-backup cleanup also failed: {backup}");
                    }
                    if let Some(rollback) = rollback_error {
                        let _ = write!(message, "; automatic rollback also failed: {rollback}");
                    }
                    if let Some(cleanup) = cleanup_error {
                        let _ = write!(message, "; snapshot cleanup also failed: {cleanup}");
                    }
                    Err(ClientError::message(message))
                }
            }
        })
    }

    /// Apply the reversible global client configuration and its credential
    /// files as one transaction. A failure after the public config write
    /// restores every prior byte and mode and removes the newly-created undo
    /// artifacts.
    #[allow(dead_code)]
    pub(crate) fn apply_configure_transaction(
        &self,
        client: ClientKind,
        base_url: &str,
        token: &str,
        credential: &ManagedCredential,
        models: &[RouterModel],
    ) -> Result<PathBuf, ClientError> {
        self.apply_configure_transaction_with_codex_backend(
            client, base_url, token, credential, models, None,
        )
    }

    pub(crate) fn apply_configure_transaction_with_codex_backend(
        &self,
        client: ClientKind,
        base_url: &str,
        token: &str,
        credential: &ManagedCredential,
        models: &[RouterModel],
        codex_backend_base_url: Option<&str>,
    ) -> Result<PathBuf, ClientError> {
        crate::durable_file::with_exclusive_lock(&self.repair_lock_path(client), || {
            let before = self.analyze(client)?;
            let snapshot = self.capture_snapshot(client, &before)?;
            let mut global_applied = false;
            let result: Result<PathBuf, ClientError> = (|| {
                snapshot.verify_before()?;
                let path = crate::client_global::apply_with_manager_and_codex_backend(
                    self,
                    client,
                    base_url,
                    models,
                    codex_backend_base_url,
                )
                .map_err(|error| ClientError::message(error.to_string()))?;
                global_applied = true;
                transaction_checkpoint("config")?;
                self.write_environment(client, base_url, token)?;
                transaction_checkpoint("environment")?;
                let mut credential = credential.clone();
                credential.config_sha256 = Some(config_digest(&self.config_path(client))?);
                self.write_credential_metadata(client, &credential)?;
                transaction_checkpoint("metadata")?;
                Ok(path)
            })();
            let path = match result {
                Ok(path) => path,
                Err(error) => {
                    let undo_error = global_applied
                        .then(|| crate::client_global::undo_with_manager(self, client))
                        .transpose()
                        .err();
                    let rollback_error = snapshot.restore(false).err();
                    let cleanup_error = fs::remove_dir_all(&snapshot.root).err();
                    let mut message = error.to_string();
                    if let Some(undo) = undo_error {
                        let _ = write!(message, "; global undo also failed: {undo}");
                    }
                    if let Some(rollback) = rollback_error {
                        let _ = write!(message, "; automatic rollback also failed: {rollback}");
                    }
                    if let Some(cleanup) = cleanup_error {
                        let _ = write!(message, "; snapshot cleanup also failed: {cleanup}");
                    }
                    return Err(ClientError::message(message));
                }
            };
            if let Err(error) = fs::remove_dir_all(&snapshot.root) {
                tracing::warn!(
                    path = %snapshot.root.display(),
                    %error,
                    "configuration committed but its owner-only transaction snapshot remains"
                );
            }
            Ok(path)
        })
    }

    fn apply_repair_locked(
        &self,
        client: ClientKind,
        base_url: &str,
        token: &str,
        credential: &ManagedCredential,
        models: &[RouterModel],
        codex_backend_base_url: Option<&str>,
    ) -> Result<RepairResult, ClientError> {
        let before = self.analyze(client)?;
        let same_credential = client.token_env().is_some_and(|key| {
            super::read_environment_value(&self.environment_path(client), key)
                .ok()
                .flatten()
                .as_deref()
                == Some(token)
        });
        let same_codex_backend = codex_backend_base_url.is_none_or(|expected| {
            client != ClientKind::Codex || self.codex_backend_matches(expected).unwrap_or(false)
        });
        if before.state == OwnershipState::ManagedIntact
            && self.managed_target_matches(client, base_url)?
            && same_credential
            && same_codex_backend
        {
            return Ok(RepairResult {
                client,
                before: before.state,
                after: before.state,
                changed: false,
                restart_required: false,
                backup_id: None,
            });
        }
        if let Some(limitation) = client.setup_limitation() {
            return Err(ClientError::message(limitation));
        }

        if client == ClientKind::Codex {
            self.validate_codex_catalog_constraint()?;
        }

        let mut snapshot = self.capture_snapshot(client, &before)?;
        let id = snapshot.manifest.id.clone();
        let result = (|| {
            // A corrupt marker cannot safely be interpreted by the surgical
            // setup writers. Its exact bytes are already in the snapshot.
            if before.state == OwnershipState::Ambiguous
                && before
                    .conflicts
                    .iter()
                    .any(|conflict| conflict == "ownership-marker:invalid")
                && let Some(path) = self.ownership_marker_path(client)
            {
                snapshot.verify_before_path(&path)?;
                remove_if_present(&path)?;
            }
            snapshot.verify_before_path(&self.config_path(client))?;
            if client == ClientKind::Codex {
                self.remove_codex_catalog_constraint()?;
            }
            if let Some(path) = self.ownership_marker_path(client)
                && !before
                    .conflicts
                    .iter()
                    .any(|conflict| conflict == "ownership-marker:invalid")
            {
                snapshot.verify_before_path(&path)?;
            }
            let setup =
                self.setup_with_codex_backend(client, base_url, models, codex_backend_base_url)?;
            if let Some(path) = setup.backup.as_deref() {
                remove_if_present(path)?;
            }
            transaction_checkpoint("config")?;
            snapshot.verify_before_path(&self.environment_path(client))?;
            self.write_environment(client, base_url, token)?;
            transaction_checkpoint("environment")?;
            snapshot.verify_before_path(&self.credential_metadata_path(client))?;
            let mut credential = credential.clone();
            credential.config_sha256 = Some(config_digest(&self.config_path(client))?);
            self.write_credential_metadata(client, &credential)?;
            transaction_checkpoint("metadata")?;
            snapshot.verify_before_path(&crate::client_global::undo_state_path(
                &self.config_path(client),
            ))?;
            crate::client_global::update_post_configure_hash(
                &self.config_path(client),
                self.ownership_marker_path(client).as_deref(),
            )
            .map_err(|error| ClientError::message(error.to_string()))?;
            transaction_checkpoint("undo-state")?;
            let after = self.analyze(client)?;
            if after.state != OwnershipState::ManagedIntact {
                return Err(ClientError::message(format!(
                    "repair did not produce an intact managed configuration; remaining conflicts: {}",
                    after.conflicts.join(", ")
                )));
            }
            snapshot.record_after()?;
            snapshot.write()?;
            Ok(RepairResult {
                client,
                before: before.state,
                after: after.state,
                changed: true,
                restart_required: client == ClientKind::ClaudeCode,
                backup_id: Some(id),
            })
        })();
        if let Err(error) = result {
            if let Err(rollback_error) = snapshot.restore(false) {
                return Err(ClientError::message(format!(
                    "{error}; automatic rollback also failed: {rollback_error}"
                )));
            }
            return Err(error);
        }
        result
    }

    /// Restore a named snapshot only if none of its repaired files changed.
    pub fn rollback_repair(
        &self,
        client: ClientKind,
        id: &str,
    ) -> Result<RepairResult, ClientError> {
        crate::durable_file::with_exclusive_lock(&self.repair_lock_path(client), || {
            self.rollback_repair_locked(client, id)
        })
    }

    fn rollback_repair_locked(
        &self,
        client: ClientKind,
        id: &str,
    ) -> Result<RepairResult, ClientError> {
        validate_id(id)?;
        let root = self.repair_root().join(id);
        let source = fs::read(root.join("manifest.json")).map_err(|error| {
            ClientError::message(format!("could not read repair snapshot {id}: {error}"))
        })?;
        let manifest: SnapshotManifest = serde_json::from_slice(&source)?;
        if manifest.id != id || manifest.client != client.canonical_name() {
            return Err(ClientError::message(
                "repair snapshot does not belong to the requested client",
            ));
        }
        let before = self.analyze(client)?.state;
        let snapshot = Snapshot { root, manifest };
        snapshot.restore(true)?;
        let after = self.analyze(client)?.state;
        Ok(RepairResult {
            client,
            before,
            after,
            changed: true,
            restart_required: client == ClientKind::ClaudeCode,
            backup_id: Some(id.to_string()),
        })
    }

    fn repair_root(&self) -> PathBuf {
        self.config_home.join("link-assistant-router/repairs")
    }

    fn repair_lock_path(&self, client: ClientKind) -> PathBuf {
        self.config_home
            .join("link-assistant-router/clients")
            .join(format!("{}.repair.lock", client.canonical_name()))
    }

    fn repair_paths(&self, client: ClientKind) -> Vec<PathBuf> {
        let mut paths = vec![
            self.config_path(client),
            self.environment_path(client),
            self.credential_metadata_path(client),
        ];
        if let Some(marker) = self.ownership_marker_path(client) {
            paths.push(marker);
        }
        paths.extend(crate::client_global::transaction_paths(
            &self.config_path(client),
        ));
        paths.sort();
        paths.dedup();
        paths
    }

    fn capture_snapshot(
        &self,
        client: ClientKind,
        analysis: &ClientConfigAnalysis,
    ) -> Result<Snapshot, ClientError> {
        // Refuse paths whose type could redirect the transaction outside its
        // fixed allow-list. The observation and capture hashes also make a
        // pre-analysis edit visible before any managed write occurs.
        for observed in &analysis.observed {
            if observed.exists && observed.kind != "file" {
                return Err(ClientError::message(format!(
                    "refusing to repair {} because it is a {}",
                    observed.path.display(),
                    observed.kind
                )));
            }
            let current = current_state(&observed.path)?;
            if current.0 != observed.exists || current.1 != observed.sha256 {
                return Err(ClientError::message(format!(
                    "{} changed after analysis; retry repair",
                    observed.path.display()
                )));
            }
        }
        let id = uuid::Uuid::new_v4().simple().to_string();
        let root = self.repair_root().join(&id);
        fs::create_dir_all(&root)?;
        set_mode(&root, 0o700)?;
        let mut entries = Vec::new();
        for (index, path) in self.repair_paths(client).into_iter().enumerate() {
            entries.push(capture_entry(&root, index, path)?);
        }
        let snapshot = Snapshot {
            root,
            manifest: SnapshotManifest {
                version: 1,
                id,
                client: client.canonical_name().to_string(),
                entries,
            },
        };
        snapshot.verify_before()?;
        snapshot.write()?;
        Ok(snapshot)
    }
}

struct Snapshot {
    root: PathBuf,
    manifest: SnapshotManifest,
}

impl Snapshot {
    fn write(&self) -> Result<(), ClientError> {
        let rendered = format!("{}\n", serde_json::to_string_pretty(&self.manifest)?);
        crate::durable_file::atomic_write_owner_only(
            &self.root.join("manifest.json"),
            rendered.as_bytes(),
        )?;
        set_mode(&self.root.join("manifest.json"), 0o600)
    }

    fn verify_before(&self) -> Result<(), ClientError> {
        for entry in &self.manifest.entries {
            let current = current_state(&entry.path)?;
            if current.0 != entry.existed || current.1 != entry.before_sha256 {
                return Err(ClientError::message(format!(
                    "{} changed while repair was being prepared; retry",
                    entry.path.display()
                )));
            }
        }
        Ok(())
    }

    fn verify_before_path(&self, path: &Path) -> Result<(), ClientError> {
        let entry = self
            .manifest
            .entries
            .iter()
            .find(|entry| entry.path == path)
            .ok_or_else(|| {
                ClientError::message("repair path is outside the captured allow-list")
            })?;
        let current = current_state(path)?;
        if current.0 != entry.existed || current.1 != entry.before_sha256 {
            return Err(ClientError::message(format!(
                "{} changed immediately before replacement; retry repair",
                path.display()
            )));
        }
        Ok(())
    }

    fn record_after(&mut self) -> Result<(), ClientError> {
        for entry in &mut self.manifest.entries {
            let current = current_state(&entry.path)?;
            entry.after_sha256 = current.1;
        }
        Ok(())
    }

    fn restore(&self, detect_conflicts: bool) -> Result<(), ClientError> {
        if detect_conflicts {
            for entry in &self.manifest.entries {
                let current = current_state(&entry.path)?;
                if current.0 != entry.after_sha256.is_some() || current.1 != entry.after_sha256 {
                    return Err(ClientError::message(format!(
                        "refusing rollback because {} changed after repair",
                        entry.path.display()
                    )));
                }
            }
        }
        for entry in self.manifest.entries.iter().rev() {
            if entry.existed {
                let backup = entry.backup.as_deref().ok_or_else(|| {
                    ClientError::message("repair snapshot is missing a backup name")
                })?;
                let bytes = fs::read(self.root.join(backup))?;
                crate::durable_file::atomic_write_owner_only(&entry.path, &bytes)?;
                set_optional_mode(&entry.path, entry.mode)?;
            } else {
                remove_if_present(&entry.path)?;
            }
        }
        Ok(())
    }
}

fn capture_entry(root: &Path, index: usize, path: PathBuf) -> Result<SnapshotEntry, ClientError> {
    let (exists, hash) = current_state(&path)?;
    let mode = file_mode(&path);
    let backup = if exists {
        let name = format!("{index}.backup");
        crate::durable_file::atomic_write_owner_only(&root.join(&name), &fs::read(&path)?)?;
        set_mode(&root.join(&name), 0o600)?;
        Some(name)
    } else {
        None
    };
    Ok(SnapshotEntry {
        path,
        existed: exists,
        mode,
        before_sha256: hash,
        after_sha256: None,
        backup,
    })
}

fn current_state(path: &Path) -> Result<(bool, Option<String>), ClientError> {
    match fs::symlink_metadata(path) {
        Ok(metadata) if !metadata.file_type().is_symlink() && metadata.is_file() => {
            let bytes = fs::read(path)?;
            Ok((true, Some(hex::encode(Sha256::digest(bytes)))))
        }
        Ok(metadata) => Err(ClientError::message(format!(
            "refusing to access {} because it is {}",
            path.display(),
            if metadata.file_type().is_symlink() {
                "a symlink"
            } else {
                "not a regular file"
            }
        ))),
        Err(error) if error.kind() == std::io::ErrorKind::NotFound => Ok((false, None)),
        Err(error) => Err(error.into()),
    }
}

fn config_digest(path: &Path) -> Result<String, ClientError> {
    match fs::read(path) {
        Ok(bytes) => Ok(hex::encode(Sha256::digest(bytes))),
        Err(error) if error.kind() == std::io::ErrorKind::NotFound => {
            Ok(hex::encode(Sha256::digest([])))
        }
        Err(error) => Err(error.into()),
    }
}

fn validate_id(id: &str) -> Result<(), ClientError> {
    if id.is_empty()
        || !id
            .bytes()
            .all(|byte| byte.is_ascii_alphanumeric() || byte == b'_' || byte == b'-')
    {
        return Err(ClientError::message("invalid repair backup id"));
    }
    Ok(())
}

fn remove_if_present(path: &Path) -> Result<(), ClientError> {
    match fs::remove_file(path) {
        Ok(()) => Ok(()),
        Err(error) if error.kind() == std::io::ErrorKind::NotFound => Ok(()),
        Err(error) => Err(error.into()),
    }
}

#[cfg(unix)]
fn file_mode(path: &Path) -> Option<u32> {
    use std::os::unix::fs::PermissionsExt as _;
    fs::metadata(path)
        .ok()
        .map(|metadata| metadata.permissions().mode() & 0o7777)
}

#[cfg(not(unix))]
fn file_mode(_path: &Path) -> Option<u32> {
    None
}

#[cfg(unix)]
fn set_mode(path: &Path, mode: u32) -> Result<(), ClientError> {
    use std::os::unix::fs::PermissionsExt as _;
    fs::set_permissions(path, fs::Permissions::from_mode(mode))?;
    Ok(())
}

#[cfg(not(unix))]
fn set_mode(_path: &Path, _mode: u32) -> Result<(), ClientError> {
    Ok(())
}

fn set_optional_mode(path: &Path, mode: Option<u32>) -> Result<(), ClientError> {
    if let Some(mode) = mode {
        set_mode(path, mode)?;
    }
    Ok(())
}

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