Skip to content

Commit 84488e2

Browse files
committed
feat(oma-refresh): first debug version of mirror+file parser
1 parent 1472483 commit 84488e2

File tree

2 files changed

+94
-7
lines changed

2 files changed

+94
-7
lines changed

oma-refresh/src/db.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ use crate::{
5555
ChecksumItem, InReleaseChecksum, InReleaseError, Release, file_is_compress,
5656
split_ext_and_filename, verify_inrelease,
5757
},
58-
sourceslist::{OmaSourceEntry, OmaSourceEntryFrom, sources_lists},
58+
sourceslist::{OmaSourceEntry, OmaSourceEntryFrom, scan_sources_lists},
5959
util::DatabaseFilenameReplacer,
6060
};
6161

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

@@ -672,6 +672,7 @@ fn collect_flat_repo_no_release(
672672
.map(|auth| (auth.login.clone(), auth.password.clone())),
673673
},
674674
OmaSourceEntryFrom::Local => DownloadSourceType::Local(mirror_source.is_flat()),
675+
_ => unreachable!(),
675676
};
676677

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

724726
let not_compress_filename_before = if file_is_compress(&c.item.name) {

oma-refresh/src/sourceslist.rs

+90-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
use std::{fs::Permissions, os::unix::fs::PermissionsExt, path::Path};
1+
use std::{
2+
borrow::Cow, fs::Permissions, mem::MaybeUninit, os::unix::fs::PermissionsExt, path::Path,
3+
};
24

35
use ahash::HashMap;
46
use apt_auth_config::{AuthConfig, Authenticator};
@@ -33,7 +35,7 @@ pub struct OmaSourceEntry<'a> {
3335
from: OnceCell<OmaSourceEntryFrom>,
3436
}
3537

36-
pub async fn sources_lists<'a>(
38+
pub async fn scan_sources_lists<'a>(
3739
sysroot: impl AsRef<Path>,
3840
arch: &'a str,
3941
cb: &'a impl AsyncFn(Event),
@@ -90,6 +92,22 @@ pub async fn sources_lists<'a>(
9092
pub enum OmaSourceEntryFrom {
9193
Http,
9294
Local,
95+
MirrorHttp,
96+
MirrorFile,
97+
}
98+
99+
#[derive(Debug)]
100+
pub struct MirrorTransportItem {
101+
pub url: String,
102+
pub priority: i64,
103+
pub archs: Vec<String>,
104+
pub item_type: MirrorTransportItemType,
105+
}
106+
107+
#[derive(Debug)]
108+
pub enum MirrorTransportItemType {
109+
Index,
110+
Deb,
93111
}
94112

95113
impl<'a> OmaSourceEntry<'a> {
@@ -106,12 +124,29 @@ impl<'a> OmaSourceEntry<'a> {
106124

107125
pub fn from(&self) -> Result<&OmaSourceEntryFrom, RefreshError> {
108126
self.from.get_or_try_init(|| {
109-
let url = Url::parse(self.url())
110-
.map_err(|_| RefreshError::InvalidUrl(self.url().to_string()))?;
127+
let (transport_type, url) = self
128+
.url()
129+
.split_once("+")
130+
.unwrap_or_else(|| ("", self.url()));
131+
132+
let url =
133+
Url::parse(url).map_err(|_| RefreshError::InvalidUrl(self.url().to_string()))?;
134+
135+
let schema = if !transport_type.is_empty() {
136+
let mut schema = "".to_string();
137+
schema.push_str(transport_type);
138+
schema.push_str("+");
139+
schema.push_str(url.scheme());
140+
Cow::Owned(schema)
141+
} else {
142+
url.scheme().into()
143+
};
111144

112-
match url.scheme() {
145+
match schema.as_ref() {
113146
"file" => Ok(OmaSourceEntryFrom::Local),
114147
"http" | "https" => Ok(OmaSourceEntryFrom::Http),
148+
"mirror" | "mirror+http" | "mirror+https" => Ok(OmaSourceEntryFrom::MirrorHttp),
149+
"mirror+files" => Ok(OmaSourceEntryFrom::MirrorFile),
115150
x => Err(RefreshError::UnsupportedProtocol(x.to_string())),
116151
}
117152
})
@@ -285,6 +320,17 @@ impl MirrorSource<'_, '_> {
285320
self.fetch_local_release(replacer, index, total, download_dir, callback)
286321
.await
287322
}
323+
OmaSourceEntryFrom::MirrorHttp => todo!(),
324+
OmaSourceEntryFrom::MirrorFile => {
325+
let path = self.dist_path().strip_prefix("mirror+file:").unwrap();
326+
let f = fs::read_to_string(path)
327+
.await
328+
.map_err(|e| RefreshError::FailedToOperateDirOrFile(path.to_string(), e))?;
329+
330+
let items = parse_mirror_transport_file(&f);
331+
332+
todo!()
333+
}
288334
}
289335
}
290336

@@ -550,6 +596,45 @@ impl MirrorSource<'_, '_> {
550596
}
551597
}
552598

599+
fn parse_mirror_transport_file(f: &str) -> Vec<MirrorTransportItem> {
600+
let mut res = vec![];
601+
602+
for i in f.lines() {
603+
let mut line = i.split_ascii_whitespace();
604+
let url = line.next().expect("File contains illegal item");
605+
let mut priority = 0;
606+
let mut archs = vec![];
607+
let mut item_type = MirrorTransportItemType::Deb;
608+
609+
while let Some(entry) = line.next() {
610+
match entry.split_once(':') {
611+
Some((k, v)) => match k {
612+
"priority" => priority = v.parse::<i64>().expect("Failed to parse priority"),
613+
"arch" => archs.push(v.to_string()),
614+
"type" => {
615+
item_type = match v {
616+
"deb" => MirrorTransportItemType::Deb,
617+
"index" => MirrorTransportItemType::Index,
618+
x => panic!("Failed to parse type: {x}"),
619+
}
620+
}
621+
x => panic!("Failed to parse key: {x}"),
622+
},
623+
None => panic!("File contains illegal item"),
624+
}
625+
}
626+
627+
res.push(MirrorTransportItem {
628+
url: url.to_string(),
629+
archs,
630+
priority,
631+
item_type,
632+
});
633+
}
634+
635+
res
636+
}
637+
553638
impl<'a, 'b> MirrorSources<'a, 'b> {
554639
pub fn from_sourcelist(
555640
sourcelist: &'a [OmaSourceEntry<'a>],

0 commit comments

Comments
 (0)