Skip to content

Commit

Permalink
Retrieve crate version from cargo-registry package (#33867)
Browse files Browse the repository at this point in the history
  • Loading branch information
pgarg66 authored Oct 25, 2023
1 parent e555a61 commit 70107e2
Showing 1 changed file with 31 additions and 2 deletions.
33 changes: 31 additions & 2 deletions cargo-registry/src/crate_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,33 @@ impl CrateTarGz {

Self::new(unpacked)
}

fn version(&self) -> String {
let decoder = GzDecoder::new(self.0.as_ref());
let mut archive = Archive::new(decoder);

if let Some(Ok(entry)) = archive
.entries()
.ok()
.and_then(|mut entries| entries.nth(0))
{
if let Ok(path) = entry.path() {
if let Some(path_str) = path.to_str() {
if let Some((_, vers)) = path_str.rsplit_once('-') {
let mut version = vers.to_string();
// Removing trailing '/'
if version.ends_with('/') {
version.pop();
}
return version;
}
}
}
}

// Placeholder version.
"0.1.0".to_string()
}
}

pub(crate) struct CratePackage(pub(crate) Bytes);
Expand Down Expand Up @@ -325,7 +352,7 @@ impl UnpackedCrate {
}

pub(crate) fn fetch_index(id: Pubkey, client: Arc<Client>) -> Result<IndexEntry, Error> {
let (packed_crate, meta) = Self::fetch(id, "0.1.0", client)?;
let (packed_crate, meta) = Self::fetch(id, "", client)?;
let mut entry: IndexEntry = meta.into();
entry.cksum = format!("{:x}", Sha256::digest(&packed_crate.0));
Ok(entry)
Expand All @@ -343,9 +370,11 @@ impl UnpackedCrate {
// Decompile the program
// Generate a Cargo.toml

let meta = crate_obj.meta.clone();
let mut meta = crate_obj.meta.clone();

if APPEND_CRATE_TO_ELF {
let version = program.crate_bytes.version();
meta.vers = version;
Ok((program.crate_bytes, meta))
} else {
CrateTarGz::new(crate_obj).map(|file| (file, meta))
Expand Down

0 comments on commit 70107e2

Please sign in to comment.