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
// SPDX-FileCopyrightText: 2026 Blackcat InformaticsĀ® Inc. <paudley@blackcatinformatics.ca>
// SPDX-License-Identifier: MIT OR Apache-2.0 OR MulanPSL-2.0

//! Sealed registry contents authenticated against the selected archive checksum.

use super::git::{native as storage, recover};
use super::{Entry, EntryKind, FileId, Opened, SealedMeter, Tree, require_first_party};
use crate::meter::{ItemAdmission, Meter};
use crate::{
    LoadError, LoadErrorKind, LoadOptions, LockedCatalog, LockedPackage, LockedPackageId,
    LockedSource, LockedSourceKind, RelPath, TreeError,
};
use purrdf_lex::allocation::{Memory, Shared, StorageError};
use purrdf_lex::walk::{try_equal_range_by, try_sort_unstable_by};
use std::io;

/// Immutable package files from one checksum-authenticated registry archive.
///
/// This constructor consumes supplied bytes only: it never finds a cache, fetches,
/// extracts files or executes inspected code. Both compressed and decompressed
/// archive bytes are capped by `max_total_bytes`. Native gzip decoding and the
/// strict tar inventory run under RAF's item and work grants. RAF's
/// retained file payloads, listing and subsequent source operations are metered.
/// Authentication establishes equality to the lock checksum, not trusted authorship.
pub struct RegistryArchiveTree<'a> {
    package: &'a LockedPackage,
    source: &'a LockedSource,
    image: Shared<Image>,
    meter: SealedMeter<'a>,
}

struct Image {
    records: Vec<Record>,
    retained: usize,
}
struct Record {
    path: String,
    bytes: Option<Vec<u8>>,
}
impl<'a> RegistryArchiveTree<'a> {
    /// Authenticate and load the `.crate` bytes for an exact registry lock row.
    ///
    /// The native tar interpretation supplies paths and file bodies. RAF requires
    /// canonical paths under exactly `name-version/`, regular files/directories,
    /// a root Cargo manifest and an unambiguous file/directory inventory. Contents
    /// remain binary until a source read requires UTF-8.
    ///
    /// # Errors
    /// Refuses missing checksums, nonregistry rows, checksum mismatch, exhausted
    /// byte/storage/work grants, nested transport, native errors or diagnostics,
    /// links, special files and invalid package inventories. Original native
    /// causes remain available through the error chain.
    pub fn from_bytes(
        catalog: &'a LockedCatalog,
        package: LockedPackageId,
        archive: &[u8],
        options: &LoadOptions,
    ) -> Result<Self, LoadError> {
        let path = RelPath::root();
        let package = catalog
            .package(package)
            .filter(|row| {
                row.source().is_some_and(|source| {
                    matches!(
                        source.kind(),
                        LockedSourceKind::Registry | LockedSourceKind::SparseRegistry
                    )
                })
            })
            .ok_or_else(|| refused("select an existing registry lock row"))?;
        let checksum = package
            .checksum()
            .ok_or_else(|| refused("registry row needs its archive checksum"))?;
        let meter = Meter::new(options);
        meter.items(catalog.usage().item_bytes, &path, None)?;
        meter.read_bytes(&path, archive.len())?;
        meter.work(archive.len().saturating_add(1), &path, None)?;
        if purrdf_core::ContentDigest::of(archive).as_bytes() != checksum {
            return Err(refused(
                "supplied registry archive does not match the selected lock checksum",
            ));
        }
        let source = package
            .source()
            .ok_or_else(|| refused("registry source is absent"))?;
        let tar = decode(archive, options, &meter)?;
        let image = image(&tar, options, package, &meter)?;
        Ok(Self {
            package,
            source,
            image,
            meter: SealedMeter::Owned(meter),
        })
    }

    /// The registry identity whose archive checksum authenticated these contents.
    #[must_use]
    pub fn source(&self) -> &LockedSource {
        self.source
    }
    pub(crate) fn matches(&self, row: &LockedPackage) -> bool {
        self.package.name() == row.name()
            && self.package.version() == row.version()
            && self.package.source() == row.source()
            && self.package.checksum() == row.checksum()
    }
    pub(crate) fn under_meter<'m>(
        &'m self,
        meter: &'m Meter,
    ) -> Result<RegistryArchiveTree<'m>, LoadError> {
        meter.items(self.image.retained, &RelPath::root(), None)?;
        let image = self.image.try_clone().map_err(native_error)?;
        Ok(RegistryArchiveTree {
            package: self.package,
            source: self.source,
            image,
            meter: SealedMeter::Borrowed(meter),
        })
    }
    fn record(&self, name: &str, path: &RelPath) -> Result<Option<&Record>, LoadError> {
        let found = try_equal_range_by(&self.image.records, |record| {
            self.meter.work(
                record
                    .path
                    .len()
                    .saturating_add(name.len())
                    .saturating_add(1),
                path,
                None,
            )?;
            Ok::<_, LoadError>(record.path.as_str().cmp(name))
        })?;
        Ok(self
            .image
            .records
            .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| {
                    if record.bytes.is_some() {
                        EntryKind::File
                    } else {
                        EntryKind::Dir
                    }
                })
            })
        })
    }
}

fn decode(archive: &[u8], options: &LoadOptions, meter: &Meter) -> Result<Vec<u8>, LoadError> {
    let path = RelPath::root();
    let tar = purrdf_deflate::gzip::decompress_with_limit(archive, options.max_total_bytes as u64)
        .map_err(native_error)?;
    // The native importer detects transport even for its byte API. Refuse
    // another envelope: the strict inventory frames tar bytes only.
    if purrdf_gts::transport::detect_transport(&tar, None).is_some() {
        return Err(refused(
            "registry archive contains another transport envelope",
        ));
    }
    meter.items(tar.capacity(), &path, None)?;
    meter.work(tar.len().saturating_add(1), &path, None)?;
    Ok(tar)
}

fn image(
    tar: &[u8],
    options: &LoadOptions,
    package: &LockedPackage,
    meter: &Meter,
) -> Result<Shared<Image>, LoadError> {
    let path = RelPath::root();
    let mut admission = ItemAdmission::new(meter, &path);
    let limits = purrdf_gts::tar::inventory::Limits {
        source_bytes: tar.len() as u64,
        entries: tar.len() / 512 + 1,
        metadata_bytes: tar.len() as u64,
        storage_bytes: options.max_item_bytes,
        work: options.max_work as u64,
    };
    let inventory = purrdf_gts::tar::inventory::read(tar, limits, &mut admission)
        .map_err(|error| admission.recover(native_error(error)))?;
    let result = (|| {
        let mut memory = Memory::new(&mut admission);
        let prefix = memory
            .format(&format_args!("{}-{}/", package.name(), package.version()))
            .map_err(storage)?;
        let mut records = Vec::new();
        push_record(&mut records, "", None, &mut memory)?;
        for entry in inventory.entries() {
            let (head, tail) = entry
                .path()
                .and_then(purrdf_gts::tar::inventory::Name::utf8_parts)
                .map_err(native_error)?;
            let name = match head {
                Some(head) => memory
                    .format(&format_args!("{head}/{tail}"))
                    .map_err(storage)?,
                None => memory.string(tail).map_err(storage)?,
            };
            meter.work(name.len().saturating_add(1), &path, None)?;
            let relative = name
                .strip_prefix(&prefix)
                .ok_or_else(|| refused("registry entry is outside its package prefix"))?;
            let kind = match entry.kind() {
                purrdf_gts::files::FileEntryKind::File => EntryKind::File,
                purrdf_gts::files::FileEntryKind::Directory => EntryKind::Dir,
                _ => {
                    return Err(refused(
                        "registry entries must be regular files or directories",
                    ));
                }
            };
            let relative = if kind == EntryKind::Dir {
                relative.trim_end_matches('/')
            } else {
                relative
            };
            meter.items(crate::cost::shared_text(relative.len()), &path, None)?;
            let checked =
                RelPath::new(relative).map_err(|_| refused("invalid registry entry path"))?;
            if checked.as_str() != relative
                || checked.segments().any(|part| part == "..")
                || (relative.is_empty() && kind != EntryKind::Dir)
            {
                return Err(refused("registry paths must be canonical relative names"));
            }
            let bytes = if kind == EntryKind::File {
                let data = entry.payload().map_err(native_error)?;
                meter.work(data.len().saturating_add(1), &checked, None)?;
                let mut bytes = Vec::new();
                memory.reserve(&mut bytes, data.len()).map_err(storage)?;
                bytes.extend_from_slice(data);
                Some(bytes)
            } else {
                None
            };
            push_with_ancestors(&mut records, relative, bytes, &mut memory)?;
        }
        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))
        })?;
        meter.work(records.len().saturating_mul(11), &path, None)?;
        let retained = merge_directories(&mut records, meter)?;
        if !records
            .iter()
            .any(|record| record.path == "Cargo.toml" && record.bytes.is_some())
        {
            return Err(refused("registry package requires a root Cargo.toml file"));
        }
        let owner = Shared::<Image>::allocation_layout().size();
        meter.items(owner, &path, None)?;
        // A different load inherits only the image, after all archive-opening
        // scratch covered by this constructor's meter has been released.
        let retained = retained.saturating_add(owner);
        let image = Shared::try_new(Image { records, retained })
            .map_err(|_| storage(StorageError::AllocationFailed))?;
        Ok(image)
    })();
    result.map_err(|error| recover(error, &mut admission))
}

/// Record `relative` and the directories above it that no earlier entry recorded.
fn push_with_ancestors<S: purrdf_lex::allocation::Admission + ?Sized>(
    records: &mut Vec<Record>,
    relative: &str,
    bytes: Option<Vec<u8>>,
    memory: &mut Memory<'_, S>,
) -> Result<(), LoadError> {
    // The previous entry is the last record pushed. Its parent, when it is also
    // an ancestor of this entry, and every directory above it are already recorded.
    let recorded = records
        .last()
        .and_then(|previous| previous.path.rsplit_once('/'))
        .map(|(parent, _)| parent)
        .filter(|parent| {
            relative.len() > parent.len()
                && relative.starts_with(parent)
                && relative.as_bytes()[parent.len()] == b'/'
        })
        .map(str::len);
    let mut end = relative.len();
    while let Some(separator) = relative[..end].rfind('/') {
        if recorded.is_some_and(|recorded| separator <= recorded) {
            break;
        }
        push_record(records, &relative[..separator], None, memory)?;
        end = separator;
    }
    push_record(records, relative, bytes, memory)
}

fn push_record<S: purrdf_lex::allocation::Admission + ?Sized>(
    records: &mut Vec<Record>,
    path: &str,
    bytes: Option<Vec<u8>>,
    memory: &mut Memory<'_, S>,
) -> Result<(), LoadError> {
    let path = memory.string(path).map_err(storage)?;
    memory
        .push(records, Record { path, bytes })
        .map_err(storage)
}

/// Collapse repeated directory records of a sorted inventory; any repeated path
/// that is or names a file conflicts. Count the live storage of the resulting
/// inventory while compacting it.
fn merge_directories(records: &mut Vec<Record>, meter: &Meter) -> Result<usize, LoadError> {
    let mut kept = 0;
    let mut retained = records.capacity().saturating_mul(size_of::<Record>());
    for index in 0..records.len() {
        if kept > 0 {
            let (done, rest) = records.split_at(index);
            let (previous, current) = (&done[kept - 1], &rest[0]);
            meter.work(
                previous
                    .path
                    .len()
                    .saturating_add(current.path.len())
                    .saturating_add(1),
                &RelPath::root(),
                None,
            )?;
            if previous.path == current.path {
                if previous.bytes.is_some() || current.bytes.is_some() {
                    return Err(refused(
                        "registry inventory has conflicting file/directory paths",
                    ));
                }
                continue;
            }
        }
        let record = &records[index];
        retained = retained.saturating_add(record.path.capacity());
        if let Some(bytes) = &record.bytes {
            retained = retained.saturating_add(bytes.capacity());
        }
        records.swap(kept, index);
        kept += 1;
    }
    records.truncate(kept);
    Ok(retained)
}

impl Tree for RegistryArchiveTree<'_> {
    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(_) => unreachable!("the callback never marks a file seen"),
        }
    }
    fn read_unseen(
        &self,
        path: &RelPath,
        max_bytes: u64,
        seen: &dyn Fn(&FileId) -> bool,
    ) -> Result<Opened, TreeError> {
        require_first_party(path)?;
        let (_, canonical) = self
            .locate(path)
            .map_err(|error| tree_failure(path, error))?
            .ok_or_else(|| TreeError::NotFound {
                path: path.to_string(),
            })?;
        let record = self
            .record(&canonical, path)
            .map_err(|error| tree_failure(path, error))?
            .ok_or_else(|| TreeError::NotFound {
                path: path.to_string(),
            })?;
        let bytes = record.bytes.as_ref().ok_or_else(|| TreeError::NotRegular {
            path: path.to_string(),
        })?;
        self.meter
            .items(crate::cost::shared_text(record.path.len()), path, None)
            .map_err(|error| tree_failure(path, error))?;
        let id = FileId::from_key(record.path.as_bytes());
        if seen(&id) {
            return Ok(Opened::Seen(id));
        }
        let limit = max_bytes.min(self.meter.options().max_file_bytes);
        if bytes.len() as u64 > limit {
            return Err(TreeError::TooLarge {
                path: path.to_string(),
                limit,
            });
        }
        self.meter
            .read_bytes(path, bytes.len())
            .map_err(|error| tree_failure(path, error))?;
        self.meter
            .work(bytes.len().saturating_add(1), path, None)
            .map_err(|error| tree_failure(path, error))?;
        let text = std::str::from_utf8(bytes).map_err(|_| TreeError::NotUtf8 {
            path: path.to_string(),
        })?;
        let mut admission = ItemAdmission::new(&self.meter, path);
        let text = Memory::new(&mut admission)
            .string(text)
            .map_err(|error| tree_failure(path, admission.error(error)))?;
        Ok(Opened::Read { id, text })
    }
    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 result = (|| {
            let mut memory = Memory::new(&mut admission);
            let mut output = Vec::new();
            for record in &self.image.records {
                self.meter.work(
                    record
                        .path
                        .len()
                        .saturating_add(path.as_str().len())
                        .saturating_add(1),
                    path,
                    None,
                )?;
                let relative = if canonical.is_empty() {
                    record.path.as_str()
                } else {
                    let Some(relative) = record
                        .path
                        .strip_prefix(&canonical)
                        .and_then(|rest| rest.strip_prefix('/'))
                    else {
                        continue;
                    };
                    relative
                };
                if relative.is_empty() || relative.contains('/') {
                    continue;
                }
                let name = memory.string(relative).map_err(storage)?;
                let kind = if record.bytes.is_some() {
                    EntryKind::File
                } else {
                    EntryKind::Dir
                };
                memory
                    .push(&mut output, Entry::new(name, kind))
                    .map_err(storage)?;
            }
            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(|record| record.map(|(kind, _)| kind))
            .map_err(|error| tree_failure(path, error))
    }
}

fn refused(message: &str) -> LoadError {
    LoadError::new(
        &RelPath::root(),
        None,
        LoadErrorKind::Locked {
            field: "registry-source",
            message: message.to_owned(),
            source: None,
        },
    )
}
fn native_error(source: impl std::error::Error + Send + Sync + 'static) -> LoadError {
    let path = RelPath::root();
    LoadError::tree(
        &path,
        None,
        &crate::Origin::Discovery,
        TreeError::Io {
            path: "registry archive".to_owned(),
            source: io::Error::other(source),
        },
    )
}
fn tree_failure(path: &RelPath, source: LoadError) -> TreeError {
    TreeError::RegistrySource {
        path: path.to_string(),
        source: Box::new(source),
    }
}