zvault/src/repository/layout.rs

140 lines
3.3 KiB
Rust
Raw Normal View History

2017-07-21 09:21:59 +00:00
use prelude::*;
2017-04-08 12:35:10 +00:00
use std::path::{Path, PathBuf};
2018-03-09 22:31:20 +00:00
pub trait ChunkRepositoryLayout {
fn base_path(&self) -> &Path;
fn index_path(&self) -> PathBuf;
fn bundle_map_path(&self) -> PathBuf;
fn local_locks_path(&self) -> PathBuf;
fn remote_path(&self) -> PathBuf;
fn remote_exists(&self) -> bool;
fn remote_locks_path(&self) -> PathBuf;
fn remote_bundles_path(&self) -> PathBuf;
fn local_bundles_path(&self) -> PathBuf;
fn remote_bundle_path(&self, bundle: &BundleId, count: usize) -> PathBuf;
fn local_bundle_path(&self, bundle: &BundleId, count: usize) -> PathBuf;
fn temp_bundles_path(&self) -> PathBuf;
fn temp_bundle_path(&self) -> PathBuf;
fn local_bundle_cache_path(&self) -> PathBuf;
fn remote_bundle_cache_path(&self) -> PathBuf;
fn dirtyfile_path(&self) -> PathBuf;
fn config_path(&self) -> PathBuf;
fn remote_readme_path(&self) -> PathBuf;
}
2018-08-07 09:31:50 +00:00
fn bundle_path(bundle: &BundleId, mut folder: PathBuf, mut count: usize) -> PathBuf {
let file = bundle.to_string().to_owned() + ".bundle";
{
let mut rest = &file as &str;
while count >= 100 {
if rest.len() < 10 {
break;
2018-03-09 22:31:20 +00:00
}
2018-08-07 09:31:50 +00:00
folder = folder.join(&rest[0..2]);
rest = &rest[2..];
count /= 250;
2018-03-09 22:31:20 +00:00
}
}
2018-08-07 09:31:50 +00:00
folder.join(Path::new(&file))
2018-03-09 22:31:20 +00:00
}
2018-08-07 09:31:50 +00:00
impl ChunkRepositoryLayout for PathBuf {
2017-04-10 18:35:28 +00:00
#[inline]
2018-03-09 22:31:20 +00:00
fn base_path(&self) -> &Path {
2018-08-07 09:31:50 +00:00
&self
2017-04-08 12:35:10 +00:00
}
2017-04-10 18:35:28 +00:00
#[inline]
2018-03-09 22:31:20 +00:00
fn remote_exists(&self) -> bool {
self.remote_bundles_path().exists() && self.remote_locks_path().exists()
2017-04-08 12:35:10 +00:00
}
2017-04-10 18:35:28 +00:00
#[inline]
2018-03-09 22:31:20 +00:00
fn index_path(&self) -> PathBuf {
2018-08-07 09:31:50 +00:00
self.join("index")
2017-04-08 12:35:10 +00:00
}
2017-04-10 18:35:28 +00:00
#[inline]
2018-03-09 22:31:20 +00:00
fn bundle_map_path(&self) -> PathBuf {
2018-08-07 09:31:50 +00:00
self.join("bundles.map")
}
2017-04-10 18:35:28 +00:00
#[inline]
2018-03-09 22:31:20 +00:00
fn local_locks_path(&self) -> PathBuf {
2018-08-07 09:31:50 +00:00
self.join("locks")
2017-04-08 12:35:10 +00:00
}
2017-04-10 18:35:28 +00:00
#[inline]
2018-03-09 22:31:20 +00:00
fn remote_path(&self) -> PathBuf {
2018-08-07 09:31:50 +00:00
self.join("remote")
2018-03-09 22:31:20 +00:00
}
#[inline]
fn remote_locks_path(&self) -> PathBuf {
2018-08-07 09:31:50 +00:00
self.join("remote/locks")
2017-04-08 12:35:10 +00:00
}
2017-04-10 18:35:28 +00:00
#[inline]
2018-03-09 22:31:20 +00:00
fn remote_bundles_path(&self) -> PathBuf {
2018-08-07 09:31:50 +00:00
self.join("remote/bundles")
2017-04-08 12:35:10 +00:00
}
2017-04-10 18:35:28 +00:00
#[inline]
2018-03-09 22:31:20 +00:00
fn local_bundles_path(&self) -> PathBuf {
2018-08-07 09:31:50 +00:00
self.join("bundles/cached")
2017-04-08 12:35:10 +00:00
}
2017-04-10 18:35:28 +00:00
#[inline]
2018-03-09 22:31:20 +00:00
fn remote_bundle_path(&self, _bundle: &BundleId, count: usize) -> PathBuf {
2018-08-07 09:31:50 +00:00
bundle_path(&BundleId::random(), self.remote_bundles_path(), count)
2017-04-08 12:35:10 +00:00
}
2017-04-10 18:35:28 +00:00
#[inline]
2018-03-09 22:31:20 +00:00
fn local_bundle_path(&self, bundle: &BundleId, count: usize) -> PathBuf {
2018-08-07 09:31:50 +00:00
bundle_path(bundle, self.local_bundles_path(), count)
2017-04-08 12:35:10 +00:00
}
2017-04-10 18:35:28 +00:00
#[inline]
2018-03-09 22:31:20 +00:00
fn temp_bundles_path(&self) -> PathBuf {
2018-08-07 09:31:50 +00:00
self.join("bundles/temp")
2017-04-08 12:35:10 +00:00
}
2017-04-10 18:35:28 +00:00
#[inline]
2018-03-09 22:31:20 +00:00
fn temp_bundle_path(&self) -> PathBuf {
self.temp_bundles_path().join(BundleId::random().to_string().to_owned() + ".bundle")
2017-04-08 12:35:10 +00:00
}
2017-04-10 18:35:28 +00:00
#[inline]
2018-03-09 22:31:20 +00:00
fn local_bundle_cache_path(&self) -> PathBuf {
2018-08-07 09:31:50 +00:00
self.join("bundles/local.cache")
2017-04-08 12:35:10 +00:00
}
2017-04-10 18:35:28 +00:00
#[inline]
2018-03-09 22:31:20 +00:00
fn remote_bundle_cache_path(&self) -> PathBuf {
2018-08-07 09:31:50 +00:00
self.join("bundles/remote.cache")
2017-04-08 12:35:10 +00:00
}
2017-04-10 18:15:13 +00:00
2017-04-10 18:35:28 +00:00
#[inline]
2018-03-09 22:31:20 +00:00
fn dirtyfile_path(&self) -> PathBuf {
2018-08-07 09:31:50 +00:00
self.join("dirty")
2017-04-10 18:15:13 +00:00
}
2018-03-09 22:31:20 +00:00
#[inline]
fn config_path(&self) -> PathBuf {
2018-08-07 09:31:50 +00:00
self.join("config.yaml")
2018-03-09 22:31:20 +00:00
}
#[inline]
fn remote_readme_path(&self) -> PathBuf {
2018-08-07 09:31:50 +00:00
self.join("remote/README.md")
2018-03-09 22:31:20 +00:00
}
}