use std::fs::{File, OpenOptions};
use std::io;
use std::path::Path;
const ACCESS_DENIED: i32 = 5;
#[derive(Debug, Clone, Copy)]
pub struct Probe {
restricted: bool,
agent_account: bool,
}
impl Probe {
pub fn system() -> Self {
let restricted = crate::commands::run_in_restricted_token();
Self {
restricted,
agent_account: !restricted && crate::commands::run_as_agent_account(),
}
}
}
pub(super) fn open_lock(lock: &Path) -> io::Result<File> {
OpenOptions::new().read(true).write(true).open(lock)
}
pub(super) fn refused_by_sandbox(error: &io::Error, probe: Probe) -> bool {
(probe.restricted || probe.agent_account) && error.raw_os_error() == Some(ACCESS_DENIED)
}
#[cfg(test)]
pub(super) fn unconfined() -> Probe {
Probe {
restricted: false,
agent_account: false,
}
}
#[cfg(test)]
pub(super) fn run_sandbox() -> (io::Error, Probe) {
(
io::Error::from_raw_os_error(ACCESS_DENIED),
Probe {
restricted: true,
agent_account: false,
},
)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn the_agent_accounts_refusal_is_relayed() {
let denied = io::Error::from_raw_os_error(ACCESS_DENIED);
let account = Probe {
restricted: false,
agent_account: true,
};
assert!(refused_by_sandbox(&denied, account));
assert!(!refused_by_sandbox(&denied, unconfined()));
let missing = io::Error::from(io::ErrorKind::NotFound);
assert!(!refused_by_sandbox(&missing, account));
}
}