Skip to content

Commit

Permalink
refactor(oma-refresh): refactor select Release/InRelease logic
Browse files Browse the repository at this point in the history
  • Loading branch information
eatradish committed Jan 17, 2025
1 parent d28c33e commit 31c0e8b
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 50 deletions.
2 changes: 1 addition & 1 deletion i18n/zh-CN/oma.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ tum-2 = 根据您指定的操作,oma 还需要对系统组件执行若干变
security = 安全更新
verify-error = 在验证 { $p } 的签名时遇到错误。
sources-list-empty = 软件源配置为空。
failed-refresh = 无法刷新软件源数据
failed-refresh = 无法刷新软件源数据
unsupported-sources-list = APT 软件源配置文件 { $p } 不受支持:仅支持单行 ({ $list }) 及 DEB822 ({ $sources }) 格式的配置文件。
set-permission = 设置权限失败。
open-file-as-write-mode = 无法打开文件为写模式。
Expand Down
6 changes: 3 additions & 3 deletions oma-refresh/src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,7 @@ impl<'a> OmaRefresh<'a> {

let results = mirror_sources
.fetch_all_release(
&self.client,
self.client,
replacer,
&self.download_dir,
self.threads,
Expand Down Expand Up @@ -680,7 +680,7 @@ fn collect_flat_repo_no_release(
tasks: &mut Vec<DownloadEntry>,
replacer: &DatabaseFilenameReplacer,
) -> Result<()> {
let msg = mirror_source.get_human_download_url(Some("Packages"))?;
let msg = mirror_source.get_human_download_message(Some("Packages"))?;

let dist_url = mirror_source.dist_path();

Expand Down Expand Up @@ -727,7 +727,7 @@ fn collect_download_task(
) -> Result<()> {
let file_type = &c.msg;

let msg = mirror_source.get_human_download_url(Some(file_type))?;
let msg = mirror_source.get_human_download_message(Some(file_type))?;

let dist_url = &mirror_source.dist_path();

Expand Down
87 changes: 45 additions & 42 deletions oma-refresh/src/sourceslist.rs
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ impl MirrorSource<'_, '_> {
self.sources.first().unwrap().from()
}

pub fn get_human_download_url(
pub fn get_human_download_message(
&self,
file_name: Option<&str>,
) -> Result<String, RefreshError> {
Expand Down Expand Up @@ -313,59 +313,41 @@ impl MirrorSource<'_, '_> {
{
let dist_path = self.dist_path();

let mut r = None;
let mut u = None;
let mut is_release = false;

let msg = self.get_human_download_url(None)?;
let msg = self.get_human_download_message(None)?;

callback(Event::DownloadEvent(oma_fetch::Event::NewProgressSpinner {
index,
msg: format!("({}/{}) {}", index, total, msg),
}))
.await;

for (index, file_name) in ["InRelease", "Release"].iter().enumerate() {
let url = format!("{}/{}", dist_path, file_name);
let request = build_request_with_basic_auth(
client,
Method::GET,
&self
.auth()
.map(|x| (x.login.to_string(), x.password.to_string())),
&url,
);

let resp = request
.send()
.await
.and_then(|resp| resp.error_for_status());

r = Some(resp);
let mut url = format!("{}/InRelease", dist_path);
let mut is_release = false;

if r.as_ref().unwrap().is_ok() {
u = Some(url);
if index == 1 {
is_release = true;
let resp = match self.send_request(client, &url, Method::GET).await {
Ok(resp) => resp,
Err(e) => {
debug!("{e}");
url = format!("{}/Release", dist_path);
let resp = self.send_request(client, &url, Method::GET).await;

if resp.is_err() && self.is_flat() {
// Flat repo no release
callback(Event::DownloadEvent(oma_fetch::Event::ProgressDone(index))).await;
return Ok(());
}
break;
}
}

let r = r.unwrap();
is_release = true;

callback(Event::DownloadEvent(oma_fetch::Event::ProgressDone(index))).await;
callback(Event::DownloadEvent(oma_fetch::Event::ProgressDone(index))).await;

if r.is_err() && self.is_flat() {
// Flat repo no release
return Ok(());
}
resp.map_err(|e| SingleDownloadError::ReqwestError { source: e })
.map_err(|e| RefreshError::DownloadFailed(Some(e)))?
}
};

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

let url = u.unwrap();
let file_name = replacer.replace(&url)?;

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

async fn send_request(
&self,
client: &Client,
url: &str,
method: Method,
) -> Result<Response, oma_fetch::reqwest::Error> {
let request = build_request_with_basic_auth(
client,
method,
&self
.auth()
.map(|x| (x.login.to_string(), x.password.to_string())),
url,
);

request
.send()
.await
.and_then(|resp| resp.error_for_status())
}

async fn download_file<F, Fut>(
&self,
file_name: &str,
Expand All @@ -424,7 +427,7 @@ impl MirrorSource<'_, '_> {
"({}/{}) {}",
index,
total,
self.get_human_download_url(Some(file_name)).unwrap(),
self.get_human_download_message(Some(file_name)).unwrap(),
),
size: total_size,
}));
Expand Down Expand Up @@ -482,7 +485,7 @@ impl MirrorSource<'_, '_> {

let mut name = None;

let msg = self.get_human_download_url(None)?;
let msg = self.get_human_download_message(None)?;

callback(Event::DownloadEvent(oma_fetch::Event::NewProgressSpinner {
index,
Expand Down
17 changes: 13 additions & 4 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -427,10 +427,19 @@ impl From<RefreshError> for OutputError {
description: fl!("sources-list-empty"),
source: None,
},
RefreshError::DownloadFailed(_) => Self {
description: fl!("failed-refresh"),
source: None,
},
RefreshError::DownloadFailed(err) => {
if let Some(err) = err {
Self {
description: fl!("failed-refresh"),
source: Some(Box::new(OutputError::from(err))),
}
} else {
Self {
description: fl!("failed-refresh"),
source: None,
}
}
}
RefreshError::OperateFile(path, error) => Self {
description: fl!("failed-to-operate-path", p = path.display().to_string()),
source: Some(Box::new(error)),
Expand Down

0 comments on commit 31c0e8b

Please sign in to comment.