Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add HTTP support to SparseIndex #119

Merged
merged 5 commits into from
Jun 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.txt text eol=lf
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
target
Cargo.lock
_test

# May be created during tests, but should not be present
tests/testdata/sparse_registry_cache/cargo_home/registry/index/index.crates.io-6f17d22bba15001f/.cache/cr/at/crates-index
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ rust-version = "1.60"
git2 = { version = ">=0.16, <=0.17", default-features = false }
hex = { version = "0.4.3", features = ["serde"] }
home = "0.5.4"
http = { version = "0.2", optional = true }
memchr = "2.5.0"
rayon = { version = "1.7.0", optional = true }
rustc-hash = "1.1.0"
Expand All @@ -39,6 +40,7 @@ https = ["git2/https"]
parallel = ["dep:rayon"]
vendored-openssl = ["git2/vendored-openssl"]
ssh = ["git2/ssh"]
sparse-http = ["http"]

[badges]
maintenance = { status = "actively-developed" }
2 changes: 1 addition & 1 deletion src/bare_index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ impl Index {
/// to read majority of crates, prefer the [`Index::crates()`] iterator.
#[must_use]
pub fn crate_(&self, name: &str) -> Option<Crate> {
let rel_path = crate::crate_name_to_relative_path(name)?;
let rel_path = crate::crate_name_to_relative_path(name, None)?;

// Attempt to load the .cache/ entry first, this is purely an acceleration
// mechanism and can fail for a few reasons that are non-fatal
Expand Down
2 changes: 1 addition & 1 deletion src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ fn try_read_toml(path: &Path) -> Result<Option<toml::Value>, Error> {
return Ok(None);
}

let toml = toml::from_str(&std::fs::read_to_string(&path)?).map_err(Error::Toml)?;
let toml = toml::from_str(&std::fs::read_to_string(path)?).map_err(Error::Toml)?;
Ok(Some(toml))
}

Expand Down
29 changes: 26 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -314,10 +314,11 @@ fn crate_prefix(accumulator: &mut String, crate_name: &str, separator: char) ->
Some(())
}

fn crate_name_to_relative_path(crate_name: &str) -> Option<String> {
fn crate_name_to_relative_path(crate_name: &str, separator: Option<char>) -> Option<String> {
let separator = separator.unwrap_or(std::path::MAIN_SEPARATOR);
let mut rel_path = String::with_capacity(crate_name.len() + 6);
crate_prefix(&mut rel_path, crate_name, std::path::MAIN_SEPARATOR)?;
rel_path.push(std::path::MAIN_SEPARATOR);
crate_prefix(&mut rel_path, crate_name, separator)?;
rel_path.push(separator);
rel_path.extend(crate_name.as_bytes().iter().map(|c| c.to_ascii_lowercase() as char));

Some(rel_path)
Expand Down Expand Up @@ -454,6 +455,28 @@ impl Crate {
})
}

/// Writes a cache entry to disk in the same format as cargo
#[cfg(feature = "sparse-http")]
pub(crate) fn write_cache_entry(&self, path: &Path, version: &str) -> io::Result<()> {
const CURRENT_CACHE_VERSION: u8 = 3;
const CURRENT_INDEX_FORMAT_VERSION: u32 = 2;

let mut v = Vec::new();
v.push(CURRENT_CACHE_VERSION);
v.extend_from_slice(&CURRENT_INDEX_FORMAT_VERSION.to_le_bytes());
v.extend_from_slice(version.as_bytes());
v.push(0);

for version in self.versions() {
v.extend_from_slice(version.version().as_bytes());
v.push(0);
v.append(&mut serde_json::to_vec(version).unwrap());
v.push(0);
}

std::fs::write(path, v)
}

/// All versions of this crate sorted chronologically by date originally published
///
/// Warning: may be yanked or duplicate
Expand Down
Loading