centralcore 1.0.2

Open-source Rust library for building Minecraft Java Edition launchers
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
//! Instance verification and incremental repair planning/execution.

use std::{path::PathBuf, time::Instant};

use serde::{Deserialize, Serialize};

use crate::{
    cache::{
        load_managed_index, snapshot_managed_file, verify_index, write_managed_index, CacheError,
        FileCheck, FileState, ManagedFileIndex, ManagedFileKind, ManagedFileLocation,
        ManagedFileOrigin, VerificationReport,
    },
    download::{CancellationToken, DownloadRequest},
    events::CoreEvent,
    files::{FileHash, SafeRelativePath},
    instance::{Instance, InstanceId},
    loaders::{load_loader_plan, ArchiveEntryPlan, LoaderPlan, ProcessorPlan},
    Result,
};

use super::{
    install::{transaction_id, TransactionState},
    MinecraftManager,
};

/// Options controlling verification work.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub struct VerifyOptions {
    /// Recompute every cryptographic digest instead of using trusted metadata.
    pub full: bool,
}

/// Options controlling incremental repair.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub struct RepairOptions {
    pub full_verification: bool,
    pub offline: bool,
}

/// One cache transfer required by a repair.
#[derive(Debug, Clone)]
pub struct RepairDownload {
    pub request: DownloadRequest,
    pub previous_state: FileState,
    pub kind: ManagedFileKind,
}

/// One managed native path that must be regenerated.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct RepairExtraction {
    pub archive: SafeRelativePath,
    pub exclusions: Vec<String>,
}

/// One verified local copy used for content-addressed/provider recovery.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct RepairCopy {
    pub source: PathBuf,
    pub destination: PathBuf,
    pub expected_size: Option<u64>,
    pub expected_hash: Option<FileHash>,
    pub previous_state: FileState,
    pub kind: ManagedFileKind,
}

/// Metrics produced while executing a repair plan.
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
pub struct RepairMetrics {
    pub files_checked: u64,
    pub files_reused: u64,
    pub files_downloaded: u64,
    pub bytes_reused: u64,
    pub bytes_downloaded: u64,
    pub cache_hits: u64,
    pub cache_misses: u64,
    pub duration_millis: u64,
}

/// Immutable analysis of the changes required to restore one instance.
#[derive(Debug, Clone)]
pub struct RepairPlan {
    pub(crate) instance_id: InstanceId,
    pub(crate) checks: Vec<FileCheck>,
    pub(crate) downloads: Vec<RepairDownload>,
    pub(crate) copies: Vec<RepairCopy>,
    pub(crate) extractions: Vec<RepairExtraction>,
    pub(crate) loader_archive_entries: Vec<ArchiveEntryPlan>,
    pub(crate) loader_processors: Vec<ProcessorPlan>,
    pub(crate) removals: Vec<PathBuf>,
    pub(crate) total_download_bytes: Option<u64>,
}

impl RepairPlan {
    /// Instance whose managed state was analyzed.
    #[must_use]
    pub fn instance_id(&self) -> &InstanceId {
        &self.instance_id
    }

    /// Per-file checks that produced this plan.
    #[must_use]
    pub fn checks(&self) -> &[FileCheck] {
        &self.checks
    }

    /// Network transfers required to repair the instance.
    #[must_use]
    pub fn downloads(&self) -> &[RepairDownload] {
        &self.downloads
    }

    /// Verified local cache copies required by the repair.
    #[must_use]
    pub fn copies(&self) -> &[RepairCopy] {
        &self.copies
    }

    /// Native archives that must be re-extracted.
    #[must_use]
    pub fn extractions(&self) -> &[RepairExtraction] {
        &self.extractions
    }

    /// Paths that no longer belong to managed desired state.
    #[must_use]
    pub fn removals(&self) -> &[PathBuf] {
        &self.removals
    }

    /// Total expected network bytes, when known.
    #[must_use]
    pub const fn total_download_bytes(&self) -> Option<u64> {
        self.total_download_bytes
    }

    #[must_use]
    pub fn is_empty(&self) -> bool {
        self.downloads.is_empty()
            && self.copies.is_empty()
            && self.extractions.is_empty()
            && self.loader_archive_entries.is_empty()
            && self.loader_processors.is_empty()
            && self.removals.is_empty()
    }
}

/// Successful repair output including its final health report.
#[derive(Debug, Clone)]
pub struct RepairOutcome {
    pub plan: RepairPlan,
    pub verification: VerificationReport,
    pub metrics: RepairMetrics,
}

/// Structured verification and repair failures.
#[derive(Debug, thiserror::Error)]
#[non_exhaustive]
pub enum RepairError {
    #[error("repair requires {count} missing or corrupted cache objects, but network access is disabled")]
    OfflineObjectsUnavailable { count: usize },
    #[error("managed file `{path}` has no trusted repair source")]
    MissingSource { path: String },
    #[error("repair completed but verification still reports {missing} missing and {corrupted} corrupted files")]
    FinalVerification { missing: u64, corrupted: u64 },
}

impl MinecraftManager {
    /// Verifies one instance without changing managed or user files.
    pub async fn verify(
        &self,
        instance: &Instance,
        options: VerifyOptions,
    ) -> Result<VerificationReport> {
        let _lock = self
            .locks
            .acquire_shared(format!("instance:{}", instance.id()))
            .await?;
        self.verify_unlocked(instance, options.full).await
    }

    /// Analyzes one instance and returns an inspectable, non-mutating plan.
    pub async fn resolve_repair_plan(
        &self,
        instance: &Instance,
        options: RepairOptions,
    ) -> Result<RepairPlan> {
        let _lock = self
            .locks
            .acquire_shared(format!("instance:{}", instance.id()))
            .await?;
        self.resolve_repair_plan_unlocked(instance, options).await
    }

    /// Executes only the invalid work in a repair plan and fully verifies it.
    pub async fn repair(
        &self,
        instance: &Instance,
        options: RepairOptions,
        cancellation: &CancellationToken,
    ) -> Result<RepairOutcome> {
        let _lock = self
            .locks
            .try_acquire_exclusive(format!("instance:{}", instance.id()))
            .await?;
        let transaction = transaction_id();
        self.write_transaction(instance, &transaction, "repair", TransactionState::Created)
            .await?;
        self.events.emit(CoreEvent::InstanceRepairStarted {
            instance_id: instance.id().to_string(),
        });
        let started = Instant::now();
        let result = self
            .repair_unlocked(instance, options, cancellation, &transaction)
            .await;
        match result {
            Ok(mut outcome) => {
                outcome.metrics.duration_millis =
                    started.elapsed().as_millis().try_into().unwrap_or(u64::MAX);
                self.instances.mark_installed(instance).await?;
                self.write_transaction(
                    instance,
                    &transaction,
                    "repair",
                    TransactionState::Committed,
                )
                .await?;
                self.events.emit(CoreEvent::InstanceRepairCompleted {
                    instance_id: instance.id().to_string(),
                });
                Ok(outcome)
            }
            Err(error) => {
                let message = error.to_string();
                if cancellation.is_cancelled() {
                    let _ = self.instances.mark_interrupted(instance, &message).await;
                } else {
                    let _ = self.instances.mark_broken(instance, &message).await;
                }
                let _ = self
                    .write_transaction(
                        instance,
                        &transaction,
                        "repair",
                        if cancellation.is_cancelled() {
                            TransactionState::Cancelled
                        } else {
                            TransactionState::Failed
                        },
                    )
                    .await;
                self.events.emit(CoreEvent::InstanceRepairFailed {
                    instance_id: instance.id().to_string(),
                    message,
                });
                Err(error)
            }
        }
    }

    async fn verify_unlocked(&self, instance: &Instance, full: bool) -> Result<VerificationReport> {
        self.events.emit(CoreEvent::InstanceVerificationStarted {
            instance_id: instance.id().to_string(),
            full,
        });
        let index = load_managed_index(instance).await?;
        let report = verify_index(
            instance,
            &self.cache_root,
            &index,
            full,
            self.downloads.config().concurrency,
        )
        .await?;
        self.events.emit(CoreEvent::InstanceVerificationProgress {
            instance_id: instance.id().to_string(),
            checked_files: report.metrics.files_checked,
            total_files: report.metrics.files_checked,
        });
        self.events.emit(CoreEvent::InstanceVerificationCompleted {
            instance_id: instance.id().to_string(),
            report: report.clone(),
        });
        Ok(report)
    }

    async fn resolve_repair_plan_unlocked(
        &self,
        instance: &Instance,
        options: RepairOptions,
    ) -> Result<RepairPlan> {
        let index = load_managed_index(instance).await?;
        let verification = self
            .verify_unlocked(instance, options.full_verification)
            .await?;
        let mut downloads = Vec::new();
        let mut copies = Vec::new();
        let mut removals = Vec::new();
        let loader_plan = load_loader_plan(instance).await?;
        let mut loader_archive_entries = Vec::new();
        let mut loader_processors = Vec::new();
        let mut native_invalid = false;
        for check in verification
            .checks
            .iter()
            .filter(|check| check.state != FileState::Valid)
        {
            if check.state == FileState::Unexpected
                && check.location == ManagedFileLocation::Instance
            {
                let relative = SafeRelativePath::new(&check.path)?;
                removals.push(relative.join_under(instance.path()));
                native_invalid |= check.kind == ManagedFileKind::ExtractedNative;
                continue;
            }
            let file = index
                .files
                .iter()
                .find(|file| file.id == check.id && file.location == check.location)
                .ok_or_else(|| RepairError::MissingSource {
                    path: check.path.clone(),
                })?;
            match file.location {
                ManagedFileLocation::Cache => {
                    native_invalid |= file.kind == ManagedFileKind::NativeArchive;
                    if let Some(source) = file.source.clone() {
                        downloads.push(RepairDownload {
                            request: DownloadRequest {
                                id: format!("repair:{}", file.id),
                                source,
                                destination: file.path.clone(),
                                expected_size: file.expected_size,
                                expected_hash: file.expected_hash.clone(),
                            },
                            previous_state: check.state,
                            kind: file.kind,
                        });
                    } else if let Some(source) = file.source_path.clone() {
                        copies.push(RepairCopy {
                            source,
                            destination: file.path.join_under(&self.cache_root),
                            expected_size: file.expected_size,
                            expected_hash: file.expected_hash.clone(),
                            previous_state: check.state,
                            kind: file.kind,
                        });
                    } else if file.origin == ManagedFileOrigin::Loader {
                        let loader = loader_plan
                            .as_ref()
                            .ok_or(crate::loaders::LoaderError::OfflinePlanUnavailable)?;
                        if let Some(processor) = loader.processors.iter().find(|processor| {
                            processor
                                .outputs
                                .iter()
                                .any(|output| output.path == file.path)
                        }) {
                            if !loader_processors
                                .iter()
                                .any(|queued: &ProcessorPlan| queued.id == processor.id)
                            {
                                loader_processors.push(processor.clone());
                            }
                        } else if let Some(extraction) = loader
                            .archive_entries
                            .iter()
                            .find(|extraction| extraction.destination == file.path)
                        {
                            if !loader_archive_entries
                                .iter()
                                .any(|queued: &ArchiveEntryPlan| {
                                    queued.destination == extraction.destination
                                })
                            {
                                loader_archive_entries.push(extraction.clone());
                            }
                        } else {
                            return Err(RepairError::MissingSource {
                                path: check.path.clone(),
                            }
                            .into());
                        }
                    } else {
                        return Err(RepairError::MissingSource {
                            path: check.path.clone(),
                        }
                        .into());
                    }
                }
                ManagedFileLocation::Instance => {
                    if let Some(source) = &file.repair_from {
                        copies.push(RepairCopy {
                            source: source.join_under(&self.cache_root),
                            destination: file.path.join_under(instance.path()),
                            expected_size: file.expected_size,
                            expected_hash: file.expected_hash.clone(),
                            previous_state: check.state,
                            kind: file.kind,
                        });
                    } else {
                        removals.push(file.path.join_under(instance.path()));
                        native_invalid |= file.kind == ManagedFileKind::ExtractedNative;
                    }
                }
            }
        }
        let extractions = if native_invalid {
            index
                .native_archives
                .iter()
                .map(|native| RepairExtraction {
                    archive: native.archive.clone(),
                    exclusions: native.exclusions.clone(),
                })
                .collect()
        } else {
            Vec::new()
        };
        let total_download_bytes = downloads
            .iter()
            .map(|download| download.request.expected_size)
            .collect::<Option<Vec<_>>>()
            .map(|sizes| sizes.into_iter().sum());
        let plan = RepairPlan {
            instance_id: instance.id().clone(),
            checks: verification.checks,
            downloads,
            copies,
            extractions,
            loader_archive_entries,
            loader_processors,
            removals,
            total_download_bytes,
        };
        self.events.emit(CoreEvent::RepairPlanCreated {
            instance_id: instance.id().to_string(),
            downloads: plan.downloads.len() as u64,
            extractions: plan.extractions.len() as u64,
        });
        Ok(plan)
    }

    async fn repair_unlocked(
        &self,
        instance: &Instance,
        options: RepairOptions,
        cancellation: &CancellationToken,
        transaction: &str,
    ) -> Result<RepairOutcome> {
        if matches!(
            load_managed_index(instance).await,
            Err(crate::Error::Cache(CacheError::MissingManagedIndex(_)))
        ) {
            if options.offline {
                return Err(CacheError::MissingManagedIndex(instance.id().to_string()).into());
            }
            let install_plan = self.resolve_install_plan(instance, cancellation).await?;
            self.persist_managed_index(instance, &install_plan).await?;
        }
        let mut plan = self.resolve_repair_plan_unlocked(instance, options).await?;
        if options.offline && !plan.downloads.is_empty() {
            for download in &plan.downloads {
                let _ = self
                    .downloads
                    .recover_local(&self.cache_root, &download.request)
                    .await?;
            }
            plan = self.resolve_repair_plan_unlocked(instance, options).await?;
            if !plan.downloads.is_empty() {
                return Err(RepairError::OfflineObjectsUnavailable {
                    count: plan.downloads.len(),
                }
                .into());
            }
        }
        self.write_transaction(
            instance,
            transaction,
            "repair",
            TransactionState::Downloading,
        )
        .await?;
        let outcomes = self
            .downloads
            .download_all(
                &self.cache_root,
                plan.downloads
                    .iter()
                    .map(|download| download.request.clone())
                    .collect(),
                cancellation,
            )
            .await?;
        if !plan.loader_archive_entries.is_empty() || !plan.loader_processors.is_empty() {
            let installed = load_loader_plan(instance)
                .await?
                .ok_or(crate::loaders::LoaderError::OfflinePlanUnavailable)?;
            let mut repair_loader = LoaderPlan {
                archive_entries: plan.loader_archive_entries.clone(),
                processors: plan.loader_processors.clone(),
                downloads: Vec::new(),
                classpath_additions: Vec::new(),
                jvm_arguments: Vec::new(),
                game_arguments: Vec::new(),
                main_class: None,
                ..installed
            };
            crate::loaders::execute_loader_plan(
                &mut repair_loader,
                instance,
                &self.cache_root,
                self.loaders.java(),
                &self.events,
                cancellation,
            )
            .await?;
        }
        for copy in plan
            .copies
            .iter()
            .filter(|copy| copy.destination.starts_with(&self.cache_root))
        {
            repair_copy(instance, &self.cache_root, copy).await?;
        }
        for copy in plan
            .copies
            .iter()
            .filter(|copy| !copy.destination.starts_with(&self.cache_root))
        {
            repair_copy(instance, &self.cache_root, copy).await?;
        }
        if plan.extractions.is_empty() {
            for removal in &plan.removals {
                if !removal.starts_with(instance.path()) {
                    return Err(crate::Error::UnsafeFilesystemEntry {
                        path: removal.clone(),
                        reason: "repair removal escaped instance root",
                    });
                }
                match tokio::fs::symlink_metadata(removal).await {
                    Ok(metadata) if metadata.is_file() && !metadata.file_type().is_symlink() => {
                        tokio::fs::remove_file(removal).await?;
                    }
                    Ok(_) => {
                        return Err(crate::Error::UnsafeFilesystemEntry {
                            path: removal.clone(),
                            reason: "repair removal must be a real file",
                        });
                    }
                    Err(error) if error.kind() == std::io::ErrorKind::NotFound => {}
                    Err(error) => return Err(error.into()),
                }
            }
        }
        self.events.emit(CoreEvent::InstanceRepairProgress {
            instance_id: instance.id().to_string(),
            completed_files: outcomes.len() as u64,
            total_files: plan.downloads.len() as u64,
        });
        if !plan.extractions.is_empty() {
            self.write_transaction(
                instance,
                transaction,
                "repair",
                TransactionState::Committing,
            )
            .await?;
            let native_archives = plan
                .extractions
                .iter()
                .map(|extraction| crate::cache::ManagedExtraction {
                    archive: extraction.archive.clone(),
                    exclusions: extraction.exclusions.clone(),
                })
                .collect::<Vec<_>>();
            self.reextract_managed_natives(instance, &native_archives, transaction, cancellation)
                .await?;
        }
        self.write_transaction(
            instance,
            transaction,
            "repair",
            TransactionState::Validating,
        )
        .await?;
        let mut index = load_managed_index(instance).await?;
        refresh_index(instance, &self.cache_root, &mut index).await?;
        write_managed_index(instance, &index).await?;
        self.cache.register(&index).await?;
        let verification = self.verify_unlocked(instance, true).await?;
        if !verification.is_healthy() {
            return Err(RepairError::FinalVerification {
                missing: verification.missing,
                corrupted: verification.corrupted,
            }
            .into());
        }
        let files_downloaded = outcomes.iter().filter(|outcome| !outcome.reused).count() as u64;
        let bytes_downloaded = outcomes
            .iter()
            .filter(|outcome| !outcome.reused)
            .map(|outcome| outcome.bytes)
            .sum();
        let bytes_reused = index
            .files
            .iter()
            .map(|file| file.verified_size)
            .sum::<u64>()
            .saturating_sub(bytes_downloaded);
        let metrics = RepairMetrics {
            files_checked: plan.checks.len() as u64,
            files_reused: (plan.checks.len() as u64)
                .saturating_sub(plan.downloads.len() as u64)
                .saturating_sub(plan.copies.len() as u64),
            files_downloaded,
            bytes_reused,
            bytes_downloaded,
            cache_hits: outcomes.iter().filter(|outcome| outcome.reused).count() as u64,
            cache_misses: files_downloaded,
            duration_millis: 0,
        };
        Ok(RepairOutcome {
            plan,
            verification,
            metrics,
        })
    }
}

async fn repair_copy(
    instance: &Instance,
    cache_root: &std::path::Path,
    copy: &RepairCopy,
) -> Result<()> {
    if !copy.destination.starts_with(cache_root) && !copy.destination.starts_with(instance.path()) {
        return Err(crate::Error::UnsafeFilesystemEntry {
            path: copy.destination.clone(),
            reason: "repair copy destination escaped managed roots",
        });
    }
    let source_metadata = tokio::fs::symlink_metadata(&copy.source).await?;
    if source_metadata.file_type().is_symlink() || !source_metadata.is_file() {
        return Err(crate::Error::UnsafeFilesystemEntry {
            path: copy.source.clone(),
            reason: "repair copy source must be a real file",
        });
    }
    crate::download::verify_file(
        &copy.source,
        copy.expected_size,
        copy.expected_hash.as_ref(),
    )
    .await?;
    let parent = copy
        .destination
        .parent()
        .ok_or_else(|| crate::Error::UnsafeFilesystemEntry {
            path: copy.destination.clone(),
            reason: "repair copy destination has no parent",
        })?;
    ensure_repair_parent(
        if copy.destination.starts_with(cache_root) {
            cache_root
        } else {
            instance.path()
        },
        parent,
    )
    .await?;
    let temporary = copy.destination.with_extension("centralcore-repair.tmp");
    reject_repair_symlink(&temporary).await?;
    tokio::fs::copy(&copy.source, &temporary).await?;
    crate::download::verify_file(&temporary, copy.expected_size, copy.expected_hash.as_ref())
        .await?;
    reject_repair_symlink(&copy.destination).await?;
    if tokio::fs::try_exists(&copy.destination).await? {
        tokio::fs::remove_file(&copy.destination).await?;
    }
    tokio::fs::rename(temporary, &copy.destination).await?;
    Ok(())
}

async fn ensure_repair_parent(root: &std::path::Path, parent: &std::path::Path) -> Result<()> {
    let relative = parent
        .strip_prefix(root)
        .map_err(|_| crate::Error::UnsafeFilesystemEntry {
            path: parent.to_path_buf(),
            reason: "repair parent escaped managed root",
        })?;
    let mut current = root.to_path_buf();
    for component in relative.components() {
        current.push(component);
        match tokio::fs::symlink_metadata(&current).await {
            Ok(metadata) if metadata.file_type().is_symlink() || !metadata.is_dir() => {
                return Err(crate::Error::UnsafeFilesystemEntry {
                    path: current,
                    reason: "repair parent must be a real directory",
                });
            }
            Ok(_) => {}
            Err(error) if error.kind() == std::io::ErrorKind::NotFound => {
                tokio::fs::create_dir(&current).await?;
            }
            Err(error) => return Err(error.into()),
        }
    }
    Ok(())
}

async fn reject_repair_symlink(path: &std::path::Path) -> Result<()> {
    match tokio::fs::symlink_metadata(path).await {
        Ok(metadata) if metadata.file_type().is_symlink() || !metadata.is_file() => {
            Err(crate::Error::UnsafeFilesystemEntry {
                path: path.to_path_buf(),
                reason: "repair destination must be a real file",
            })
        }
        Ok(_) => Ok(()),
        Err(error) if error.kind() == std::io::ErrorKind::NotFound => Ok(()),
        Err(error) => Err(error.into()),
    }
}

async fn refresh_index(
    instance: &Instance,
    cache_root: &std::path::Path,
    index: &mut ManagedFileIndex,
) -> Result<()> {
    let mut refreshed = Vec::with_capacity(index.files.len());
    for file in index.files.drain(..) {
        let base = match file.location {
            ManagedFileLocation::Cache => cache_root,
            ManagedFileLocation::Instance => instance.path(),
        };
        refreshed.push(snapshot_managed_file(base, file).await?);
    }
    index.files = refreshed;
    Ok(())
}