Skip to content

Commit

Permalink
feat(oma-refresh): support Acquire-By-Hash
Browse files Browse the repository at this point in the history
  • Loading branch information
eatradish committed May 24, 2024
1 parent eef5160 commit f95a988
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 10 deletions.
34 changes: 24 additions & 10 deletions oma-refresh/src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ pub enum RefreshError {
FailedToOperateDirOrFile(String, tokio::io::Error),
#[error("Failed to parse InRelease file: {0}")]
InReleaseParseError(String, InReleaseParserError),
#[error("No checksum")]
NoChecksum,
}

#[cfg(not(feature = "aosc"))]
Expand Down Expand Up @@ -553,6 +555,7 @@ impl<'a> OmaRefresh<'a> {
&self.download_dir,
&mut tasks,
m,
inrelease.acquire_by_hash,
)?;
}
}
Expand Down Expand Up @@ -580,6 +583,7 @@ fn collect_download_task(
download_dir: &Path,
tasks: &mut Vec<DownloadEntry>,
m: &Option<HashMap<String, MirrorMapItem>>,
acquire_by_hash: bool,
) -> Result<()> {
let (typ, not_compress_filename_before) = match &c.file_type {
DistFileType::CompressContents(s) => ("Contents", s),
Expand All @@ -592,22 +596,13 @@ fn collect_download_task(

let msg = human_download_url(&source_index.dist_path, m)?;

let dist_url = source_index.dist_path.clone();
let download_url = format!("{}/{}", dist_url, c.name);

let file_path = if let DistFileType::CompressContents(_) = c.file_type {
download_url.clone()
} else {
format!("{}/{}", dist_url, not_compress_filename_before)
};
let dist_url = &source_index.dist_path;

let from = match source_index.from {
OmaSourceEntryFrom::Http => DownloadSourceType::Http,
OmaSourceEntryFrom::Local => DownloadSourceType::Local,
};

let sources = vec![DownloadSource::new(download_url.clone(), from)];

let checksum = if matches!(c.file_type, DistFileType::CompressContents(_)) {
Some(&c.checksum)
} else {
Expand All @@ -618,6 +613,25 @@ fn collect_download_task(
.map(|c| &c.checksum)
};

let download_url = if acquire_by_hash {
let checksum = checksum.ok_or_else(|| RefreshError::NoChecksum)?;
let path = Path::new(&c.name);
let parent = path.parent().unwrap_or(path);
let path = parent.join("by-hash").join("SHA256").join(checksum);

format!("{}/{}", dist_url, path.display())
} else {
format!("{}/{}", dist_url, c.name)
};

let sources = vec![DownloadSource::new(download_url.clone(), from)];

let file_path = if let DistFileType::CompressContents(_) = c.file_type {
download_url.clone()
} else {
format!("{}/{}", dist_url, not_compress_filename_before)
};

let mut task = DownloadEntryBuilder::default();
task.source(sources);
task.filename(database_filename(&file_path).into());
Expand Down
7 changes: 7 additions & 0 deletions oma-refresh/src/inrelease.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use crate::verify;
pub struct InReleaseParser {
_source: Vec<SmallMap<16, String, String>>,
pub checksums: SmallVec<[ChecksumItem; 32]>,
pub acquire_by_hash: bool,
}

#[derive(Debug, Clone, PartialEq, Eq)]
Expand Down Expand Up @@ -128,6 +129,11 @@ impl InReleaseParser {
}
}

let acquire_by_hash = source_first
.and_then(|x| x.get("Acquire-By-Hash"))
.map(|x| x.to_lowercase() == "yes")
.unwrap_or(false);

let sha256 = source_first
.and_then(|x| x.get("SHA256"))
.take()
Expand Down Expand Up @@ -220,6 +226,7 @@ impl InReleaseParser {
Ok(Self {
_source: source,
checksums: res,
acquire_by_hash,
})
}
}
Expand Down
4 changes: 4 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,10 @@ impl From<RefreshError> for OutputError {
description: fl!("failed-to-operate-path", p = path),
source: Some(Box::new(e)),
},
RefreshError::NoChecksum => Self {
description: value.to_string(),
source: None,
},
}
#[cfg(not(feature = "aosc"))]
match value {
Expand Down

0 comments on commit f95a988

Please sign in to comment.