summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLars Wirzenius <liw@liw.fi>2022-01-16 15:39:53 +0000
committerLars Wirzenius <liw@liw.fi>2022-01-16 15:39:53 +0000
commit31e67c871cf3c92274e9782f141ee7291d104e53 (patch)
tree41f416121187f6a7b277dce7f0c9ad77b7cc64f9
parent8018d3edf571063a94aea4cc5e053882eb48717d (diff)
parentf61b43c6b646193be4b9e6705d8c3d83b2c3ba93 (diff)
downloadobnam2-31e67c871cf3c92274e9782f141ee7291d104e53.tar.gz
Merge branch 'rename' into 'main'
some refactorings Closes #181 and #182 See merge request obnam/obnam!209
-rw-r--r--src/backup_run.rs11
-rw-r--r--src/client.rs111
-rw-r--r--src/cmd/backup.rs4
-rw-r--r--src/cmd/gen_info.rs4
-rw-r--r--src/cmd/get_chunk.rs4
-rw-r--r--src/cmd/list.rs4
-rw-r--r--src/cmd/list_files.rs4
-rw-r--r--src/cmd/resolve.rs4
-rw-r--r--src/cmd/restore.rs8
-rw-r--r--src/cmd/show_gen.rs4
10 files changed, 60 insertions, 98 deletions
diff --git a/src/backup_run.rs b/src/backup_run.rs
index ade5ee0..2286fca 100644
--- a/src/backup_run.rs
+++ b/src/backup_run.rs
@@ -5,7 +5,7 @@ use crate::backup_reason::Reason;
use crate::chunk::{GenerationChunk, GenerationChunkError};
use crate::chunker::{Chunker, ChunkerError};
use crate::chunkid::ChunkId;
-use crate::client::{AsyncBackupClient, ClientError};
+use crate::client::{BackupClient, ClientError};
use crate::config::ClientConfig;
use crate::error::ObnamError;
use crate::fsentry::{FilesystemEntry, FilesystemKind};
@@ -24,7 +24,7 @@ const SQLITE_CHUNK_SIZE: usize = MIB as usize;
/// A running backup.
pub struct BackupRun<'a> {
- client: &'a AsyncBackupClient,
+ client: &'a BackupClient,
policy: BackupPolicy,
buffer_size: usize,
progress: Option<BackupProgress>,
@@ -95,10 +95,7 @@ pub struct RootsBackupOutcome {
impl<'a> BackupRun<'a> {
/// Create a new run for an initial backup.
- pub fn initial(
- config: &ClientConfig,
- client: &'a AsyncBackupClient,
- ) -> Result<Self, BackupError> {
+ pub fn initial(config: &ClientConfig, client: &'a BackupClient) -> Result<Self, BackupError> {
Ok(Self {
client,
policy: BackupPolicy::default(),
@@ -110,7 +107,7 @@ impl<'a> BackupRun<'a> {
/// Create a new run for an incremental backup.
pub fn incremental(
config: &ClientConfig,
- client: &'a AsyncBackupClient,
+ client: &'a BackupClient,
) -> Result<Self, BackupError> {
Ok(Self {
client,
diff --git a/src/client.rs b/src/client.rs
index ed6b86b..bcc31b4 100644
--- a/src/client.rs
+++ b/src/client.rs
@@ -96,81 +96,17 @@ pub enum ClientError {
}
/// Client for the Obnam server HTTP API.
-///
-/// This is the async version.
-pub struct AsyncBackupClient {
- chunk_client: AsyncChunkClient,
-}
-
-impl AsyncBackupClient {
- /// Create a new backup client.
- pub fn new(config: &ClientConfig) -> Result<Self, ClientError> {
- info!("creating backup client with config: {:#?}", config);
- Ok(Self {
- chunk_client: AsyncChunkClient::new(config)?,
- })
- }
-
- /// Does the server have a chunk?
- pub async fn has_chunk(&self, meta: &ChunkMeta) -> Result<Option<ChunkId>, ClientError> {
- self.chunk_client.has_chunk(meta).await
- }
-
- /// Upload a data chunk to the srver.
- pub async fn upload_chunk(&self, chunk: DataChunk) -> Result<ChunkId, ClientError> {
- self.chunk_client.upload_chunk(chunk).await
- }
-
- /// List backup generations known by the server.
- pub async fn list_generations(&self) -> Result<GenerationList, ClientError> {
- self.chunk_client.list_generations().await
- }
-
- /// Fetch a data chunk from the server, given the chunk identifier.
- pub async fn fetch_chunk(&self, chunk_id: &ChunkId) -> Result<DataChunk, ClientError> {
- self.chunk_client.fetch_chunk(chunk_id).await
- }
-
- async fn fetch_generation_chunk(&self, gen_id: &GenId) -> Result<GenerationChunk, ClientError> {
- let chunk = self.fetch_chunk(gen_id.as_chunk_id()).await?;
- let gen = GenerationChunk::from_data_chunk(&chunk)?;
- Ok(gen)
- }
-
- /// Fetch a backup generation's metadata, given it's identifier.
- pub async fn fetch_generation(
- &self,
- gen_id: &GenId,
- dbname: &Path,
- ) -> Result<LocalGeneration, ClientError> {
- let gen = self.fetch_generation_chunk(gen_id).await?;
-
- // Fetch the SQLite file, storing it in the named file.
- let mut dbfile = File::create(&dbname)
- .map_err(|err| ClientError::FileCreate(dbname.to_path_buf(), err))?;
- for id in gen.chunk_ids() {
- let chunk = self.fetch_chunk(id).await?;
- dbfile
- .write_all(chunk.data())
- .map_err(|err| ClientError::FileWrite(dbname.to_path_buf(), err))?;
- }
- info!("downloaded generation to {}", dbname.display());
-
- let gen = LocalGeneration::open(dbname)?;
- Ok(gen)
- }
-}
-
-/// Client for using chunk part of Obnam server HTTP API.
-pub struct AsyncChunkClient {
+pub struct BackupClient {
client: reqwest::Client,
base_url: String,
cipher: CipherEngine,
}
-impl AsyncChunkClient {
- /// Create a new chunk client.
+impl BackupClient {
+ /// Create a new backup client.
pub fn new(config: &ClientConfig) -> Result<Self, ClientError> {
+ info!("creating backup client with config: {:#?}", config);
+
let pass = config.passwords()?;
let client = reqwest::Client::builder()
@@ -192,7 +128,7 @@ impl AsyncChunkClient {
format!("{}/chunks", self.base_url())
}
- /// Does server have a chunk?
+ /// Does the server have a chunk?
pub async fn has_chunk(&self, meta: &ChunkMeta) -> Result<Option<ChunkId>, ClientError> {
let body = match self.get("", &[("sha256", meta.sha256())]).await {
Ok((_, body)) => body,
@@ -211,7 +147,7 @@ impl AsyncChunkClient {
Ok(has)
}
- /// Upload a new chunk to the server.
+ /// Upload a data chunk to the srver.
pub async fn upload_chunk(&self, chunk: DataChunk) -> Result<ChunkId, ClientError> {
let enc = self.cipher.encrypt_chunk(&chunk)?;
let res = self
@@ -234,7 +170,7 @@ impl AsyncChunkClient {
Ok(chunk_id)
}
- /// List all generation chunks on the server.
+ /// List backup generations known by the server.
pub async fn list_generations(&self) -> Result<GenerationList, ClientError> {
let (_, body) = self.get("", &[("generation", "true")]).await?;
@@ -248,7 +184,7 @@ impl AsyncChunkClient {
Ok(GenerationList::new(finished))
}
- /// Fetch a chunk from the server, given its id.
+ /// Fetch a data chunk from the server, given the chunk identifier.
pub async fn fetch_chunk(&self, chunk_id: &ChunkId) -> Result<DataChunk, ClientError> {
let (headers, body) = self.get(&format!("/{}", chunk_id), &[]).await?;
let meta = self.get_chunk_meta_header(chunk_id, &headers)?;
@@ -259,6 +195,35 @@ impl AsyncChunkClient {
Ok(chunk)
}
+ async fn fetch_generation_chunk(&self, gen_id: &GenId) -> Result<GenerationChunk, ClientError> {
+ let chunk = self.fetch_chunk(gen_id.as_chunk_id()).await?;
+ let gen = GenerationChunk::from_data_chunk(&chunk)?;
+ Ok(gen)
+ }
+
+ /// Fetch a backup generation's metadata, given it's identifier.
+ pub async fn fetch_generation(
+ &self,
+ gen_id: &GenId,
+ dbname: &Path,
+ ) -> Result<LocalGeneration, ClientError> {
+ let gen = self.fetch_generation_chunk(gen_id).await?;
+
+ // Fetch the SQLite file, storing it in the named file.
+ let mut dbfile = File::create(&dbname)
+ .map_err(|err| ClientError::FileCreate(dbname.to_path_buf(), err))?;
+ for id in gen.chunk_ids() {
+ let chunk = self.fetch_chunk(id).await?;
+ dbfile
+ .write_all(chunk.data())
+ .map_err(|err| ClientError::FileWrite(dbname.to_path_buf(), err))?;
+ }
+ info!("downloaded generation to {}", dbname.display());
+
+ let gen = LocalGeneration::open(dbname)?;
+ Ok(gen)
+ }
+
async fn get(
&self,
path: &str,
diff --git a/src/cmd/backup.rs b/src/cmd/backup.rs
index 6e09d37..92b0f40 100644
--- a/src/cmd/backup.rs
+++ b/src/cmd/backup.rs
@@ -1,7 +1,7 @@
//! The `backup` subcommand.
use crate::backup_run::BackupRun;
-use crate::client::AsyncBackupClient;
+use crate::client::BackupClient;
use crate::config::ClientConfig;
use crate::error::ObnamError;
use crate::generation::GenId;
@@ -26,7 +26,7 @@ impl Backup {
async fn run_async(&self, config: &ClientConfig) -> Result<(), ObnamError> {
let runtime = SystemTime::now();
- let client = AsyncBackupClient::new(config)?;
+ let client = BackupClient::new(config)?;
let genlist = client.list_generations().await?;
let oldtemp = NamedTempFile::new()?;
diff --git a/src/cmd/gen_info.rs b/src/cmd/gen_info.rs
index 2663d9b..2ce1f64 100644
--- a/src/cmd/gen_info.rs
+++ b/src/cmd/gen_info.rs
@@ -1,6 +1,6 @@
//! The `gen-info` subcommand.
-use crate::client::AsyncBackupClient;
+use crate::client::BackupClient;
use crate::config::ClientConfig;
use crate::error::ObnamError;
use log::info;
@@ -26,7 +26,7 @@ impl GenInfo {
async fn run_async(&self, config: &ClientConfig) -> Result<(), ObnamError> {
let temp = NamedTempFile::new()?;
- let client = AsyncBackupClient::new(config)?;
+ let client = BackupClient::new(config)?;
let genlist = client.list_generations().await?;
let gen_id = genlist.resolve(&self.gen_ref)?;
diff --git a/src/cmd/get_chunk.rs b/src/cmd/get_chunk.rs
index 905e997..0b27084 100644
--- a/src/cmd/get_chunk.rs
+++ b/src/cmd/get_chunk.rs
@@ -1,7 +1,7 @@
//! The `get-chunk` subcommand.
use crate::chunkid::ChunkId;
-use crate::client::AsyncBackupClient;
+use crate::client::BackupClient;
use crate::config::ClientConfig;
use crate::error::ObnamError;
use std::io::{stdout, Write};
@@ -24,7 +24,7 @@ impl GetChunk {
}
async fn run_async(&self, config: &ClientConfig) -> Result<(), ObnamError> {
- let client = AsyncBackupClient::new(config)?;
+ let client = BackupClient::new(config)?;
let chunk_id: ChunkId = self.chunk_id.parse().unwrap();
let chunk = client.fetch_chunk(&chunk_id).await?;
let stdout = stdout();
diff --git a/src/cmd/list.rs b/src/cmd/list.rs
index 6c58e30..f176594 100644
--- a/src/cmd/list.rs
+++ b/src/cmd/list.rs
@@ -1,6 +1,6 @@
//! The `list` subcommand.
-use crate::client::AsyncBackupClient;
+use crate::client::BackupClient;
use crate::config::ClientConfig;
use crate::error::ObnamError;
use structopt::StructOpt;
@@ -18,7 +18,7 @@ impl List {
}
async fn run_async(&self, config: &ClientConfig) -> Result<(), ObnamError> {
- let client = AsyncBackupClient::new(config)?;
+ let client = BackupClient::new(config)?;
let generations = client.list_generations().await?;
for finished in generations.iter() {
diff --git a/src/cmd/list_files.rs b/src/cmd/list_files.rs
index 888943e..12d34b1 100644
--- a/src/cmd/list_files.rs
+++ b/src/cmd/list_files.rs
@@ -1,7 +1,7 @@
//! The `list-files` subcommand.
use crate::backup_reason::Reason;
-use crate::client::AsyncBackupClient;
+use crate::client::BackupClient;
use crate::config::ClientConfig;
use crate::error::ObnamError;
use crate::fsentry::{FilesystemEntry, FilesystemKind};
@@ -27,7 +27,7 @@ impl ListFiles {
async fn run_async(&self, config: &ClientConfig) -> Result<(), ObnamError> {
let temp = NamedTempFile::new()?;
- let client = AsyncBackupClient::new(config)?;
+ let client = BackupClient::new(config)?;
let genlist = client.list_generations().await?;
let gen_id = genlist.resolve(&self.gen_id)?;
diff --git a/src/cmd/resolve.rs b/src/cmd/resolve.rs
index cd08908..3b9570a 100644
--- a/src/cmd/resolve.rs
+++ b/src/cmd/resolve.rs
@@ -1,6 +1,6 @@
//! The `resolve` subcommand.
-use crate::client::AsyncBackupClient;
+use crate::client::BackupClient;
use crate::config::ClientConfig;
use crate::error::ObnamError;
use structopt::StructOpt;
@@ -21,7 +21,7 @@ impl Resolve {
}
async fn run_async(&self, config: &ClientConfig) -> Result<(), ObnamError> {
- let client = AsyncBackupClient::new(config)?;
+ let client = BackupClient::new(config)?;
let generations = client.list_generations().await?;
match generations.resolve(&self.generation) {
diff --git a/src/cmd/restore.rs b/src/cmd/restore.rs
index 2a36986..7b3d95e 100644
--- a/src/cmd/restore.rs
+++ b/src/cmd/restore.rs
@@ -1,7 +1,7 @@
//! The `restore` subcommand.
use crate::backup_reason::Reason;
-use crate::client::{AsyncBackupClient, ClientError};
+use crate::client::{BackupClient, ClientError};
use crate::config::ClientConfig;
use crate::error::ObnamError;
use crate::fsentry::{FilesystemEntry, FilesystemKind};
@@ -43,7 +43,7 @@ impl Restore {
async fn run_async(&self, config: &ClientConfig) -> Result<(), ObnamError> {
let temp = NamedTempFile::new()?;
- let client = AsyncBackupClient::new(config)?;
+ let client = BackupClient::new(config)?;
let genlist = client.list_generations().await?;
let gen_id = genlist.resolve(&self.gen_id)?;
@@ -130,7 +130,7 @@ pub enum RestoreError {
}
async fn restore_generation(
- client: &AsyncBackupClient,
+ client: &BackupClient,
gen: &LocalGeneration,
fileid: i64,
entry: &FilesystemEntry,
@@ -182,7 +182,7 @@ fn restored_path(entry: &FilesystemEntry, to: &Path) -> Result<PathBuf, RestoreE
}
async fn restore_regular(
- client: &AsyncBackupClient,
+ client: &BackupClient,
gen: &LocalGeneration,
path: &Path,
fileid: i64,
diff --git a/src/cmd/show_gen.rs b/src/cmd/show_gen.rs
index 6ec1203..6c8ba19 100644
--- a/src/cmd/show_gen.rs
+++ b/src/cmd/show_gen.rs
@@ -1,6 +1,6 @@
//! The `show-generation` subcommand.
-use crate::client::AsyncBackupClient;
+use crate::client::BackupClient;
use crate::config::ClientConfig;
use crate::error::ObnamError;
use crate::fsentry::FilesystemKind;
@@ -26,7 +26,7 @@ impl ShowGeneration {
async fn run_async(&self, config: &ClientConfig) -> Result<(), ObnamError> {
let temp = NamedTempFile::new()?;
- let client = AsyncBackupClient::new(config)?;
+ let client = BackupClient::new(config)?;
let genlist = client.list_generations().await?;
let gen_id = genlist.resolve(&self.gen_id)?;