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};
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()
}
}
pub struct PinnedGitTree<'a> {
repository: PathBuf,
native: crate::git::Repository,
commit: [u8; 20],
entries: Vec<Record>,
meter: SealedMeter<'a>,
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> {
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 })
}
#[must_use]
pub const fn source(&self) -> &LockedSource {
self.source
}
#[must_use]
pub fn usage(&self) -> Usage {
self.tree.usage()
}
}
impl PinnedGitTree<'static> {
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,
})
}
#[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),
}
}