Make dumping an empty cache succeed.

This commit is contained in:
Martin Hoffmann
2024-01-10 10:56:32 +01:00
parent 617655190f
commit 4400d2cd45
5 changed files with 60 additions and 9 deletions
+1
View File
@@ -215,6 +215,7 @@ impl Collector {
repos: DumpRegistry,
states: HashMap<uri::Https, RepositoryState>,
) -> Result<(), Failed> {
fatal::create_dir_all(repos.base_dir())?;
let path = repos.base_dir().join("repositories.json");
if let Err(err) = fs::write(
&path,
+1 -1
View File
@@ -1119,7 +1119,7 @@ impl Config {
///
/// Uses default values for everything except for the cache directory
/// which needs to be provided.
fn default_with_paths(cache_dir: PathBuf) -> Self {
pub fn default_with_paths(cache_dir: PathBuf) -> Self {
Config {
cache_dir,
no_rir_tals: false,
+19
View File
@@ -2020,3 +2020,22 @@ pub trait ProcessPubPoint: Sized + Send + Sync {
}
}
//============ Tests =========================================================
#[cfg(test)]
mod test {
use super::*;
#[test]
fn dump_empty_cache() {
let _ = crate::process::Process::init(); // May be inited already.
let src = tempfile::tempdir().unwrap();
let target = tempfile::tempdir().unwrap();
let target = target.path().join("dump");
let config = Config::default_with_paths(src.path().into());
let engine = Engine::new(&config, true).unwrap();
engine.dump(&target).unwrap();
}
}
+7 -2
View File
@@ -174,7 +174,7 @@ impl Store {
let target = target_base.join("ta");
debug!("Dumping trust anchor certificates to {}", target.display());
fatal::remove_dir_all(&target)?;
fatal::copy_dir_all(&source, &target)?;
fatal::try_copy_dir_all(&source, &target)?;
debug!("Trust anchor certificate dump complete.");
Ok(())
}
@@ -188,7 +188,11 @@ impl Store {
path: &Path,
repos: &mut DumpRegistry,
) -> Result<(), Failed> {
for entry in fatal::read_dir(path)? {
let dir = match fatal::try_read_dir(path)? {
Some(dir) => dir,
None => return Ok(())
};
for entry in dir {
let entry = entry?;
if entry.is_dir() {
self.dump_tree(entry.path(), repos)?;
@@ -295,6 +299,7 @@ impl Store {
&self,
repos: DumpRegistry,
) -> Result<(), Failed> {
fatal::create_dir_all(repos.base_dir())?;
let path = repos.base_dir().join("repositories.json");
fatal::write_file(
&path,
+32 -6
View File
@@ -130,6 +130,28 @@ pub fn read_dir(path: &Path) -> Result<ReadDir, Failed> {
}
//------------ try_read_dir --------------------------------------------------
/// Returns an iterator over a directory if it exists.
pub fn try_read_dir(path: &Path) -> Result<Option<ReadDir>, Failed> {
match fs::read_dir(path) {
Ok(iter) => Ok(Some(ReadDir { path, iter })),
Err(err) => {
if err.kind() == io::ErrorKind::NotFound {
Ok(None)
}
else {
error!(
"Fatal: failed to open directory {}: {}",
path.display(), IoErrorDisplay(err)
);
Err(Failed)
}
}
}
}
//------------ read_existing_dir ---------------------------------------------
/// Returns an iterator over an existing directory.
@@ -316,15 +338,19 @@ pub fn write_file(path: &Path, contents: &[u8]) -> Result<(), Failed> {
}
//------------ copy_dir_all --------------------------------------------------
//------------ try_copy_dir_all ----------------------------------------------
/// Copies the content of a directory.
///
/// Creates the target directory with `create_dir_all`. Errors out if
/// anything goes wrong.
pub fn copy_dir_all(source: &Path, target: &Path) -> Result<(), Failed> {
/// Creates the target directory with `create_dir_all`. Errors out if
/// anything goes wrong. The source path not existing is not an error.
pub fn try_copy_dir_all(source: &Path, target: &Path) -> Result<(), Failed> {
let source_dir = match try_read_dir(source)? {
Some(entry) => entry,
None => return Ok(()),
};
create_dir_all(target)?;
for entry in read_dir(source)? {
for entry in source_dir {
let entry = entry?;
if entry.is_file() {
if let Err(err) = fs::copy(
@@ -338,7 +364,7 @@ pub fn copy_dir_all(source: &Path, target: &Path) -> Result<(), Failed> {
}
}
else if entry.is_dir() {
copy_dir_all(
try_copy_dir_all(
entry.path(),
&target.join(entry.file_name())
)?;