|
| 1 | +use crate::storage::Storage; |
| 2 | +use anyhow::Result; |
| 3 | +use async_trait::async_trait; |
| 4 | +use noosphere_common::ConditionalSend; |
| 5 | +use std::path::{Path, PathBuf}; |
| 6 | + |
| 7 | +#[cfg(not(target_arch = "wasm32"))] |
| 8 | +fn create_backup_path<P: AsRef<Path>>(path: P) -> Result<PathBuf> { |
| 9 | + use instant::SystemTime; |
| 10 | + use rand::Rng; |
| 11 | + |
| 12 | + let mut path = path.as_ref().to_owned(); |
| 13 | + let timestamp = SystemTime::UNIX_EPOCH |
| 14 | + .elapsed() |
| 15 | + .map_err(|_| anyhow::anyhow!("Could not generate timestamp."))? |
| 16 | + .as_secs(); |
| 17 | + let nonce = rand::thread_rng().gen::<u32>(); |
| 18 | + path.set_extension(format!("backup.{}-{}", timestamp, nonce)); |
| 19 | + Ok(path) |
| 20 | +} |
| 21 | +/* |
| 22 | +impl TryFrom<PathBuf> for BackupPath { |
| 23 | + fn try_from(value: PathBuf) -> Result<Self> { |
| 24 | + let file_name = value |
| 25 | + .file_name() |
| 26 | + .ok_or_else(|| anyhow::anyhow!("Could not derive file name."))? |
| 27 | + .to_str() |
| 28 | + .ok_or_else(|| anyhow::anyhow!("Could not decode file name."))?; |
| 29 | + match file_name.split('.').collect::<Vec<_>>()[..] { |
| 30 | + [source, "backup", time_and_nonce] => match time_and_nonce.split('-').collect()[..] { |
| 31 | + [time, nonce] => Duration::from_secs(), |
| 32 | + }, |
| 33 | + _ => Err(anyhow::anyhow!("Invalid backup path format.")), |
| 34 | + } |
| 35 | + } |
| 36 | +} |
| 37 | +*/ |
| 38 | + |
| 39 | +/// [Storage] that can be backed up and restored. |
| 40 | +/// [FsBackedStorage] types get a blanket implementation. |
| 41 | +#[cfg_attr(not(target_arch = "wasm32"), async_trait)] |
| 42 | +#[cfg_attr(target_arch = "wasm32", async_trait(?Send))] |
| 43 | +pub trait BackupStorage: Storage { |
| 44 | + /// Backup [Storage] located at `path`, moving to a backup location. |
| 45 | + async fn backup<P: AsRef<Path> + ConditionalSend>(path: P) -> Result<PathBuf>; |
| 46 | + /// Backup [Storage] at `restore_to`, moving [Storage] from `backup_path` to `restore_to`. |
| 47 | + async fn restore<P: AsRef<Path> + ConditionalSend, Q: AsRef<Path> + ConditionalSend>( |
| 48 | + backup_path: P, |
| 49 | + restore_to: Q, |
| 50 | + ) -> Result<PathBuf>; |
| 51 | + /// List paths to backups for `path`. |
| 52 | + async fn list_backups<P: AsRef<Path> + ConditionalSend>(path: P) -> Result<Vec<PathBuf>>; |
| 53 | +} |
| 54 | + |
| 55 | +#[cfg(not(target_arch = "wasm32"))] |
| 56 | +#[async_trait] |
| 57 | +impl<T> BackupStorage for T |
| 58 | +where |
| 59 | + T: crate::FsBackedStorage, |
| 60 | +{ |
| 61 | + async fn backup<P: AsRef<Path> + ConditionalSend>(path: P) -> Result<PathBuf> { |
| 62 | + let backup_path = create_backup_path(path.as_ref())?; |
| 63 | + T::rename(path, &backup_path).await?; |
| 64 | + Ok(backup_path) |
| 65 | + } |
| 66 | + |
| 67 | + async fn restore<P: AsRef<Path> + ConditionalSend, Q: AsRef<Path> + ConditionalSend>( |
| 68 | + backup_path: P, |
| 69 | + restore_to: Q, |
| 70 | + ) -> Result<PathBuf> { |
| 71 | + let restoration_path = restore_to.as_ref().to_owned(); |
| 72 | + let original_backup = T::backup(&restoration_path).await?; |
| 73 | + T::rename(backup_path, &restoration_path).await?; |
| 74 | + Ok(original_backup) |
| 75 | + } |
| 76 | + |
| 77 | + async fn list_backups<P: AsRef<Path> + ConditionalSend>(path: P) -> Result<Vec<PathBuf>> { |
| 78 | + let mut backups = vec![]; |
| 79 | + let matcher = format!( |
| 80 | + "{}.backup.", |
| 81 | + path.as_ref() |
| 82 | + .file_name() |
| 83 | + .ok_or_else(|| anyhow::anyhow!("Could not stringify path."))? |
| 84 | + .to_str() |
| 85 | + .ok_or_else(|| anyhow::anyhow!("Could not stringify path."))? |
| 86 | + ); |
| 87 | + let parent_dir = path |
| 88 | + .as_ref() |
| 89 | + .parent() |
| 90 | + .ok_or_else(|| anyhow::anyhow!("Could not find storage parent directory."))?; |
| 91 | + let mut stream = tokio::fs::read_dir(parent_dir).await?; |
| 92 | + while let Ok(Some(entry)) = stream.next_entry().await { |
| 93 | + if let Ok(file_name) = entry.file_name().into_string() { |
| 94 | + if file_name.starts_with(&matcher) { |
| 95 | + backups.push(entry.path()); |
| 96 | + } |
| 97 | + } |
| 98 | + } |
| 99 | + Ok(backups) |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | +#[cfg(all(not(target_arch = "wasm32"), test))] |
| 104 | +mod test { |
| 105 | + use crate::{OpenStorage, PreferredPlatformStorage, Store}; |
| 106 | + |
| 107 | + use super::*; |
| 108 | + |
| 109 | + #[tokio::test] |
| 110 | + pub async fn it_can_backup_storages() -> Result<()> { |
| 111 | + noosphere_core_dev::tracing::initialize_tracing(None); |
| 112 | + |
| 113 | + let temp_dir = tempfile::TempDir::new()?; |
| 114 | + let db_source = temp_dir.path().join("db"); |
| 115 | + |
| 116 | + { |
| 117 | + let storage = PreferredPlatformStorage::open(&db_source).await?; |
| 118 | + let mut store = storage.get_key_value_store("links").await?; |
| 119 | + store.write(b"1", b"1").await?; |
| 120 | + } |
| 121 | + |
| 122 | + let backup_1 = PreferredPlatformStorage::backup(&db_source).await?; |
| 123 | + |
| 124 | + { |
| 125 | + let storage = PreferredPlatformStorage::open(&db_source).await?; |
| 126 | + let mut store = storage.get_key_value_store("links").await?; |
| 127 | + assert!(store.read(b"1").await?.is_none(), "Backup is a move"); |
| 128 | + store.write(b"2", b"2").await?; |
| 129 | + } |
| 130 | + |
| 131 | + let backup_2 = PreferredPlatformStorage::backup(&db_source).await?; |
| 132 | + |
| 133 | + { |
| 134 | + let storage = PreferredPlatformStorage::open(&db_source).await?; |
| 135 | + let mut store = storage.get_key_value_store("links").await?; |
| 136 | + assert!(store.read(b"1").await?.is_none(), "Backup is a move"); |
| 137 | + assert!(store.read(b"2").await?.is_none(), "Backup is a move"); |
| 138 | + store.write(b"3", b"3").await?; |
| 139 | + } |
| 140 | + |
| 141 | + let backups = PreferredPlatformStorage::list_backups(&db_source).await?; |
| 142 | + assert_eq!(backups.len(), 2); |
| 143 | + assert!(backups.contains(&backup_1)); |
| 144 | + assert!(backups.contains(&backup_2)); |
| 145 | + |
| 146 | + let backup_3 = PreferredPlatformStorage::restore(&backup_1, &db_source).await?; |
| 147 | + { |
| 148 | + let storage = PreferredPlatformStorage::open(&db_source).await?; |
| 149 | + let store = storage.get_key_value_store("links").await?; |
| 150 | + assert_eq!(store.read(b"1").await?.unwrap(), b"1"); |
| 151 | + assert!(store.read(b"2").await?.is_none(), "Backup is a move"); |
| 152 | + assert!(store.read(b"3").await?.is_none(), "Backup is a move"); |
| 153 | + } |
| 154 | + |
| 155 | + let backups = PreferredPlatformStorage::list_backups(db_source).await?; |
| 156 | + assert_eq!(backups.len(), 2); |
| 157 | + assert!( |
| 158 | + backups.contains(&backup_3), |
| 159 | + "contains backup from restoration." |
| 160 | + ); |
| 161 | + assert!( |
| 162 | + !backups.contains(&backup_1), |
| 163 | + "moves backup that was restored." |
| 164 | + ); |
| 165 | + assert!( |
| 166 | + backups.contains(&backup_2), |
| 167 | + "contains backups that were untouched." |
| 168 | + ); |
| 169 | + Ok(()) |
| 170 | + } |
| 171 | +} |
0 commit comments