From ac4f8ec4cbcbbafc0720c68b19c7ea4ebdc5b5f4 Mon Sep 17 00:00:00 2001 From: lukas-heilgenbrunner Date: Tue, 13 Aug 2024 23:05:47 +0200 Subject: [PATCH] check if files exist and not just folder --- .../src/pacman-repo-utils/src/repo_init.rs | 44 ++++++++++--------- 1 file changed, 24 insertions(+), 20 deletions(-) diff --git a/backend/src/pacman-repo-utils/src/repo_init.rs b/backend/src/pacman-repo-utils/src/repo_init.rs index 905754e..cab8811 100644 --- a/backend/src/pacman-repo-utils/src/repo_init.rs +++ b/backend/src/pacman-repo-utils/src/repo_init.rs @@ -8,28 +8,32 @@ use std::os::unix::fs::symlink; use std::path::PathBuf; pub fn init_repo_impl(path: &PathBuf, name: &str) -> anyhow::Result<()> { + let db_file = path.join(format!("{}.db.tar.gz", name)); + let files_file = path.join(format!("{}.files.tar.gz", name)); + // create repo folder - if fs::metadata(path).is_err() { - info!("Initializing empty pacman Repo archive"); - fs::create_dir(path)?; + if fs::metadata(&db_file).is_ok() || fs::metadata(&files_file).is_ok() { + info!("Pacman repo archive already exists"); + return Ok(()); + } - let db_file = path.join(format!("{}.db.tar.gz", name)); - let tar_gz = File::create(&db_file)?; - let enc = GzEncoder::new(tar_gz, Compression::default()); - let mut tar = tar::Builder::new(enc); - tar.finish() - .map_err(|_| anyhow!("failed to create repo archive"))?; - symlink(&db_file, path.join(format!("{}.db", name))) - .map_err(|_| anyhow!("failed to create repo symlink"))?; + info!("Initializing empty pacman Repo archive"); + fs::create_dir(path)?; - let files_file = path.join(format!("{}.files.tar.gz", name)); - let tar_gz = File::create(&files_file)?; - let enc = GzEncoder::new(tar_gz, Compression::default()); - let mut tar = tar::Builder::new(enc); - tar.finish() - .map_err(|_| anyhow!("failed to create repo archive"))?; - symlink(&files_file, path.join(format!("{}.files", name))) - .map_err(|_| anyhow!("failed to create repo symlink"))?; - } + let tar_gz = File::create(&db_file)?; + let enc = GzEncoder::new(tar_gz, Compression::default()); + let mut tar = tar::Builder::new(enc); + tar.finish() + .map_err(|_| anyhow!("failed to create repo archive"))?; + symlink(&db_file, path.join(format!("{}.db", name))) + .map_err(|_| anyhow!("failed to create repo symlink"))?; + + let tar_gz = File::create(&files_file)?; + let enc = GzEncoder::new(tar_gz, Compression::default()); + let mut tar = tar::Builder::new(enc); + tar.finish() + .map_err(|_| anyhow!("failed to create repo archive"))?; + symlink(&files_file, path.join(format!("{}.files", name))) + .map_err(|_| anyhow!("failed to create repo symlink"))?; Ok(()) }