diff --git a/native/native_rust_library/src/backup.rs b/native/native_rust_library/src/backup.rs --- a/native/native_rust_library/src/backup.rs +++ b/native/native_rust_library/src/backup.rs @@ -3,7 +3,7 @@ aes, secure_store, BACKUP_SERVICE_CONNECTION_RETRY_DELAY, }; use crate::ffi::{ - get_backup_directory_path, get_backup_file_path, + get_backup_directory_path, get_backup_file_path, get_backup_log_file_path, get_backup_user_keys_file_path, secure_store_get, }; use crate::BACKUP_SOCKET_ADDR; @@ -12,13 +12,14 @@ use backup_client::{ BackupClient, BackupData, BackupDescriptor, DownloadLogsRequest, Error as BackupError, LatestBackupIDResponse, LogUploadConfirmation, - LogWSResponse, RequestedData, SinkExt, Stream, StreamExt, UploadLogRequest, - UserIdentity, WSError, + LogWSResponse, RequestedData, Sink, SinkExt, Stream, StreamExt, + UploadLogRequest, UserIdentity, WSError, }; use lazy_static::lazy_static; use regex::Regex; use serde::{Deserialize, Serialize}; use serde_json::json; +use std::collections::HashSet; use std::convert::Infallible; use std::error::Error; use std::future::Future; @@ -125,20 +126,23 @@ } }; - let mut _tx = Box::pin(tx); + let mut tx = Box::pin(tx); let mut rx = Box::pin(rx); + let logs_waiting_for_confirmation = Mutex::new(HashSet::::new()); + loop { let err = tokio::select! { - Err(err) = backup_data_sender(&backup_client, &user_identity) => err, - Err(err) = backup_confirmation_receiver(&mut rx) => err, + Err(err) = backup_data_sender(&backup_client, &user_identity, &mut tx, &logs_waiting_for_confirmation) => err, + Err(err) = backup_confirmation_receiver(&mut rx,&logs_waiting_for_confirmation) => err, }; println!("Backup handler error: '{err:?}'"); match err { BackupHandlerError::BackupError(_) | BackupHandlerError::BackupWSError(_) - | BackupHandlerError::WSClosed => break, + | BackupHandlerError::WSClosed + | BackupHandlerError::LockError => break, BackupHandlerError::IoError(_) | BackupHandlerError::CxxException(_) => continue, } @@ -152,6 +156,8 @@ async fn backup_data_sender( backup_client: &BackupClient, user_identity: &UserIdentity, + tx: &mut Pin>>, + logs_waiting_for_confirmation: &Mutex>, ) -> Result { loop { let mut file_stream = match tokio::fs::read_dir(&*BACKUP_FOLDER_PATH).await @@ -167,6 +173,10 @@ while let Some(file) = file_stream.next_entry().await? { let path = file.path(); + if logs_waiting_for_confirmation.lock()?.contains(&path) { + continue; + } + let Some(file_name) = path.file_name() else { continue; }; @@ -209,7 +219,24 @@ let content = tokio::fs::read(&path).await?; - if let Some(_) = log_id { + if let Some(log_id) = log_id { + let attachments_path = + get_backup_log_file_path(&backup_id, &log_id.to_string(), true)?; + let attachments = match tokio::fs::read(&attachments_path).await { + Ok(data) => Some(data.lines().collect::>()?), + Err(err) if err.kind() == ErrorKind::NotFound => None, + Err(err) => return Err(err.into()), + }; + + let log_data = UploadLogRequest { + backup_id, + log_id, + content, + attachments, + }; + tx.send(log_data.clone()).await?; + + logs_waiting_for_confirmation.lock()?.insert(path); } else { let user_keys_path = get_backup_user_keys_file_path(&backup_id)?; let user_keys = tokio::fs::read(&user_keys_path).await?; @@ -248,8 +275,27 @@ async fn backup_confirmation_receiver( rx: &mut Pin>>>, + logs_waiting_for_confirmation: &Mutex>, ) -> Result { - while let Some(_) = rx.next().await.transpose()? {} + while let Some(LogUploadConfirmation { backup_id, log_id }) = + rx.next().await.transpose()? + { + let log_id = log_id.to_string(); + + let path = get_backup_log_file_path(&backup_id, &log_id, false)?; + tokio::fs::remove_file(&path).await?; + + let attachments_path = get_backup_log_file_path(&backup_id, &log_id, true)?; + match tokio::fs::remove_file(&attachments_path).await { + Ok(()) => (), + Err(err) if err.kind() == ErrorKind::NotFound => (), + Err(err) => return Err(err.into()), + } + + logs_waiting_for_confirmation + .lock()? + .remove(&PathBuf::from(path)); + } Err(BackupHandlerError::WSClosed) } @@ -263,6 +309,13 @@ WSClosed, IoError(std::io::Error), CxxException(cxx::Exception), + LockError, +} + +impl From> for BackupHandlerError { + fn from(_: std::sync::PoisonError) -> Self { + Self::LockError + } } pub async fn create_backup(