From c76569bbfdd3c40e427c99fa1a88158344cdfa67 Mon Sep 17 00:00:00 2001 From: Lars Wirzenius Date: Fri, 13 May 2022 08:13:55 +0300 Subject: use new chunk store for all of client.rs Sponsored-by: author --- src/backup_run.rs | 25 ++++++++++++++----------- src/client.rs | 43 ++++--------------------------------------- src/cmd/backup.rs | 6 +++--- 3 files changed, 21 insertions(+), 53 deletions(-) diff --git a/src/backup_run.rs b/src/backup_run.rs index 516e172..372ef65 100644 --- a/src/backup_run.rs +++ b/src/backup_run.rs @@ -31,7 +31,7 @@ const SQLITE_CHUNK_SIZE: usize = MIB as usize; /// A running backup. pub struct BackupRun<'a> { checksum_kind: Option, - client: &'a BackupClient, + client: &'a mut BackupClient, policy: BackupPolicy, buffer_size: usize, progress: Option, @@ -106,7 +106,10 @@ pub struct RootsBackupOutcome { impl<'a> BackupRun<'a> { /// Create a new run for an initial backup. - pub fn initial(config: &ClientConfig, client: &'a BackupClient) -> Result { + pub fn initial( + config: &ClientConfig, + client: &'a mut BackupClient, + ) -> Result { Ok(Self { checksum_kind: Some(DEFAULT_CHECKSUM_KIND), client, @@ -119,7 +122,7 @@ impl<'a> BackupRun<'a> { /// Create a new run for an incremental backup. pub fn incremental( config: &ClientConfig, - client: &'a BackupClient, + client: &'a mut BackupClient, ) -> Result { Ok(Self { checksum_kind: None, @@ -189,7 +192,7 @@ impl<'a> BackupRun<'a> { /// Back up all the roots for this run. pub async fn backup_roots( - &self, + &mut self, config: &ClientConfig, old: &LocalGeneration, newpath: &Path, @@ -236,7 +239,7 @@ impl<'a> BackupRun<'a> { } async fn backup_one_root( - &self, + &mut self, config: &ClientConfig, old: &LocalGeneration, new: &mut NascentGeneration, @@ -287,7 +290,7 @@ impl<'a> BackupRun<'a> { } async fn backup_if_needed( - &self, + &mut self, entry: AnnotatedFsEntry, old: &LocalGeneration, ) -> Result, BackupError> { @@ -322,7 +325,7 @@ impl<'a> BackupRun<'a> { } async fn backup_one_entry( - &self, + &mut self, entry: &AnnotatedFsEntry, path: &Path, reason: Reason, @@ -351,7 +354,7 @@ impl<'a> BackupRun<'a> { /// Upload any file content for a file system entry. pub async fn upload_filesystem_entry( - &self, + &mut self, e: &FilesystemEntry, size: usize, ) -> Result, BackupError> { @@ -370,7 +373,7 @@ impl<'a> BackupRun<'a> { /// Upload the metadata for the backup of this run. pub async fn upload_generation( - &self, + &mut self, filename: &Path, size: usize, ) -> Result { @@ -384,7 +387,7 @@ impl<'a> BackupRun<'a> { } async fn upload_regular_file( - &self, + &mut self, filename: &Path, size: usize, ) -> Result, BackupError> { @@ -407,7 +410,7 @@ impl<'a> BackupRun<'a> { Ok(chunk_ids) } - async fn upload_nascent_generation(&self, filename: &Path) -> Result { + async fn upload_nascent_generation(&mut self, filename: &Path) -> Result { let progress = BackupProgress::upload_generation(); let gen_id = self.upload_generation(filename, SQLITE_CHUNK_SIZE).await?; progress.finish(); diff --git a/src/client.rs b/src/client.rs index 18e1f72..7b15bba 100644 --- a/src/client.rs +++ b/src/client.rs @@ -12,8 +12,7 @@ use crate::generation::{FinishedGeneration, GenId, LocalGeneration, LocalGenerat use crate::genlist::GenerationList; use crate::label::Label; -use log::{debug, error, info}; -use std::collections::HashMap; +use log::{error, info}; use std::fs::File; use std::io::prelude::*; use std::path::{Path, PathBuf}; @@ -109,8 +108,6 @@ pub enum ClientError { /// Client for the Obnam server HTTP API. pub struct BackupClient { store: ChunkStore, - client: reqwest::Client, - base_url: String, cipher: CipherEngine, } @@ -118,29 +115,13 @@ impl BackupClient { /// Create a new backup client. pub fn new(config: &ClientConfig) -> Result { info!("creating backup client with config: {:#?}", config); - let pass = config.passwords()?; - - let client = reqwest::Client::builder() - .danger_accept_invalid_certs(!config.verify_tls_cert) - .build() - .map_err(ClientError::ReqwestError)?; Ok(Self { store: ChunkStore::remote(config)?, - client, - base_url: config.server_url.to_string(), cipher: CipherEngine::new(&pass), }) } - fn base_url(&self) -> &str { - &self.base_url - } - - fn chunks_url(&self) -> String { - format!("{}/v1/chunks", self.base_url()) - } - /// Does the server have a chunk? pub async fn has_chunk(&self, meta: &ChunkMeta) -> Result, ClientError> { let mut ids = self.store.find_by_label(meta).await?; @@ -148,26 +129,10 @@ impl BackupClient { } /// Upload a data chunk to the server. - pub async fn upload_chunk(&self, chunk: DataChunk) -> Result { + pub async fn upload_chunk(&mut self, chunk: DataChunk) -> Result { let enc = self.cipher.encrypt_chunk(&chunk)?; - let res = self - .client - .post(&self.chunks_url()) - .header("chunk-meta", chunk.meta().to_json()) - .body(enc.ciphertext().to_vec()) - .send() - .await - .map_err(ClientError::ReqwestError)?; - debug!("upload_chunk: res={:?}", res); - let res: HashMap = res.json().await.map_err(ClientError::ReqwestError)?; - let chunk_id = if let Some(chunk_id) = res.get("chunk_id") { - debug!("upload_chunk: id={}", chunk_id); - chunk_id.parse().unwrap() - } else { - return Err(ClientError::NoCreatedChunkId); - }; - info!("uploaded_chunk {}", chunk_id); - Ok(chunk_id) + let id = self.store.put(enc, chunk.meta()).await?; + Ok(id) } /// Get current client trust chunk from repository, if there is one. diff --git a/src/cmd/backup.rs b/src/cmd/backup.rs index a18027b..70e9eac 100644 --- a/src/cmd/backup.rs +++ b/src/cmd/backup.rs @@ -45,7 +45,7 @@ impl Backup { let major = self.backup_version.unwrap_or(DEFAULT_SCHEMA_MAJOR); let schema = schema_version(major)?; - let client = BackupClient::new(config)?; + let mut client = BackupClient::new(config)?; let trust = client .get_client_trust() .await? @@ -68,7 +68,7 @@ impl Backup { let (is_incremental, outcome) = if let Some(old_id) = old_id { info!("incremental backup based on {}", old_id); - let mut run = BackupRun::incremental(config, &client)?; + let mut run = BackupRun::incremental(config, &mut client)?; let old = run.start(Some(&old_id), &oldtemp, perf).await?; ( true, @@ -77,7 +77,7 @@ impl Backup { ) } else { info!("fresh backup without a previous generation"); - let mut run = BackupRun::initial(config, &client)?; + let mut run = BackupRun::initial(config, &mut client)?; let old = run.start(None, &oldtemp, perf).await?; ( false, -- cgit v1.2.1