Skip to content

Commit b17b19a

Browse files
authored
Merge pull request #583 from AOSC-Dev/use-apt-config-to-adjust-compression-order
refactor(oma-refresh)!: Use apt config to adjust compression file order
2 parents 1a44990 + 4a8f962 commit b17b19a

File tree

7 files changed

+208
-69
lines changed

7 files changed

+208
-69
lines changed

Cargo.lock

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

oma-fetch/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ faster-hex = "0.10"
1515
sha2 = "0.10"
1616
futures = "0.3"
1717
# FIXME: issue https://github.com/AOSC-Dev/oma/issues/547, workaround downgrade to 0.4.27
18-
async-compression = { version = "=0.4.27", features = ["gzip", "xz", "futures-io", "bzip2", "zstd"] }
18+
async-compression = { version = "=0.4.27", features = ["gzip", "xz", "futures-io", "bzip2", "zstd", "lz4"] }
1919
# Note: Use the `uncheck_liblzma_version` feature here to force dynamic linking
2020
# even with older liblzma (Ubuntu 22.04 uses 5.2.x). Be mindful, however, that
2121
# async-compression depends on a different liblzma-rs than what is specified here.

oma-fetch/src/download.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ use std::{
66
time::Duration,
77
};
88

9-
use async_compression::futures::bufread::{BzDecoder, GzipDecoder, XzDecoder, ZstdDecoder};
9+
use async_compression::futures::bufread::{
10+
BzDecoder, GzipDecoder, Lz4Decoder, LzmaDecoder, XzDecoder, ZstdDecoder,
11+
};
1012
use bon::bon;
1113
use futures::{AsyncRead, TryStreamExt, io::BufReader};
1214
use reqwest::{
@@ -477,8 +479,10 @@ impl<'a> SingleDownloader<'a> {
477479
CompressFile::Xz => &mut XzDecoder::new(BufReader::new(bytes_stream)),
478480
CompressFile::Gzip => &mut GzipDecoder::new(BufReader::new(bytes_stream)),
479481
CompressFile::Bz2 => &mut BzDecoder::new(BufReader::new(bytes_stream)),
480-
CompressFile::Nothing => &mut BufReader::new(bytes_stream),
481482
CompressFile::Zstd => &mut ZstdDecoder::new(BufReader::new(bytes_stream)),
483+
CompressFile::Lzma => &mut LzmaDecoder::new(BufReader::new(bytes_stream)),
484+
CompressFile::Lz4 => &mut Lz4Decoder::new(BufReader::new(bytes_stream)),
485+
CompressFile::Nothing => &mut BufReader::new(bytes_stream),
482486
};
483487

484488
let mut reader = reader.compat();
@@ -625,8 +629,10 @@ impl<'a> SingleDownloader<'a> {
625629
CompressFile::Xz => &mut XzDecoder::new(BufReader::new(from)),
626630
CompressFile::Gzip => &mut GzipDecoder::new(BufReader::new(from)),
627631
CompressFile::Bz2 => &mut BzDecoder::new(BufReader::new(from)),
628-
CompressFile::Nothing => &mut BufReader::new(from),
629632
CompressFile::Zstd => &mut ZstdDecoder::new(BufReader::new(from)),
633+
CompressFile::Lzma => &mut LzmaDecoder::new(BufReader::new(from)),
634+
CompressFile::Lz4 => &mut Lz4Decoder::new(BufReader::new(from)),
635+
CompressFile::Nothing => &mut BufReader::new(from),
630636
};
631637

632638
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: 141 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,106 @@
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;
55
#[cfg(feature = "apt")]
66
use oma_apt::config::{Config, ConfigTree};
77
use oma_fetch::CompressFile;
8+
use once_cell::sync::OnceCell;
89
use tracing::debug;
910

1011
use crate::{db::RefreshError, inrelease::ChecksumItem};
1112

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

234326
fn uncompress_file_name(target: &str) -> Cow<'_, str> {
235-
if compress_file(target) == CompressFile::Nothing {
327+
if compress_file(target) == CompressFile::Nothing.into() {
236328
Cow::Borrowed(target)
237329
} else {
238330
let compress_target_without_ext = Path::new(target).with_extension("");
@@ -287,15 +379,53 @@ fn get_matches_language(locales: impl IntoIterator<Item = String>) -> Vec<String
287379
langs
288380
}
289381

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-
)
382+
fn compress_file(name: &str) -> CompressFileWrapper {
383+
CompressFileWrapper {
384+
compress_file: CompressFile::from(
385+
Path::new(name)
386+
.extension()
387+
.map(|x| x.to_string_lossy())
388+
.unwrap_or_default()
389+
.to_string()
390+
.as_str(),
391+
),
392+
}
393+
}
394+
395+
#[cfg(feature = "apt")]
396+
#[test]
397+
fn test_compression_order() {
398+
let config = Config::new();
399+
400+
config.set_vector(
401+
"Acquire::CompressionTypes::Order",
402+
&vec!["zst", "xz", "bz2", "lzma", "gz", "lz4"],
403+
);
404+
405+
let mut types = config
406+
.get_compression_types()
407+
.iter()
408+
.map(|t| CompressFileWrapper::from(t.as_str()))
409+
.collect::<Vec<_>>();
410+
411+
types.sort_unstable();
412+
types.reverse();
413+
414+
assert_eq!(
415+
types,
416+
vec![
417+
CompressFile::Zstd,
418+
CompressFile::Xz,
419+
CompressFile::Bz2,
420+
CompressFile::Lzma,
421+
CompressFile::Gzip,
422+
CompressFile::Lz4,
423+
CompressFile::Nothing
424+
]
425+
.into_iter()
426+
.map(|x| x.into())
427+
.collect::<Vec<CompressFileWrapper>>()
428+
);
299429
}
300430

301431
#[test]

oma-refresh/src/db.rs

Lines changed: 31 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,25 @@ 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
240+
.source
241+
.join("etc/apt/sources.list")
242+
.to_string_lossy()
243+
.to_string();
236244

237-
debug!("sources.list is: {list_file}");
238-
debug!("sources.list.d is: {list_dir}");
245+
#[cfg(not(feature = "apt"))]
246+
let list_dir = self
247+
.source
248+
.join("etc/apt/sources.list.d")
249+
.to_string_lossy()
250+
.to_string();
239251

240252
scan_sources_lists_paths_from_sysroot(list_file, list_dir)
241253
.await
@@ -320,6 +332,18 @@ impl<'a> OmaRefresh<'a> {
320332
debug!("Setting apt opt: {k}={v}");
321333
self.apt_config.set(k, v);
322334
}
335+
336+
// default compression order
337+
if self
338+
.apt_config
339+
.find_vector("Acquire::CompressionTypes::Order")
340+
.is_empty()
341+
{
342+
self.apt_config.set_vector(
343+
"Acquire::CompressionTypes::Order",
344+
&vec!["zst", "xz", "bz2", "lzma", "gz", "lz4"],
345+
);
346+
}
323347
}
324348

325349
async fn download_release_data(
@@ -862,6 +886,8 @@ fn collect_download_task(
862886
Some("xz") => CompressFile::Xz,
863887
Some("bz2") => CompressFile::Bz2,
864888
Some("zst") => CompressFile::Zstd,
889+
Some("lzma") => CompressFile::Lzma,
890+
Some("lz4") => CompressFile::Lz4,
865891
_ => CompressFile::Nothing,
866892
}
867893
}

0 commit comments

Comments
 (0)