Skip to content

Commit

Permalink
Fix Windows error with HTTP components
Browse files Browse the repository at this point in the history
Signed-off-by: itowlson <[email protected]>
  • Loading branch information
itowlson committed Dec 11, 2023
1 parent 4847ced commit 24d8a43
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 3 deletions.
39 changes: 37 additions & 2 deletions crates/loader/src/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,12 +91,12 @@ impl Cache {

/// The path of contents in the cache's wasm directory, which may or may not exist.
pub fn wasm_path(&self, digest: impl AsRef<str>) -> PathBuf {
self.wasm_dir().join(digest.as_ref())
self.wasm_dir().join(safe_name(digest).as_ref())
}

/// The path of contents in the cache's wasm directory, which may or may not exist.
pub fn data_path(&self, digest: impl AsRef<str>) -> PathBuf {
self.data_dir().join(digest.as_ref())
self.data_dir().join(safe_name(digest).as_ref())
}

/// Ensure the expected configuration directories are found in the root.
Expand Down Expand Up @@ -132,3 +132,38 @@ impl Cache {
Ok(())
}
}

#[cfg(windows)]
fn safe_name(digest: impl AsRef<str>) -> impl AsRef<Path> {
digest.as_ref().replace(':', "_")
}

#[cfg(not(windows))]
fn safe_name(digest: impl AsRef<str>) -> impl AsRef<str> {
digest
}

#[cfg(test)]
mod test {
use spin_common::sha256::hex_digest_from_bytes;

use super::*;

#[tokio::test]
async fn accepts_prefixed_digests() -> anyhow::Result<()> {
let temp_dir = tempfile::tempdir()?;
let cache = Cache::new(Some(temp_dir.path().to_owned())).await?;

let wasm = "Wasm".as_bytes();
let digest = format!("sha256:{}", hex_digest_from_bytes(wasm));
cache.write_wasm(wasm, &digest).await?;
assert_eq!(wasm, std::fs::read(cache.wasm_path(&digest))?);

let data = "hello".as_bytes();
let digest = format!("sha256:{}", hex_digest_from_bytes(data));
cache.write_data(wasm, &digest).await?;
assert_eq!(data, std::fs::read(cache.data_path(&digest))?);

Ok(())
}
}
4 changes: 3 additions & 1 deletion crates/loader/src/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@ pub async fn verified_download(url: &str, digest: &str, dest: &Path) -> Result<(
);

// Move to final destination
temp_path.persist_noclobber(dest)?;
temp_path
.persist_noclobber(dest)
.with_context(|| format!("Failed to save download from {url} to {}", dest.display()))?;

Ok(())
}

0 comments on commit 24d8a43

Please sign in to comment.