Skip to content

Commit bf837fb

Browse files
committed
use altered libalpm-rs interface with separated config parsing
1 parent 1f99619 commit bf837fb

File tree

2 files changed

+55
-47
lines changed

2 files changed

+55
-47
lines changed

client/src/main.rs

Lines changed: 31 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ use std::{
1313
io::{Cursor, ErrorKind},
1414
path::{Path, PathBuf},
1515
process::{Command, Stdio},
16-
str::FromStr,
1716
sync::Arc,
1817
time::Duration,
1918
};
@@ -56,6 +55,7 @@ enum Commands {
5655
Sync {
5756
#[arg(default_value = "http://bogen.moeh.re/")]
5857
server: Url,
58+
target_dir: Option<PathBuf>,
5959
},
6060
/// Download the newest packages to the provided delta_cache path
6161
///
@@ -80,8 +80,7 @@ enum Commands {
8080
Download {
8181
#[arg(default_value = "http://bogen.moeh.re/")]
8282
server: Url,
83-
#[arg(default_value = "/var/cache/pacman/pkg/")]
84-
delta_cache: PathBuf,
83+
delta_cache: Option<PathBuf>,
8584
/// Disable fuzzy search if an exact package can not be found.
8685
/// This might find renames like -qt5 to -qt6
8786
#[arg(long)]
@@ -148,7 +147,7 @@ fn main() {
148147
.expect("initializing logger failed");
149148
log::set_max_level(level);
150149

151-
let global = GlobalState::new(multi.clone(), 5, 1);
150+
let mut global = GlobalState::new(multi.clone(), 5, 1);
152151

153152
match args.command {
154153
#[cfg(feature = "diff")]
@@ -178,15 +177,21 @@ fn main() {
178177
only_delta,
179178
} => {
180179
renice();
180+
if let Some(p) = delta_cache {
181+
global.pacman_config.cache_dir = p;
182+
}
181183
let db = libalpm_rs::db::DBLock::new().unwrap();
182-
std::fs::create_dir_all(&delta_cache).unwrap();
184+
std::fs::create_dir_all(&global.pacman_config.cache_dir).unwrap();
183185
mkruntime()
184-
.block_on(do_upgrade(global, server, vec![], delta_cache, !no_fuz, only_delta))
186+
.block_on(do_upgrade(global, server, vec![], !no_fuz, only_delta))
185187
.unwrap();
186188
std::mem::drop(db)
187189
}
188-
Commands::Sync { server } => {
190+
Commands::Sync { server, target_dir } => {
189191
renice();
192+
if let Some(t) = target_dir {
193+
global.pacman_config.db_path = t;
194+
}
190195
let db = libalpm_rs::db::DBLock::new().unwrap();
191196
mkruntime().block_on(sync(global, server)).unwrap();
192197
std::mem::drop(db)
@@ -254,16 +259,14 @@ fn full_upgrade(
254259
info!("syncing databases");
255260
runtime.block_on(sync(global.clone(), server.clone())).unwrap();
256261
}
257-
let cachepath = PathBuf::from_str("/var/cache/pacman/pkg").unwrap();
258-
let (deltasize, newsize, comptime) = match runtime
259-
.block_on(async move { do_upgrade(global, server, blacklist, cachepath, !no_fuz, only_delta).await })
260-
{
261-
Ok((d, n, c)) => (d, n, c),
262-
Err(e) => {
263-
error!("{e}");
264-
panic!("{e}")
265-
}
266-
};
262+
let (deltasize, newsize, comptime) =
263+
match runtime.block_on(async move { do_upgrade(global, server, blacklist, !no_fuz, only_delta).await }) {
264+
Ok((d, n, c)) => (d, n, c),
265+
Err(e) => {
266+
error!("{e}");
267+
panic!("{e}")
268+
}
269+
};
267270
std::mem::drop(db);
268271
info!("running pacman -Su to install updates");
269272
let exit = Command::new("pacman")
@@ -302,7 +305,6 @@ async fn do_upgrade(
302305
global: GlobalState,
303306
server: Url,
304307
blacklist: Vec<Str>,
305-
delta_cache: PathBuf,
306308
fuz: bool,
307309
only_delta: bool,
308310
) -> anyhow::Result<(u64, u64, Option<Duration>)> {
@@ -323,10 +325,9 @@ async fn do_upgrade(
323325
oldpkg.clone(),
324326
oldfile,
325327
dec_size,
326-
delta_cache.clone(),
327328
server.clone(),
328329
);
329-
let get_sig_f = get_signature(url, global.client.clone(), delta_cache.clone(), newpkg.clone());
330+
let get_sig_f = get_signature(global.clone(), url, newpkg.clone());
330331

331332
set.spawn_local_on(
332333
async move {
@@ -346,10 +347,10 @@ async fn do_upgrade(
346347
let mut dlset = JoinSet::new();
347348
if !only_delta {
348349
for url in downloads {
349-
let boring_dl = util::do_boring_download(global.clone(), url.clone(), delta_cache.clone());
350+
let boring_dl = util::do_boring_download(global.clone(), url.clone());
350351
let name = url.path_segments().and_then(Iterator::last).context("malformed url")?;
351352
let pkg = Package::try_from(name).unwrap();
352-
let get_sig_f = get_signature(url, global.client.clone(), delta_cache.clone(), pkg);
353+
let get_sig_f = get_signature(global.clone(), url, pkg);
353354
dlset.spawn_local_on(
354355
async move {
355356
let (f, s) = tokio::join!(boring_dl, get_sig_f);
@@ -422,6 +423,7 @@ pub(crate) struct GlobalState {
422423
maxpar_dl: Arc<Semaphore>,
423424
maxpar_cpu: Arc<Semaphore>,
424425
client: Client,
426+
pacman_config: libalpm_rs::config::PacmanConfig,
425427
}
426428

427429
impl GlobalState {
@@ -445,13 +447,16 @@ impl GlobalState {
445447
total_pg.tick();
446448
total_pg.enable_steady_tick(Duration::from_millis(100));
447449

450+
let pacman_config = libalpm_rs::config::extract_relevant_config();
451+
448452
Self {
449453
multi,
450454
total_pg,
451455
maxpar_req,
452456
maxpar_dl,
453457
maxpar_cpu,
454458
client,
459+
pacman_config,
455460
}
456461
}
457462

@@ -471,11 +476,10 @@ async fn get_delta(
471476
oldpkg: Package,
472477
oldfile: Mmap,
473478
mut dec_size: u64,
474-
delta_cache: PathBuf,
475479
server: Url,
476480
) -> Result<(u64, u64, Option<Duration>), anyhow::Error> {
477481
global.total_pg.inc_length(dec_size);
478-
let mut file_name = delta_cache.clone();
482+
let mut file_name = global.pacman_config.cache_dir.clone();
479483
file_name.push(newpkg.to_string());
480484
let delta = common::Delta::try_from((oldpkg.clone(), newpkg.clone()))?;
481485
let deltafile_name = file_name.with_file_name(format!("{delta}.delta"));
@@ -519,8 +523,8 @@ async fn get_delta(
519523
Ok((deltasize, newsize, comptime))
520524
}
521525

522-
async fn get_signature(url: Url, client: Client, delta_cache: PathBuf, newpkg: Package) -> Result<(), anyhow::Error> {
523-
let mut sigfile = delta_cache.clone();
526+
async fn get_signature(global: GlobalState, url: Url, newpkg: Package) -> Result<(), anyhow::Error> {
527+
let mut sigfile = global.pacman_config.cache_dir;
524528
sigfile.push(format!("{newpkg}.sig"));
525529
let mut sigfile = match tokio::fs::OpenOptions::new()
526530
.write(true)
@@ -535,7 +539,7 @@ async fn get_signature(url: Url, client: Client, delta_cache: PathBuf, newpkg: P
535539
};
536540
let sigurl = format!("{url}.sig");
537541
debug!("getting signature from {}", url);
538-
let mut res = client.get(&sigurl).send().await?.error_for_status()?;
542+
let mut res = global.client.get(&sigurl).send().await?.error_for_status()?;
539543
while let Some(chunk) = res.chunk().await? {
540544
sigfile.write_all(&chunk).await?
541545
}

client/src/util.rs

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::{collections::HashMap, io::Read, path::PathBuf};
1+
use std::{collections::HashMap, io::Read};
22

33
use anyhow::{Context, bail};
44
use bytesize::ByteSize;
@@ -17,8 +17,6 @@ use tokio::{
1717

1818
type Str = Box<str>;
1919

20-
static PACMAN_CACHE: &str = "/var/cache/pacman/pkg/";
21-
2220
pub(crate) fn progress_style() -> ProgressStyle {
2321
ProgressStyle::with_template("{prefix} {msg} [{wide_bar}] {bytes}/{total_bytes} {binary_bytes_per_sec} {eta}")
2422
.unwrap()
@@ -42,35 +40,33 @@ pub(crate) async fn do_delta_download<W: AsyncRead + AsyncWrite + AsyncSeek>(
4240
.map(|()| pg)
4341
}
4442

45-
pub(crate) async fn do_boring_download(
46-
global: crate::GlobalState,
47-
url: Url,
48-
mut target_dir: PathBuf,
49-
) -> Result<u64, common::DLError> {
43+
pub(crate) async fn do_boring_download(global: crate::GlobalState, url: Url) -> Result<u64, common::DLError> {
5044
let name = url.path_segments().and_then(Iterator::last).expect("malformed url");
5145
let pkg = Package::try_from(name).expect("invalid file name");
5246
let pg = ProgressBar::hidden()
5347
.with_message(format!("{}-{}", pkg.get_name(), pkg.get_version()))
5448
.with_style(progress_style());
5549
let pg = global.multi.add(pg);
5650

57-
target_dir.push(name);
51+
let limits = global.to_limits();
52+
53+
let target_dir = global.pacman_config.cache_dir.join(name);
5854
let target = tokio::fs::File::create(target_dir).await?;
5955

6056
pin!(target);
61-
common::dl_body(global.to_limits(), global.client, pg.clone(), url, &mut target).await?;
57+
common::dl_body(limits, global.client, pg.clone(), url, &mut target).await?;
6258
let size = target.seek(std::io::SeekFrom::End(0)).await?;
6359
Ok(size)
6460
}
6561

6662
type DeltaUpgradeCandidate = (Url, Delta, Mmap, u64);
6763

6864
pub(crate) fn find_deltaupgrade_candidates(
69-
_global: &crate::GlobalState,
65+
global: &crate::GlobalState,
7066
blacklist: &[Str],
7167
_fuz: bool,
7268
) -> Result<(Vec<DeltaUpgradeCandidate>, Vec<Url>), anyhow::Error> {
73-
let upgrades = libalpm_rs::upgrade_urls(&["core", "extra", "multilib"]);
69+
let upgrades = libalpm_rs::upgrade_urls(&global.pacman_config, &["core", "extra", "multilib"]);
7470

7571
let mut direct_downloads = Vec::new();
7672
let mut deltas = Vec::new();
@@ -89,8 +85,11 @@ pub(crate) fn find_deltaupgrade_candidates(
8985
}
9086
let old_version = old.version.r(&i);
9187
let old_arch = old.arch.as_str();
92-
let cache_path = format!("{PACMAN_CACHE}/{old_name}-{old_version}-{old_arch}.pkg.tar.zst");
93-
debug!("evaluating {cache_path}");
88+
let cache_path = global
89+
.pacman_config
90+
.cache_dir
91+
.join(format!("{old_name}-{old_version}-{old_arch}.pkg.tar.zst"));
92+
debug!("evaluating {cache_path:?}");
9493
let cached = std::fs::File::open(cache_path);
9594
match cached {
9695
Ok(f) => {
@@ -137,7 +136,9 @@ pub(crate) fn find_deltaupgrade_candidates(
137136
pub async fn sync_db(global: crate::GlobalState, server: Url, name: Str) -> anyhow::Result<()> {
138137
//TODO use the same logic as the main delta downloads, including retries and progress bars
139138
info!("syncing {}", name);
140-
let max = common::find_latest_db(&name, "/var/lib/pacman/sync/")?;
139+
let mut syncpath = global.pacman_config.db_path.clone();
140+
syncpath.push("sync");
141+
let max = common::find_latest_db(&name, syncpath)?;
141142
if let Some(old_ts) = max {
142143
debug!("upgrading {name} from {old_ts}");
143144
let url = server.join(&format!("{name}/{old_ts}"))?;
@@ -212,14 +213,15 @@ pub async fn sync_db(global: crate::GlobalState, server: Url, name: Str) -> anyh
212213
let (_, new_ts) = patchname.rsplit_once('-').context("malformed http filename")?;
213214
let new_ts: u64 = new_ts.parse()?;
214215

215-
let new_p = format!("/var/lib/pacman/sync/{name}-{new_ts}");
216+
let new_p = global.pacman_config.db_path.join(format!("{name}-{new_ts}"));
216217
let mut new = tokio::fs::File::create(&new_p).await?;
217218
while let Some(chunk) = response.chunk().await? {
218219
new.write_all(&chunk).await?;
219220
}
220221

221-
let db_p = format!("/var/lib/pacman/sync/{name}.db");
222-
trace!("linking {new_p} to {db_p}");
222+
let db_p = global.pacman_config.db_path.join(format!("{name}.db"));
223+
224+
trace!("linking {new_p:?} to {db_p:?}");
223225
let res = std::fs::remove_file(&db_p);
224226
if let Err(e) = res {
225227
// Not found is fine, just means the db does not exist yet
@@ -238,7 +240,8 @@ pub(crate) fn calc_stats(count: usize) -> std::io::Result<()> {
238240
let mut deltas: HashMap<(Str, Str, Str), u64> = HashMap::new();
239241
let mut pkgs: HashMap<(Str, Str, Str), u64> = HashMap::new();
240242
let mut pairs: HashMap<Str, (u64, u64, u64)> = HashMap::new();
241-
for line in std::fs::read_dir(PACMAN_CACHE)? {
243+
let config = libalpm_rs::config::extract_relevant_config();
244+
for line in std::fs::read_dir(config.cache_dir)? {
242245
let line = line?;
243246
if !line.file_type()?.is_file() {
244247
continue;
@@ -393,7 +396,8 @@ fn test_dec_sizes() {
393396
use std::io::Seek;
394397
use std::os::unix::ffi::OsStrExt;
395398

396-
let dirents: Vec<_> = std::fs::read_dir(PACMAN_CACHE)
399+
let config = libalpm_rs::config::extract_relevant_config();
400+
let dirents: Vec<_> = std::fs::read_dir(config.cache_dir)
397401
.unwrap()
398402
.map(Result::unwrap)
399403
.filter(|line| line.file_type().unwrap().is_file() && line.file_name().as_bytes().ends_with(b".tar.zst"))

0 commit comments

Comments
 (0)