phora 0.1.2

A git-based artifact package manager and multiplexer for content-addressed file distribution
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
use std::collections::{BTreeMap, BTreeSet};
use std::path::{Path, PathBuf};

use crate::config::{Config, LayoutKind, ParsedSource, Target, TemplateOptIn};
use crate::error::{Error, Result};
use crate::kernel::{Materialization, SourceName};
use crate::lock::{Lock, encode_ref, ref_discriminator};
use crate::source::{ExportLeaf, ExportRequest, SourceBackend};
use crate::store::{
    ArtifactKey, ManifestFile, ProjectedRecord, RecordKind, Registry, RegistryRecord,
};

use super::plan::{ResolvedBindingPlan, plan_target};
use super::{StagingGuard, nonce, remote_for, resolved_remotes};

/// Summary of a [`rebuild_registry`] run: which artifacts were reconstructed and
/// which on-disk content failed to match the recomputed hash or lacked any config
/// match.
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct RebuildReport {
    /// Managed artifacts whose registry record was reconstructed from mirror + disk.
    pub reconstructed: Vec<ArtifactKey>,
    /// Managed artifacts whose on-disk content fails the recomputed per-file hash.
    pub modified: Vec<ArtifactKey>,
    /// On-disk artifact dirs under a target with no matching config/lock source.
    pub foreign: Vec<std::path::PathBuf>,
}

pub fn rebuild_registry(
    config: &Config,
    lock: &Lock,
    backend: &dyn SourceBackend,
    registry: &dyn Registry,
) -> Result<RebuildReport> {
    let parsed = config.parsed_sources()?;
    let remotes = resolved_remotes(config, &parsed)?;
    rebuild_registry_with(config, &parsed, &remotes, lock, backend, registry)
}

/// Like [`rebuild_registry`] but over caller-supplied `parsed`/`remotes`, so an observer that has
/// already injected the composed transitive graph rebuilds the namespaced composed sources too.
pub fn rebuild_registry_with(
    config: &Config,
    parsed: &BTreeMap<String, ParsedSource>,
    remotes: &BTreeMap<String, String>,
    lock: &Lock,
    backend: &dyn SourceBackend,
    registry: &dyn Registry,
) -> Result<RebuildReport> {
    let resolved_commits = locked_commits(config, lock, parsed)?;

    let mut report = RebuildReport::default();

    for (target_name, target) in &config.targets {
        let plan = plan_target(
            target_name,
            target,
            parsed,
            remotes,
            backend,
            &resolved_commits,
        )?;

        let mut managed_dests: BTreeSet<PathBuf> = BTreeSet::new();
        for binding in &plan.bindings {
            managed_dests.extend(binding.items.iter().map(|item| item.destination.clone()));
            let source = parsed.get(&binding.source).ok_or_else(|| {
                Error::Config(format!(
                    "target references undefined source: {}",
                    binding.source
                ))
            })?;
            rebuild_binding(
                &BindingRun {
                    config,
                    parsed,
                    target_name,
                    target,
                    backend,
                    registry,
                    binding,
                    source,
                    git: remote_for(remotes, &binding.source)?,
                },
                &mut report,
            )?;
        }

        report.foreign.extend(scan_foreign(target, &managed_dests)?);
    }

    Ok(report)
}

/// The `(name, encoded_ref) -> commit` map `plan_target` needs, drawn from the lock:
/// every binding's effective ref resolves to its locked commit (link sources included).
fn locked_commits(
    config: &Config,
    lock: &Lock,
    parsed: &BTreeMap<String, crate::config::ParsedSource>,
) -> Result<BTreeMap<(String, String), String>> {
    let mut commits = BTreeMap::new();
    for target in config.targets.values() {
        for binding in target.resolve_sources(parsed) {
            let source = parsed.get(binding.source).ok_or_else(|| {
                Error::Config(format!(
                    "target references undefined source: {}",
                    binding.source
                ))
            })?;
            let discriminator = ref_discriminator(&binding.effective_ref, &source.refspec());
            let locked = lock
                .find_entry(binding.source, discriminator.as_deref())
                .ok_or_else(|| {
                    Error::Sync(format!(
                        "no locked commit for source {} at its ref; run sync first",
                        binding.source
                    ))
                })?;
            commits.insert(
                (
                    binding.source.to_owned(),
                    encode_ref(&binding.effective_ref),
                ),
                locked.commit.clone(),
            );
        }
    }
    Ok(commits)
}

struct BindingRun<'a> {
    config: &'a Config,
    parsed: &'a BTreeMap<String, crate::config::ParsedSource>,
    target_name: &'a str,
    target: &'a Target,
    backend: &'a dyn SourceBackend,
    registry: &'a dyn Registry,
    binding: &'a ResolvedBindingPlan,
    source: &'a crate::config::ParsedSource,
    git: &'a str,
}

fn rebuild_binding(run: &BindingRun<'_>, report: &mut RebuildReport) -> Result<()> {
    let source_name = SourceName::trusted(&run.binding.source);
    let policy = run.source.export_policy();
    let template_opt_in = run
        .target
        .resolve_sources(run.parsed)
        .into_iter()
        .find(|b| b.identity == run.binding.identity)
        .map_or(TemplateOptIn::SuffixOnly, |b| b.template_opt_in);

    for item in &run.binding.items {
        let published_key = item.materialization.published_key().to_owned();
        let key = ArtifactKey {
            target: run.target_name.to_owned(),
            source: run.binding.identity.clone(),
            artifact: published_key.clone(),
        };
        let artifact_dst = run.target.expanded_path().join(
            run.target
                .layout()
                .artifact_path(&run.binding.identity, &published_key),
        );

        if run.binding.commit == "link" {
            rebuild_linked(
                run.registry,
                &run.binding.source,
                &policy,
                run.target.layout().kind,
                record_kind(&item.materialization),
                key.clone(),
            )?;
        } else {
            let leaves = item_leaves(&item.materialization, &item.kept_leaves, &template_opt_in);
            rebuild_one(RebuildOne {
                backend: run.backend,
                registry: run.registry,
                git: run.git,
                source_name: &source_name,
                underlying_source: &run.binding.source,
                root: run.source.offer().root(),
                commit: &run.binding.commit,
                policy: &policy,
                leaves: &leaves,
                materialization: &item.materialization,
                artifact_dst: &artifact_dst,
                layout_kind: run.target.layout().kind,
                key: key.clone(),
                report,
                template_opt_in: &template_opt_in,
                vars: &run.config.vars,
            })?;
        }
        report.reconstructed.push(key);
    }
    Ok(())
}

fn record_kind(materialization: &Materialization) -> RecordKind {
    match materialization {
        Materialization::CollapsedDir { .. } => RecordKind::Dir,
        Materialization::Leaf(_) => RecordKind::File,
    }
}

/// The export leaf plan for one materialization, mirroring `deploy_one`: a leaf maps
/// its single dest basename; a collapsed dir maps each kept child to its dir-relative
/// deployed name.
fn item_leaves(
    materialization: &Materialization,
    kept_leaves: &[crate::kernel::ResolvedTake],
    template_opt_in: &TemplateOptIn,
) -> Vec<ExportLeaf> {
    match materialization {
        Materialization::CollapsedDir { dir } => {
            let prefix = format!("{dir}/");
            kept_leaves
                .iter()
                .filter_map(|kept| {
                    let child = kept.dest.strip_prefix(&prefix)?;
                    Some(ExportLeaf {
                        source: PathBuf::from(&kept.source),
                        dest: PathBuf::from(template_opt_in.deployed_name(child)),
                    })
                })
                .collect()
        }
        Materialization::Leaf(take) => {
            let dest = take.dest.rsplit('/').next().unwrap_or(&take.dest);
            vec![ExportLeaf {
                source: PathBuf::from(&take.source),
                dest: PathBuf::from(dest),
            }]
        }
    }
}

struct RebuildOne<'a> {
    backend: &'a dyn SourceBackend,
    registry: &'a dyn Registry,
    git: &'a str,
    source_name: &'a SourceName,
    underlying_source: &'a str,
    root: Option<&'a Path>,
    commit: &'a str,
    policy: &'a crate::source::ExportPolicy,
    leaves: &'a [ExportLeaf],
    materialization: &'a Materialization,
    artifact_dst: &'a Path,
    layout_kind: LayoutKind,
    key: ArtifactKey,
    report: &'a mut RebuildReport,
    template_opt_in: &'a TemplateOptIn,
    vars: &'a BTreeMap<String, String>,
}

fn rebuild_one(args: RebuildOne<'_>) -> Result<()> {
    let RebuildOne {
        backend,
        registry,
        git,
        source_name,
        underlying_source,
        root,
        commit,
        policy,
        leaves,
        materialization,
        artifact_dst,
        layout_kind,
        key,
        report,
        template_opt_in,
        vars,
    } = args;

    let key_label = key.artifact.replace('/', "_");
    let staging_base = std::env::temp_dir().join("phora-rebuild");
    let staging = staging_base.join(format!("{key_label}-{}-{}", std::process::id(), nonce()));
    let _guard = StagingGuard::new(&staging_base, &staging);

    let commit_time = backend.commit_time(source_name, git, commit)?;
    let export = backend.export_artifact(&ExportRequest {
        source: source_name,
        url: git,
        commit,
        root,
        policy,
        staging_dir: &staging,
        commit_time,
        template_opt_in,
        vars,
        leaves,
    })?;

    let manifest_base = match materialization {
        Materialization::CollapsedDir { .. } => artifact_dst.to_path_buf(),
        Materialization::Leaf(_) => artifact_dst
            .parent()
            .map_or_else(|| artifact_dst.to_path_buf(), Path::to_path_buf),
    };

    let mut modified = false;
    let mut files = Vec::with_capacity(export.files.len());
    for mf in export.files {
        let on_disk = manifest_base.join(&mf.path);
        let (size, mtime) = if let Some(actual) = disk_hash(&on_disk)? {
            if actual.hash != mf.blake3 {
                modified = true;
            }
            (actual.size, actual.mtime)
        } else {
            modified = true;
            (mf.size, mf.mtime)
        };
        files.push(ManifestFile {
            path: mf.path,
            size,
            mtime,
            blake3: mf.blake3,
        });
    }

    let record = RegistryRecord::projected(ProjectedRecord {
        key: key.clone(),
        underlying_source,
        commit,
        digest: export.digest,
        layout: format!("{layout_kind:?}").to_lowercase(),
        kind: record_kind(materialization),
        allow_symlinks: policy.allow_symlinks,
        preserve_executable: policy.preserve_executable,
        files,
        vars_digest: export.vars_digest,
    });
    registry.put(&record)?;
    if modified {
        report.modified.push(key);
    }
    Ok(())
}

/// Reconstruct a linked artifact's registry record without hashing or export:
/// a link source has no mirror, so its marker is synthesized from disk discovery.
fn rebuild_linked(
    registry: &dyn Registry,
    underlying_source: &str,
    policy: &crate::source::ExportPolicy,
    layout_kind: LayoutKind,
    kind: RecordKind,
    key: ArtifactKey,
) -> Result<()> {
    let record = RegistryRecord {
        version: 1,
        key,
        source: underlying_source.to_owned(),
        commit: "link".to_owned(),
        digest: "link:".to_owned(),
        projected_at: chrono::Utc::now().to_rfc3339(),
        layout: format!("{layout_kind:?}").to_lowercase(),
        kind,
        allow_symlinks: policy.allow_symlinks,
        preserve_executable: policy.preserve_executable,
        files: vec![],
        linked: true,
        vars_digest: None,
    };
    registry.put(&record)?;
    Ok(())
}

struct DiskHash {
    hash: String,
    size: u64,
    mtime: u64,
}

/// `Ok(None)` when the file is absent on disk.
fn disk_hash(path: &Path) -> Result<Option<DiskHash>> {
    let content = match std::fs::read(path) {
        Ok(bytes) => bytes,
        Err(e) if e.kind() == std::io::ErrorKind::NotFound => return Ok(None),
        Err(e) => return Err(Error::Sync(format!("read {}: {e}", path.display()))),
    };
    let meta = std::fs::metadata(path)
        .map_err(|e| Error::Sync(format!("stat {}: {e}", path.display())))?;
    let mtime = filetime::FileTime::from_last_modification_time(&meta).unix_seconds();
    Ok(Some(DiskHash {
        hash: blake3::hash(&content).to_hex().to_string(),
        size: meta.len(),
        mtime: u64::try_from(mtime).unwrap_or(0),
    }))
}

/// On-disk top-level entries under `target` that no binding deploys. `managed_dests`
/// holds every planned destination already composed under the target's layout AND
/// binding identity (so by-source nests each identity under its own dir, prefixed joins
/// identity to the key); an entry is spared when it is a managed destination or the
/// ancestor of one. A user-owned top-level dotfile is never reported foreign — an
/// orphaned managed dotfile is reclaimed by `sync --prune`, not flagged here.
fn scan_foreign(target: &Target, managed_dests: &BTreeSet<PathBuf>) -> Result<Vec<PathBuf>> {
    let target_path = target.expanded_path();

    let entries = match std::fs::read_dir(&target_path) {
        Ok(rd) => rd,
        Err(e) if e.kind() == std::io::ErrorKind::NotFound => return Ok(Vec::new()),
        Err(e) => {
            return Err(Error::Sync(format!(
                "read target dir {}: {e}",
                target_path.display()
            )));
        }
    };

    let mut foreign = Vec::new();
    for entry in entries {
        let entry =
            entry.map_err(|e| Error::Sync(format!("read {}: {e}", target_path.display())))?;
        if entry.file_name().to_string_lossy().starts_with('.') {
            continue;
        }
        let path = entry.path();
        if managed_dests.contains(&path) || is_managed_ancestor(&path, managed_dests) {
            continue;
        }
        foreign.push(path);
    }
    foreign.sort();
    Ok(foreign)
}

/// Whether `dir` is an ancestor of any managed destination, so a parent directory
/// holding only managed children is not itself reported foreign (by-source layout nests
/// each identity under its own dir).
fn is_managed_ancestor(dir: &Path, managed_dests: &BTreeSet<PathBuf>) -> bool {
    managed_dests.iter().any(|dest| dest.starts_with(dir))
}