Skip to content

Commit

Permalink
pkgs cache
Browse files Browse the repository at this point in the history
  • Loading branch information
samuelemusiani committed Sep 13, 2024
1 parent 0b3b535 commit cf35333
Show file tree
Hide file tree
Showing 3 changed files with 212 additions and 5 deletions.
153 changes: 153 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ version = "0.0.1"
edition = "2021"

[dependencies]
chrono = "0.4.38"
clap = { version = "4.5.17", features = ["derive"] }
colored = "2.1.0"
minreq = {version = "2.12.0", features = ["https"] }
Expand Down
63 changes: 58 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,25 +1,78 @@
use std::fmt;
use std::io::Read;
use chrono::{DateTime, Utc};
use std::{
fmt, fs,
io::{Read, Write},
path::Path,
str::FromStr,
u16,
};

use minreq;
use xz::read::XzDecoder;

const ARCH_URL: &str = "https://archive.archlinux.org";
const INDEX_PATH: &str = "/packages/.all/index.0.xz";
const PKG_POSTFIX: &str = ".pkg.tar.zst";
const CACHE_DURATION: i64 = 5; // 5 minutes

pub type Result<T> = core::result::Result<T, Box<dyn std::error::Error>>;

pub fn get_pkg_index() -> Result<String> {
let pkg_list = read_cache();
match pkg_list {
Ok(s) => match s {
Some(s) => return Ok(s),
None => eprintln!("Cache is empty"),
},
Err(e) => eprintln!("Error: Reading cache: {e}"),
}

let res = minreq::get(ARCH_URL.to_owned() + INDEX_PATH).send()?;
let cursor = std::io::Cursor::new(res.into_bytes());

// The 34M value is derived by printing the capacity of the string when full
let mut pkg_list = String::with_capacity(34_000_000);
XzDecoder::new(cursor).read_to_string(&mut pkg_list)?;

write_cache(&pkg_list)?;

Ok(pkg_list)
}

fn read_cache() -> Result<Option<String>> {
let p = Path::new("/tmp/.proll");

if !p.exists() {
return Ok(None);
}

let last_write = fs::read_to_string(p.join("date"))?;
let last_write: DateTime<Utc> = DateTime::from_str(&last_write)?;
let minutes = (Utc::now() - last_write).num_minutes();
if minutes > CACHE_DURATION {
return Ok(None);
}

let pkgs = fs::read_to_string(p.join("pkgs"))?;
Ok(Some(pkgs))
}

fn write_cache(pkgs: &str) -> Result<()> {
let p = Path::new("/tmp/.proll");
if !p.exists() {
fs::create_dir(p)?;
};

let mut pkgs_file = fs::File::create(p.join("pkgs"))?;
pkgs_file.write_all(pkgs.as_bytes())?;

let last_write = Utc::now();
let mut date_file = fs::File::create(p.join("date"))?;
date_file.write_all(&last_write.to_string().as_bytes())?;

Ok(())
}

#[derive(Debug)]
pub enum Arch {
X86_64,
Expand All @@ -39,7 +92,7 @@ impl fmt::Display for Arch {
pub struct Package {
name: String,
version: String,
build_version: u8,
build_version: u16,
arch: Arch,
}

Expand Down Expand Up @@ -67,7 +120,7 @@ impl Package {
.ok_or("Cannot find second '-' in package str")?;

let (package, build_version) = package.split_at(index);
let build_version: u8 = (build_version.strip_prefix('-'))
let build_version: u16 = (build_version.strip_prefix('-'))
.ok_or("Parse error. Cannot strip - from build_version")?
.parse()?;

Expand Down Expand Up @@ -114,7 +167,7 @@ impl Package {
&self.version
}

pub fn build_version(&self) -> u8 {
pub fn build_version(&self) -> u16 {
self.build_version
}

Expand Down

0 comments on commit cf35333

Please sign in to comment.