Skip to content

Commit

Permalink
Fixed a blocking issue inside an async and added sha256 caching of we…
Browse files Browse the repository at this point in the history
…bc files
  • Loading branch information
john-sharratt committed Aug 29, 2023
1 parent 6cf37b9 commit 7885976
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 12 deletions.
2 changes: 1 addition & 1 deletion lib/wasix/src/runtime/package_loader/builtin_loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ impl FileSystemCache {
async fn lookup(&self, hash: &WebcHash) -> Result<Option<Container>, Error> {
let path = self.path(hash);

match Container::from_disk(&path) {
match tokio::task::block_in_place(|| Container::from_disk(&path)) {
Ok(c) => Ok(Some(c)),
Err(ContainerError::Open { error, .. })
| Err(ContainerError::Read { error, .. })
Expand Down
5 changes: 2 additions & 3 deletions lib/wasix/src/runtime/resolver/filesystem_source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,9 @@ impl Source for FileSystemSource {
_ => return Err(QueryError::Unsupported),
};

// FIXME: These two operations will block
let webc_sha256 = WebcHash::for_file(&path)
let webc_sha256 = tokio::task::block_in_place(|| WebcHash::for_file(&path))
.with_context(|| format!("Unable to hash \"{}\"", path.display()))?;
let container = Container::from_disk(&path)
let container = tokio::task::block_in_place(|| Container::from_disk(&path))
.with_context(|| format!("Unable to parse \"{}\"", path.display()))?;

let url = crate::runtime::resolver::utils::url_from_file_path(&path)
Expand Down
25 changes: 20 additions & 5 deletions lib/wasix/src/runtime/resolver/inputs.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
use std::{
fmt::{self, Display, Formatter},
fs::File,
io::{BufRead, BufReader},
path::{Path, PathBuf},
str::FromStr,
str::FromStr, fs::File, io::{BufReader, BufRead, Read},
};

use anyhow::{Context, Error};
Expand Down Expand Up @@ -344,7 +342,19 @@ impl WebcHash {
Ok(Self(hash))
}

pub fn for_file(path: impl AsRef<Path>) -> Result<Self, std::io::Error> {
pub fn for_file(path: &PathBuf) -> Result<Self, std::io::Error> {
// check for a hash at the file location
let path_hash = path.join(".sha256");
if let Ok(mut file) = File::open(&path_hash) {
let mut hash = Vec::new();
if let Ok(amt) = file.read_to_end(&mut hash) {
if amt == 32 {
return Ok(WebcHash::from_bytes(hash[0..32].try_into().unwrap()));
}
}
}

// compute the hash
let mut hasher = Sha256::default();
let mut reader = BufReader::new(File::open(path)?);

Expand All @@ -359,7 +369,12 @@ impl WebcHash {
}

let hash = hasher.finalize().into();
Ok(WebcHash::from_bytes(hash))

// write the cache of the hash to the file system
std::fs::write(path_hash, hash).ok();
let hash = WebcHash::from_bytes(hash);

Ok(hash)
}

/// Generate a new [`WebcHash`] based on the SHA-256 hash of some bytes.
Expand Down
5 changes: 2 additions & 3 deletions lib/wasix/src/runtime/resolver/web_source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -240,13 +240,12 @@ impl Source for WebSource {
.await
.context("Unable to get the locally cached file")?;

// FIXME: this will block
let webc_sha256 = WebcHash::for_file(&local_path)
let webc_sha256 = tokio::task::block_in_place(|| WebcHash::for_file(&local_path))
.with_context(|| format!("Unable to hash \"{}\"", local_path.display()))?;

// Note: We want to use Container::from_disk() rather than the bytes
// our HTTP client gave us because then we can use memory-mapped files
let container = Container::from_disk(&local_path)
let container = tokio::task::block_in_place(|| Container::from_disk(&local_path))
.with_context(|| format!("Unable to load \"{}\"", local_path.display()))?;
let pkg = PackageInfo::from_manifest(container.manifest())
.context("Unable to determine the package's metadata")?;
Expand Down

0 comments on commit 7885976

Please sign in to comment.