Skip to content

Commit 2290f34

Browse files
committed
init
1 parent 04b5aa5 commit 2290f34

File tree

5 files changed

+133
-74
lines changed

5 files changed

+133
-74
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/src/config.rs

Lines changed: 101 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,80 @@ 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+
self_pos.cmp(&other_pos)
68+
}
69+
}
70+
71+
#[cfg(not(feature = "apt"))]
72+
impl Ord for CompressFileWrapper {
73+
fn cmp(&self, other: &Self) -> Ordering {
74+
unimplemented!()
75+
}
76+
}
77+
78+
impl From<CompressFile> for CompressFileWrapper {
79+
fn from(value: CompressFile) -> Self {
80+
Self {
81+
compress_file: value,
82+
}
83+
}
84+
}
85+
1286
#[cfg(feature = "apt")]
1387
fn modify_result(
1488
tree: ConfigTree,
@@ -232,7 +306,7 @@ fn flat_repo_template_match(
232306
}
233307

234308
fn uncompress_file_name(target: &str) -> Cow<'_, str> {
235-
if compress_file(target) == CompressFile::Nothing {
309+
if compress_file(target) == CompressFile::Nothing.into() {
236310
Cow::Borrowed(target)
237311
} else {
238312
let compress_target_without_ext = Path::new(target).with_extension("");
@@ -287,15 +361,31 @@ fn get_matches_language(locales: impl IntoIterator<Item = String>) -> Vec<String
287361
langs
288362
}
289363

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-
)
364+
fn compress_file(name: &str) -> CompressFileWrapper {
365+
CompressFileWrapper {
366+
compress_file: CompressFile::from(
367+
Path::new(name)
368+
.extension()
369+
.map(|x| x.to_string_lossy())
370+
.unwrap_or_default()
371+
.to_string()
372+
.as_str(),
373+
),
374+
}
375+
}
376+
377+
#[cfg(feature = "apt")]
378+
#[test]
379+
fn test_compression_order() {
380+
let mut t = Config::new()
381+
.get_compression_types()
382+
.iter()
383+
.map(|t| CompressFileWrapper::from(t.as_str()))
384+
.collect::<Vec<_>>();
385+
386+
t.sort();
387+
388+
dbg!(t);
299389
}
300390

301391
#[test]

oma-refresh/src/db.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,18 @@ impl<'a> OmaRefresh<'a> {
320320
debug!("Setting apt opt: {k}={v}");
321321
self.apt_config.set(k, v);
322322
}
323+
324+
// default compression order
325+
if self
326+
.apt_config
327+
.find_vector("Acquire::CompressionTypes::Order")
328+
.is_empty()
329+
{
330+
self.apt_config.set_vector(
331+
"Acquire::CompressionTypes::Order",
332+
&vec!["zst", "xz", "bz2", "lzma", "gz", "lz4"],
333+
);
334+
}
323335
}
324336

325337
async fn download_release_data(

0 commit comments

Comments
 (0)