From bcd15ac50ee2ad46b330bb8fcfe6bf5fa5f7b51c Mon Sep 17 00:00:00 2001 From: John Turner Date: Fri, 12 Dec 2025 01:59:57 +0000 Subject: [PATCH] read repo_name when opening repos --- fuzz/atom/parser/gencorpus.rs | 2 +- fuzz/atom/vercmp/gencorpus.rs | 2 +- src/lib.rs | 3 ++- src/repo/mod.rs | 20 +++++++++++++++++--- src/repo/profile/mod.rs | 3 ++- tests/profile/read_all_profiles.rs | 2 +- tests/repo/repo.rs | 2 +- 7 files changed, 25 insertions(+), 9 deletions(-) diff --git a/fuzz/atom/parser/gencorpus.rs b/fuzz/atom/parser/gencorpus.rs index 299835a..344e713 100644 --- a/fuzz/atom/parser/gencorpus.rs +++ b/fuzz/atom/parser/gencorpus.rs @@ -20,7 +20,7 @@ fn main() -> Result<(), Box> { fs::create_dir_all(&corpus_dir)?; - let repo = Repo::new("/var/db/repos/gentoo"); + let repo = Repo::new("/var/db/repos/gentoo").expect("failed to open repo"); let mut atoms = Vec::new(); for category in repo.categories()? { diff --git a/fuzz/atom/vercmp/gencorpus.rs b/fuzz/atom/vercmp/gencorpus.rs index 6f96f4f..e88ada5 100644 --- a/fuzz/atom/vercmp/gencorpus.rs +++ b/fuzz/atom/vercmp/gencorpus.rs @@ -17,7 +17,7 @@ fn main() -> Result<(), Box> { fs::create_dir_all(&corpus_dir)?; - let repo = Repo::new("/var/db/repos/gentoo"); + let repo = Repo::new("/var/db/repos/gentoo").expect("failed to open repo"); let mut versions = Vec::new(); for category in repo.categories()? { diff --git a/src/lib.rs b/src/lib.rs index b7b01a7..66cf2b9 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -69,7 +69,8 @@ pub mod atom; /// ``` /// use gentoo_utils::repo::Repo; /// -/// let repo = Repo::new("/var/db/repos/gentoo"); +/// let repo = Repo::new("/var/db/repos/gentoo") +/// .expect("failed to open repo"); /// /// for result in repo.categories().expect("failed to read categories") { /// let category = result.expect("failed to read category"); diff --git a/src/repo/mod.rs b/src/repo/mod.rs index 750cd15..165f96a 100644 --- a/src/repo/mod.rs +++ b/src/repo/mod.rs @@ -23,6 +23,8 @@ pub mod profile; #[derive(Debug, thiserror::Error)] pub enum Error { + #[error("invalid repo: {0}")] + Invalid(String), #[error("io error: {0}")] Io(PathBuf, io::Error), #[error("error while reading directory: {0:?}: {1}")] @@ -39,6 +41,8 @@ pub enum Error { pub struct Repo { #[get(kind = "deref")] path: PathBuf, + #[get(kind = "deref")] + name: String, } #[derive(Debug, Clone, Get)] @@ -55,10 +59,20 @@ pub struct Categories(PathBuf, fs::ReadDir); pub struct Ebuilds(PathBuf, fs::ReadDir); impl Repo { - pub fn new>(path: P) -> Self { - Self { + pub fn new>(path: P) -> Result { + let name_path = path.as_ref().join("profiles/repo_name"); + let name = match fs::read_to_string(&name_path) { + Ok(repo_name) => repo_name, + Err(e) if matches!(e.kind(), io::ErrorKind::NotFound) => { + return Err(Error::Invalid("missing repo_name".to_string())); + } + Err(e) => return Err(Error::Io(name_path, e)), + }; + + Ok(Self { path: path.as_ref().to_path_buf(), - } + name, + }) } pub fn categories(&self) -> Result { diff --git a/src/repo/profile/mod.rs b/src/repo/profile/mod.rs index 8bdb724..28c6b06 100644 --- a/src/repo/profile/mod.rs +++ b/src/repo/profile/mod.rs @@ -2,7 +2,8 @@ //! ```rust //! use gentoo_utils::repo::Repo; //! -//! let repo = Repo::new("/var/db/repos/gentoo"); +//! let repo = Repo::new("/var/db/repos/gentoo") +//! .expect("failed to open repo"); //! let profile = repo.evaluate_profile("default/linux/23.0") //! .expect("failed to evaluate profile"); //! diff --git a/tests/profile/read_all_profiles.rs b/tests/profile/read_all_profiles.rs index 6295ec8..4a214a0 100644 --- a/tests/profile/read_all_profiles.rs +++ b/tests/profile/read_all_profiles.rs @@ -474,7 +474,7 @@ fn main() { "prefix/sunos/solaris/5.11/x64", ]; - let repo = Repo::new("/var/db/repos/gentoo"); + let repo = Repo::new("/var/db/repos/gentoo").expect("failed to open repo"); for profile in profiles { repo.evaluate_profile(profile) diff --git a/tests/repo/repo.rs b/tests/repo/repo.rs index 20b6980..cf1ffa2 100644 --- a/tests/repo/repo.rs +++ b/tests/repo/repo.rs @@ -3,7 +3,7 @@ use std::error::Error; use gentoo_utils::repo::Repo; fn main() -> Result<(), Box> { - let repo = Repo::new("/var/db/repos/gentoo"); + let repo = Repo::new("/var/db/repos/gentoo").unwrap(); for result in repo.categories()? { let cat = result?;