From f512cd4bf30bb48a498a4fbca422ff77a50a2508 Mon Sep 17 00:00:00 2001 From: Lars Wirzenius Date: Sat, 27 Mar 2021 10:31:16 +0200 Subject: test: don't silence clippy warnings --- check | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/check b/check index 4f26da1..5f7184d 100755 --- a/check +++ b/check @@ -21,7 +21,7 @@ got_cargo_cmd() } $hideok cargo build --all-targets -got_cargo_cmd clippy && $hideok cargo clippy +got_cargo_cmd clippy && cargo clippy -q --all-targets got_cargo_cmd fmt && $hideok cargo fmt -- --check $hideok cargo test -- cgit v1.2.1 From 2d7dd4be132fa90965017ae46a16009cd67cdf8c Mon Sep 17 00:00:00 2001 From: Lars Wirzenius Date: Sat, 27 Mar 2021 10:57:05 +0200 Subject: fix: how ./check looks for subcommands --- check | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/check b/check index 5f7184d..e463ca2 100755 --- a/check +++ b/check @@ -17,11 +17,11 @@ fi got_cargo_cmd() { - cargo --list | grep " $1 " > /dev/null + cargo "$1" --help > /dev/null } -$hideok cargo build --all-targets got_cargo_cmd clippy && cargo clippy -q --all-targets +$hideok cargo build --all-targets got_cargo_cmd fmt && $hideok cargo fmt -- --check $hideok cargo test -- cgit v1.2.1 From 5b09f60a408b743e78640d850854901a3565274c Mon Sep 17 00:00:00 2001 From: Lars Wirzenius Date: Sat, 27 Mar 2021 10:58:54 +0200 Subject: refactor: avoid confusing function name from_str --- src/backup_reason.rs | 2 +- src/generation.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/backup_reason.rs b/src/backup_reason.rs index f785dea..0a51556 100644 --- a/src/backup_reason.rs +++ b/src/backup_reason.rs @@ -14,7 +14,7 @@ pub enum Reason { } impl Reason { - pub fn from_str(text: &str) -> Reason { + pub fn from(text: &str) -> Reason { match text { "skipped" => Reason::Skipped, "new" => Reason::IsNew, diff --git a/src/generation.rs b/src/generation.rs index 240f46a..174247a 100644 --- a/src/generation.rs +++ b/src/generation.rs @@ -162,7 +162,7 @@ pub struct BackedUpFile { impl BackedUpFile { pub fn new(fileno: FileId, entry: FilesystemEntry, reason: &str) -> Self { - let reason = Reason::from_str(reason); + let reason = Reason::from(reason); Self { fileno, entry, -- cgit v1.2.1 From 6b6060ed71dce3d80485516267c6a94eb111c3eb Mon Sep 17 00:00:00 2001 From: Lars Wirzenius Date: Sat, 27 Mar 2021 11:00:32 +0200 Subject: refactor: drop unnecessary reference --- src/backup_run.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/backup_run.rs b/src/backup_run.rs index ea5888a..85c40ff 100644 --- a/src/backup_run.rs +++ b/src/backup_run.rs @@ -48,7 +48,7 @@ impl<'a> InitialBackup<'a> { } pub fn drop(&self) { - &self.progress.finish(); + self.progress.finish(); } pub fn backup( -- cgit v1.2.1 From 08c2de031ea9556711673901f36675cba7744e26 Mon Sep 17 00:00:00 2001 From: Lars Wirzenius Date: Sat, 27 Mar 2021 11:01:26 +0200 Subject: refactor: drop unnecessary clone --- src/backup_run.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/backup_run.rs b/src/backup_run.rs index 85c40ff..a1faafd 100644 --- a/src/backup_run.rs +++ b/src/backup_run.rs @@ -140,7 +140,7 @@ impl<'a> IncrementalBackup<'a> { } else { vec![] }; - Ok((entry.clone(), ids, reason)) + Ok((entry, ids, reason)) } } } -- cgit v1.2.1 From 5af0888b60ba124d537efda829a5ba3f7130d07a Mon Sep 17 00:00:00 2001 From: Lars Wirzenius Date: Sat, 27 Mar 2021 11:03:07 +0200 Subject: refactor: don't return a Result from function that can't fail --- src/backup_run.rs | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/src/backup_run.rs b/src/backup_run.rs index a1faafd..5e26da4 100644 --- a/src/backup_run.rs +++ b/src/backup_run.rs @@ -65,7 +65,13 @@ impl<'a> InitialBackup<'a> { let path = &entry.pathbuf(); info!("backup: {}", path.display()); self.progress.found_live_file(path); - backup_file(&self.client, &entry, &path, self.buffer_size, Reason::IsNew) + Ok(backup_file( + &self.client, + &entry, + &path, + self.buffer_size, + Reason::IsNew, + )) } } } @@ -130,9 +136,13 @@ impl<'a> IncrementalBackup<'a> { Reason::IsNew | Reason::Changed | Reason::GenerationLookupError - | Reason::Unknown => { - backup_file(&self.client, &entry, &path, self.buffer_size, reason) - } + | Reason::Unknown => Ok(backup_file( + &self.client, + &entry, + &path, + self.buffer_size, + reason, + )), Reason::Unchanged | Reason::Skipped | Reason::FileError => { let fileno = old.get_fileno(&entry.pathbuf())?; let ids = if let Some(fileno) = fileno { @@ -166,13 +176,13 @@ fn backup_file( path: &Path, chunk_size: usize, reason: Reason, -) -> BackupResult<(FilesystemEntry, Vec, Reason)> { +) -> (FilesystemEntry, Vec, Reason) { let ids = client.upload_filesystem_entry(&entry, chunk_size); match ids { Err(err) => { warn!("error backing up {}, skipping it: {}", path.display(), err); - Ok((entry.clone(), vec![], Reason::FileError)) + (entry.clone(), vec![], Reason::FileError) } - Ok(ids) => Ok((entry.clone(), ids, reason)), + Ok(ids) => (entry.clone(), ids, reason), } } -- cgit v1.2.1 From 4618ccc2a2dbc73503c32464e2a05043e4f04227 Mon Sep 17 00:00:00 2001 From: Lars Wirzenius Date: Sat, 27 Mar 2021 11:07:29 +0200 Subject: refactor: rename ChunkId::from_str to ChunkId::recreate Less confusion with FromStr::from_str this way. --- src/benchmark.rs | 2 +- src/chunkid.rs | 7 ++++--- src/client.rs | 2 +- src/index.rs | 2 +- 4 files changed, 7 insertions(+), 6 deletions(-) diff --git a/src/benchmark.rs b/src/benchmark.rs index b484aa1..d214939 100644 --- a/src/benchmark.rs +++ b/src/benchmark.rs @@ -21,7 +21,7 @@ impl Iterator for ChunkGenerator { if self.next >= self.goal { None } else { - let id = ChunkId::from_str(&format!("{}", self.next)); + let id = ChunkId::recreate(&format!("{}", self.next)); let checksum = id.sha256(); let meta = ChunkMeta::new(&checksum); let chunk = DataChunk::new(vec![]); diff --git a/src/chunkid.rs b/src/chunkid.rs index 3933d4b..2f67d79 100644 --- a/src/chunkid.rs +++ b/src/chunkid.rs @@ -37,7 +37,8 @@ impl ChunkId { } } - pub fn from_str(s: &str) -> Self { + /// Re-construct an identifier from a previous values. + pub fn recreate(s: &str) -> Self { ChunkId { id: s.to_string() } } @@ -85,7 +86,7 @@ impl FromStr for ChunkId { type Err = (); fn from_str(s: &str) -> Result { - Ok(ChunkId::from_str(s)) + Ok(ChunkId::recreate(s)) } } @@ -117,6 +118,6 @@ mod test { fn survives_round_trip() { let id = ChunkId::new(); let id_str = id.to_string(); - assert_eq!(id, ChunkId::from_str(&id_str)) + assert_eq!(id, ChunkId::recreate(&id_str)) } } diff --git a/src/client.rs b/src/client.rs index f74e184..c054aca 100644 --- a/src/client.rs +++ b/src/client.rs @@ -341,7 +341,7 @@ impl BackupClient { } fn fetch_generation_chunk(&self, gen_id: &str) -> ClientResult { - let chunk_id = ChunkId::from_str(gen_id); + let chunk_id = ChunkId::recreate(gen_id); let chunk = self.fetch_chunk(&chunk_id)?; let gen = GenerationChunk::from_data_chunk(&chunk)?; Ok(gen) diff --git a/src/index.rs b/src/index.rs index 9386e73..c8d8d86 100644 --- a/src/index.rs +++ b/src/index.rs @@ -272,6 +272,6 @@ mod sql { fn row_to_id(row: &Row) -> rusqlite::Result { let id: String = row.get(row.column_index("id")?)?; - Ok(ChunkId::from_str(&id)) + Ok(ChunkId::recreate(&id)) } } -- cgit v1.2.1 From 96cab4a2fda6726437403b00cfcba1ee69bc9432 Mon Sep 17 00:00:00 2001 From: Lars Wirzenius Date: Sat, 27 Mar 2021 11:09:11 +0200 Subject: refactor: use matches! for clarity --- src/chunkmeta.rs | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/chunkmeta.rs b/src/chunkmeta.rs index fc5ffff..37e2ed5 100644 --- a/src/chunkmeta.rs +++ b/src/chunkmeta.rs @@ -67,10 +67,7 @@ impl ChunkMeta { /// Is this a generation chunk? pub fn is_generation(&self) -> bool { - match self.generation { - Some(true) => true, - _ => false, - } + matches!(self.generation, Some(true)) } /// When did this generation end? -- cgit v1.2.1 From 0483cfcc6f85711b7b75fc8f0182e4baf7c0019f Mon Sep 17 00:00:00 2001 From: Lars Wirzenius Date: Sat, 27 Mar 2021 11:10:34 +0200 Subject: refactor: use Option::or_else to avoid unnecessary allocation --- src/client.rs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/client.rs b/src/client.rs index c054aca..80ae788 100644 --- a/src/client.rs +++ b/src/client.rs @@ -71,7 +71,10 @@ impl ClientConfig { roots: tentative.roots, verify_tls_cert: tentative.verify_tls_cert.or(Some(false)).unwrap(), chunk_size: tentative.chunk_size.or(Some(DEFAULT_CHUNK_SIZE)).unwrap(), - log: tentative.log.or(Some(PathBuf::from(DEVNULL))).unwrap(), + log: tentative + .log + .or_else(|| Some(PathBuf::from(DEVNULL))) + .unwrap(), }; config.check()?; -- cgit v1.2.1 From 939433798b708d986928a124e81dd03c8488ad90 Mon Sep 17 00:00:00 2001 From: Lars Wirzenius Date: Sat, 27 Mar 2021 11:14:38 +0200 Subject: refactor: drop unnecessary conversions to the same type --- src/client.rs | 10 +++++----- src/fsentry.rs | 2 +- src/generation.rs | 2 +- src/index.rs | 4 ++-- src/server.rs | 6 +++--- 5 files changed, 12 insertions(+), 12 deletions(-) diff --git a/src/client.rs b/src/client.rs index 80ae788..d513011 100644 --- a/src/client.rs +++ b/src/client.rs @@ -245,7 +245,7 @@ impl BackupClient { debug!("upload_chunk: id={}", chunk_id); chunk_id.parse().unwrap() } else { - return Err(ClientError::NoCreatedChunkId.into()); + return Err(ClientError::NoCreatedChunkId); }; info!("uploaded_chunk {} meta {:?}", chunk_id, meta); Ok(chunk_id) @@ -264,7 +264,7 @@ impl BackupClient { debug!("upload_chunk: id={}", chunk_id); chunk_id.parse().unwrap() } else { - return Err(ClientError::NoCreatedChunkId.into()); + return Err(ClientError::NoCreatedChunkId); }; info!("uploaded_generation chunk {}", chunk_id); Ok(chunk_id) @@ -313,7 +313,7 @@ impl BackupClient { if res.status() != 200 { let err = ClientError::ChunkNotFound(chunk_id.to_string()); error!("fetching chunk {} failed: {}", chunk_id, err); - return Err(err.into()); + return Err(err); } let headers = res.headers(); @@ -321,7 +321,7 @@ impl BackupClient { if meta.is_none() { let err = ClientError::NoChunkMeta(chunk_id.clone()); error!("fetching chunk {} failed: {}", chunk_id, err); - return Err(err.into()); + return Err(err); } let meta = meta.unwrap().to_str()?; debug!("fetching chunk {}: meta={:?}", chunk_id, meta); @@ -335,7 +335,7 @@ impl BackupClient { let err = ClientError::WrongChecksum(chunk_id.clone(), actual, meta.sha256().to_string()); error!("fetching chunk {} failed: {}", chunk_id, err); - return Err(err.into()); + return Err(err); } let chunk: DataChunk = DataChunk::new(body); diff --git a/src/fsentry.rs b/src/fsentry.rs index 28f7c61..35931ab 100644 --- a/src/fsentry.rs +++ b/src/fsentry.rs @@ -196,7 +196,7 @@ impl FilesystemKind { 2 => Ok(FilesystemKind::Symlink), 3 => Ok(FilesystemKind::Socket), 4 => Ok(FilesystemKind::Fifo), - _ => Err(FsEntryError::UnknownFileKindCode(code).into()), + _ => Err(FsEntryError::UnknownFileKindCode(code)), } } } diff --git a/src/generation.rs b/src/generation.rs index 174247a..6eabb9b 100644 --- a/src/generation.rs +++ b/src/generation.rs @@ -344,7 +344,7 @@ mod sql { if iter.next() == None { Ok(Some((fileno, entry, reason))) } else { - Err(LocalGenerationError::TooManyFiles(filename.to_path_buf()).into()) + Err(LocalGenerationError::TooManyFiles(filename.to_path_buf())) } } } diff --git a/src/index.rs b/src/index.rs index c8d8d86..6628b50 100644 --- a/src/index.rs +++ b/src/index.rs @@ -214,12 +214,12 @@ mod sql { } else { let err = IndexError::DuplicateChunk(id.clone()); error!("{}", err); - return Err(err.into()); + return Err(err); } } if metas.len() == 0 { eprintln!("lookup: no hits"); - return Err(IndexError::MissingChunk(id.clone()).into()); + return Err(IndexError::MissingChunk(id.clone())); } let r = metas[0].clone(); Ok(r) diff --git a/src/server.rs b/src/server.rs index abf3f1e..6ea8ac4 100644 --- a/src/server.rs +++ b/src/server.rs @@ -49,13 +49,13 @@ impl ServerConfig { pub fn check(&self) -> Result<(), ServerConfigError> { if !self.chunks.exists() { - return Err(ServerConfigError::ChunksDirNotFound(self.chunks.clone()).into()); + return Err(ServerConfigError::ChunksDirNotFound(self.chunks.clone())); } if !self.tls_cert.exists() { - return Err(ServerConfigError::TlsCertNotFound(self.tls_cert.clone()).into()); + return Err(ServerConfigError::TlsCertNotFound(self.tls_cert.clone())); } if !self.tls_key.exists() { - return Err(ServerConfigError::TlsKeyNotFound(self.tls_key.clone()).into()); + return Err(ServerConfigError::TlsKeyNotFound(self.tls_key.clone())); } Ok(()) } -- cgit v1.2.1 From dd8ecdece25f8434d84eb548e8f3c9a8b7ebc74d Mon Sep 17 00:00:00 2001 From: Lars Wirzenius Date: Sat, 27 Mar 2021 11:15:34 +0200 Subject: refactor: return assigned value directly --- src/cmd/restore.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/cmd/restore.rs b/src/cmd/restore.rs index 147c422..183f207 100644 --- a/src/cmd/restore.rs +++ b/src/cmd/restore.rs @@ -234,8 +234,7 @@ fn restore_metadata(path: &Path, entry: &FilesystemEntry) -> RestoreResult<()> { fn path_to_cstring(path: &Path) -> CString { let path = path.as_os_str(); let path = path.as_bytes(); - let path = CString::new(path).unwrap(); - path + CString::new(path).unwrap() } fn create_progress_bar(file_count: i64, verbose: bool) -> ProgressBar { -- cgit v1.2.1 From 3592fb5dff0384ed82c5146059caa6840263ebbe Mon Sep 17 00:00:00 2001 From: Lars Wirzenius Date: Sat, 27 Mar 2021 11:16:30 +0200 Subject: refactor: drop unused lifetime annotation --- src/generation.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/generation.rs b/src/generation.rs index 6eabb9b..72d4468 100644 --- a/src/generation.rs +++ b/src/generation.rs @@ -62,7 +62,7 @@ impl NascentGeneration { Ok(()) } - pub fn insert_iter<'a>( + pub fn insert_iter( &mut self, entries: impl Iterator, Reason)>>, ) -> NascentResult> { -- cgit v1.2.1 From d27911513b1b6fc4d48d4a8f316e780b765f6c7a Mon Sep 17 00:00:00 2001 From: Lars Wirzenius Date: Sat, 27 Mar 2021 11:18:32 +0200 Subject: refactor: drop unnecessary clones --- src/genlist.rs | 2 +- src/index.rs | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/genlist.rs b/src/genlist.rs index 5eec248..dbc437a 100644 --- a/src/genlist.rs +++ b/src/genlist.rs @@ -13,7 +13,7 @@ pub enum GenerationListError { impl GenerationList { pub fn new(gens: Vec) -> Self { - let mut list = gens.clone(); + let mut list = gens; list.sort_by_cached_key(|gen| gen.ended().to_string()); Self { list } } diff --git a/src/index.rs b/src/index.rs index 6628b50..83f854c 100644 --- a/src/index.rs +++ b/src/index.rs @@ -138,7 +138,7 @@ mod test { let meta = ChunkMeta::new_generation("abc", "timestamp"); let dir = tempdir().unwrap(); let mut idx = new_index(dir.path()); - idx.insert_meta(id.clone(), meta.clone()).unwrap(); + idx.insert_meta(id.clone(), meta).unwrap(); assert_eq!(idx.find_generations().unwrap(), vec![id]); } @@ -148,7 +148,7 @@ mod test { let meta = ChunkMeta::new_generation("abc", "timestamp"); let dir = tempdir().unwrap(); let mut idx = new_index(dir.path()); - idx.insert_meta(id.clone(), meta.clone()).unwrap(); + idx.insert_meta(id.clone(), meta).unwrap(); idx.remove_meta(&id).unwrap(); assert_eq!(idx.find_generations().unwrap(), vec![]); } -- cgit v1.2.1 From b3dc71521f9a24ba1d7c13a4d599589af858cdde Mon Sep 17 00:00:00 2001 From: Lars Wirzenius Date: Sat, 27 Mar 2021 11:20:12 +0200 Subject: refactor: use .cloned() to clone items in an iterator --- src/genlist.rs | 2 +- src/indexedstore.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/genlist.rs b/src/genlist.rs index dbc437a..9d7e39d 100644 --- a/src/genlist.rs +++ b/src/genlist.rs @@ -33,7 +33,7 @@ impl GenerationList { let hits: Vec = self .iter() .filter(|gen| gen.id() == genref) - .map(|gen| gen.clone()) + .cloned() .collect(); if hits.len() == 1 { Some(hits[0].clone()) diff --git a/src/indexedstore.rs b/src/indexedstore.rs index f2d1831..7f67a1f 100644 --- a/src/indexedstore.rs +++ b/src/indexedstore.rs @@ -84,7 +84,7 @@ impl IndexedStore { let file_chunks = all_chunk_ids .iter() .filter(|id| !sql_chunks.contains(id)) - .map(|id| id.clone()) + .cloned() .collect(); Ok(file_chunks) -- cgit v1.2.1 From 9236e74b3dac05729338e98613529a2a11aa0b79 Mon Sep 17 00:00:00 2001 From: Lars Wirzenius Date: Sat, 27 Mar 2021 11:21:34 +0200 Subject: refactor: use idiomatic .is_empty() instead of comparing length --- src/index.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/index.rs b/src/index.rs index 83f854c..887238c 100644 --- a/src/index.rs +++ b/src/index.rs @@ -217,7 +217,7 @@ mod sql { return Err(err); } } - if metas.len() == 0 { + if metas.is_empty() { eprintln!("lookup: no hits"); return Err(IndexError::MissingChunk(id.clone())); } -- cgit v1.2.1 From 155e818e89cd36cd2d786e58d3783ca820ba7b0e Mon Sep 17 00:00:00 2001 From: Lars Wirzenius Date: Sat, 27 Mar 2021 11:25:42 +0200 Subject: refactor: rename BackupPoliy::new() to more idiomatic ::default() --- src/backup_run.rs | 2 +- src/policy.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/backup_run.rs b/src/backup_run.rs index 5e26da4..c5d5382 100644 --- a/src/backup_run.rs +++ b/src/backup_run.rs @@ -79,7 +79,7 @@ impl<'a> InitialBackup<'a> { impl<'a> IncrementalBackup<'a> { pub fn new(config: &ClientConfig, client: &'a BackupClient) -> BackupResult { - let policy = BackupPolicy::new(); + let policy = BackupPolicy::default(); Ok(Self { client, policy, diff --git a/src/policy.rs b/src/policy.rs index 8a65e09..39c73fc 100644 --- a/src/policy.rs +++ b/src/policy.rs @@ -9,7 +9,7 @@ pub struct BackupPolicy { } impl BackupPolicy { - pub fn new() -> Self { + pub fn default() -> Self { Self { new: true, old_if_changed: true, -- cgit v1.2.1 From dfab3cbb3f5200ab8675ecbff0c1554baf09b3e3 Mon Sep 17 00:00:00 2001 From: Lars Wirzenius Date: Sat, 27 Mar 2021 11:27:33 +0200 Subject: refactor: don't return Result if main can't fail --- src/bin/benchmark-null.rs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/bin/benchmark-null.rs b/src/bin/benchmark-null.rs index 6df8ca1..259a837 100644 --- a/src/bin/benchmark-null.rs +++ b/src/bin/benchmark-null.rs @@ -17,13 +17,11 @@ struct Opt { num: u32, } -fn main() -> anyhow::Result<()> { +fn main() { pretty_env_logger::init(); let opt = Opt::from_args(); let gen = ChunkGenerator::new(opt.num); for (_, _, _, _) in gen {} - - Ok(()) } -- cgit v1.2.1