refactoring, still wip on versioning
This commit is contained in:
parent
ae4ed1f985
commit
18881df4a9
8 changed files with 278 additions and 114 deletions
108
Cargo.lock
generated
108
Cargo.lock
generated
|
@ -1,5 +1,10 @@
|
|||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
[[package]]
|
||||
name = "autocfg"
|
||||
version = "0.1.6"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
|
||||
[[package]]
|
||||
name = "backtrace"
|
||||
version = "0.3.35"
|
||||
|
@ -25,11 +30,18 @@ name = "bakare"
|
|||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"failure 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"rmp-serde 0.13.7 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"rust-crypto 0.2.36 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"serde 1.0.99 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"tempfile 3.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"walkdir 2.2.9 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "byteorder"
|
||||
version = "1.3.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
|
||||
[[package]]
|
||||
name = "c2-chacha"
|
||||
version = "0.2.2"
|
||||
|
@ -99,6 +111,22 @@ name = "libc"
|
|||
version = "0.2.62"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
|
||||
[[package]]
|
||||
name = "num-traits"
|
||||
version = "0.1.43"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
dependencies = [
|
||||
"num-traits 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "num-traits"
|
||||
version = "0.2.8"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
dependencies = [
|
||||
"autocfg 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "ppv-lite86"
|
||||
version = "0.2.5"
|
||||
|
@ -112,6 +140,14 @@ dependencies = [
|
|||
"unicode-xid 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "proc-macro2"
|
||||
version = "1.0.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
dependencies = [
|
||||
"unicode-xid 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "quote"
|
||||
version = "0.6.13"
|
||||
|
@ -120,6 +156,14 @@ dependencies = [
|
|||
"proc-macro2 0.4.30 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "quote"
|
||||
version = "1.0.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
dependencies = [
|
||||
"proc-macro2 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "rand"
|
||||
version = "0.3.23"
|
||||
|
@ -212,6 +256,25 @@ dependencies = [
|
|||
"winapi 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "rmp"
|
||||
version = "0.8.7"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
dependencies = [
|
||||
"byteorder 1.3.2 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"num-traits 0.1.43 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "rmp-serde"
|
||||
version = "0.13.7"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
dependencies = [
|
||||
"byteorder 1.3.2 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"rmp 0.8.7 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"serde 1.0.99 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "rust-crypto"
|
||||
version = "0.2.36"
|
||||
|
@ -242,6 +305,24 @@ dependencies = [
|
|||
"winapi-util 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "serde"
|
||||
version = "1.0.99"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
dependencies = [
|
||||
"serde_derive 1.0.99 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "serde_derive"
|
||||
version = "1.0.99"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
dependencies = [
|
||||
"proc-macro2 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"quote 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"syn 1.0.5 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "syn"
|
||||
version = "0.15.44"
|
||||
|
@ -252,6 +333,16 @@ dependencies = [
|
|||
"unicode-xid 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "syn"
|
||||
version = "1.0.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
dependencies = [
|
||||
"proc-macro2 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"quote 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"unicode-xid 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "synstructure"
|
||||
version = "0.10.2"
|
||||
|
@ -291,6 +382,11 @@ name = "unicode-xid"
|
|||
version = "0.1.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
|
||||
[[package]]
|
||||
name = "unicode-xid"
|
||||
version = "0.2.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
|
||||
[[package]]
|
||||
name = "walkdir"
|
||||
version = "2.2.9"
|
||||
|
@ -334,8 +430,10 @@ version = "0.4.0"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
|
||||
[metadata]
|
||||
"checksum autocfg 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)" = "b671c8fb71b457dd4ae18c4ba1e59aa81793daacc361d82fcd410cef0d491875"
|
||||
"checksum backtrace 0.3.35 (registry+https://github.com/rust-lang/crates.io-index)" = "1371048253fa3bac6704bfd6bbfc922ee9bdcee8881330d40f308b81cc5adc55"
|
||||
"checksum backtrace-sys 0.1.31 (registry+https://github.com/rust-lang/crates.io-index)" = "82a830b4ef2d1124a711c71d263c5abdc710ef8e907bd508c88be475cebc422b"
|
||||
"checksum byteorder 1.3.2 (registry+https://github.com/rust-lang/crates.io-index)" = "a7c3dd8985a7111efc5c80b44e23ecdd8c007de8ade3b96595387e812b957cf5"
|
||||
"checksum c2-chacha 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "7d64d04786e0f528460fc884753cf8dddcc466be308f6026f8e355c41a0e4101"
|
||||
"checksum cc 1.0.41 (registry+https://github.com/rust-lang/crates.io-index)" = "8dae9c4b8fedcae85592ba623c4fd08cfdab3e3b72d6df780c6ead964a69bfff"
|
||||
"checksum cfg-if 0.1.9 (registry+https://github.com/rust-lang/crates.io-index)" = "b486ce3ccf7ffd79fdeb678eac06a9e6c09fc88d33836340becb8fffe87c5e33"
|
||||
|
@ -346,9 +444,13 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
|||
"checksum getrandom 0.1.11 (registry+https://github.com/rust-lang/crates.io-index)" = "fc344b02d3868feb131e8b5fe2b9b0a1cc42942679af493061fc13b853243872"
|
||||
"checksum lazy_static 1.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646"
|
||||
"checksum libc 0.2.62 (registry+https://github.com/rust-lang/crates.io-index)" = "34fcd2c08d2f832f376f4173a231990fa5aef4e99fb569867318a227ef4c06ba"
|
||||
"checksum num-traits 0.1.43 (registry+https://github.com/rust-lang/crates.io-index)" = "92e5113e9fd4cc14ded8e499429f396a20f98c772a47cc8622a736e1ec843c31"
|
||||
"checksum num-traits 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)" = "6ba9a427cfca2be13aa6f6403b0b7e7368fe982bfa16fccc450ce74c46cd9b32"
|
||||
"checksum ppv-lite86 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)" = "e3cbf9f658cdb5000fcf6f362b8ea2ba154b9f146a61c7a20d647034c6b6561b"
|
||||
"checksum proc-macro2 0.4.30 (registry+https://github.com/rust-lang/crates.io-index)" = "cf3d2011ab5c909338f7887f4fc896d35932e29146c12c8d01da6b22a80ba759"
|
||||
"checksum proc-macro2 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "175a40b9cf564ce9bf050654633dbf339978706b8ead1a907bb970b63185dd95"
|
||||
"checksum quote 0.6.13 (registry+https://github.com/rust-lang/crates.io-index)" = "6ce23b6b870e8f94f81fb0a363d65d86675884b34a09043c81e5562f11c1f8e1"
|
||||
"checksum quote 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "053a8c8bcc71fcce321828dc897a98ab9760bef03a4fc36693c231e5b3216cfe"
|
||||
"checksum rand 0.3.23 (registry+https://github.com/rust-lang/crates.io-index)" = "64ac302d8f83c0c1974bf758f6b041c6c8ada916fbb44a609158ca8b064cc76c"
|
||||
"checksum rand 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)" = "552840b97013b1a26992c11eac34bdd778e464601a4c2054b5f0bff7c6761293"
|
||||
"checksum rand 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)" = "d47eab0e83d9693d40f825f86948aa16eff6750ead4bdffc4ab95b8b3a7f052c"
|
||||
|
@ -360,15 +462,21 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
|||
"checksum rdrand 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "678054eb77286b51581ba43620cc911abf02758c91f93f479767aed0f90458b2"
|
||||
"checksum redox_syscall 0.1.56 (registry+https://github.com/rust-lang/crates.io-index)" = "2439c63f3f6139d1b57529d16bc3b8bb855230c8efcc5d3a896c8bea7c3b1e84"
|
||||
"checksum remove_dir_all 0.5.2 (registry+https://github.com/rust-lang/crates.io-index)" = "4a83fa3702a688b9359eccba92d153ac33fd2e8462f9e0e3fdf155239ea7792e"
|
||||
"checksum rmp 0.8.7 (registry+https://github.com/rust-lang/crates.io-index)" = "a3d45d7afc9b132b34a2479648863aa95c5c88e98b32285326a6ebadc80ec5c9"
|
||||
"checksum rmp-serde 0.13.7 (registry+https://github.com/rust-lang/crates.io-index)" = "011e1d58446e9fa3af7cdc1fb91295b10621d3ac4cb3a85cc86385ee9ca50cd3"
|
||||
"checksum rust-crypto 0.2.36 (registry+https://github.com/rust-lang/crates.io-index)" = "f76d05d3993fd5f4af9434e8e436db163a12a9d40e1a58a726f27a01dfd12a2a"
|
||||
"checksum rustc-demangle 0.1.16 (registry+https://github.com/rust-lang/crates.io-index)" = "4c691c0e608126e00913e33f0ccf3727d5fc84573623b8d65b2df340b5201783"
|
||||
"checksum rustc-serialize 0.3.24 (registry+https://github.com/rust-lang/crates.io-index)" = "dcf128d1287d2ea9d80910b5f1120d0b8eede3fbf1abe91c40d39ea7d51e6fda"
|
||||
"checksum same-file 1.0.5 (registry+https://github.com/rust-lang/crates.io-index)" = "585e8ddcedc187886a30fa705c47985c3fa88d06624095856b36ca0b82ff4421"
|
||||
"checksum serde 1.0.99 (registry+https://github.com/rust-lang/crates.io-index)" = "fec2851eb56d010dc9a21b89ca53ee75e6528bab60c11e89d38390904982da9f"
|
||||
"checksum serde_derive 1.0.99 (registry+https://github.com/rust-lang/crates.io-index)" = "cb4dc18c61206b08dc98216c98faa0232f4337e1e1b8574551d5bad29ea1b425"
|
||||
"checksum syn 0.15.44 (registry+https://github.com/rust-lang/crates.io-index)" = "9ca4b3b69a77cbe1ffc9e198781b7acb0c7365a883670e8f1c1bc66fba79a5c5"
|
||||
"checksum syn 1.0.5 (registry+https://github.com/rust-lang/crates.io-index)" = "66850e97125af79138385e9b88339cbcd037e3f28ceab8c5ad98e64f0f1f80bf"
|
||||
"checksum synstructure 0.10.2 (registry+https://github.com/rust-lang/crates.io-index)" = "02353edf96d6e4dc81aea2d8490a7e9db177bf8acb0e951c24940bf866cb313f"
|
||||
"checksum tempfile 3.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "7a6e24d9338a0a5be79593e2fa15a648add6138caa803e2d5bc782c371732ca9"
|
||||
"checksum time 0.1.42 (registry+https://github.com/rust-lang/crates.io-index)" = "db8dcfca086c1143c9270ac42a2bbd8a7ee477b78ac8e45b19abfb0cbede4b6f"
|
||||
"checksum unicode-xid 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "fc72304796d0818e357ead4e000d19c9c174ab23dc11093ac919054d20a6a7fc"
|
||||
"checksum unicode-xid 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "826e7639553986605ec5979c7dd957c7895e93eabed50ab2ffa7f6128a75097c"
|
||||
"checksum walkdir 2.2.9 (registry+https://github.com/rust-lang/crates.io-index)" = "9658c94fa8b940eab2250bd5a457f9c48b748420d71293b165c8cdbe2f55f71e"
|
||||
"checksum wasi 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)" = "fd5442abcac6525a045cc8c795aedb60da7a2e5e89c7bf18a0d5357849bb23c7"
|
||||
"checksum winapi 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)" = "8093091eeb260906a183e6ae1abdba2ef5ef2257a21801128899c3fc699229c6"
|
||||
|
|
|
@ -6,7 +6,9 @@ edition = "2018"
|
|||
license = "AGPL-3.0"
|
||||
|
||||
[dependencies]
|
||||
walkdir = "2.2"
|
||||
rust-crypto = "0.2"
|
||||
tempfile = "3.1"
|
||||
failure = "0.1"
|
||||
rmp-serde = "0.13"
|
||||
rust-crypto = "0.2"
|
||||
serde = { version = "1.0", features = ["derive"] }
|
||||
tempfile = "3.1"
|
||||
walkdir = "2.2"
|
||||
|
|
|
@ -17,6 +17,8 @@ pub enum BakareError {
|
|||
DirectoryNotFile,
|
||||
#[fail(display = "corrupted repository - cannot find file")]
|
||||
CorruptedRepoNoFile,
|
||||
#[fail(display = "index loading error")]
|
||||
IndexLoadingError,
|
||||
}
|
||||
|
||||
impl From<io::Error> for BakareError {
|
||||
|
@ -36,3 +38,9 @@ impl From<StripPrefixError> for BakareError {
|
|||
BakareError::IOError(None)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<rmp_serde::decode::Error> for BakareError {
|
||||
fn from(_: rmp_serde::decode::Error) -> Self {
|
||||
BakareError::IndexLoadingError
|
||||
}
|
||||
}
|
||||
|
|
79
src/index.rs
Normal file
79
src/index.rs
Normal file
|
@ -0,0 +1,79 @@
|
|||
use std::path::Path;
|
||||
|
||||
use rmp_serde::{Deserializer, Serializer};
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use crate::repository_item::RepositoryItem;
|
||||
|
||||
#[derive(Clone, Debug, PartialOrd, PartialEq, Ord, Eq, Serialize, Deserialize)]
|
||||
pub struct IndexItem {
|
||||
relative_path: String,
|
||||
original_source_path: String,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
pub struct Index<'a> {
|
||||
items: Vec<IndexItem>,
|
||||
index_path: &'a str,
|
||||
repository_path: &'a str,
|
||||
}
|
||||
|
||||
impl<'a> Index<'a> {
|
||||
pub fn len(&self) -> usize {
|
||||
self.items.len()
|
||||
}
|
||||
|
||||
pub fn is_empty(&self) -> bool {
|
||||
self.items.is_empty()
|
||||
}
|
||||
|
||||
pub fn iter(&self) -> IndexIterator {
|
||||
IndexIterator {
|
||||
index: self,
|
||||
current_item_number: 0,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn remember(&mut self, item: RepositoryItem) {
|
||||
self.items.push(item.into());
|
||||
}
|
||||
|
||||
fn repository_item(&self, i: &IndexItem) -> RepositoryItem {
|
||||
let index_item = i.clone();
|
||||
let relative_path = Path::new(index_item.relative_path.as_str());
|
||||
let repository_path = Path::new(self.repository_path);
|
||||
let original_source_path = Path::new(index_item.original_source_path.as_str());
|
||||
let absolute_path = repository_path.join(relative_path);
|
||||
let absolute_path = absolute_path.as_path();
|
||||
RepositoryItem::from(original_source_path, absolute_path, relative_path)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<RepositoryItem> for IndexItem {
|
||||
fn from(i: RepositoryItem) -> Self {
|
||||
IndexItem {
|
||||
relative_path: i.relative_path().to_string_lossy().to_string(),
|
||||
original_source_path: i.original_source_path().to_string_lossy().to_string(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub struct IndexIterator<'a> {
|
||||
index: &'a Index<'a>,
|
||||
current_item_number: usize,
|
||||
}
|
||||
|
||||
impl<'a> Iterator for IndexIterator<'a> {
|
||||
type Item = RepositoryItem;
|
||||
|
||||
fn next(&mut self) -> Option<Self::Item> {
|
||||
if self.index.is_empty() || self.current_item_number > self.index.len() - 1 {
|
||||
None
|
||||
} else {
|
||||
let current_item_number = self.current_item_number;
|
||||
self.current_item_number += 1;
|
||||
let index_item = &self.index.items[current_item_number];
|
||||
Some(self.index.repository_item(index_item))
|
||||
}
|
||||
}
|
||||
}
|
16
src/lib.rs
16
src/lib.rs
|
@ -1,19 +1,9 @@
|
|||
use crate::repository::RepositoryItem;
|
||||
|
||||
pub mod backup;
|
||||
pub mod error;
|
||||
pub mod restore;
|
||||
pub mod source;
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
mod index;
|
||||
pub mod repository;
|
||||
|
||||
#[derive(Copy, Clone, Debug, PartialOrd, PartialEq, Ord, Eq)]
|
||||
pub struct ItemVersion<'a>(&'a str);
|
||||
|
||||
#[derive(Copy, Clone)]
|
||||
pub struct IndexVersion;
|
||||
|
||||
struct IndexViewReadonly<'a> {
|
||||
index_version: IndexVersion,
|
||||
items: Vec<RepositoryItem<'a>>,
|
||||
}
|
||||
mod repository_item;
|
||||
|
|
|
@ -4,9 +4,11 @@ use std::path::Path;
|
|||
use walkdir::WalkDir;
|
||||
|
||||
use crate::error::BakareError;
|
||||
use crate::IndexVersion;
|
||||
use crate::IndexViewReadonly;
|
||||
use crate::ItemVersion;
|
||||
use crate::index::{Index, IndexIterator};
|
||||
use crate::repository_item::RepositoryItem;
|
||||
use rmp_serde::{Deserializer, Serializer};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::fs::File;
|
||||
|
||||
/// represents a place where backup is stored an can be restored from.
|
||||
/// right now only on-disk directory storage is supported
|
||||
|
@ -15,63 +17,18 @@ use crate::ItemVersion;
|
|||
pub struct Repository<'a> {
|
||||
/// absolute path to where the repository is stored on disk
|
||||
path: &'a Path,
|
||||
index: IndexViewReadonly<'a>,
|
||||
newest_index_version: IndexVersion,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialOrd, PartialEq, Ord, Eq)]
|
||||
pub struct RepositoryItem<'a> {
|
||||
version: ItemVersion<'a>,
|
||||
relative_path: Box<Path>,
|
||||
absolute_path: Box<Path>,
|
||||
original_source_path: Box<Path>,
|
||||
index: Index<'a>,
|
||||
}
|
||||
|
||||
pub struct RepositoryIterator<'a> {
|
||||
version: IndexVersion,
|
||||
index: &'a IndexViewReadonly<'a>,
|
||||
current_item_number: usize,
|
||||
}
|
||||
|
||||
impl<'a> RepositoryItem<'a> {
|
||||
pub fn save(&self, save_to: &Path) -> Result<(), BakareError> {
|
||||
if !save_to.is_absolute() {
|
||||
return Err(BakareError::PathToStoreNotAbsolute);
|
||||
}
|
||||
|
||||
let target_path = save_to.join(self.relative_path.clone());
|
||||
let parent = target_path.parent().unwrap();
|
||||
if !parent.exists() {
|
||||
println!("Creating {}", parent.display());
|
||||
fs::create_dir_all(parent)?;
|
||||
}
|
||||
if !self.absolute_path.exists() {
|
||||
return Err(BakareError::CorruptedRepoNoFile);
|
||||
}
|
||||
println!("Saving {} to {}", self.absolute_path.display(), target_path.display());
|
||||
fs::copy(self.absolute_path.clone(), target_path.clone())?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
index_iterator: IndexIterator<'a>,
|
||||
}
|
||||
|
||||
impl<'a> Iterator for RepositoryIterator<'a> {
|
||||
type Item = &'a RepositoryItem<'a>;
|
||||
type Item = RepositoryItem;
|
||||
|
||||
fn next(&mut self) -> Option<Self::Item> {
|
||||
if self.index.items.is_empty() || self.current_item_number > self.index.items.len() - 1 {
|
||||
None
|
||||
} else {
|
||||
let current_item_number = self.current_item_number;
|
||||
self.current_item_number += 1;
|
||||
Some(&self.index.items[current_item_number])
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> RepositoryItem<'a> {
|
||||
pub fn version(&self) -> &ItemVersion {
|
||||
&self.version
|
||||
self.index_iterator.next()
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -81,38 +38,18 @@ impl<'a> Repository<'a> {
|
|||
return Err(BakareError::RepositoryPathNotAbsolute);
|
||||
}
|
||||
|
||||
let all_files = Repository::get_all_files_recursively(path)?;
|
||||
let all_items: Result<Vec<RepositoryItem>, BakareError> = all_files
|
||||
.into_iter()
|
||||
.map(|p| {
|
||||
let relative_path = Box::from(p.strip_prefix(path)?);
|
||||
Ok(RepositoryItem {
|
||||
version: ItemVersion(""),
|
||||
relative_path,
|
||||
absolute_path: Box::from(p),
|
||||
})
|
||||
})
|
||||
.collect();
|
||||
let mut all_items = all_items?;
|
||||
all_items.sort();
|
||||
let index_file_path = path.join("index");
|
||||
let index_file = File::open(index_file_path)?;
|
||||
let mut deserializer = Deserializer::from_read(index_file);
|
||||
let index: Result<Index, _> = Deserialize::deserialize(&mut deserializer);
|
||||
let index = index?;
|
||||
|
||||
let version = IndexVersion;
|
||||
println!("opened repository at {} - {} items present", path.display(), all_items.len());
|
||||
Ok(Repository {
|
||||
path,
|
||||
index: IndexViewReadonly {
|
||||
index_version: version,
|
||||
items: all_items,
|
||||
},
|
||||
newest_index_version: version,
|
||||
})
|
||||
Ok(Repository { path, index })
|
||||
}
|
||||
|
||||
pub fn iter(&self) -> RepositoryIterator {
|
||||
RepositoryIterator {
|
||||
index: &self.index,
|
||||
version: self.index.index_version,
|
||||
current_item_number: 0,
|
||||
index_iterator: self.index.iter(),
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -132,30 +69,27 @@ impl<'a> Repository<'a> {
|
|||
println!("storing {} as {}", source_path.display(), destination_path.display());
|
||||
fs::create_dir_all(destination_path.parent().unwrap())?;
|
||||
fs::copy(source_path, destination_path)?;
|
||||
self.index.items.push(RepositoryItem {
|
||||
version: ItemVersion(""),
|
||||
relative_path: Box::from(destination_path.strip_prefix(self.path)?),
|
||||
absolute_path: Box::from(destination_path),
|
||||
});
|
||||
|
||||
self.index.remember(RepositoryItem::from(
|
||||
source_path,
|
||||
destination_path,
|
||||
destination_path.strip_prefix(self.path)?,
|
||||
));
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn item_by_source_path(&self, path: &Path) -> Result<Option<&RepositoryItem>, BakareError> {
|
||||
pub fn item_by_source_path(&self, path: &Path) -> Result<Option<RepositoryItem>, BakareError> {
|
||||
println!(
|
||||
"trying to find {} in a repo [{}] of {} items",
|
||||
path.display(),
|
||||
self.path.display(),
|
||||
self.index.items.len()
|
||||
self.index.len()
|
||||
);
|
||||
if !path.is_absolute() {
|
||||
return Err(BakareError::RepositoryPathNotAbsolute);
|
||||
}
|
||||
self.index.items.iter().find(|i| *i.original_source_path == *path)
|
||||
}
|
||||
|
||||
pub fn newest_version_for(&self, item: RepositoryItem) -> ItemVersion {
|
||||
ItemVersion("")
|
||||
Ok(self.iter().find(|i| i.original_source_path() == path))
|
||||
}
|
||||
|
||||
fn get_all_files_recursively(path: &Path) -> Result<Vec<Box<Path>>, BakareError> {
|
||||
|
|
48
src/repository_item.rs
Normal file
48
src/repository_item.rs
Normal file
|
@ -0,0 +1,48 @@
|
|||
use crate::error::BakareError;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::fs;
|
||||
use std::path::Path;
|
||||
|
||||
#[derive(Clone, Debug, PartialOrd, PartialEq, Ord, Eq)]
|
||||
pub struct RepositoryItem {
|
||||
relative_path: Box<Path>,
|
||||
absolute_path: Box<Path>,
|
||||
original_source_path: Box<Path>,
|
||||
}
|
||||
|
||||
impl RepositoryItem {
|
||||
pub fn from(original_source_path: &Path, absolute_path: &Path, relative_path: &Path) -> Self {
|
||||
RepositoryItem {
|
||||
relative_path: Box::from(relative_path),
|
||||
absolute_path: Box::from(absolute_path),
|
||||
original_source_path: Box::from(original_source_path),
|
||||
}
|
||||
}
|
||||
pub fn save(&self, save_to: &Path) -> Result<(), BakareError> {
|
||||
if !save_to.is_absolute() {
|
||||
return Err(BakareError::PathToStoreNotAbsolute);
|
||||
}
|
||||
|
||||
let target_path = save_to.join(self.relative_path.clone());
|
||||
let parent = target_path.parent().unwrap();
|
||||
if !parent.exists() {
|
||||
println!("Creating {}", parent.display());
|
||||
fs::create_dir_all(parent)?;
|
||||
}
|
||||
if !self.absolute_path.exists() {
|
||||
return Err(BakareError::CorruptedRepoNoFile);
|
||||
}
|
||||
println!("Saving {} to {}", self.absolute_path.display(), target_path.display());
|
||||
fs::copy(self.absolute_path.clone(), target_path.clone())?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn relative_path(&self) -> &Path {
|
||||
&self.relative_path
|
||||
}
|
||||
|
||||
pub fn original_source_path(&self) -> &Path {
|
||||
&self.original_source_path
|
||||
}
|
||||
}
|
|
@ -5,8 +5,7 @@ use walkdir::DirEntry;
|
|||
|
||||
use crate::error::BakareError;
|
||||
use crate::repository::Repository;
|
||||
use crate::repository::RepositoryItem;
|
||||
use crate::ItemVersion;
|
||||
use crate::repository_item::RepositoryItem;
|
||||
|
||||
pub struct Engine<'a> {
|
||||
repository: &'a Repository<'a>,
|
||||
|
@ -23,7 +22,7 @@ impl<'a> Engine<'a> {
|
|||
|
||||
pub fn restore_all(&self) -> Result<(), BakareError> {
|
||||
for item in self.repository.iter() {
|
||||
self.restore(item)?;
|
||||
self.restore(&item)?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
@ -33,8 +32,4 @@ impl<'a> Engine<'a> {
|
|||
item.save(self.target_path)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn restore_as_of_version(&self, item: &RepositoryItem, version: &ItemVersion) -> Result<(), BakareError> {
|
||||
unimplemented!()
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue