feat(host): expose memcard controls and qualify process persistence
Intrepid/Prometeu/Runtime/pipeline/head This commit looks good
Intrepid/Prometeu/Runtime/pipeline/head This commit looks good
This commit is contained in:
@@ -37,6 +37,7 @@ pub struct Admission {
|
||||
pub state: PreparationState,
|
||||
pub snapshot_budget: usize,
|
||||
faulted: bool,
|
||||
replacement_observation: Option<super::diagnostics::SessionDiagnostics>,
|
||||
}
|
||||
impl Default for Admission {
|
||||
fn default() -> Self {
|
||||
@@ -48,10 +49,26 @@ impl Default for Admission {
|
||||
state: PreparationState::Idle,
|
||||
snapshot_budget: DEFAULT_SNAPSHOT_BUDGET,
|
||||
faulted: false,
|
||||
replacement_observation: None,
|
||||
}
|
||||
}
|
||||
}
|
||||
impl MemcardService {
|
||||
pub fn session_diagnostics(&self) -> super::diagnostics::SessionDiagnostics {
|
||||
self.admission.replacement_observation.clone().unwrap_or_else(|| self.session.diagnostics())
|
||||
}
|
||||
pub fn preparation_active(&self) -> bool {
|
||||
self.admission.receiver.is_some()
|
||||
}
|
||||
pub fn installed_card_id(&self) -> Option<[u8; 16]> {
|
||||
self.admission.installed.as_ref().map(|c| c.current().card_id)
|
||||
}
|
||||
pub fn cancel_pending(&mut self) {
|
||||
self.session.cancel_pending();
|
||||
if let Some(control) = &self.admission.control {
|
||||
control.cancel();
|
||||
}
|
||||
}
|
||||
pub fn host_faulted(&self) -> bool {
|
||||
self.admission.faulted || self.session.host_faulted()
|
||||
}
|
||||
@@ -60,7 +77,11 @@ impl MemcardService {
|
||||
}
|
||||
pub fn installed_health(&self) -> Option<(bool, bool, bool)> {
|
||||
self.admission.installed.as_ref().map(|card| {
|
||||
(card.recovered, card.degraded, card.write_blocked || card.storage_read_only())
|
||||
(
|
||||
card.recovered,
|
||||
card.degraded,
|
||||
card.write_blocked || card.storage_read_only() || card.uncertain,
|
||||
)
|
||||
})
|
||||
}
|
||||
pub fn card_has_game(&self, app_id: u32) -> bool {
|
||||
@@ -135,6 +156,9 @@ impl MemcardService {
|
||||
let owned =
|
||||
card.catalog.games.iter().find(|g| g.app_id == app_id).map_or(0, |g| g.slots.len());
|
||||
if choice == Choice::Persistent {
|
||||
if card.uncertain {
|
||||
return Err(Error::Indeterminate);
|
||||
}
|
||||
if card.storage_read_only() || card.write_blocked {
|
||||
return Err(Error::ReadOnly);
|
||||
}
|
||||
@@ -171,6 +195,10 @@ impl MemcardService {
|
||||
}
|
||||
Choice::Persistent => {
|
||||
let mut card = card.take().unwrap();
|
||||
if card.uncertain {
|
||||
session.retain_idle_card(card);
|
||||
return Err(Error::Indeterminate);
|
||||
}
|
||||
if card.storage_read_only() || card.write_blocked {
|
||||
session.retain_idle_card(card);
|
||||
return Err(Error::ReadOnly);
|
||||
@@ -256,9 +284,24 @@ impl MemcardService {
|
||||
&mut self,
|
||||
storage: Box<dyn MemcardStorage>,
|
||||
load_existing: bool,
|
||||
) -> Result<()> {
|
||||
self.replace_card_job(move |session, control| {
|
||||
session.replace_storage(storage, load_existing, control)
|
||||
})
|
||||
}
|
||||
pub fn reconcile_card_with(
|
||||
&mut self,
|
||||
open: impl FnOnce() -> Result<Box<dyn MemcardStorage>> + Send + 'static,
|
||||
) -> Result<()> {
|
||||
self.replace_card_job(move |session, control| session.reconcile_storage_with(open, control))
|
||||
}
|
||||
fn replace_card_job(
|
||||
&mut self,
|
||||
operation: impl FnOnce(&mut Session, &PublicationControl) -> Result<()> + Send + 'static,
|
||||
) -> Result<()> {
|
||||
if self.admission.receiver.is_some()
|
||||
|| self.session.has_in_flight()
|
||||
|| self.session.io_active()
|
||||
|| self.session.has_deferred_write()
|
||||
|| self.session.is_boot_active()
|
||||
{
|
||||
return Err(Error::Busy);
|
||||
@@ -266,6 +309,7 @@ impl MemcardService {
|
||||
if self.admission.faulted {
|
||||
return Err(Error::ReadOnly);
|
||||
}
|
||||
self.admission.replacement_observation = Some(self.session.diagnostics());
|
||||
let handoff = Arc::new(Mutex::new(Some(std::mem::take(&mut self.session))));
|
||||
let worker = handoff.clone();
|
||||
let control = Arc::new(PublicationControl::default());
|
||||
@@ -275,13 +319,14 @@ impl MemcardService {
|
||||
.lane
|
||||
.submit_memcard_commit_write(move |_| {
|
||||
let mut session = worker.lock().unwrap().take().unwrap();
|
||||
let result = session.replace_storage(storage, load_existing, &worker_control);
|
||||
let result = operation(&mut session, &worker_control);
|
||||
let _ = tx.send(Prepared::Replacement(session, result));
|
||||
crate::AsyncWorkJobOutcome::Completed
|
||||
})
|
||||
.is_err()
|
||||
{
|
||||
self.session = handoff.lock().unwrap().take().unwrap();
|
||||
self.admission.replacement_observation = None;
|
||||
return Err(Error::InvalidState);
|
||||
}
|
||||
self.admission.control = Some(control);
|
||||
@@ -331,6 +376,7 @@ impl MemcardService {
|
||||
}
|
||||
self.admission.receiver = None;
|
||||
self.admission.control = None;
|
||||
self.admission.replacement_observation = None;
|
||||
}
|
||||
Err(mpsc::TryRecvError::Disconnected) => {
|
||||
self.admission.state = PreparationState::Failed(Error::InvalidState);
|
||||
@@ -361,3 +407,37 @@ impl MemcardService {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(all(test, unix))]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use prometeu_drivers::memcard::FileMemcard;
|
||||
use prometeu_hal::memcard::MemcardIoError;
|
||||
|
||||
#[test]
|
||||
fn idle_uncertain_media_remains_blocked_before_another_persistent_admission() {
|
||||
let path = std::env::temp_dir().join(format!(
|
||||
"memcard-uncertain-idle-{}-{}",
|
||||
std::process::id(),
|
||||
std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).unwrap().as_nanos()
|
||||
));
|
||||
let io = FileMemcard::create(&path, IMAGE_BYTES, |io, id| {
|
||||
Card::format(io, id).map_err(|_| MemcardIoError::IoError)
|
||||
})
|
||||
.unwrap();
|
||||
let mut card = Card::mount(Box::new(io)).unwrap();
|
||||
card.uncertain = true;
|
||||
let mut service = MemcardService::new();
|
||||
// An ended session returns its backend to the same installed-card state.
|
||||
service.admission.installed = Some(card);
|
||||
assert_eq!(service.installed_health(), Some((false, false, true)));
|
||||
assert_eq!(
|
||||
service.choose_for_game(42, 1, Choice::Persistent, 0),
|
||||
Err(Error::Indeterminate)
|
||||
);
|
||||
assert!(service.card_installed());
|
||||
assert_eq!(service.lane.pending_depth(), 0);
|
||||
service.uninstall_card().unwrap();
|
||||
std::fs::remove_file(path).unwrap();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user