Skip to content

Commit 10647ec

Browse files
committed
refactor(oma-refresh)!: Use apt config to adjust compression file order
1 parent 04b5aa5 commit 10647ec

File tree

6 files changed

+154
-79
lines changed

6 files changed

+154
-79
lines changed

Cargo.lock

Lines changed: 14 additions & 14 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

oma-fetch/src/download.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -478,6 +478,8 @@ impl<'a> SingleDownloader<'a> {
478478
CompressFile::Bz2 => &mut BzDecoder::new(BufReader::new(bytes_stream)),
479479
CompressFile::Nothing => &mut BufReader::new(bytes_stream),
480480
CompressFile::Zstd => &mut ZstdDecoder::new(BufReader::new(bytes_stream)),
481+
CompressFile::Lzma => unimplemented!(),
482+
CompressFile::Lz4 => unimplemented!(),
481483
};
482484

483485
let mut reader = reader.compat();
@@ -634,6 +636,8 @@ impl<'a> SingleDownloader<'a> {
634636
CompressFile::Bz2 => &mut BzDecoder::new(BufReader::new(from)),
635637
CompressFile::Nothing => &mut BufReader::new(from),
636638
CompressFile::Zstd => &mut ZstdDecoder::new(BufReader::new(from)),
639+
CompressFile::Lzma => unimplemented!(),
640+
CompressFile::Lz4 => unimplemented!(),
637641
};
638642

639643
let mut reader = reader.compat();

oma-fetch/src/lib.rs

Lines changed: 2 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -46,59 +46,12 @@ pub enum CompressFile {
4646
Gzip,
4747
Xz,
4848
Zstd,
49+
Lzma,
50+
Lz4,
4951
#[default]
5052
Nothing,
5153
}
5254

53-
// 压缩文件下载顺序:Zstd -> XZ -> Gzip -> Bz2 -> 未压缩
54-
impl Ord for CompressFile {
55-
fn cmp(&self, other: &Self) -> Ordering {
56-
match self {
57-
CompressFile::Bz2 => match other {
58-
CompressFile::Bz2 => Ordering::Equal,
59-
CompressFile::Gzip => Ordering::Less,
60-
CompressFile::Xz => Ordering::Less,
61-
CompressFile::Zstd => Ordering::Less,
62-
CompressFile::Nothing => Ordering::Greater,
63-
},
64-
CompressFile::Gzip => match other {
65-
CompressFile::Bz2 => Ordering::Greater,
66-
CompressFile::Gzip => Ordering::Less,
67-
CompressFile::Xz => Ordering::Less,
68-
CompressFile::Zstd => Ordering::Less,
69-
CompressFile::Nothing => Ordering::Greater,
70-
},
71-
CompressFile::Xz => match other {
72-
CompressFile::Bz2 => Ordering::Greater,
73-
CompressFile::Gzip => Ordering::Greater,
74-
CompressFile::Xz => Ordering::Equal,
75-
CompressFile::Zstd => Ordering::Less,
76-
CompressFile::Nothing => Ordering::Greater,
77-
},
78-
CompressFile::Zstd => match other {
79-
CompressFile::Bz2 => Ordering::Greater,
80-
CompressFile::Gzip => Ordering::Greater,
81-
CompressFile::Xz => Ordering::Greater,
82-
CompressFile::Zstd => Ordering::Equal,
83-
CompressFile::Nothing => Ordering::Greater,
84-
},
85-
CompressFile::Nothing => match other {
86-
CompressFile::Bz2 => Ordering::Less,
87-
CompressFile::Gzip => Ordering::Less,
88-
CompressFile::Xz => Ordering::Less,
89-
CompressFile::Zstd => Ordering::Less,
90-
CompressFile::Nothing => Ordering::Equal,
91-
},
92-
}
93-
}
94-
}
95-
96-
impl PartialOrd for CompressFile {
97-
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
98-
Some(self.cmp(other))
99-
}
100-
}
101-
10255
impl From<&str> for CompressFile {
10356
fn from(s: &str) -> Self {
10457
match s {

oma-refresh/Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,7 @@ default = ["aosc", "sequoia-nettle-backend", "rustls", "apt"]
4646
tokio = { version = "1.28", default-features = false, features = ["rt-multi-thread"] }
4747
flume = "0.11.1"
4848
oma-utils = { version = "^0.12.0", path = "../oma-utils", features = ["dpkg"] }
49+
50+
[[example]]
51+
name = "update"
52+
required-features = ["apt"]

oma-refresh/src/config.rs

Lines changed: 109 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::{borrow::Cow, collections::HashMap, path::Path};
1+
use std::{borrow::Cow, cmp::Ordering, collections::HashMap, path::Path};
22

33
use ahash::AHashMap;
44
use aho_corasick::AhoCorasick;
@@ -9,6 +9,88 @@ use tracing::debug;
99

1010
use crate::{db::RefreshError, inrelease::ChecksumItem};
1111

12+
#[derive(Debug, Eq, PartialEq)]
13+
struct CompressFileWrapper {
14+
compress_file: CompressFile,
15+
}
16+
17+
impl From<&str> for CompressFileWrapper {
18+
fn from(value: &str) -> Self {
19+
match value {
20+
"xz" => CompressFileWrapper {
21+
compress_file: CompressFile::Xz,
22+
},
23+
"bz2" => CompressFileWrapper {
24+
compress_file: CompressFile::Bz2,
25+
},
26+
"lzma" => CompressFileWrapper {
27+
compress_file: CompressFile::Lzma,
28+
},
29+
"gz" => CompressFileWrapper {
30+
compress_file: CompressFile::Gzip,
31+
},
32+
"lz4" => CompressFileWrapper {
33+
compress_file: CompressFile::Lz4,
34+
},
35+
"zst" => CompressFileWrapper {
36+
compress_file: CompressFile::Zstd,
37+
},
38+
x => {
39+
debug!("{x} format is not compress format");
40+
CompressFileWrapper {
41+
compress_file: CompressFile::Nothing,
42+
}
43+
}
44+
}
45+
}
46+
}
47+
48+
impl PartialOrd for CompressFileWrapper {
49+
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
50+
Some(self.cmp(other))
51+
}
52+
}
53+
54+
#[cfg(feature = "apt")]
55+
impl Ord for CompressFileWrapper {
56+
fn cmp(&self, other: &Self) -> Ordering {
57+
let config = Config::new();
58+
let t = config
59+
.get_compression_types()
60+
.iter()
61+
.map(|t| CompressFileWrapper::from(t.as_str()))
62+
.collect::<Vec<_>>();
63+
64+
let self_pos = t.iter().position(|x| x == self).unwrap();
65+
let other_pos = t.iter().position(|x| x == other).unwrap();
66+
67+
other_pos.cmp(&self_pos)
68+
}
69+
}
70+
71+
#[cfg(not(feature = "apt"))]
72+
impl Ord for CompressFileWrapper {
73+
fn cmp(&self, other: &Self) -> Ordering {
74+
let t = vec!["zst", "xz", "bz2", "lzma", "gz", "lz4"]
75+
.into_iter()
76+
.map(CompressFileWrapper::from)
77+
.collect::<Vec<_>>();
78+
79+
let self_pos = t.iter().position(|x| x == self).unwrap();
80+
let other_pos = t.iter().position(|x| x == other).unwrap();
81+
82+
other_pos.cmp(&self_pos)
83+
}
84+
}
85+
86+
impl From<CompressFile> for CompressFileWrapper {
87+
fn from(value: CompressFile) -> Self {
88+
Self {
89+
compress_file: value,
90+
}
91+
}
92+
}
93+
1294
#[cfg(feature = "apt")]
1395
fn modify_result(
1496
tree: ConfigTree,
@@ -232,7 +314,7 @@ fn flat_repo_template_match(
232314
}
233315

234316
fn uncompress_file_name(target: &str) -> Cow<'_, str> {
235-
if compress_file(target) == CompressFile::Nothing {
317+
if compress_file(target) == CompressFile::Nothing.into() {
236318
Cow::Borrowed(target)
237319
} else {
238320
let compress_target_without_ext = Path::new(target).with_extension("");
@@ -287,15 +369,31 @@ fn get_matches_language(locales: impl IntoIterator<Item = String>) -> Vec<String
287369
langs
288370
}
289371

290-
fn compress_file(name: &str) -> CompressFile {
291-
CompressFile::from(
292-
Path::new(name)
293-
.extension()
294-
.map(|x| x.to_string_lossy())
295-
.unwrap_or_default()
296-
.to_string()
297-
.as_str(),
298-
)
372+
fn compress_file(name: &str) -> CompressFileWrapper {
373+
CompressFileWrapper {
374+
compress_file: CompressFile::from(
375+
Path::new(name)
376+
.extension()
377+
.map(|x| x.to_string_lossy())
378+
.unwrap_or_default()
379+
.to_string()
380+
.as_str(),
381+
),
382+
}
383+
}
384+
385+
#[cfg(feature = "apt")]
386+
#[test]
387+
fn test_compression_order() {
388+
let mut t = Config::new()
389+
.get_compression_types()
390+
.iter()
391+
.map(|t| CompressFileWrapper::from(t.as_str()))
392+
.collect::<Vec<_>>();
393+
394+
t.sort();
395+
396+
dbg!(t);
299397
}
300398

301399
#[test]

oma-refresh/src/db.rs

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ pub struct OmaRefresh<'a> {
127127
topic_msg: &'a str,
128128
auth_config: Option<&'a AuthConfig>,
129129
sources_lists_paths: Option<Vec<PathBuf>>,
130+
#[cfg(feature = "apt")]
130131
#[builder(default)]
131132
another_apt_options: Vec<String>,
132133
}
@@ -228,14 +229,17 @@ impl<'a> OmaRefresh<'a> {
228229
.apt_config
229230
.dir("Dir::Etc::sourceparts", "sources.list.d");
230231

231-
#[cfg(not(feature = "apt"))]
232-
let list_file = self.source.join("etc/apt/sources.list");
232+
#[cfg(feature = "apt")]
233+
{
234+
debug!("sources.list is: {list_file}");
235+
debug!("sources.list.d is: {list_dir}");
236+
}
233237

234238
#[cfg(not(feature = "apt"))]
235-
let list_dir = self.source.join("etc/apt/sources.list.d");
239+
let list_file = self.source.join("etc/apt/sources.list").to_string_lossy().to_string();
236240

237-
debug!("sources.list is: {list_file}");
238-
debug!("sources.list.d is: {list_dir}");
241+
#[cfg(not(feature = "apt"))]
242+
let list_dir = self.source.join("etc/apt/sources.list.d").to_string_lossy().to_string();
239243

240244
scan_sources_lists_paths_from_sysroot(list_file, list_dir)
241245
.await
@@ -320,6 +324,18 @@ impl<'a> OmaRefresh<'a> {
320324
debug!("Setting apt opt: {k}={v}");
321325
self.apt_config.set(k, v);
322326
}
327+
328+
// default compression order
329+
if self
330+
.apt_config
331+
.find_vector("Acquire::CompressionTypes::Order")
332+
.is_empty()
333+
{
334+
self.apt_config.set_vector(
335+
"Acquire::CompressionTypes::Order",
336+
&vec!["zst", "xz", "bz2", "lzma", "gz", "lz4"],
337+
);
338+
}
323339
}
324340

325341
async fn download_release_data(

0 commit comments

Comments
 (0)