summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJohn Turner <jturner.usa@gmail.com>2025-12-12 01:59:57 +0000
committerJohn Turner <jturner.usa@gmail.com>2025-12-14 01:37:49 +0000
commita09241dc8940d0a9b01a698d3e71d08f71fadea4 (patch)
tree99e2a6e378c03fe133366faa101352cda1df0248
parent682cfdac9f88b614f502c6352d72a051b6de9440 (diff)
downloadgentoo-utils-a09241dc8940d0a9b01a698d3e71d08f71fadea4.tar.gz
read repo_name when opening repos
-rw-r--r--fuzz/atom/parser/gencorpus.rs2
-rw-r--r--fuzz/atom/vercmp/gencorpus.rs2
-rw-r--r--src/lib.rs3
-rw-r--r--src/repo/mod.rs20
-rw-r--r--src/repo/profile/mod.rs3
-rw-r--r--tests/profile/read_all_profiles.rs2
-rw-r--r--tests/repo/repo.rs2
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<dyn Error>> {
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<dyn Error>> {
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<P: AsRef<Path>>(path: P) -> Self {
- Self {
+ pub fn new<P: AsRef<Path>>(path: P) -> Result<Self, Error> {
+ 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<Categories, Error> {
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<dyn Error>> {
- 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?;