diff --git a/libarchive/src/write/file.rs b/libarchive/src/write/file.rs index ba84197..c0559e1 100644 --- a/libarchive/src/write/file.rs +++ b/libarchive/src/write/file.rs @@ -68,19 +68,24 @@ impl FileWriter { } } - pub fn close(&mut self) { + pub fn close(&mut self) -> crate::Result<()> { unsafe { - ffi::archive_write_close(self.handle_mut()); - } + match ffi::archive_write_close(self.handle_mut()) { + ffi::ARCHIVE_OK => { + self.closed = true; - self.closed = true; + Ok(()) + } + _ => Err(ArchiveError::from(self as &dyn Handle)), + } + } } } impl Drop for FileWriter { fn drop(&mut self) { if !self.closed { - self.close(); + let _ = self.close(); } unsafe { diff --git a/server/src/main.rs b/server/src/main.rs index 8924272..f8a959d 100644 --- a/server/src/main.rs +++ b/server/src/main.rs @@ -32,7 +32,7 @@ async fn main() { repo_dir: "./data/repos".into(), pkg_dir: "./data/pkgs".into(), }; - let repo_manager = RepoGroupManager::new("./data/repos", "./data/pkgs"); + let repo_manager = RepoGroupManager::new("./data/repos", "./data/pkgs", "x86_64"); let global = Global { config, diff --git a/server/src/repo/manager.rs b/server/src/repo/manager.rs index ac64877..7b3635d 100644 --- a/server/src/repo/manager.rs +++ b/server/src/repo/manager.rs @@ -1,6 +1,7 @@ use super::package::Package; use libarchive::write::{Builder, WriteEntry}; use libarchive::{Entry, WriteFilter, WriteFormat}; +use std::collections::HashSet; use std::fs; use std::io; use std::path::{Path, PathBuf}; @@ -9,13 +10,19 @@ use std::path::{Path, PathBuf}; pub struct RepoGroupManager { repo_dir: PathBuf, pkg_dir: PathBuf, + default_arch: String, } impl RepoGroupManager { - pub fn new, P2: AsRef>(repo_dir: P1, pkg_dir: P2) -> Self { + pub fn new, P2: AsRef>( + repo_dir: P1, + pkg_dir: P2, + default_arch: &str, + ) -> Self { RepoGroupManager { repo_dir: repo_dir.as_ref().to_path_buf(), pkg_dir: pkg_dir.as_ref().to_path_buf(), + default_arch: String::from(default_arch), } } @@ -61,7 +68,7 @@ impl RepoGroupManager { } } - Ok(()) + ar_db.close().and(ar_files.close()).map_err(Into::into) } pub fn remove_pkg_from_arch_repo( @@ -139,14 +146,31 @@ impl RepoGroupManager { } /// Add a package to the given repo, returning to what architectures the package was added. - pub fn add_pkg_in_repo(&mut self, repo: &str, pkg: &Package) -> io::Result> { + pub fn add_pkg_in_repo(&mut self, repo: &str, pkg: &Package) -> io::Result> { + let mut arch_repos: HashSet = HashSet::new(); + if pkg.info.arch != "any" { self.add_pkg_in_arch_repo(repo, &pkg.info.arch, pkg)?; + arch_repos.insert(pkg.info.arch.clone()); + } + // Packages of arch "any" are added to every existing arch + else { + arch_repos.insert(self.default_arch.clone()); - return Ok(vec![String::from(&pkg.info.arch)]); + let repo_dir = self.repo_dir.join(repo); + + if repo_dir.exists() { + for entry in repo_dir.read_dir()? { + arch_repos.insert(entry?.file_name().to_string_lossy().to_string()); + } + } + + for arch in arch_repos.iter() { + self.add_pkg_in_arch_repo(repo, &arch, pkg)?; + } } - todo!() + Ok(arch_repos) } pub fn add_pkg_in_arch_repo(