use std::io;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum Point {
Preallocate,
MapSegment,
Link,
CheckpointSync,
RetentionRename,
RetentionCopySync,
RetentionDestinationSync,
SparePreallocate,
SpareMap,
SpareHeader,
}
#[cfg(feature = "fault-injection")]
mod armed {
use super::Point;
use std::cell::RefCell;
thread_local! {
pub(super) static ARMED: RefCell<Vec<(Point, i32)>> = const { RefCell::new(Vec::new()) };
}
}
#[cfg(feature = "fault-injection")]
pub fn fail_next(point: Point, errno: i32) {
armed::ARMED.with(|a| a.borrow_mut().push((point, errno)));
}
#[cfg(feature = "fault-injection")]
pub fn clear() {
armed::ARMED.with(|a| a.borrow_mut().clear());
background::ARMED
.lock()
.unwrap_or_else(|e| e.into_inner())
.clear();
}
#[cfg(feature = "fault-injection")]
pub fn fail_next_background(point: Point, errno: i32) {
background::ARMED
.lock()
.unwrap_or_else(|e| e.into_inner())
.push((point, errno));
}
#[cfg(feature = "fault-injection")]
mod background {
use super::Point;
use std::sync::Mutex;
pub(super) static ARMED: Mutex<Vec<(Point, i32)>> = Mutex::new(Vec::new());
}
#[cfg(feature = "fault-injection")]
pub(crate) fn inject_background(point: Point) -> io::Result<()> {
let mut armed = background::ARMED.lock().unwrap_or_else(|e| e.into_inner());
match armed.iter().position(|(p, _)| *p == point) {
Some(i) => Err(io::Error::from_raw_os_error(armed.remove(i).1)),
None => Ok(()),
}
}
#[cfg(not(feature = "fault-injection"))]
#[inline(always)]
pub(crate) fn inject_background(_point: Point) -> io::Result<()> {
Ok(())
}
#[cfg(feature = "fault-injection")]
pub(crate) fn inject(point: Point) -> io::Result<()> {
armed::ARMED.with(|a| {
let mut a = a.borrow_mut();
match a.iter().position(|(p, _)| *p == point) {
Some(i) => Err(io::Error::from_raw_os_error(a.remove(i).1)),
None => Ok(()),
}
})
}
#[cfg(not(feature = "fault-injection"))]
#[inline(always)]
pub(crate) fn inject(_point: Point) -> io::Result<()> {
Ok(())
}