raf-source 0.0.0

Workspace source index: a Tree file seam with disk and memory backends under canonical path containment.
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
// SPDX-FileCopyrightText: 2026 Blackcat InformaticsĀ® Inc. <paudley@blackcatinformatics.ca>
// SPDX-License-Identifier: MIT OR Apache-2.0 OR MulanPSL-2.0

//! A sealed Tree over native immutable Git objects, without executable helpers.

use super::{Entry, EntryKind, FileId, Opened, SealedMeter, Tree, require_first_party};
use crate::meter::{ItemAdmission, Meter, Usage};
use crate::{
    LoadError, LoadErrorKind, LoadOptions, LockedCatalog, LockedPackageId, LockedSource,
    LockedSourceKind, RelPath, TreeError,
};
use purrdf_hash::{hex, sha1::Sha1};
use purrdf_lex::allocation::Memory;
use purrdf_lex::walk::{try_equal_range_by, try_sort_unstable_by};
use std::path::{Path, PathBuf};

/// A committed repository read by the native Git library, without checkout reads.
///
/// Only this host constructor admits a source to the locked package join. The
/// native object library owns decoding; public authority/FileId constructors cannot fabricate
/// admission. Complete committed listings govern paths and absence. This trusts
/// the native decoder and object store, not authorship or a clean checkout.
/// Decoder internals are outside RAF's portable meters; acquired buffers and
/// model processing retain their original grants.
pub struct CommittedGitTree<'a> {
    source: &'a LockedSource,
    tree: PinnedGitTree<'a>,
}
impl std::fmt::Debug for CommittedGitTree<'_> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("CommittedGitTree")
            .field("source", &self.source)
            .field("repository", &self.tree.repository)
            .finish_non_exhaustive()
    }
}

/// The committed objects of one caller-declared commit, without a lock identity.
///
/// It reads exactly like [`CommittedGitTree`]: committed objects only, through
/// the native library, with no process, fetch, hook, filter or checkout read.
/// It carries no locked source, so it never enters the locked package join or
/// supplies preferred-provider proof; it is comparison evidence only.
pub struct PinnedGitTree<'a> {
    repository: PathBuf,
    native: crate::git::Repository,
    commit: [u8; 20],
    entries: Vec<Record>,
    meter: SealedMeter<'a>,
    /// Whether a file the caller already holds is still read and hashed,
    /// which a locked snapshot requires and a declared pin does not.
    verify_seen: bool,
}
impl std::fmt::Debug for PinnedGitTree<'_> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("PinnedGitTree")
            .field("repository", &self.repository)
            .field("commit", &hex::Lower(&self.commit).to_string())
            .finish_non_exhaustive()
    }
}
struct Record {
    path: String,
    kind: EntryKind,
    id: [u8; 20],
    size: usize,
}

impl<'a> CommittedGitTree<'a> {
    /// Open an exact catalog Git commit natively, without processes or fetching.
    ///
    /// Paths come solely from committed objects. A dirty worktree is neither
    /// read nor certified clean. Locked source identities require SHA-1;
    /// portable callers retain the ordinary Tree boundary.
    ///
    /// # Errors
    /// Refuses missing repositories/commits, non-Git or unsupported revisions,
    /// invalid objects/modes and original resource failures.
    pub fn open(
        catalog: &'a LockedCatalog,
        package: LockedPackageId,
        repository: impl AsRef<Path>,
        options: &LoadOptions,
    ) -> Result<Self, LoadError> {
        let path = RelPath::root();
        let source = catalog
            .package(package)
            .and_then(|row| row.source())
            .filter(|source| source.kind() == LockedSourceKind::Git)
            .ok_or_else(|| refused(&path, "select an existing Git lock row with a full commit"))?;
        Self::checked(
            source,
            repository.as_ref(),
            SealedMeter::Owned(Meter::new(options)),
        )
    }
    pub(crate) fn under_meter<'m>(
        &'m self,
        meter: &'m Meter,
    ) -> Result<CommittedGitTree<'m>, LoadError> {
        CommittedGitTree::checked(
            self.source,
            &self.tree.repository,
            SealedMeter::Borrowed(meter),
        )
    }
    fn checked(
        source: &'a LockedSource,
        repository: &Path,
        meter: SealedMeter<'a>,
    ) -> Result<Self, LoadError> {
        let revision = source
            .revision()
            .filter(|revision| revision.len() == 40)
            .ok_or_else(|| {
                refused(
                    &RelPath::root(),
                    "committed Git source loading requires a full SHA-1 revision",
                )
            })?;
        let mut tree = PinnedGitTree::checked(revision, repository, meter)?;
        tree.verify_seen = true;
        Ok(Self { source, tree })
    }
    /// The exact locked source selected for this committed host Tree.
    #[must_use]
    pub const fn source(&self) -> &LockedSource {
        self.source
    }
    /// RAF's acquired protocol, native storage and processing charges.
    #[must_use]
    pub fn usage(&self) -> Usage {
        self.tree.usage()
    }
}

impl PinnedGitTree<'static> {
    /// Open the committed objects of `commit` in the repository at `repository`.
    ///
    /// `commit` is a full 40-character lowercase hexadecimal SHA-1 object ID.
    /// `repository` must be the repository's own checkout root or Git
    /// directory, not a directory inside another repository. The commit must
    /// already be present: nothing is fetched. A dirty worktree is neither read
    /// nor certified clean.
    ///
    /// # Errors
    /// Refuses a malformed commit ID, a missing repository or commit, a
    /// location that is not the repository root, invalid objects/modes and
    /// original resource failures.
    pub fn open(
        repository: impl AsRef<Path>,
        commit: &str,
        options: &LoadOptions,
    ) -> Result<Self, LoadError> {
        let path = RelPath::root();
        let mut id = [0; 20];
        if commit.len() != 40 || hex::decode_canonical_to_slice(commit, &mut id).is_err() {
            return Err(refused(
                &path,
                "a pinned commit is a full 40-character lowercase hexadecimal SHA-1 object ID",
            ));
        }
        let tree = Self::checked(
            commit,
            repository.as_ref(),
            SealedMeter::Owned(Meter::new(options)),
        )?;
        let roots = [tree.native.work_dir(), Some(tree.native.git_dir())];
        let canonical = |directory: &Path| directory.canonicalize().ok();
        if !roots
            .into_iter()
            .flatten()
            .any(|root| canonical(root).as_deref() == Some(tree.repository.as_path()))
        {
            return Err(refused(
                &path,
                "the pinned location is inside a repository rather than its root; name the repository's checkout root or Git directory",
            ));
        }
        Ok(tree)
    }
}

impl<'a> PinnedGitTree<'a> {
    fn checked(
        revision: &str,
        repository: &Path,
        meter: SealedMeter<'a>,
    ) -> Result<Self, LoadError> {
        let path = RelPath::root();
        let commit = parse_id(revision.as_bytes(), &path)?;
        let repository = super::disk::canonical_root(repository)
            .map_err(|source| git_io(&path, "repository", source))?;
        meter.items(repository.as_os_str().len(), &path, None)?;
        let native = crate::git::Repository::open_with_allocation_limit(
            &repository,
            meter.options().max_item_bytes,
        )
        .map_err(|error| git_io(&path, "repository", std::io::Error::other(error)))?;
        meter.work(revision.len().saturating_add(1), &path, None)?;
        meter.read_bytes(&path, revision.len())?;
        meter.items(revision.len(), &path, None)?;
        let actual = native
            .resolve_commit(revision)
            .map_err(|error| git_io(&path, "commit", std::io::Error::other(error)))?;
        if actual != revision {
            return Err(refused(
                &path,
                "the full commit ID does not name that exact commit",
            ));
        }
        let mut admission = ItemAdmission::new(&meter, &path);
        let listing = native
            .tree_admitted(
                revision,
                meter.options().max_total_bytes,
                &mut Memory::new(&mut admission),
            )
            .map_err(|error| match error.storage() {
                Some(storage) => admission.error(storage),
                None => git_io(&path, "tree", std::io::Error::other(error)),
            })?;
        let entries = admit_listing(listing, &meter, &path)?;
        Ok(Self {
            repository,
            native,
            commit,
            entries,
            meter,
            verify_seen: false,
        })
    }
    /// RAF's acquired protocol, native storage and processing charges.
    #[must_use]
    pub fn usage(&self) -> Usage {
        self.meter.usage()
    }

    fn record(&self, path: &str, file: &RelPath) -> Result<Option<&Record>, LoadError> {
        let found = try_equal_range_by(&self.entries, |record| {
            self.meter.work(
                record
                    .path
                    .len()
                    .saturating_add(path.len())
                    .saturating_add(1),
                file,
                None,
            )?;
            Ok::<_, LoadError>(record.path.as_str().cmp(path))
        })?;
        Ok(self.entries.get(found.start).filter(|_| !found.is_empty()))
    }
    fn locate(&self, path: &RelPath) -> Result<Option<(EntryKind, String)>, LoadError> {
        super::inventory::locate(path, &self.meter, |canonical| {
            self.record(canonical, path)
                .map(|record| record.map(|record| record.kind))
        })
    }

    fn file_record(&self, path: &RelPath) -> Result<&Record, TreeError> {
        require_first_party(path)?;
        let (kind, canonical) = self
            .locate(path)
            .map_err(|error| tree_failure(path, error))?
            .ok_or_else(|| TreeError::NotFound {
                path: path.to_string(),
            })?;
        if kind != EntryKind::File {
            return Err(TreeError::NotRegular {
                path: path.to_string(),
            });
        }
        self.record(&canonical, path)
            .map_err(|error| tree_failure(path, error))?
            .ok_or_else(|| {
                tree_failure(
                    path,
                    refused(path, "located Git file has no committed record"),
                )
            })
    }

    fn read_blob(
        &self,
        path: &RelPath,
        record: &Record,
        max_bytes: u64,
    ) -> Result<Vec<u8>, TreeError> {
        let limit = max_bytes.min(self.meter.options().max_file_bytes);
        if u64::try_from(record.size).unwrap_or(u64::MAX) > limit {
            return Err(TreeError::TooLarge {
                path: path.to_string(),
                limit,
            });
        }
        let mut admission = ItemAdmission::new(&self.meter, path);
        let mut memory = Memory::new(&mut admission);
        let result = (|| {
            let object = memory
                .format(&format_args!("{}", hex::Lower(&record.id)))
                .map_err(native)?;
            self.meter.work(1, path, None)?;
            let bytes = self
                .native
                .blob_admitted(&object, record.size, &mut memory)
                .map_err(|error| match error.storage() {
                    Some(storage) => native(storage),
                    None => git_io(path, "blob", std::io::Error::other(error)),
                })?;
            self.meter.read_bytes(path, bytes.len())?;
            if bytes.len() != record.size {
                return Err(refused(
                    path,
                    "committed blob size differs from its complete listing",
                ));
            }
            self.meter
                .work(bytes.len().saturating_add(64), path, None)?;
            let length = memory
                .format(&format_args!("{}", bytes.len()))
                .map_err(native)?;
            let mut hash = Sha1::new();
            hash.update(b"blob ");
            hash.update(length.as_bytes());
            hash.update(&[0]);
            hash.update(&bytes);
            if hash.finalize() != record.id {
                return Err(refused(
                    path,
                    "committed blob hash does not match its listed object ID",
                ));
            }
            Ok(bytes)
        })();
        result.map_err(|error| tree_failure(path, recover(error, &mut admission)))
    }
}

impl Tree for CommittedGitTree<'_> {
    fn read(&self, path: &RelPath, max_bytes: u64) -> Result<String, TreeError> {
        self.tree.read(path, max_bytes)
    }
    fn read_bytes(&self, path: &RelPath, max_bytes: u64) -> Result<Vec<u8>, TreeError> {
        self.tree.read_bytes(path, max_bytes)
    }
    fn read_unseen(
        &self,
        path: &RelPath,
        max_bytes: u64,
        seen: &dyn Fn(&FileId) -> bool,
    ) -> Result<Opened, TreeError> {
        self.tree.read_unseen(path, max_bytes, seen)
    }
    fn identity(&self, path: &RelPath) -> Result<FileId, TreeError> {
        self.tree.identity(path)
    }
    fn list(&self, path: &RelPath) -> Result<Vec<Entry>, TreeError> {
        self.tree.list(path)
    }
    fn entry(&self, path: &RelPath) -> Result<Option<EntryKind>, TreeError> {
        self.tree.entry(path)
    }
}

impl Tree for PinnedGitTree<'_> {
    fn read(&self, path: &RelPath, max_bytes: u64) -> Result<String, TreeError> {
        match self.read_unseen(path, max_bytes, &|_| false)? {
            Opened::Read { text, .. } => Ok(text),
            Opened::Seen(_) => Err(tree_failure(
                path,
                refused(path, "forced committed blob read returned Seen"),
            )),
        }
    }
    fn read_bytes(&self, path: &RelPath, max_bytes: u64) -> Result<Vec<u8>, TreeError> {
        self.read_blob(path, self.file_record(path)?, max_bytes)
    }
    fn read_unseen(
        &self,
        path: &RelPath,
        max_bytes: u64,
        seen: &dyn Fn(&FileId) -> bool,
    ) -> Result<Opened, TreeError> {
        let record = self.file_record(path)?;
        self.meter
            .items(crate::cost::shared_text(record.path.len()), path, None)
            .map_err(|error| tree_failure(path, error))?;
        let canonical_path = RelPath::new(&record.path).map_err(TreeError::from)?;
        let id = FileId::git(self.commit, &canonical_path);
        let previously_seen = seen(&id);
        if previously_seen && !self.verify_seen {
            return Ok(Opened::Seen(id));
        }
        let limit = if previously_seen { u64::MAX } else { max_bytes };
        let bytes = self.read_blob(path, record, limit)?;
        if previously_seen {
            return Ok(Opened::Seen(id));
        }
        let mut admission = ItemAdmission::new(&self.meter, path);
        let mut memory = Memory::new(&mut admission);
        let result = (|| {
            let text = std::str::from_utf8(&bytes).map_err(|_| {
                LoadError::tree(
                    path,
                    None,
                    &crate::Origin::Discovery,
                    TreeError::NotUtf8 {
                        path: path.to_string(),
                    },
                )
            })?;
            let text = memory.string(text).map_err(native)?;
            Ok(Opened::Read { id, text })
        })();
        result.map_err(|error| tree_failure(path, recover(error, &mut admission)))
    }
    fn list(&self, path: &RelPath) -> Result<Vec<Entry>, TreeError> {
        require_first_party(path)?;
        let (kind, canonical) = self
            .locate(path)
            .map_err(|error| tree_failure(path, error))?
            .ok_or_else(|| TreeError::NotFound {
                path: path.to_string(),
            })?;
        if kind != EntryKind::Dir {
            return Err(TreeError::NotRegular {
                path: path.to_string(),
            });
        }
        let mut admission = ItemAdmission::new(&self.meter, path);
        let mut memory = Memory::new(&mut admission);
        let result = (|| {
            let mut output = Vec::new();
            for record in &self.entries {
                self.meter.work(
                    record
                        .path
                        .len()
                        .saturating_add(canonical.len())
                        .saturating_add(1),
                    path,
                    None,
                )?;
                let (parent, name) = record
                    .path
                    .rsplit_once('/')
                    .unwrap_or(("", record.path.as_str()));
                if parent == canonical {
                    let name = memory.string(name).map_err(native)?;
                    memory
                        .push(&mut output, Entry::new(name, record.kind))
                        .map_err(native)?;
                }
            }
            Ok(output)
        })();
        result.map_err(|error| tree_failure(path, recover(error, &mut admission)))
    }
    fn entry(&self, path: &RelPath) -> Result<Option<EntryKind>, TreeError> {
        require_first_party(path)?;
        self.locate(path)
            .map(|entry| entry.map(|entry| entry.0))
            .map_err(|error| tree_failure(path, error))
    }
}
fn admit_listing(
    listing: Vec<crate::git::Entry>,
    meter: &Meter,
    path: &RelPath,
) -> Result<Vec<Record>, LoadError> {
    let mut admission = ItemAdmission::new(meter, path);
    let mut memory = Memory::new(&mut admission);
    let result = (|| {
        let mut records = Vec::new();
        for entry in listing {
            meter.work(entry.path.len().saturating_add(64), path, None)?;
            meter.read_bytes(path, entry.path.len().saturating_add(64))?;
            for name in entry.path.split('/') {
                validate_name(name, path)?;
            }
            meter.items(crate::cost::shared_text(entry.path.len()), path, None)?;
            let checked = RelPath::new(&entry.path)
                .map_err(|_| refused(path, "unsupported committed path"))?;
            if checked.as_str() != entry.path {
                return Err(refused(path, "committed path changes under normalization"));
            }
            let kind = match entry.kind {
                crate::git::EntryKind::Tree => EntryKind::Dir,
                crate::git::EntryKind::Blob => EntryKind::File,
                crate::git::EntryKind::Symlink => EntryKind::Symlink,
                crate::git::EntryKind::Submodule => {
                    return Err(refused(path, "Git submodules are not admitted"));
                }
            };
            let record = Record {
                path: memory.string(&entry.path).map_err(native)?,
                kind,
                id: parse_id(entry.id.as_bytes(), path)?,
                size: entry.size,
            };
            memory.push(&mut records, record).map_err(native)?;
        }
        try_sort_unstable_by(&mut records, |a, b| {
            meter.work(
                a.path.len().saturating_add(b.path.len()).saturating_add(1),
                path,
                None,
            )?;
            Ok::<_, LoadError>(a.path.cmp(&b.path))
        })?;
        Ok(records)
    })();
    result.map_err(|error| recover(error, &mut admission))
}

fn git_io(path: &RelPath, operation: &'static str, source: std::io::Error) -> LoadError {
    LoadError::new(path, None, LoadErrorKind::GitIo { operation, source })
}

fn parse_id(bytes: &[u8], path: &RelPath) -> Result<[u8; 20], LoadError> {
    if bytes.len() != 40 {
        return Err(refused(
            path,
            "Git source requires complete SHA-1 object IDs",
        ));
    }
    let mut id = [0; 20];
    let text = std::str::from_utf8(bytes).map_err(|_| refused(path, "invalid Git object ID"))?;
    hex::decode_to_slice(text, &mut id).map_err(|_| refused(path, "invalid Git object ID"))?;
    Ok(id)
}
fn validate_name(name: &str, path: &RelPath) -> Result<(), LoadError> {
    if name.is_empty() || name == "." || name == ".." || name.contains(['\\', '\0']) {
        return Err(refused(path, "Git directory has an invalid path component"));
    }
    Ok(())
}
fn refused(path: &RelPath, message: &str) -> LoadError {
    LoadError::new(
        path,
        None,
        LoadErrorKind::Locked {
            field: "git-source",
            message: message.to_owned(),
            source: None,
        },
    )
}
pub(super) fn native(source: purrdf_lex::allocation::StorageError) -> LoadError {
    LoadError::new(&RelPath::root(), None, LoadErrorKind::Storage(source))
}
pub(super) fn recover(error: LoadError, admission: &mut ItemAdmission<'_>) -> LoadError {
    match error.kind {
        LoadErrorKind::Storage(source) => admission.error(source),
        _ => error,
    }
}
fn tree_failure(path: &RelPath, source: LoadError) -> TreeError {
    TreeError::GitSource {
        path: path.to_string(),
        source: Box::new(source),
    }
}