Skip to content
Open
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
32 changes: 19 additions & 13 deletions src/crates.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ pub async fn sync_one_crate_entry(
)
};

let file_path = get_crate_path(path, &crate_entry.name, &crate_entry.vers)
let file_path = get_crate_file_for_version(path, &crate_entry.name, &crate_entry.vers)
.ok_or_else(|| DownloadError::BadCrate(crate_entry.name.clone()))?;

download(
Expand Down Expand Up @@ -181,7 +181,9 @@ pub async fn sync_crates_files(
if oid.is_zero() {
// The crate was removed, continue to next crate.
// Note that this does not include yanked crates.
removed_crates.push(p.to_path_buf());
if let Some(crate_name) = p.file_name().and_then(|x| x.to_str()) {
removed_crates.push(crate_name.to_owned());
}
return true;
}
let blob = repo.find_blob(oid).unwrap();
Expand Down Expand Up @@ -297,8 +299,10 @@ pub async fn sync_crates_files(

// Delete any removed crates
for rc in removed_crates {
// Try to remove the file, but ignore it if it doesn't exist
let _ = fs::remove_file(repo_path.join(rc));
// Try to remove the crate directory, but ignore it if it doesn't exist
if let Some(crate_dir) = get_crate_directory(path, &rc) {
let _ = fs::remove_dir_all(crate_dir);
}
}

// Set master to origin/master.
Expand Down Expand Up @@ -342,11 +346,7 @@ pub fn is_new_crates_format(path: &Path) -> Result<bool, io::Error> {
Ok(true)
}

pub fn get_crate_path(
mirror_path: &Path,
crate_name: &str,
crate_version: &str,
) -> Option<PathBuf> {
pub fn get_crate_directory(mirror_path: &Path, crate_name: &str) -> Option<PathBuf> {
let crate_path = match crate_name.len() {
1 => PathBuf::from("1"),
2 => PathBuf::from("2"),
Expand All @@ -362,11 +362,17 @@ pub fn get_crate_path(
_ => return None,
};

Some(mirror_path.join("crates").join(crate_path).join(crate_name))
}

pub fn get_crate_file_for_version(
mirror_path: &Path,
crate_name: &str,
crate_version: &str,
) -> Option<PathBuf> {
let crate_dir = get_crate_directory(mirror_path, crate_name)?;
Some(
mirror_path
.join("crates")
.join(crate_path)
.join(crate_name)
crate_dir
.join(crate_version)
.join(format!("{crate_name}-{crate_version}.crate")),
)
Expand Down
6 changes: 3 additions & 3 deletions src/serve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use warp::{
Filter, Rejection, Stream,
};

use crate::crates::get_crate_path;
use crate::crates::get_crate_file_for_version;

pub struct TlsConfig {
pub cert_path: PathBuf,
Expand Down Expand Up @@ -256,8 +256,8 @@ async fn get_crate_file(
name: &str,
version: &str,
) -> Result<Response<Body>, Rejection> {
let full_path =
get_crate_path(&mirror_path, name, version).ok_or_else(warp::reject::not_found)?;
let full_path = get_crate_file_for_version(&mirror_path, name, version)
.ok_or_else(warp::reject::not_found)?;

let file = File::open(full_path)
.await
Expand Down
10 changes: 7 additions & 3 deletions src/verify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use warp::http::HeaderValue;

use crate::{
crates::{
cargo_lock_to_mirror_entries, get_crate_path, sync_one_crate_entry,
cargo_lock_to_mirror_entries, get_crate_file_for_version, sync_one_crate_entry,
vendor_path_to_mirror_entries, CrateEntry,
},
download::DownloadError,
Expand Down Expand Up @@ -226,8 +226,12 @@ pub(crate) async fn verify_mirror(
}

// Building crates local path.
let file_path =
get_crate_path(&path, crate_entry.get_name(), crate_entry.get_vers()).unwrap();
let file_path = get_crate_file_for_version(
&path,
crate_entry.get_name(),
crate_entry.get_vers(),
)
.unwrap();

// Checking if crate is missing.
if !CRATES_403
Expand Down