Skip to content

Commit 4e0a5ee

Browse files
authored
Merge pull request #567 from AOSC-Dev/support-custom-sourceslist
feat(oma-refresh): support custom `Dir::Etc::SourceList`
2 parents 19f1cde + ea02cbe commit 4e0a5ee

File tree

13 files changed

+86
-54
lines changed

13 files changed

+86
-54
lines changed

oma-pm/src/apt.rs

Lines changed: 1 addition & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -247,10 +247,6 @@ impl OmaApt {
247247
})?;
248248

249249
config.set("Dir", &sysroot.display().to_string());
250-
config.set(
251-
"Dir::State::status",
252-
&sysroot.join("var/lib/dpkg/status").display().to_string(),
253-
);
254250
}
255251

256252
debug!("Dir is: {:?}", config.get("Dir"));
@@ -768,41 +764,7 @@ impl OmaApt {
768764
return PathBuf::from("/data/data/com.termux/cache/apt/archives/");
769765
}
770766

771-
let archives_dir = self
772-
.config
773-
.get("Dir::Cache::Archives")
774-
.unwrap_or_else(|| "archives/".to_string());
775-
776-
let cache = self
777-
.config
778-
.get("Dir::Cache")
779-
.unwrap_or_else(|| "var/cache/apt".to_string());
780-
781-
let dir = self.config.get("Dir").unwrap_or_else(|| "/".to_string());
782-
783-
let archive_dir_p = PathBuf::from(archives_dir);
784-
if archive_dir_p.is_absolute() {
785-
return archive_dir_p;
786-
}
787-
788-
debug!("archive_dir_p is: {}", archive_dir_p.display());
789-
790-
let cache_dir_p = PathBuf::from(cache);
791-
if cache_dir_p.is_absolute() {
792-
return cache_dir_p.join(archive_dir_p);
793-
}
794-
795-
debug!("cache_dir_p is: {}", cache_dir_p.display());
796-
797-
let dir_p = PathBuf::from(dir);
798-
799-
debug!("dir_p is: {}", dir_p.display());
800-
801-
let res = dir_p.join(cache_dir_p).join(archive_dir_p);
802-
803-
debug!("get_archive_dir is: {}", res.display());
804-
805-
res
767+
PathBuf::from(&self.config.dir("Dir::Cache::Archives", "archives/"))
806768
})
807769
}
808770

oma-refresh/examples/update.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ async fn main() -> Result<(), RefreshError> {
2727
.topic_msg("test")
2828
.refresh_topics(false)
2929
.auth_config(&auth)
30+
.another_apt_options(vec![])
3031
.build();
3132

3233
tokio::spawn(async move {

oma-refresh/src/db.rs

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,8 @@ pub struct OmaRefresh<'a> {
128128
topic_msg: &'a str,
129129
auth_config: Option<&'a AuthConfig>,
130130
sources_lists_paths: Option<Vec<PathBuf>>,
131+
#[builder(default)]
132+
another_apt_options: Vec<String>,
131133
}
132134

133135
/// Create `apt update` file lock
@@ -213,10 +215,30 @@ impl<'a> OmaRefresh<'a> {
213215
return Err(RefreshError::WrongThreadCount(self.threads));
214216
}
215217

218+
#[cfg(feature = "apt")]
219+
self.init_apt_options();
220+
216221
let paths = if let Some(ref paths) = self.sources_lists_paths {
217222
paths.to_vec()
218223
} else {
219-
scan_sources_lists_paths_from_sysroot(&self.source)
224+
#[cfg(feature = "apt")]
225+
let list_file = self.apt_config.file("Dir::Etc::sourcelist", "sources.list");
226+
227+
#[cfg(feature = "apt")]
228+
let list_dir = self
229+
.apt_config
230+
.dir("Dir::Etc::sourceparts", "sources.list.d");
231+
232+
#[cfg(not(feature = "apt"))]
233+
let list_file = self.source.join("etc/apt/sources.list");
234+
235+
#[cfg(not(feature = "apt"))]
236+
let list_dir = self.source.join("etc/apt/sources.list.d");
237+
238+
debug!("sources.list is: {list_file}");
239+
debug!("sources.list.d is: {list_dir}");
240+
241+
scan_sources_lists_paths_from_sysroot(list_file, list_dir)
220242
.await
221243
.map_err(RefreshError::ScanSourceError)?
222244
};
@@ -301,6 +323,17 @@ impl<'a> OmaRefresh<'a> {
301323
Ok(res.success)
302324
}
303325

326+
#[cfg(feature = "apt")]
327+
fn init_apt_options(&self) {
328+
self.apt_config.set("Dir", &self.source.to_string_lossy());
329+
330+
for i in &self.another_apt_options {
331+
let (k, v) = i.split_once('=').unwrap_or((i.as_str(), ""));
332+
debug!("Setting apt opt: {k}={v}");
333+
self.apt_config.set(k, v);
334+
}
335+
}
336+
304337
async fn download_release_data(
305338
&self,
306339
callback: &impl AsyncFn(Event),

oma-refresh/src/sourceslist.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,24 +55,26 @@ impl Debug for OmaSourceEntry<'_> {
5555
}
5656

5757
pub(crate) async fn scan_sources_lists_paths_from_sysroot(
58-
sysroot: impl AsRef<Path>,
58+
list_file: impl AsRef<str>,
59+
list_dir: impl AsRef<str>,
5960
) -> Result<Vec<PathBuf>, SourcesListError> {
6061
let mut paths = vec![];
61-
let default = sysroot.as_ref().join("etc/apt/sources.list");
62+
let default = Path::new(list_file.as_ref());
63+
let list_dir_path = Path::new(list_dir.as_ref());
6264

6365
if default.exists() {
64-
paths.push(default);
66+
paths.push(default.to_path_buf());
6567
}
6668

67-
if sysroot.as_ref().join("etc/apt/sources.list.d/").exists() {
68-
let mut dir = tokio::fs::read_dir(sysroot.as_ref().join("etc/apt/sources.list.d/")).await?;
69+
if list_dir_path.exists() {
70+
let mut dir = tokio::fs::read_dir(list_dir_path).await?;
6971
while let Some(entry) = dir.next_entry().await? {
7072
let path = entry.path();
7173
if !path.is_file() {
7274
continue;
7375
}
7476

75-
paths.push(path);
77+
paths.push(path.to_path_buf());
7678
}
7779
}
7880

src/subcommand/history.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,8 @@ impl CliExecuter for Undo {
194194
.network_thread(download_threads.unwrap_or_else(|| config.network_thread()))
195195
.sysroot(&sysroot)
196196
.config(&apt_config)
197-
.maybe_auth_config(auth_config.as_ref());
197+
.maybe_auth_config(auth_config.as_ref())
198+
.apt_options(apt_options.clone());
198199

199200
#[cfg(feature = "aosc")]
200201
let refresh = builder

src/subcommand/install.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,7 @@ impl CliExecuter for Install {
169169
.network_thread(download_threads.unwrap_or_else(|| config.network_thread()))
170170
.sysroot(&sysroot)
171171
.config(&apt_config)
172+
.apt_options(apt_options.clone())
172173
.maybe_auth_config(auth_config);
173174

174175
#[cfg(feature = "aosc")]

src/subcommand/mirror.rs

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,9 @@ pub struct CliMirror {
9494
/// Setup download threads (default as 4)
9595
#[arg(from_global, help = fl!("clap-download-threads-help"))]
9696
download_threads: Option<usize>,
97+
/// Set apt options
98+
#[arg(from_global, help = fl!("clap-apt-options-help"))]
99+
apt_options: Vec<String>,
97100
}
98101

99102
#[derive(Debug, Subcommand)]
@@ -190,6 +193,7 @@ impl CliExecuter for CliMirror {
190193
no_refresh,
191194
dry_run,
192195
download_threads,
196+
apt_options,
193197
} = self;
194198

195199
if dry_run {
@@ -212,6 +216,7 @@ impl CliExecuter for CliMirror {
212216
names.iter().map(|x| x.as_str()).collect::<Vec<_>>(),
213217
sysroot,
214218
Operate::Set,
219+
apt_options,
215220
),
216221
MirrorSubCmd::Speedtest {
217222
set_fastest,
@@ -224,6 +229,7 @@ impl CliExecuter for CliMirror {
224229
!no_refresh_topics && !config.no_refresh_topics(),
225230
download_threads.unwrap_or_else(|| config.network_thread()),
226231
no_refresh,
232+
apt_options,
227233
),
228234
MirrorSubCmd::Add {
229235
names,
@@ -238,6 +244,7 @@ impl CliExecuter for CliMirror {
238244
names.iter().map(|x| x.as_str()).collect::<Vec<_>>(),
239245
sysroot,
240246
Operate::Add,
247+
apt_options,
241248
),
242249
MirrorSubCmd::Remove {
243250
names,
@@ -252,6 +259,7 @@ impl CliExecuter for CliMirror {
252259
names.iter().map(|x| x.as_str()).collect::<Vec<_>>(),
253260
sysroot,
254261
Operate::Remove,
262+
apt_options,
255263
),
256264
MirrorSubCmd::SortMirrors {
257265
no_refresh_topics,
@@ -261,6 +269,7 @@ impl CliExecuter for CliMirror {
261269
!no_refresh_topics && !config.no_refresh_topics(),
262270
download_threads.unwrap_or_else(|| config.network_thread()),
263271
no_refresh,
272+
apt_options,
264273
),
265274
}
266275
} else {
@@ -269,6 +278,7 @@ impl CliExecuter for CliMirror {
269278
!no_refresh_topics && !config.no_refresh_topics(),
270279
download_threads.unwrap_or_else(|| config.network_thread()),
271280
no_refresh,
281+
apt_options,
272282
)
273283
}
274284
}
@@ -279,6 +289,7 @@ pub fn tui(
279289
refresh_topic: bool,
280290
network_threads: usize,
281291
no_refresh: bool,
292+
apt_options: Vec<String>,
282293
) -> Result<i32, OutputError> {
283294
root()?;
284295

@@ -347,26 +358,28 @@ pub fn tui(
347358

348359
if !no_refresh {
349360
refresh_enabled_topics_sources_list(no_progress)?;
350-
refresh(no_progress, network_threads, refresh_topic)?;
361+
refresh(no_progress, network_threads, refresh_topic, apt_options)?;
351362
}
352363

353364
Ok(0)
354365
}
355366

356-
pub enum Operate {
367+
enum Operate {
357368
Set,
358369
Add,
359370
Remove,
360371
}
361372

362-
pub fn operate(
373+
#[allow(clippy::too_many_arguments)]
374+
fn operate(
363375
no_progress: bool,
364376
refresh_topic: bool,
365377
network_threads: usize,
366378
no_refresh: bool,
367379
args: Vec<&str>,
368380
sysroot: PathBuf,
369381
subcmd: Operate,
382+
apt_options: Vec<String>,
370383
) -> Result<i32, OutputError> {
371384
root()?;
372385

@@ -392,7 +405,7 @@ pub fn operate(
392405

393406
if !no_refresh {
394407
refresh_enabled_topics_sources_list(no_progress)?;
395-
refresh(no_progress, network_threads, refresh_topic)?;
408+
refresh(no_progress, network_threads, refresh_topic, apt_options)?;
396409
}
397410

398411
Ok(0)
@@ -403,6 +416,7 @@ pub fn set_order(
403416
refresh_topic: bool,
404417
network_threads: usize,
405418
no_refresh: bool,
419+
apt_options: Vec<String>,
406420
) -> Result<i32, OutputError> {
407421
root()?;
408422

@@ -428,7 +442,7 @@ pub fn set_order(
428442

429443
if !no_refresh {
430444
refresh_enabled_topics_sources_list(no_progress)?;
431-
refresh(no_progress, network_threads, refresh_topic)?;
445+
refresh(no_progress, network_threads, refresh_topic, apt_options)?;
432446
}
433447

434448
Ok(0)
@@ -446,6 +460,7 @@ pub fn speedtest(
446460
refresh_topic: bool,
447461
network_threads: usize,
448462
no_refresh: bool,
463+
apt_options: Vec<String>,
449464
) -> Result<i32, OutputError> {
450465
if set_fastest {
451466
root()?;
@@ -570,7 +585,7 @@ pub fn speedtest(
570585

571586
if !no_refresh {
572587
refresh_enabled_topics_sources_list(no_progress)?;
573-
refresh(no_progress, network_threads, refresh_topic)?;
588+
refresh(no_progress, network_threads, refresh_topic, apt_options)?;
574589
}
575590
}
576591

@@ -581,6 +596,7 @@ fn refresh(
581596
no_progress: bool,
582597
network_threads: usize,
583598
refresh_topic: bool,
599+
apt_options: Vec<String>,
584600
) -> Result<(), OutputError> {
585601
let auth_config = auth_config("/");
586602
let auth_config = auth_config.as_ref();
@@ -593,6 +609,7 @@ fn refresh(
593609
.refresh_topics(refresh_topic)
594610
.config(&AptConfig::new())
595611
.maybe_auth_config(auth_config)
612+
.apt_options(apt_options.clone())
596613
.build()
597614
.run()?;
598615

src/subcommand/pick.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ impl CliExecuter for Pick {
131131
.network_thread(download_threads.unwrap_or_else(|| config.network_thread()))
132132
.sysroot(&sysroot)
133133
.config(&apt_config)
134+
.apt_options(apt_options.clone())
134135
.maybe_auth_config(auth_config);
135136

136137
#[cfg(feature = "aosc")]

src/subcommand/refresh.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ pub struct Refresh {
2626
/// Setup download threads (default as 4)
2727
#[arg(from_global, help = fl!("clap-download-threads-help"))]
2828
download_threads: Option<usize>,
29+
/// Set apt options
30+
#[arg(from_global, help = fl!("clap-apt-options-help"))]
31+
apt_options: Vec<String>,
2932
}
3033

3134
impl CliExecuter for Refresh {
@@ -36,6 +39,7 @@ impl CliExecuter for Refresh {
3639
sysroot,
3740
dry_run,
3841
download_threads,
42+
apt_options,
3943
} = self;
4044

4145
if dry_run {
@@ -57,6 +61,7 @@ impl CliExecuter for Refresh {
5761
.network_thread(download_threads.unwrap_or_else(|| config.network_thread()))
5862
.sysroot(&sysroot_str)
5963
.config(&apt_config)
64+
.apt_options(apt_options)
6065
.maybe_auth_config(auth_config);
6166

6267
#[cfg(feature = "aosc")]

0 commit comments

Comments
 (0)