diff --git a/Cargo.toml b/Cargo.toml index e1750d8..12bc988 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -32,3 +32,4 @@ proxmox-backup = { git = "git://git.proxmox.com/git/proxmox-backup.git", tag = " #proxmox-backup = { path = "../proxmox-backup" } serde_json = "1.0" tokio = { version = "0.2.9", features = [ "blocking", "fs", "io-util", "macros", "rt-threaded", "signal", "stream", "tcp", "time", "uds" ] } +bincode = "1.0" diff --git a/current-api.h b/current-api.h index d77eff6..77e8c4b 100644 --- a/current-api.h +++ b/current-api.h @@ -263,6 +263,26 @@ void proxmox_backup_write_data_async(ProxmoxBackupHandle *handle, int *result, char **error); +/** + * Serialize all state data into a byte buffer. Can be loaded again with + * proxmox_import_state. Use for migration for example. + * + * Length of the returned buffer is written to buf_size. Returned buffer must + * be freed with proxmox_free_state_buf. + */ +uint8_t *proxmox_export_state(uintptr_t *buf_size); + +/** + * Free a buffer acquired from proxmox_export_state. + */ +void proxmox_free_state_buf(uint8_t *buf); + +/** + * Load state serialized by proxmox_export_state. If loading fails, a message + * will be logged to stderr, but the function will not fail. + */ +void proxmox_import_state(const uint8_t *buf, uintptr_t buf_size); + /** * Open connection to the backup server (sync) * diff --git a/src/commands.rs b/src/commands.rs index f7e0f62..7a24b7c 100644 --- a/src/commands.rs +++ b/src/commands.rs @@ -16,6 +16,9 @@ use crate::upload_queue::*; use lazy_static::lazy_static; lazy_static!{ + // Note: Any state stored here that needs to be sent along with migration + // needs to be specified in (de)serialize_state as well! + static ref PREVIOUS_CSUMS: Mutex> = { Mutex::new(HashMap::new()) }; @@ -35,6 +38,22 @@ pub struct ImageUploadInfo { } +pub(crate) fn serialize_state() -> Vec { + let prev_csums = &*PREVIOUS_CSUMS.lock().unwrap(); + let prev_crypt_digest = &*PREVIOUS_CRYPT_CONFIG_DIGEST.lock().unwrap(); + bincode::serialize(&(prev_csums, prev_crypt_digest)).unwrap() +} + +pub(crate) fn deserialize_state(data: &[u8]) -> Result<(), Error> { + let (prev_csums, prev_crypt_digest) = bincode::deserialize(data)?; + let mut prev_csums_guard = PREVIOUS_CSUMS.lock().unwrap(); + let mut prev_crypt_digest_guard = PREVIOUS_CRYPT_CONFIG_DIGEST.lock().unwrap(); + *prev_csums_guard = prev_csums; + *prev_crypt_digest_guard = prev_crypt_digest; + Ok(()) +} + + // Note: We alway register/upload a chunk containing zeros async fn register_zero_chunk( client: Arc, diff --git a/src/lib.rs b/src/lib.rs index b9ca905..8511cbf 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -967,3 +967,37 @@ pub extern "C" fn proxmox_restore_read_image_at_async( callback_info.send_result(result); }); } + +/// Serialize all state data into a byte buffer. Can be loaded again with +/// proxmox_import_state. Use for migration for example. +/// +/// Length of the returned buffer is written to buf_size. Returned buffer must +/// be freed with proxmox_free_state_buf. +#[no_mangle] +#[allow(clippy::not_unsafe_ptr_arg_deref)] +pub extern "C" fn proxmox_export_state(buf_size: *mut usize) -> *mut u8 { + let data = commands::serialize_state().into_boxed_slice(); + unsafe { *buf_size = data.len(); } + Box::leak(data).as_mut_ptr() +} + +/// Load state serialized by proxmox_export_state. If loading fails, a message +/// will be logged to stderr, but the function will not fail. +#[no_mangle] +#[allow(clippy::not_unsafe_ptr_arg_deref)] +pub extern "C" fn proxmox_import_state(buf: *const u8, buf_size: usize) { + let data = unsafe { std::slice::from_raw_parts(buf, buf_size) }; + // ignore errors, just log what happened + if let Err(err) = commands::deserialize_state(data) { + eprintln!("error deserializing PBS state - {}", err); + } +} + +/// Free a buffer acquired from proxmox_export_state. +#[no_mangle] +#[allow(clippy::not_unsafe_ptr_arg_deref)] +pub extern "C" fn proxmox_free_state_buf(buf: *mut u8) { + if !buf.is_null() { + unsafe { Box::from_raw(buf); } + } +}