Skip to content

feat(oma-refresh): first debug version of mirror+file parser #371

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions oma-refresh/src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ use crate::{
ChecksumItem, InReleaseChecksum, InReleaseError, Release, file_is_compress,
split_ext_and_filename, verify_inrelease,
},
sourceslist::{OmaSourceEntry, OmaSourceEntryFrom, sources_lists},
sourceslist::{OmaSourceEntry, OmaSourceEntryFrom, scan_sources_lists},
util::DatabaseFilenameReplacer,
};

Expand Down Expand Up @@ -193,7 +193,7 @@ pub enum Event {
impl<'a> OmaRefresh<'a> {
pub async fn start(mut self, callback: impl AsyncFn(Event)) -> Result<()> {
let arch = dpkg_arch(&self.source)?;
let sourcelist = sources_lists(&self.source, &arch, &callback)
let sourcelist = scan_sources_lists(&self.source, &arch, &callback)
.await
.map_err(RefreshError::ScanSourceError)?;

Expand Down Expand Up @@ -672,6 +672,7 @@ fn collect_flat_repo_no_release(
.map(|auth| (auth.login.clone(), auth.password.clone())),
},
OmaSourceEntryFrom::Local => DownloadSourceType::Local(mirror_source.is_flat()),
_ => unreachable!(),
};

let download_url = format!("{}/Packages", dist_url);
Expand Down Expand Up @@ -719,6 +720,7 @@ fn collect_download_task(
.map(|auth| (auth.login.clone(), auth.password.clone())),
},
OmaSourceEntryFrom::Local => DownloadSourceType::Local(mirror_source.is_flat()),
_ => unreachable!(),
};

let not_compress_filename_before = if file_is_compress(&c.item.name) {
Expand Down
95 changes: 90 additions & 5 deletions oma-refresh/src/sourceslist.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use std::{fs::Permissions, os::unix::fs::PermissionsExt, path::Path};
use std::{
borrow::Cow, fs::Permissions, mem::MaybeUninit, os::unix::fs::PermissionsExt, path::Path,

Check warning on line 2 in oma-refresh/src/sourceslist.rs

View workflow job for this annotation

GitHub Actions / clippy

unused import: `mem::MaybeUninit`

warning: unused import: `mem::MaybeUninit` --> oma-refresh/src/sourceslist.rs:2:35 | 2 | borrow::Cow, fs::Permissions, mem::MaybeUninit, os::unix::fs::PermissionsExt, path::Path, | ^^^^^^^^^^^^^^^^ | = note: `#[warn(unused_imports)]` on by default
};

use ahash::HashMap;
use apt_auth_config::{AuthConfig, Authenticator};
Expand Down Expand Up @@ -33,7 +35,7 @@
from: OnceCell<OmaSourceEntryFrom>,
}

pub async fn sources_lists<'a>(
pub async fn scan_sources_lists<'a>(
sysroot: impl AsRef<Path>,
arch: &'a str,
cb: &'a impl AsyncFn(Event),
Expand Down Expand Up @@ -90,6 +92,22 @@
pub enum OmaSourceEntryFrom {
Http,
Local,
MirrorHttp,
MirrorFile,
}

#[derive(Debug)]
pub struct MirrorTransportItem {
pub url: String,

Check warning on line 101 in oma-refresh/src/sourceslist.rs

View workflow job for this annotation

GitHub Actions / clippy

fields `url`, `priority`, `archs`, and `item_type` are never read

warning: fields `url`, `priority`, `archs`, and `item_type` are never read --> oma-refresh/src/sourceslist.rs:101:9 | 100 | pub struct MirrorTransportItem { | ------------------- fields in this struct 101 | pub url: String, | ^^^ 102 | pub priority: i64, | ^^^^^^^^ 103 | pub archs: Vec<String>, | ^^^^^ 104 | pub item_type: MirrorTransportItemType, | ^^^^^^^^^ | = note: `MirrorTransportItem` has a derived impl for the trait `Debug`, but this is intentionally ignored during dead code analysis = note: `#[warn(dead_code)]` on by default
pub priority: i64,
pub archs: Vec<String>,
pub item_type: MirrorTransportItemType,
}

#[derive(Debug)]
pub enum MirrorTransportItemType {
Index,
Deb,
}

impl<'a> OmaSourceEntry<'a> {
Expand All @@ -106,12 +124,29 @@

pub fn from(&self) -> Result<&OmaSourceEntryFrom, RefreshError> {
self.from.get_or_try_init(|| {
let url = Url::parse(self.url())
.map_err(|_| RefreshError::InvalidUrl(self.url().to_string()))?;
let (transport_type, url) = self
.url()
.split_once("+")
.unwrap_or_else(|| ("", self.url()));

let url =
Url::parse(url).map_err(|_| RefreshError::InvalidUrl(self.url().to_string()))?;

let schema = if !transport_type.is_empty() {
let mut schema = "".to_string();
schema.push_str(transport_type);
schema.push_str("+");

Check warning on line 138 in oma-refresh/src/sourceslist.rs

View workflow job for this annotation

GitHub Actions / clippy

calling `push_str()` using a single-character string literal

warning: calling `push_str()` using a single-character string literal --> oma-refresh/src/sourceslist.rs:138:17 | 138 | schema.push_str("+"); | ^^^^^^^^^^^^^^^^^^^^ help: consider using `push` with a character literal: `schema.push('+')` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#single_char_add_str = note: `#[warn(clippy::single_char_add_str)]` on by default
schema.push_str(url.scheme());
Cow::Owned(schema)
} else {
url.scheme().into()
};

match url.scheme() {
match schema.as_ref() {
"file" => Ok(OmaSourceEntryFrom::Local),
"http" | "https" => Ok(OmaSourceEntryFrom::Http),
"mirror" | "mirror+http" | "mirror+https" => Ok(OmaSourceEntryFrom::MirrorHttp),
"mirror+files" => Ok(OmaSourceEntryFrom::MirrorFile),
x => Err(RefreshError::UnsupportedProtocol(x.to_string())),
}
})
Expand Down Expand Up @@ -285,6 +320,17 @@
self.fetch_local_release(replacer, index, total, download_dir, callback)
.await
}
OmaSourceEntryFrom::MirrorHttp => todo!(),
OmaSourceEntryFrom::MirrorFile => {
let path = self.dist_path().strip_prefix("mirror+file:").unwrap();
let f = fs::read_to_string(path)
.await
.map_err(|e| RefreshError::FailedToOperateDirOrFile(path.to_string(), e))?;

let items = parse_mirror_transport_file(&f);

Check warning on line 330 in oma-refresh/src/sourceslist.rs

View workflow job for this annotation

GitHub Actions / clippy

unused variable: `items`

warning: unused variable: `items` --> oma-refresh/src/sourceslist.rs:330:21 | 330 | let items = parse_mirror_transport_file(&f); | ^^^^^ help: if this is intentional, prefix it with an underscore: `_items` | = note: `#[warn(unused_variables)]` on by default

todo!()
}
}
}

Expand Down Expand Up @@ -550,6 +596,45 @@
}
}

fn parse_mirror_transport_file(f: &str) -> Vec<MirrorTransportItem> {
let mut res = vec![];

for i in f.lines() {
let mut line = i.split_ascii_whitespace();
let url = line.next().expect("File contains illegal item");
let mut priority = 0;
let mut archs = vec![];
let mut item_type = MirrorTransportItemType::Deb;

while let Some(entry) = line.next() {

Check warning on line 609 in oma-refresh/src/sourceslist.rs

View workflow job for this annotation

GitHub Actions / clippy

this loop could be written as a `for` loop

warning: this loop could be written as a `for` loop --> oma-refresh/src/sourceslist.rs:609:9 | 609 | while let Some(entry) = line.next() { | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `for entry in line` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#while_let_on_iterator = note: `#[warn(clippy::while_let_on_iterator)]` on by default
match entry.split_once(':') {
Some((k, v)) => match k {
"priority" => priority = v.parse::<i64>().expect("Failed to parse priority"),
"arch" => archs.push(v.to_string()),
"type" => {
item_type = match v {
"deb" => MirrorTransportItemType::Deb,
"index" => MirrorTransportItemType::Index,
x => panic!("Failed to parse type: {x}"),
}
}
x => panic!("Failed to parse key: {x}"),
},
None => panic!("File contains illegal item"),
}
}

res.push(MirrorTransportItem {
url: url.to_string(),
archs,
priority,
item_type,
});
}

res
}

impl<'a, 'b> MirrorSources<'a, 'b> {
pub fn from_sourcelist(
sourcelist: &'a [OmaSourceEntry<'a>],
Expand Down
Loading