init
This commit is contained in:
commit
221b2a82e7
7 changed files with 3817 additions and 0 deletions
154
src/pkg.rs
Normal file
154
src/pkg.rs
Normal file
|
@ -0,0 +1,154 @@
|
|||
use std::path::{Path, PathBuf};
|
||||
|
||||
pub struct Repository {
|
||||
pub name: String,
|
||||
}
|
||||
|
||||
impl Repository {
|
||||
pub fn new(name: &str) -> Option<Self> {
|
||||
if PathBuf::from("./data").join(name).exists() {
|
||||
Some(Repository {
|
||||
name: name.to_string(),
|
||||
})
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
pub fn base_path(&self, arch: &str) -> PathBuf {
|
||||
PathBuf::from("./data").join(&self.name).join(arch)
|
||||
}
|
||||
|
||||
pub fn db_content(&self, arch: &str) -> Option<Vec<u8>> {
|
||||
std::fs::read(
|
||||
self.base_path(arch)
|
||||
.join(format!("{}.db.tar.gz", self.name)),
|
||||
)
|
||||
.ok()
|
||||
}
|
||||
|
||||
pub fn sig_content(&self, arch: &str) -> Option<Vec<u8>> {
|
||||
std::fs::read(
|
||||
self.base_path(arch)
|
||||
.join(format!("{}.db.tar.gz.sig", self.name)),
|
||||
)
|
||||
.ok()
|
||||
}
|
||||
|
||||
pub fn get_pkg(&self, arch: &str, pkg_name: &str) -> Option<Package> {
|
||||
let pkg = if pkg_name.ends_with(".pkg.tar.zst") {
|
||||
Package::new(&self.name, arch, pkg_name.trim_end_matches(".pkg.tar.zst"))
|
||||
} else if pkg_name.ends_with(".pkg.tar.zst.sig") {
|
||||
Package::new(
|
||||
&self.name,
|
||||
arch,
|
||||
pkg_name.trim_end_matches(".pkg.tar.zst.sig"),
|
||||
)
|
||||
} else {
|
||||
Package::new(&self.name, arch, pkg_name)
|
||||
};
|
||||
|
||||
if pkg.exists() {
|
||||
return Some(pkg);
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct Package {
|
||||
repo: String,
|
||||
arch: String,
|
||||
name: String,
|
||||
version: Option<String>,
|
||||
}
|
||||
|
||||
impl Package {
|
||||
pub fn new(repo: &str, arch: &str, pkg_name: &str) -> Self {
|
||||
Package {
|
||||
repo: repo.to_string(),
|
||||
arch: arch.to_string(),
|
||||
name: pkg_name.to_string(),
|
||||
version: None,
|
||||
}
|
||||
}
|
||||
|
||||
fn base_path(&self) -> PathBuf {
|
||||
Path::new("./data").join(&self.repo).join(&self.arch)
|
||||
}
|
||||
|
||||
pub fn switch_arch(&self, arch: &str) -> Self {
|
||||
let mut new = self.clone();
|
||||
new.arch = arch.to_string();
|
||||
new
|
||||
}
|
||||
|
||||
pub fn exists(&self) -> bool {
|
||||
let pkg_file = self.base_path().join(self.file_name());
|
||||
pkg_file.exists()
|
||||
}
|
||||
|
||||
pub fn get_version(&self, version: &str) -> Self {
|
||||
let mut new_pkg = self.clone();
|
||||
new_pkg.version = Some(version.to_string());
|
||||
new_pkg
|
||||
}
|
||||
|
||||
pub fn is_signed(&self) -> bool {
|
||||
let signed_file = self.base_path().join(format!("{}.sig", self.file_name()));
|
||||
signed_file.exists()
|
||||
}
|
||||
|
||||
pub fn file_name(&self) -> String {
|
||||
format!(
|
||||
"{}-{}-{}-{}.pkg.tar.zst",
|
||||
self.name,
|
||||
if let Some(ver) = &self.version {
|
||||
ver.to_string()
|
||||
} else {
|
||||
let versions = self.versions();
|
||||
versions.first().unwrap().clone()
|
||||
},
|
||||
1,
|
||||
self.arch,
|
||||
)
|
||||
}
|
||||
|
||||
pub fn versions(&self) -> Vec<String> {
|
||||
let dir_path = self.base_path();
|
||||
let mut versions = vec![];
|
||||
|
||||
if let Ok(entries) = std::fs::read_dir(dir_path) {
|
||||
for entry in entries.filter_map(Result::ok) {
|
||||
let file_name = entry.file_name().into_string().unwrap_or_default();
|
||||
if file_name.starts_with(&self.name) && file_name.ends_with(".pkg.tar.zst") {
|
||||
// Extract version (assuming the filename is "<pkg_name>-<version>-<relation>-<arch>.pkg.tar.zst")
|
||||
if let Some(version) = file_name.split('-').nth(1) {
|
||||
versions.push(version.to_string());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Sort versions in descending order (most recent version first)
|
||||
versions.sort_by(|a, b| b.cmp(a));
|
||||
versions
|
||||
}
|
||||
|
||||
pub fn pkg_content(&self) -> Option<Vec<u8>> {
|
||||
if self.exists() {
|
||||
return std::fs::read(self.base_path().join(self.file_name())).ok();
|
||||
}
|
||||
|
||||
None
|
||||
}
|
||||
|
||||
pub fn sig_content(&self) -> Option<Vec<u8>> {
|
||||
if self.exists() {
|
||||
return std::fs::read(self.base_path().join(format!("{}.sig", &self.file_name()))).ok();
|
||||
}
|
||||
|
||||
None
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue