Skip to content
This repository has been archived by the owner on Aug 15, 2024. It is now read-only.

Commit

Permalink
feat: add method to check directory integrity
Browse files Browse the repository at this point in the history
  • Loading branch information
PascalinDe committed Nov 6, 2023
1 parent 8ab670d commit 2fd4a9f
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/flatfiledirmgr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ impl<R: Runtime> FlatFileDirMgr<R> {
const MICRODESCRIPTORS_FILENAME: &'static str = "microdescriptors.txt";
const CERTIFICATE_FILENAME: &'static str = "certificate.txt";
const CHURN_FILENAME: &'static str = "churn.txt";
const AUTHORITY_FILENAME: &'static str = "authority.json";
/// Create a new FlatFileDirMgr from a given configuration.
pub fn from_config(config: DirMgrConfig, circmgr: Arc<CircMgr<R>>) -> Result<Arc<Self>> {
let netdir = SharedMutArc::new();
Expand All @@ -75,13 +76,29 @@ impl<R: Runtime> FlatFileDirMgr<R> {
}))
}

/// Check cache directory content.
pub fn check_directory(&self, cache_path: &Path) -> Result<()> {
let mut any_missing = false;
for filename in vec![Self::CONSENSUS_FILENAME, Self::MICRODESCRIPTORS_FILENAME, Self::CERTIFICATE_FILENAME, Self::CHURN_FILENAME, Self::AUTHORITY_FILENAME].iter() {
if !cache_path.join(filename).exists(){
any_missing = true;
debug!("required file missing: {filename}");
}
}
if any_missing {
return Err(Error::CacheCorruption("required files missing in cache"))
}
Ok(())
}

/// Try to load the directory from flat files.
///
/// This is strongly inspired by the add_from_cache() methods from the various states in
/// DirMgr, combined and simplified to directly use the data from the loaded files.
pub async fn load_directory(&self) -> Result<bool> {
let config = self.config.get();
let cache_path = &config.cache_path;
self.check_directory(cache_path)?;

// Consensus
let unvalidated = self.load_consensus(cache_path)?;
Expand Down

0 comments on commit 2fd4a9f

Please sign in to comment.