use std::path::{Component, Path};
use crate::domain::{FileDisposition, IndexTargets, LanguageId, MetadataOnlyReason};
use crate::live_index::LiveIndex;
use crate::protocol::format;
#[derive(Debug, PartialEq, Eq)]
pub enum GenerationResolution<'a> {
Published(&'a [u8]),
NotInGeneration,
}
pub fn resolve_generation_bytes<'a>(
live: &'a LiveIndex,
relative_path: &str,
) -> GenerationResolution<'a> {
match live.get_file(relative_path) {
Some(file) => GenerationResolution::Published(&file.content),
None => GenerationResolution::NotInGeneration,
}
}
pub fn observe_disk_beneath(
live: &LiveIndex,
workspace_root: &Path,
relative_path: &str,
) -> Result<Vec<u8>, String> {
let candidate = Path::new(relative_path);
let escapes = candidate.components().any(|component| {
matches!(
component,
Component::ParentDir | Component::RootDir | Component::Prefix(_)
)
});
if escapes || candidate.is_absolute() {
return Err(format!(
"{relative_path} [error: path escapes the workspace root; a disk \
observation is confined beneath it]"
));
}
refuse_disk_spelling(workspace_root, relative_path)?;
let full_path = workspace_root.join(candidate);
admit_disk_read(live, relative_path, &full_path)
}
fn refuse_disk_spelling(root: &Path, relative_path: &str) -> Result<(), String> {
crate::protocol::edit::refuse_path_alias(root, relative_path)?;
match hard_scope_refusal(relative_path) {
Some(refusal) => Err(refusal),
None => Ok(()),
}
}
pub(crate) fn hard_scope_refusal(relative_path: &str) -> Option<String> {
crate::discovery::path_is_hard_scope_excluded(Path::new(relative_path)).then(|| {
format!(
"{relative_path} [error: VCS and runtime-state internals are outside \
source scope; a disk observation never reads them]"
)
})
}
pub(crate) fn admit_worktree_text(
live: &LiveIndex,
repo: &crate::git::GitRepo,
relative_path: &str,
) -> Result<Option<String>, String> {
let Some(workdir) = repo.workdir() else {
return Err("bare repository has no working directory".to_string());
};
let full_path = workdir.join(relative_path);
if !full_path.is_file() {
return Ok(None);
}
refuse_disk_spelling(workdir, relative_path)?;
let bytes = admit_disk_read(live, relative_path, &full_path)?;
Ok(String::from_utf8(bytes).ok())
}
pub(crate) fn disk_read_would_refuse(
live: &LiveIndex,
relative_path: &str,
size: Option<u64>,
) -> bool {
if crate::knowledge::sensitive_path_rule(relative_path).is_some() {
return true;
}
if let Some(FileDisposition::MetadataOnly {
reason:
MetadataOnlyReason::SensitivePath { .. } | MetadataOnlyReason::SensitiveContent { .. },
}) = live.capture_file_disposition(relative_path)
{
return true;
}
size.is_some_and(|size| crate::knowledge::exceeds_scan_limit(size as usize))
}
pub(crate) fn refuse_by_policy(live: &LiveIndex, relative_path: &str) -> Option<String> {
if crate::knowledge::sensitive_path_rule(relative_path).is_some() {
return Some(format::content_withheld_by_admission(relative_path));
}
if let Some(FileDisposition::MetadataOnly { reason }) =
live.capture_file_disposition(relative_path)
{
match reason {
MetadataOnlyReason::SensitiveContent { rule_ids, .. }
if rule_ids
.iter()
.any(|id| id == crate::knowledge::INDETERMINATE_RULE_ID) =>
{
return Some(format::content_withheld_unscanned(relative_path));
}
MetadataOnlyReason::SensitivePath { .. }
| MetadataOnlyReason::SensitiveContent { .. } => {
return Some(format::content_withheld_by_admission(relative_path));
}
_ => {}
}
}
None
}
pub(crate) fn admit_bytes(
live: &LiveIndex,
relative_path: &str,
bytes: Vec<u8>,
) -> Result<Vec<u8>, String> {
if let Some(refusal) = refuse_by_policy(live, relative_path) {
return Err(refusal);
}
if let Some(refusal) = classify_admitted_bytes(relative_path, &bytes) {
return Err(refusal);
}
Ok(bytes)
}
pub(crate) fn admit_git_text(
live: &LiveIndex,
repo: &crate::git::GitRepo,
git_ref: &str,
relative_path: &str,
) -> Result<Option<String>, String> {
if let Some(refusal) = refuse_by_policy(live, relative_path) {
return Err(refusal);
}
let Some(text) = repo.file_at_ref(git_ref, relative_path)? else {
return Ok(None);
};
let admitted = admit_bytes(live, relative_path, text.into_bytes())?;
Ok(String::from_utf8(admitted).ok())
}
pub(crate) fn admit_disk_read(
live: &LiveIndex,
relative_path: &str,
canon_path: &Path,
) -> Result<Vec<u8>, String> {
if let Some(refusal) = refuse_by_policy(live, relative_path) {
return Err(refusal);
}
let bytes = match std::fs::read(canon_path) {
Ok(bytes) => bytes,
Err(e) => return Err(format!("{relative_path} [error: could not read file: {e}]")),
};
if let Some(refusal) = classify_admitted_bytes(relative_path, &bytes) {
return Err(refusal);
}
Ok(bytes)
}
fn classify_admitted_bytes(relative_path: &str, bytes: &[u8]) -> Option<String> {
if crate::knowledge::exceeds_scan_limit(bytes.len())
|| crate::knowledge::decode_searchable_text(bytes).is_err()
{
return Some(format::content_withheld_unscanned(relative_path));
}
let language = Path::new(relative_path)
.extension()
.and_then(|extension| extension.to_str())
.and_then(LanguageId::from_extension);
let targets = IndexTargets::for_path(relative_path, language.as_ref());
if let crate::knowledge::StableContentAdmission::MetadataOnly(
MetadataOnlyReason::SensitiveContent { rule_ids, .. },
) = crate::knowledge::classify_stable_content(relative_path, targets, bytes)
{
return Some(
if rule_ids
.iter()
.any(|id| id == crate::knowledge::INDETERMINATE_RULE_ID)
{
format::content_withheld_unscanned(relative_path)
} else {
format::content_withheld_by_admission(relative_path)
},
);
}
None
}
pub struct ReadGate;
impl ReadGate {
pub fn admit_disk_read(
live: &LiveIndex,
relative_path: &str,
canon_path: &Path,
) -> Result<Vec<u8>, String> {
admit_disk_read(live, relative_path, canon_path)
}
}