Skip to content

Commit 31c0e8b

Browse files
committed
refactor(oma-refresh): refactor select Release/InRelease logic
1 parent d28c33e commit 31c0e8b

File tree

4 files changed

+62
-50
lines changed

4 files changed

+62
-50
lines changed

i18n/zh-CN/oma.ftl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ tum-2 = 根据您指定的操作,oma 还需要对系统组件执行若干变
233233
security = 安全更新
234234
verify-error = 在验证 { $p } 的签名时遇到错误。
235235
sources-list-empty = 软件源配置为空。
236-
failed-refresh = 无法刷新软件源数据
236+
failed-refresh = 无法刷新软件源数据
237237
unsupported-sources-list = APT 软件源配置文件 { $p } 不受支持:仅支持单行 ({ $list }) 及 DEB822 ({ $sources }) 格式的配置文件。
238238
set-permission = 设置权限失败。
239239
open-file-as-write-mode = 无法打开文件为写模式。

oma-refresh/src/db.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -352,7 +352,7 @@ impl<'a> OmaRefresh<'a> {
352352

353353
let results = mirror_sources
354354
.fetch_all_release(
355-
&self.client,
355+
self.client,
356356
replacer,
357357
&self.download_dir,
358358
self.threads,
@@ -680,7 +680,7 @@ fn collect_flat_repo_no_release(
680680
tasks: &mut Vec<DownloadEntry>,
681681
replacer: &DatabaseFilenameReplacer,
682682
) -> Result<()> {
683-
let msg = mirror_source.get_human_download_url(Some("Packages"))?;
683+
let msg = mirror_source.get_human_download_message(Some("Packages"))?;
684684

685685
let dist_url = mirror_source.dist_path();
686686

@@ -727,7 +727,7 @@ fn collect_download_task(
727727
) -> Result<()> {
728728
let file_type = &c.msg;
729729

730-
let msg = mirror_source.get_human_download_url(Some(file_type))?;
730+
let msg = mirror_source.get_human_download_message(Some(file_type))?;
731731

732732
let dist_url = &mirror_source.dist_path();
733733

oma-refresh/src/sourceslist.rs

Lines changed: 45 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ impl MirrorSource<'_, '_> {
233233
self.sources.first().unwrap().from()
234234
}
235235

236-
pub fn get_human_download_url(
236+
pub fn get_human_download_message(
237237
&self,
238238
file_name: Option<&str>,
239239
) -> Result<String, RefreshError> {
@@ -313,59 +313,41 @@ impl MirrorSource<'_, '_> {
313313
{
314314
let dist_path = self.dist_path();
315315

316-
let mut r = None;
317-
let mut u = None;
318-
let mut is_release = false;
319-
320-
let msg = self.get_human_download_url(None)?;
316+
let msg = self.get_human_download_message(None)?;
321317

322318
callback(Event::DownloadEvent(oma_fetch::Event::NewProgressSpinner {
323319
index,
324320
msg: format!("({}/{}) {}", index, total, msg),
325321
}))
326322
.await;
327323

328-
for (index, file_name) in ["InRelease", "Release"].iter().enumerate() {
329-
let url = format!("{}/{}", dist_path, file_name);
330-
let request = build_request_with_basic_auth(
331-
client,
332-
Method::GET,
333-
&self
334-
.auth()
335-
.map(|x| (x.login.to_string(), x.password.to_string())),
336-
&url,
337-
);
338-
339-
let resp = request
340-
.send()
341-
.await
342-
.and_then(|resp| resp.error_for_status());
343-
344-
r = Some(resp);
324+
let mut url = format!("{}/InRelease", dist_path);
325+
let mut is_release = false;
345326

346-
if r.as_ref().unwrap().is_ok() {
347-
u = Some(url);
348-
if index == 1 {
349-
is_release = true;
327+
let resp = match self.send_request(client, &url, Method::GET).await {
328+
Ok(resp) => resp,
329+
Err(e) => {
330+
debug!("{e}");
331+
url = format!("{}/Release", dist_path);
332+
let resp = self.send_request(client, &url, Method::GET).await;
333+
334+
if resp.is_err() && self.is_flat() {
335+
// Flat repo no release
336+
callback(Event::DownloadEvent(oma_fetch::Event::ProgressDone(index))).await;
337+
return Ok(());
350338
}
351-
break;
352-
}
353-
}
354339

355-
let r = r.unwrap();
340+
is_release = true;
356341

357-
callback(Event::DownloadEvent(oma_fetch::Event::ProgressDone(index))).await;
342+
callback(Event::DownloadEvent(oma_fetch::Event::ProgressDone(index))).await;
358343

359-
if r.is_err() && self.is_flat() {
360-
// Flat repo no release
361-
return Ok(());
362-
}
344+
resp.map_err(|e| SingleDownloadError::ReqwestError { source: e })
345+
.map_err(|e| RefreshError::DownloadFailed(Some(e)))?
346+
}
347+
};
363348

364-
let resp = r
365-
.map_err(|e| SingleDownloadError::ReqwestError { source: e })
366-
.map_err(|e| RefreshError::DownloadFailed(Some(e)))?;
349+
callback(Event::DownloadEvent(oma_fetch::Event::ProgressDone(index))).await;
367350

368-
let url = u.unwrap();
369351
let file_name = replacer.replace(&url)?;
370352

371353
self.download_file(&file_name, resp, index, total, download_dir, &callback)
@@ -403,6 +385,27 @@ impl MirrorSource<'_, '_> {
403385
Ok(())
404386
}
405387

388+
async fn send_request(
389+
&self,
390+
client: &Client,
391+
url: &str,
392+
method: Method,
393+
) -> Result<Response, oma_fetch::reqwest::Error> {
394+
let request = build_request_with_basic_auth(
395+
client,
396+
method,
397+
&self
398+
.auth()
399+
.map(|x| (x.login.to_string(), x.password.to_string())),
400+
url,
401+
);
402+
403+
request
404+
.send()
405+
.await
406+
.and_then(|resp| resp.error_for_status())
407+
}
408+
406409
async fn download_file<F, Fut>(
407410
&self,
408411
file_name: &str,
@@ -424,7 +427,7 @@ impl MirrorSource<'_, '_> {
424427
"({}/{}) {}",
425428
index,
426429
total,
427-
self.get_human_download_url(Some(file_name)).unwrap(),
430+
self.get_human_download_message(Some(file_name)).unwrap(),
428431
),
429432
size: total_size,
430433
}));
@@ -482,7 +485,7 @@ impl MirrorSource<'_, '_> {
482485

483486
let mut name = None;
484487

485-
let msg = self.get_human_download_url(None)?;
488+
let msg = self.get_human_download_message(None)?;
486489

487490
callback(Event::DownloadEvent(oma_fetch::Event::NewProgressSpinner {
488491
index,

src/error.rs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -427,10 +427,19 @@ impl From<RefreshError> for OutputError {
427427
description: fl!("sources-list-empty"),
428428
source: None,
429429
},
430-
RefreshError::DownloadFailed(_) => Self {
431-
description: fl!("failed-refresh"),
432-
source: None,
433-
},
430+
RefreshError::DownloadFailed(err) => {
431+
if let Some(err) = err {
432+
Self {
433+
description: fl!("failed-refresh"),
434+
source: Some(Box::new(OutputError::from(err))),
435+
}
436+
} else {
437+
Self {
438+
description: fl!("failed-refresh"),
439+
source: None,
440+
}
441+
}
442+
}
434443
RefreshError::OperateFile(path, error) => Self {
435444
description: fl!("failed-to-operate-path", p = path.display().to_string()),
436445
source: Some(Box::new(error)),

0 commit comments

Comments
 (0)