From b16b1af65ee58622fe8dd7689703d8cf944156de Mon Sep 17 00:00:00 2001 From: Sashanoraa Date: Sun, 20 Jul 2025 17:36:45 -0400 Subject: [PATCH] Fixes #433 do not stop recording when deleting a past entry --- daemon/src/diag.rs | 67 ++++++++++++++++++++-------------------- daemon/src/qmdl_store.rs | 24 +++++++++----- 2 files changed, 50 insertions(+), 41 deletions(-) diff --git a/daemon/src/diag.rs b/daemon/src/diag.rs index 4d1da33..0faaa33 100644 --- a/daemon/src/diag.rs +++ b/daemon/src/diag.rs @@ -20,7 +20,7 @@ use tokio_util::task::TaskTracker; use crate::analysis::{AnalysisCtrlMessage, AnalysisWriter}; use crate::display; -use crate::qmdl_store::{RecordingStore, RecordingStoreError}; +use crate::qmdl_store::{EntryType, RecordingStore, RecordingStoreError}; use crate::server::ServerState; pub enum DiagDeviceCtrlMessage { @@ -199,41 +199,40 @@ pub async fn delete_recording( } let mut qmdl_store = state.qmdl_store_lock.write().await; match qmdl_store.delete_entry(&qmdl_name).await { - Err(RecordingStoreError::NoSuchEntryError) => { - return Err(( - StatusCode::BAD_REQUEST, - format!("no recording with name {qmdl_name}"), - )); + Err(RecordingStoreError::NoSuchEntryError) => Err(( + StatusCode::BAD_REQUEST, + format!("no recording with name {qmdl_name}"), + )), + Err(e) => Err(( + StatusCode::INTERNAL_SERVER_ERROR, + format!("couldn't delete recording: {e}"), + )), + Ok(entry_type) => { + if entry_type == EntryType::Current { + state + .diag_device_ctrl_sender + .send(DiagDeviceCtrlMessage::StopRecording) + .await + .map_err(|e| { + ( + StatusCode::INTERNAL_SERVER_ERROR, + format!("couldn't send stop recording message: {e}"), + ) + })?; + state + .ui_update_sender + .send(display::DisplayState::Paused) + .await + .map_err(|e| { + ( + StatusCode::INTERNAL_SERVER_ERROR, + format!("couldn't send ui update message: {e}"), + ) + })?; + } + Ok((StatusCode::ACCEPTED, "ok".to_string())) } - Err(e) => { - return Err(( - StatusCode::INTERNAL_SERVER_ERROR, - format!("couldn't delete recording: {e}"), - )); - } - Ok(_) => {} } - state - .diag_device_ctrl_sender - .send(DiagDeviceCtrlMessage::StopRecording) - .await - .map_err(|e| { - ( - StatusCode::INTERNAL_SERVER_ERROR, - format!("couldn't send stop recording message: {e}"), - ) - })?; - state - .ui_update_sender - .send(display::DisplayState::Paused) - .await - .map_err(|e| { - ( - StatusCode::INTERNAL_SERVER_ERROR, - format!("couldn't send ui update message: {e}"), - ) - })?; - Ok((StatusCode::ACCEPTED, "ok".to_string())) } pub async fn delete_all_recordings( diff --git a/daemon/src/qmdl_store.rs b/daemon/src/qmdl_store.rs index 6a9395c..4fbc700 100644 --- a/daemon/src/qmdl_store.rs +++ b/daemon/src/qmdl_store.rs @@ -56,6 +56,12 @@ pub struct ManifestEntry { pub arch: Option, } +#[derive(PartialEq, Eq)] +pub enum EntryType { + Current, + Past, +} + impl ManifestEntry { fn new() -> Self { let now = Local::now(); @@ -341,20 +347,24 @@ impl RecordingStore { Some((entry_index, &self.manifest.entries[entry_index])) } - pub async fn delete_entry(&mut self, name: &str) -> Result { + pub async fn delete_entry(&mut self, name: &str) -> Result { let entry_to_delete_idx = self .manifest .entries .iter() .position(|entry| entry.name == name) .ok_or(RecordingStoreError::NoSuchEntryError)?; - if let Some(current_entry) = self.current_entry { - if current_entry == entry_to_delete_idx { + let is_current = match self.current_entry { + Some(current_entry) if current_entry == entry_to_delete_idx => { self.close_current_entry().await?; - } else { - self.current_entry = Some(current_entry - 1); + EntryType::Current } - } + Some(current_entry) => { + self.current_entry = Some(current_entry - 1); + EntryType::Past + } + None => EntryType::Past, + }; let entry_to_delete = self.manifest.entries.remove(entry_to_delete_idx); self.write_manifest().await?; let qmdl_filepath = entry_to_delete.get_qmdl_filepath(&self.path); @@ -365,7 +375,7 @@ impl RecordingStore { remove_file_if_exists(&analysis_filepath) .await .map_err(RecordingStoreError::DeleteFileError)?; - Ok(entry_to_delete) + Ok(is_current) } pub async fn delete_all_entries(&mut self) -> Result<(), RecordingStoreError> {