From 769bc6b9f790305fdd4363cb6eb87424e6457aac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cyryl=20P=C5=82otnicki?= Date: Fri, 25 Dec 2020 16:29:00 +0000 Subject: [PATCH] migrate some api to use AsRef --- Cargo.lock | 73 +++++++++++++++++++++++++++++++++++++++---- Cargo.toml | 3 +- src/backup.rs | 4 +-- src/index/io.rs | 3 +- src/index/mod.rs | 3 +- src/repository/mod.rs | 28 ++++++++++++----- src/restore.rs | 4 +-- 7 files changed, 97 insertions(+), 21 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 4641cbd..e27083c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -38,7 +38,7 @@ dependencies = [ "log", "nix 0.19.0", "proptest", - "rand 0.7.3", + "rand 0.8.0", "rayon", "rust-crypto", "serde", @@ -48,6 +48,7 @@ dependencies = [ "tempfile", "thiserror", "uuid", + "vfs", "walkdir", ] @@ -262,6 +263,17 @@ dependencies = [ "wasi 0.9.0+wasi-snapshot-preview1", ] +[[package]] +name = "getrandom" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ee8025cf36f917e6a52cce185b7c7177689b838b7ec138364e50cc2277a56cf4" +dependencies = [ + "cfg-if 0.1.10", + "libc", + "wasi 0.9.0+wasi-snapshot-preview1", +] + [[package]] name = "glob" version = "0.3.0" @@ -412,7 +424,7 @@ dependencies = [ "num-traits", "quick-error", "rand 0.7.3", - "rand_chacha", + "rand_chacha 0.2.2", "rand_xorshift", "regex-syntax", "rusty-fork", @@ -463,11 +475,23 @@ version = "0.7.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6a6b1679d49b24bbfe0c803429aa1874472f50d9b363131f0e89fc356b544d03" dependencies = [ - "getrandom", + "getrandom 0.1.15", "libc", - "rand_chacha", + "rand_chacha 0.2.2", "rand_core 0.5.1", - "rand_hc", + "rand_hc 0.2.0", +] + +[[package]] +name = "rand" +version = "0.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a76330fb486679b4ace3670f117bbc9e16204005c4bde9c4bd372f45bed34f12" +dependencies = [ + "libc", + "rand_chacha 0.3.0", + "rand_core 0.6.0", + "rand_hc 0.3.0", ] [[package]] @@ -480,6 +504,16 @@ dependencies = [ "rand_core 0.5.1", ] +[[package]] +name = "rand_chacha" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e12735cf05c9e10bf21534da50a147b924d555dc7a547c42e6bb2d5b6017ae0d" +dependencies = [ + "ppv-lite86", + "rand_core 0.6.0", +] + [[package]] name = "rand_core" version = "0.3.1" @@ -501,7 +535,16 @@ version = "0.5.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "90bde5296fc891b0cef12a6d03ddccc162ce7b2aff54160af9338f8d40df6d19" dependencies = [ - "getrandom", + "getrandom 0.1.15", +] + +[[package]] +name = "rand_core" +version = "0.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a8b34ba8cfb21243bd8df91854c830ff0d785fff2e82ebd4434c2644cb9ada18" +dependencies = [ + "getrandom 0.2.0", ] [[package]] @@ -513,6 +556,15 @@ dependencies = [ "rand_core 0.5.1", ] +[[package]] +name = "rand_hc" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3190ef7066a446f2e7f42e239d161e905420ccab01eb967c9eb27d21b2322a73" +dependencies = [ + "rand_core 0.6.0", +] + [[package]] name = "rand_xorshift" version = "0.2.0" @@ -776,6 +828,15 @@ version = "0.9.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b5a972e5669d67ba988ce3dc826706fb0a8b01471c088cb0b6110b805cc36aed" +[[package]] +name = "vfs" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7cc382cffc2f8d6f9490b96d42a7408d6aa719c2db86491b8b31780208ac9d86" +dependencies = [ + "thiserror", +] + [[package]] name = "void" version = "1.0.2" diff --git a/Cargo.toml b/Cargo.toml index a2fd139..746a22a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -15,7 +15,7 @@ glob = "0.3" hex = "0.4" log = "0.4" nix = "0.19" -rand = "0.7" +rand = "0.8" rayon = "1.5" rust-crypto = "0.2" serde = { version = "1.0", features = ["derive"] } @@ -29,6 +29,7 @@ walkdir = "2.3" [dev-dependencies] proptest = "0.10" +vfs = "0.4" [dev-dependencies.cargo-husky] version = "1" diff --git a/src/backup.rs b/src/backup.rs index 5aa07b8..21f694b 100644 --- a/src/backup.rs +++ b/src/backup.rs @@ -8,11 +8,11 @@ use crate::repository::Repository; pub struct Engine<'a> { source_path: &'a Path, - repository: &'a mut Repository<'a>, + repository: &'a mut Repository, } impl<'a> Engine<'a> { - pub fn new(source_path: &'a Path, repository: &'a mut Repository<'a>) -> Result { + pub fn new(source_path: &'a Path, repository: &'a mut Repository) -> Result { if source_path.ancestors().any(|a| a == repository.path()) { return Err(anyhow!("source same as repository")); } diff --git a/src/index/io.rs b/src/index/io.rs index 6357468..0c94887 100644 --- a/src/index/io.rs +++ b/src/index/io.rs @@ -15,7 +15,8 @@ use nix::unistd::getpid; use std::{cmp::max, io::Write}; impl Index { - pub fn load(repository_path: &Path) -> Result { + pub fn load>(repository_path: T) -> Result { + let repository_path = repository_path.as_ref(); if !repository_path.exists() { let mut index = Index::new(repository_path); index.save()?; diff --git a/src/index/mod.rs b/src/index/mod.rs index a1e8600..e63110d 100644 --- a/src/index/mod.rs +++ b/src/index/mod.rs @@ -24,7 +24,8 @@ pub struct Index { } impl Index { - pub fn new(repository_path: &Path) -> Self { + pub fn new>(repository_path: T) -> Self { + let repository_path = repository_path.as_ref(); Index { newest_items_by_source_path: Default::default(), items_by_file_id: Default::default(), diff --git a/src/repository/mod.rs b/src/repository/mod.rs index e850983..bb3f64f 100644 --- a/src/repository/mod.rs +++ b/src/repository/mod.rs @@ -19,9 +19,9 @@ use walkdir::WalkDir; /// right now only on-disk directory storage is supported /// repository always knows the newest version of the index and is responsible for syncing the index to disk /// and making sure that different threads can access index in parallel -pub struct Repository<'a> { +pub struct Repository { /// absolute path to where the repository is stored on disk - path: &'a Path, + path: PathBuf, index: Index, } @@ -81,20 +81,32 @@ impl fmt::Display for ItemId { } } -impl<'a> Repository<'a> { - pub fn init(path: &Path) -> Result<()> { - let mut index = Index::new(path); +impl<'a> Repository { + pub fn init(path: T) -> Result<()> + where + T: AsRef, + { + let mut index = Index::new(path.as_ref()); index.save()?; Ok(()) } - pub fn open(path: &Path) -> Result { + pub fn open(path: T) -> Result + where + T: AsRef, + { + let path = path.as_ref(); if !path.is_absolute() { return Err(anyhow!("path to repository not absolute")); } let index = Index::load(path)?; - Ok(Repository { path, index }) + let mut repository_path = PathBuf::new(); + repository_path.push(path); + Ok(Repository { + path: repository_path, + index, + }) } pub fn path(&self) -> &Path { @@ -120,7 +132,7 @@ impl<'a> Repository<'a> { .ok_or_else(|| anyhow!("cannot compute parent path for {}", &destination_path.to_string_lossy()))?; fs::create_dir_all(parent)?; fs::copy(source_path, destination_path)?; - let relative_path = destination_path.strip_prefix(self.path)?; + let relative_path = destination_path.strip_prefix(&self.path)?; self.index.remember(source_path, relative_path, id); } Ok(()) diff --git a/src/restore.rs b/src/restore.rs index 885c736..4078767 100644 --- a/src/restore.rs +++ b/src/restore.rs @@ -5,12 +5,12 @@ use anyhow::Result; use anyhow::*; pub struct Engine<'a> { - repository: &'a mut Repository<'a>, + repository: &'a mut Repository, target_path: &'a Path, } impl<'a> Engine<'a> { - pub fn new(repository: &'a mut Repository<'a>, target_path: &'a Path) -> Result { + pub fn new(repository: &'a mut Repository, target_path: &'a Path) -> Result { if !target_path.is_absolute() { return Err(anyhow!("path to store not absolute")); }