use super::CommandEnv;
use base::interface::exec::{EnvScope, ReadScope};
use base::path::resolve_physically;
use std::ffi::{OsStr, OsString};
use std::path::{Path, PathBuf};
const HOME_READ_ROOTS: &[&str] = &[
".gitconfig",
".config/git/config",
".config/git/ignore",
".config/git/attributes",
#[cfg(target_os = "macos")]
".CFUserTextEncoding",
];
const XDG_CONFIG_READ_ROOTS: &[&str] = &["git/config", "git/ignore", "git/attributes"];
#[cfg(target_os = "macos")]
fn selected_developer_dir() -> Option<PathBuf> {
std::fs::canonicalize("/private/var/db/xcode_select_link").ok()
}
#[cfg(not(target_os = "macos"))]
fn selected_developer_dir() -> Option<PathBuf> {
None
}
#[derive(Debug, Clone, Default)]
pub(super) struct UserDirs {
pub(super) home: Option<PathBuf>,
pub(super) xdg_config_home: Option<PathBuf>,
}
impl UserDirs {
pub(super) fn of(env: CommandEnv<'_>) -> Self {
Self {
home: var(env, "HOME").map(PathBuf::from),
xdg_config_home: var(env, "XDG_CONFIG_HOME")
.filter(|v| !v.is_empty())
.map(PathBuf::from),
}
}
}
pub(super) fn var(env: CommandEnv<'_>, name: &str) -> Option<OsString> {
env.set
.iter()
.rev()
.find(|(k, _)| k == name)
.map(|(_, v)| OsString::from(v))
.or_else(|| {
let inherited =
env.scope == EnvScope::Session || EnvScope::keeps_under_minimal(OsStr::new(name));
inherited.then(|| std::env::var_os(name)).flatten()
})
}
pub(super) fn strict_read_roots(scope: &[PathBuf], dirs: &UserDirs) -> Vec<PathBuf> {
let mut roots: Vec<PathBuf> = super::SYSTEM_READ_ROOTS
.iter()
.map(|root| resolve_physically(Path::new(root)))
.chain(selected_developer_dir())
.chain(scope_read_roots(scope, dirs))
.collect();
roots.sort();
roots.dedup();
roots
}
fn scope_read_roots(scope: &[PathBuf], dirs: &UserDirs) -> Vec<PathBuf> {
dirs.home
.iter()
.flat_map(|h| HOME_READ_ROOTS.iter().map(move |r| h.join(r)))
.chain(
dirs.xdg_config_home
.iter()
.flat_map(|x| XDG_CONFIG_READ_ROOTS.iter().map(move |r| x.join(r))),
)
.chain(scope.iter().cloned())
.map(|p| resolve_physically(&p))
.collect()
}
pub(super) fn unmet(read: &ReadScope, dirs: &UserDirs, path: Option<&OsStr>) -> Vec<String> {
let ReadScope::Only(scope) = read else {
return Vec::new();
};
let home = dirs.home.as_deref();
let roots = strict_read_roots(scope, dirs);
let readable = |p: &Path| roots.iter().any(|root| p.starts_with(root));
let mut unmet = Vec::new();
if let Some(git) = path.and_then(git_on_path) {
let real = resolve_physically(&git);
if !readable(&real) {
unmet.push(format!(
"git: the `git` on PATH is {}, which a command under the strict profile cannot \
read, so git does not run there",
real.display()
));
}
}
let mut seen = Vec::new();
let xdg_config = dirs
.xdg_config_home
.clone()
.or_else(|| home.map(|h| h.join(".config")))
.map(|dir| dir.join("git/config"));
let configs = home
.map(|h| h.join(".gitconfig"))
.into_iter()
.chain(xdg_config);
for config in configs {
let real = resolve_physically(&config);
if real.is_file() && !readable(&real) {
unmet.push(format!(
"git: the user's git configuration is {}, which a command under the strict \
profile cannot read, so git stops there",
real.display()
));
}
for included in git_includes(&config, home, &mut seen) {
if !readable(&included) {
unmet.push(format!(
"git: the user's git configuration includes {}, which a command under the \
strict profile cannot read, so git stops there",
included.display()
));
}
}
}
unmet
}
fn git_on_path(path: &OsStr) -> Option<PathBuf> {
use std::os::unix::fs::PermissionsExt;
std::env::split_paths(path)
.map(|dir| dir.join("git"))
.find(|git| {
std::fs::metadata(git).is_ok_and(|m| m.is_file() && m.permissions().mode() & 0o111 != 0)
})
}
fn git_includes(config: &Path, home: Option<&Path>, seen: &mut Vec<PathBuf>) -> Vec<PathBuf> {
let config = resolve_physically(config);
if seen.contains(&config) {
return Vec::new();
}
seen.push(config.clone());
let Ok(text) = std::fs::read_to_string(&config) else {
return Vec::new();
};
let mut found = Vec::new();
let mut including = false;
for line in text.lines().map(str::trim) {
if let Some(header) = line.strip_prefix('[') {
let name = header
.split(|c: char| c == ']' || c.is_whitespace() || c == '"')
.next()
.unwrap_or_default()
.to_ascii_lowercase();
including = name == "include" || name == "includeif";
continue;
}
let Some((key, value)) = line.split_once('=') else {
continue;
};
if !including || !key.trim().eq_ignore_ascii_case("path") {
continue;
}
let value = value.trim().trim_matches('"');
let named = match (value.strip_prefix("~/"), home) {
(Some(rest), Some(home)) => home.join(rest),
_ => config.parent().unwrap_or(Path::new("/")).join(value),
};
let included = resolve_physically(&named);
if included.is_file() {
found.extend(git_includes(&included, home, seen));
found.push(included);
}
}
found
}
#[cfg(target_os = "macos")]
pub(super) fn macos_rules(read: &ReadScope, dirs: &UserDirs) -> String {
let ReadScope::Only(scope) = read else {
return String::new();
};
let path = |p: &Path| super::sandbox_escape(&p.display().to_string());
let mut s = String::from(
"(deny file-read*)\n(allow file-read-metadata)\n(allow file-read* (literal \"/\")",
);
for p in strict_read_roots(scope, dirs)
.iter()
.flat_map(|p| super::macos::spellings(p))
{
s.push_str(&format!(" (subpath \"{}\")", path(&p)));
}
if let Some(tmp) = super::macos::darwin_user_temp_dir() {
s.push_str(&format!(" (literal \"{}\")", path(&tmp.join("xcrun_db"))));
}
s.push_str(")\n");
s
}
#[cfg(target_os = "linux")]
pub(super) struct LinuxReadScope<'a> {
scope: Option<&'a [PathBuf]>,
dirs: UserDirs,
readable: Option<Vec<PathBuf>>,
}
#[cfg(target_os = "linux")]
impl<'a> LinuxReadScope<'a> {
pub(super) fn new(read: &'a ReadScope, dirs: UserDirs) -> Self {
let scope = match read {
ReadScope::Everything => None,
ReadScope::Only(scope) => Some(scope.as_slice()),
};
Self {
scope,
readable: scope.map(|scope| strict_read_roots(scope, &dirs)),
dirs,
}
}
pub(super) fn base_mounts(&self, args: &mut Vec<String>) {
match self.scope {
None => super::linux::bind(args, "--ro-bind", Path::new("/")),
Some(_) => {
for root in super::SYSTEM_READ_ROOTS {
super::linux::bind(args, "--ro-bind-try", Path::new(root));
}
}
}
}
pub(super) fn scope_mounts(&self, args: &mut Vec<String>) {
let roots = self.scope.map(|scope| scope_read_roots(scope, &self.dirs));
for root in roots.iter().flatten() {
super::linux::bind(args, "--ro-bind-try", root);
}
}
pub(super) fn shows(&self, path: &Path) -> bool {
self.readable
.as_ref()
.is_none_or(|readable| readable.iter().any(|r| path.starts_with(r)))
}
}