use std::sync::Arc;
use rustix::io::Errno;
use crate::error::{Error, Failures};
#[derive(Debug, Clone, Copy)]
enum Call {
PidfdOpen,
PidfsIno,
Poll,
Openat2,
}
#[derive(Debug, Clone, Copy)]
pub(crate) struct FailedCall {
call: Call,
errno: Errno,
}
impl FailedCall {
pub(crate) const fn pidfd_open(errno: Errno) -> FailedCall {
FailedCall {
call: Call::PidfdOpen,
errno,
}
}
pub(crate) const fn pidfs_ino(errno: Errno) -> FailedCall {
FailedCall {
call: Call::PidfsIno,
errno,
}
}
pub(crate) const fn poll(errno: Errno) -> FailedCall {
FailedCall {
call: Call::Poll,
errno,
}
}
pub(crate) const fn openat2(errno: Errno) -> FailedCall {
FailedCall {
call: Call::Openat2,
errno,
}
}
pub(crate) const fn name(&self) -> &'static str {
match self.call {
Call::PidfdOpen => "pidfd_open",
Call::PidfsIno => "pidfs_ino",
Call::Poll => "poll",
Call::Openat2 => "openat2",
}
}
pub(crate) const fn errno(&self) -> Errno {
self.errno
}
pub(crate) fn requirement(&self) -> Option<String> {
let (floor, errno) = match (self.call, self.errno) {
(Call::PidfdOpen, Errno::NOSYS) => ("5.3", "ENOSYS"),
(Call::PidfdOpen, Errno::PERM) => ("5.3", "EPERM"),
(Call::PidfdOpen, Errno::ACCESS) => ("5.3", "EACCES"),
(Call::PidfdOpen, Errno::NODEV) => ("5.3", "ENODEV"),
(Call::Openat2, Errno::NOSYS) => ("5.6", "ENOSYS"),
(Call::Openat2, Errno::PERM) => ("5.6", "EPERM"),
(Call::Openat2, Errno::NODEV) => ("5.6", "ENODEV"),
_ => return None,
};
let name = self.name();
Some(format!(
"cosca requires {name} (Linux \u{2265} {floor}), refused here: {name} answered {errno}"
))
}
pub(crate) fn refusal(self) -> Option<Refusal> {
self.requirement().map(|requirement| Refusal {
call: self,
requirement,
beside: None,
})
}
pub(crate) fn unsupported(&self, op: impl Into<String>) -> Option<Error> {
self.refusal().map(|refusal| refusal.unsupported(op))
}
}
#[derive(Debug, Clone)]
pub(crate) struct Refusal {
call: FailedCall,
requirement: String,
beside: Option<Arc<Error>>,
}
impl Refusal {
pub(crate) fn beside(mut self, failure: Arc<Error>) -> Refusal {
self.beside = Some(failure);
self
}
pub(crate) fn requirement(&self) -> &str {
&self.requirement
}
pub(crate) fn os_error(&self) -> std::io::Error {
std::io::Error::from(self.call.errno())
}
pub(crate) fn cause(&self) -> Error {
let own = self.os_error();
match &self.beside {
None => Error::Io(own),
Some(beside) => Error::Several(Failures::new(Error::io_cause(own), [Arc::clone(beside)])),
}
}
pub(crate) fn cause_io(&self) -> std::io::Error {
self.cause()
.into_io_over("the refusal and the failure it was met beside")
}
pub(crate) fn unsupported(&self, op: impl Into<String>) -> Error {
Error::Unsupported {
op: op.into(),
platform: "linux",
detail: self.requirement.clone(),
source: Some(Arc::new(self.cause())),
}
}
}
pub(crate) fn warn_refused<E: std::error::Error + 'static>(caller: &str, error: E) -> E {
log::warn!("{caller}: {report}", report = crate::error::Report(&error));
error
}