Refactor metadata

This commit is contained in:
Dennis Schwerdel 2018-03-11 23:32:54 +01:00
parent f3f6a3cf49
commit 6f0172bdd9
7 changed files with 21 additions and 42 deletions

View File

@ -204,7 +204,7 @@ impl BackupRepository {
inode.group = group.gid();
}
}
try!(self.repo.save_inode_at(&inode, &path));
try!(self.save_inode_at(&inode, &path));
}
if inode.file_type == FileType::Directory {
let path = if is_root {
@ -231,7 +231,7 @@ impl BackupRepository {
failed_paths: &mut Vec<PathBuf>,
) -> Result<Inode, RepositoryError> {
let path = path.as_ref();
let mut inode = try!(self.repo.create_inode(path, reference));
let mut inode = try!(self.create_inode(path, reference));
if !backup.user_names.contains_key(&inode.user) {
if let Some(user) = users::get_user_by_uid(inode.user) {
backup.user_names.insert(
@ -296,7 +296,7 @@ impl BackupRepository {
}
Err(err) => return Err(err),
};
let chunks = try!(self.repo.put_inode(&child_inode));
let chunks = try!(self.put_inode(&child_inode));
inode.cum_size += child_inode.cum_size;
for &(_, len) in chunks.iter() {
meta_size += u64::from(len);
@ -352,7 +352,7 @@ impl BackupRepository {
&mut backup,
&mut failed_paths
));
backup.root = try!(self.repo.put_inode(&root_inode));
backup.root = try!(self.put_inode(&root_inode));
try!(self.repo.flush());
let elapsed = Local::now().signed_duration_since(start);
backup.timestamp = start.timestamp();
@ -393,14 +393,14 @@ impl BackupRepository {
remove_from.children.as_mut().unwrap().remove(
&to_remove.name
);
let mut last_inode_chunks = try!(self.repo.put_inode(&remove_from));
let mut last_inode_chunks = try!(self.put_inode(&remove_from));
let mut last_inode_name = remove_from.name;
while let Some(mut inode) = inodes.pop() {
inode.children.as_mut().unwrap().insert(
last_inode_name,
last_inode_chunks
);
last_inode_chunks = try!(self.repo.put_inode(&inode));
last_inode_chunks = try!(self.put_inode(&inode));
last_inode_name = inode.name;
}
backup.root = last_inode_chunks;

View File

@ -106,7 +106,7 @@ impl BackupRepository {
}
}
if modified {
Ok(Some(try!(self.repo.put_inode(&inode))))
Ok(Some(try!(self.put_inode(&inode))))
} else {
try!(self.repo.check_chunks(checked, chunks, true));
Ok(None)
@ -242,11 +242,11 @@ impl BackupRepository {
children.remove(&name);
}
}
let mut chunks = try!(self.repo.put_inode(&inode));
let mut chunks = try!(self.put_inode(&inode));
while let Some(mut parent) = inodes.pop() {
parent.children.as_mut().unwrap().insert(inode.name, chunks);
inode = parent;
chunks = try!(self.repo.put_inode(&inode));
chunks = try!(self.put_inode(&inode));
}
if modified {
try!(self.repo.flush());

View File

@ -4,8 +4,10 @@ use std::path::Path;
use std::fs::File;
use std::io::{Read, Write};
use super::*;
impl Repository {
impl BackupRepository {
pub fn create_inode<P: AsRef<Path>>(
&mut self,
path: P,
@ -25,13 +27,13 @@ impl Repository {
try!(file.read_to_end(&mut data));
inode.data = Some(FileData::Inline(data.into()));
} else {
let mut chunks = try!(self.put_stream(BundleMode::Data, &mut file));
let mut chunks = try!(self.repo.put_stream(BundleMode::Data, &mut file));
if chunks.len() < 10 {
inode.data = Some(FileData::ChunkedDirect(chunks));
} else {
let mut chunk_data = Vec::with_capacity(chunks.encoded_size());
chunks.write_to(&mut chunk_data).unwrap();
chunks = try!(self.put_data(BundleMode::Meta, &chunk_data));
chunks = try!(self.repo.put_data(BundleMode::Meta, &chunk_data));
inode.data = Some(FileData::ChunkedIndirect(chunks));
}
}
@ -41,7 +43,7 @@ impl Repository {
#[inline]
pub fn put_inode(&mut self, inode: &Inode) -> Result<ChunkList, RepositoryError> {
self.put_data(BundleMode::Meta, &try!(inode.encode()))
self.repo.put_data(BundleMode::Meta, &try!(inode.encode()))
}
#[inline]
@ -61,12 +63,12 @@ impl Repository {
try!(file.write_all(data));
}
FileData::ChunkedDirect(ref chunks) => {
try!(self.get_stream(chunks, &mut file));
try!(self.repo.get_stream(chunks, &mut file));
}
FileData::ChunkedIndirect(ref chunks) => {
let chunk_data = try!(self.get_data(chunks));
let chunks = ChunkList::read_from(&chunk_data);
try!(self.get_stream(&chunks, &mut file));
try!(self.repo.get_stream(&chunks, &mut file));
}
}
}

View File

@ -5,6 +5,7 @@ mod tarfile;
mod backup;
mod integrity;
mod vacuum;
mod metadata;
pub use self::backup::{BackupOptions, BackupError, DiffType};
pub use self::backup_file::{BackupFile, BackupFileError};
@ -37,6 +38,7 @@ impl BackupRepository {
try!(File::create(layout.excludes_path()).and_then(|mut f| {
f.write_all(DEFAULT_EXCLUDES)
}));
try!(fs::create_dir_all(layout.backups_path()));
try!(fs::create_dir(layout.keys_path()));
let crypto = Arc::new(try!(Crypto::open(layout.keys_path())));
Ok(BackupRepository {
@ -96,11 +98,6 @@ impl BackupRepository {
self.repo.set_encryption(public)
}
#[inline]
pub fn get_inode(&mut self, chunks: &[Chunk]) -> Result<Inode, RepositoryError> {
self.repo.get_inode(chunks)
}
pub fn get_config(&self) -> &Config {
self.repo.get_config()
}

View File

@ -219,7 +219,7 @@ impl BackupRepository {
}
for path in childless {
let (inode, _) = inodes.remove(&path).unwrap();
let chunks = try!(self.repo.put_inode(&inode));
let chunks = try!(self.put_inode(&inode));
if let Some(parent_path) = path.parent() {
if let Some(&mut (ref mut parent_inode, ref mut children)) =
inodes.get_mut(parent_path)
@ -265,7 +265,7 @@ impl BackupRepository {
children.insert(inode.name, chunks);
}
root_inode.children = Some(children);
let chunks = try!(self.repo.put_inode(&root_inode));
let chunks = try!(self.put_inode(&root_inode));
Ok((root_inode, chunks))
}
}

View File

@ -23,9 +23,6 @@ pub trait ChunkRepositoryLayout {
fn config_path(&self) -> PathBuf;
fn excludes_path(&self) -> PathBuf;
fn backups_path(&self) -> PathBuf;
fn backup_path(&self, name: &str) -> PathBuf;
fn remote_readme_path(&self) -> PathBuf;
}
@ -181,21 +178,6 @@ impl ChunkRepositoryLayout for RepositoryLayout {
self.0.join("config.yaml")
}
#[inline]
fn excludes_path(&self) -> PathBuf {
self.0.join("excludes")
}
#[inline]
fn backups_path(&self) -> PathBuf {
self.0.join("remote/backups")
}
#[inline]
fn backup_path(&self, name: &str) -> PathBuf {
self.backups_path().join(format!("{}.backup", name))
}
#[inline]
fn remote_readme_path(&self) -> PathBuf {
self.0.join("remote/README.md")

View File

@ -3,7 +3,6 @@ mod bundle_map;
mod integrity;
mod basic_io;
mod info;
mod metadata;
mod error;
mod vacuum;
mod layout;
@ -108,7 +107,6 @@ impl Repository {
INDEX_VERSION
));
try!(BundleMap::create().save(layout.bundle_map_path()));
try!(fs::create_dir_all(layout.backups_path()));
Self::open(layout, crypto, true)
}