Skip to content

Commit

Permalink
updatehub: rework download so it is easier to read and avoid duplicated
Browse files Browse the repository at this point in the history
Signed-off-by: Otavio Salvador <otavio@ossystems.com.br>
  • Loading branch information
otavio committed Mar 8, 2024
1 parent 8c6440e commit 93f65c3
Showing 1 changed file with 41 additions and 28 deletions.
69 changes: 41 additions & 28 deletions updatehub/src/states/download.rs
Expand Up @@ -27,40 +27,53 @@ impl Download {
context: &Mutex<&mut Context>,
) -> Result<()> {
let installation_set =
installation_set::inactive().log_error_msg("unable to get current isntall set")?;
installation_set::inactive().log_error_msg("unable to get current installation set")?;
let download_dir = context.lock().await.settings.update.download_dir.to_owned();

update_package
.clear_unrelated_files(&download_dir, installation_set, &context.lock().await.settings)
.log_error_msg("failed to cleanup files unrelated to current update")?;

// Get shasums of missing or incomplete objects
let shasum_list: Vec<_> = update_package
.objects(installation_set)
.iter()
.filter_map(|o| {
let name = o.filename();
if o.allow_remote_install() {
trace!("Skiping download for {} as it can be installed without download", name);
return None;
}
let shasum = o.sha256sum();
let obj_status = o
.status(&download_dir)
.map_err(|e| {
error!("fail accessing the object: {} ({}) (err: {})", name, shasum, e)
})
.unwrap_or(object::info::Status::Missing);
if obj_status == object::info::Status::Missing
|| obj_status == object::info::Status::Incomplete
{
Some((name.to_owned(), shasum.to_owned()))
} else {
debug!("skiping object: {} ({})", name, shasum);
None
}
})
.collect();
let shasum_list = {
let mut objects: Vec<_> = update_package
.objects(installation_set)
.iter()
.filter_map(|o| {
if o.allow_remote_install() {
trace!(
"skip download for {} as it can be installed without download",
o.filename()
);
return None;
}

match (o.filename(), o.sha256sum(), o.status(&download_dir)) {
(filename, sha256sum, Err(err)) => {
error!(
"fail accessing the object: {} ({}) (err: {})",
filename, sha256sum, err
);

Some((filename, sha256sum))
}

(filename, sha256sum, Ok(object::info::Status::Missing))
| (filename, sha256sum, Ok(object::info::Status::Incomplete))
| (filename, sha256sum, Ok(object::info::Status::Corrupted)) => {
Some((filename, sha256sum))
}

(_, _, Ok(object::info::Status::Ready)) => None,
}
})
.collect();

// Remove duplicated objects to avoid duplicated downloads
objects.dedup();

objects
};

trace!("the following objects are missing: {:?}", shasum_list);

Expand All @@ -74,7 +87,7 @@ impl Download {
&product_uid,
&update_package.package_uid(),
&download_dir,
&shasum,
shasum,
)
.await
.log_error_msg("failed to download object from update package")?;
Expand Down

0 comments on commit 93f65c3

Please sign in to comment.