zvault/src/repository/layout.rs

139 lines
3.2 KiB
Rust
Raw Normal View History

2017-04-08 12:35:10 +00:00
use ::prelude::*;
use std::path::{Path, PathBuf};
#[derive(Clone)]
pub struct RepositoryLayout(PathBuf);
impl RepositoryLayout {
pub fn new<P: AsRef<Path>>(path: P) -> Self {
RepositoryLayout(path.as_ref().to_path_buf())
}
2017-04-10 18:35:28 +00:00
#[inline]
2017-04-08 12:35:10 +00:00
pub fn base_path(&self) -> &Path {
&self.0
}
2017-04-10 18:35:28 +00:00
#[inline]
2017-04-08 12:35:10 +00:00
pub fn config_path(&self) -> PathBuf {
self.0.join("config.yaml")
}
2017-04-10 18:35:28 +00:00
#[inline]
2017-04-08 12:35:10 +00:00
pub fn excludes_path(&self) -> PathBuf {
self.0.join("excludes")
}
2017-04-10 18:35:28 +00:00
#[inline]
2017-04-08 12:35:10 +00:00
pub fn index_path(&self) -> PathBuf {
self.0.join("index")
}
2017-04-10 18:35:28 +00:00
#[inline]
2017-04-08 12:35:10 +00:00
pub fn keys_path(&self) -> PathBuf {
self.0.join("keys")
}
2017-04-10 18:35:28 +00:00
#[inline]
2017-04-08 12:35:10 +00:00
pub fn bundle_map_path(&self) -> PathBuf {
self.0.join("bundles.map")
}
#[inline]
pub fn local_locks_path(&self) -> PathBuf {
self.0.join("locks")
}
2017-04-10 18:35:28 +00:00
#[inline]
2017-04-08 12:35:10 +00:00
pub fn backups_path(&self) -> PathBuf {
self.0.join("remote/backups")
}
2017-04-10 18:35:28 +00:00
#[inline]
2017-04-08 12:35:10 +00:00
pub fn backup_path(&self, name: &str) -> PathBuf {
2017-04-12 16:11:40 +00:00
self.backups_path().join(format!("{}.backup", name))
2017-04-08 12:35:10 +00:00
}
2017-04-10 18:35:28 +00:00
#[inline]
2017-04-08 12:35:10 +00:00
pub fn remote_path(&self) -> PathBuf {
self.0.join("remote")
}
2017-04-10 18:35:28 +00:00
#[inline]
pub fn remote_exists(&self) -> bool {
self.remote_bundles_path().exists() && self.backups_path().exists() && self.remote_locks_path().exists()
}
2017-04-10 18:35:28 +00:00
#[inline]
2017-04-08 12:35:10 +00:00
pub fn remote_readme_path(&self) -> PathBuf {
self.0.join("remote/README.md")
}
2017-04-10 18:35:28 +00:00
#[inline]
2017-04-08 12:35:10 +00:00
pub fn remote_locks_path(&self) -> PathBuf {
self.0.join("remote/locks")
}
2017-04-10 18:35:28 +00:00
#[inline]
2017-04-08 12:35:10 +00:00
pub fn remote_bundles_path(&self) -> PathBuf {
self.0.join("remote/bundles")
}
2017-04-10 18:35:28 +00:00
#[inline]
2017-04-08 12:35:10 +00:00
pub fn local_bundles_path(&self) -> PathBuf {
self.0.join("bundles/cached")
}
fn bundle_path(&self, bundle: &BundleId, mut folder: PathBuf, mut count: usize) -> (PathBuf, PathBuf) {
let file = bundle.to_string().to_owned() + ".bundle";
{
let mut rest = &file as &str;
while count >= 100 {
if rest.len() < 10 {
break
}
folder = folder.join(&rest[0..2]);
rest = &rest[2..];
count /= 250;
2017-04-08 12:35:10 +00:00
}
}
(folder, file.into())
}
2017-04-10 18:35:28 +00:00
#[inline]
2017-04-08 12:35:10 +00:00
pub fn remote_bundle_path(&self, count: usize) -> (PathBuf, PathBuf) {
self.bundle_path(&BundleId::random(), self.remote_bundles_path(), count)
}
2017-04-10 18:35:28 +00:00
#[inline]
2017-04-08 12:35:10 +00:00
pub fn local_bundle_path(&self, bundle: &BundleId, count: usize) -> (PathBuf, PathBuf) {
self.bundle_path(bundle, self.local_bundles_path(), count)
}
2017-04-10 18:35:28 +00:00
#[inline]
2017-04-08 12:35:10 +00:00
pub fn temp_bundles_path(&self) -> PathBuf {
self.0.join("bundles/temp")
}
2017-04-10 18:35:28 +00:00
#[inline]
2017-04-08 12:35:10 +00:00
pub fn temp_bundle_path(&self) -> PathBuf {
self.temp_bundles_path().join(BundleId::random().to_string().to_owned() + ".bundle")
}
2017-04-10 18:35:28 +00:00
#[inline]
2017-04-08 12:35:10 +00:00
pub fn local_bundle_cache_path(&self) -> PathBuf {
self.0.join("bundles/local.cache")
}
2017-04-10 18:35:28 +00:00
#[inline]
2017-04-08 12:35:10 +00:00
pub fn remote_bundle_cache_path(&self) -> PathBuf {
self.0.join("bundles/remote.cache")
}
2017-04-10 18:15:13 +00:00
2017-04-10 18:35:28 +00:00
#[inline]
2017-04-10 18:15:13 +00:00
pub fn dirtyfile_path(&self) -> PathBuf {
self.0.join("dirty")
}
2017-04-08 12:35:10 +00:00
}