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 support for local repodata #158

Merged
merged 1 commit into from
Apr 11, 2023
Merged
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
102 changes: 87 additions & 15 deletions crates/rattler_repodata_gateway/src/fetch/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,54 @@ pub enum CacheResult {
CacheNotPresent,
}

/// handle file:/// urls
async fn repodata_from_file(
subdir_url: Url,
out_path: PathBuf,
cache_state_path: PathBuf,
lock_file: LockedFile,
) -> Result<CachedRepoData, FetchRepoDataError> {
// copy file from subdir_url to out_path
tokio::fs::copy(&subdir_url.to_file_path().unwrap(), &out_path)
.await
.map_err(FetchRepoDataError::FailedToDownloadRepoData)?;

// create a dummy cache state
let new_cache_state = RepoDataState {
url: subdir_url.clone(),
cache_size: tokio::fs::metadata(&out_path)
.await
.map_err(FetchRepoDataError::FailedToDownloadRepoData)?
.len(),
cache_headers: CacheHeaders {
etag: None,
last_modified: None,
cache_control: None,
},
cache_last_modified: SystemTime::now(),
blake2_hash: None,
has_zst: None,
has_bz2: None,
has_jlap: None,
};

// write the cache state
let new_cache_state = tokio::task::spawn_blocking(move || {
new_cache_state
.to_path(&cache_state_path)
.map(|_| new_cache_state)
.map_err(FetchRepoDataError::FailedToWriteCacheState)
})
.await??;

Ok(CachedRepoData {
lock_file,
repo_data_json_path: out_path.to_path_buf(),
cache_state: new_cache_state,
cache_result: CacheResult::CacheHit,
})
}

/// Fetch the repodata.json file for the given subdirectory. The result is cached on disk using the
/// HTTP cache headers returned from the server.
///
Expand Down Expand Up @@ -229,8 +277,21 @@ pub async fn fetch_repo_data(
.await?
.map_err(FetchRepoDataError::FailedToAcquireLock)?;

let cache_action = if subdir_url.scheme() == "file" {
// If we are dealing with a local file, we can skip the cache entirely.
return repodata_from_file(
subdir_url.join(options.variant.file_name()).unwrap(),
repo_data_json_path,
cache_state_path,
lock_file,
)
.await;
} else {
options.cache_action
};

// Validate the current state of the cache
let cache_state = if options.cache_action != CacheAction::NoCache {
let cache_state = if cache_action != CacheAction::NoCache {
let owned_subdir_url = subdir_url.clone();
let owned_cache_path = cache_path.to_owned();
let owned_cache_key = cache_key.clone();
Expand Down Expand Up @@ -623,23 +684,34 @@ pub async fn check_variant_availability(
async fn check_valid_download_target(url: &Url, client: &Client) -> bool {
tracing::debug!("checking availability of '{url}'");

// Otherwise, perform a HEAD request to determine whether the url seems valid.
match client.head(url.clone()).send().await {
Ok(response) => {
if response.status().is_success() {
tracing::debug!("'{url}' seems to be available");
true
} else {
tracing::debug!("'{url}' seems to be unavailable");
if url.scheme() == "file" {
// If the url is a file url we can simply check if the file exists.
let path = url.to_file_path().unwrap();
let exists = tokio::fs::metadata(path).await.is_ok();
tracing::debug!(
"'{url}' seems to be {}",
if exists { "available" } else { "unavailable" }
);
exists
} else {
// Otherwise, perform a HEAD request to determine whether the url seems valid.
match client.head(url.clone()).send().await {
Ok(response) => {
if response.status().is_success() {
tracing::debug!("'{url}' seems to be available");
true
} else {
tracing::debug!("'{url}' seems to be unavailable");
false
}
}
Err(e) => {
tracing::warn!(
"failed to perform HEAD request on '{url}': {e}. Assuming its unavailable.."
);
false
}
}
Err(e) => {
tracing::warn!(
"failed to perform HEAD request on '{url}': {e}. Assuming its unavailable.."
);
false
}
}
}

Expand Down