1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
use anyhow::Context;
use wasmer_config::package::{PackageHash, PackageId, PackageSource};
use wasmer_package::utils::from_disk;

use crate::runtime::resolver::{
    DistributionInfo, PackageInfo, PackageSummary, QueryError, Source, WebcHash,
};

/// A [`Source`] that knows how to query files on the filesystem.
#[derive(Debug, Default, Clone, PartialEq, Eq)]
pub struct FileSystemSource {}

impl FileSystemSource {
    async fn load_path(path: &std::path::Path) -> Result<Vec<PackageSummary>, anyhow::Error> {
        let webc_sha256 = crate::block_in_place(|| WebcHash::for_file(path))
            .with_context(|| format!("Unable to hash \"{}\"", path.display()))?;
        let container = crate::block_in_place(|| from_disk(path))
            .with_context(|| format!("Unable to parse \"{}\"", path.display()))?;

        let url = crate::runtime::resolver::utils::url_from_file_path(path)
            .ok_or_else(|| anyhow::anyhow!("Unable to turn \"{}\" into a URL", path.display()))?;

        let id = PackageInfo::package_id_from_manifest(container.manifest())
            .context("Unable to determine the package's ID")?
            .unwrap_or_else(|| PackageId::from(PackageHash::from_sha256_bytes(webc_sha256.0)));

        let pkg = PackageInfo::from_manifest(id, container.manifest(), container.version())
            .context("Unable to determine the package's metadata")?;
        let summary = PackageSummary {
            pkg,
            dist: DistributionInfo {
                webc: url,
                webc_sha256,
            },
        };

        Ok(vec![summary])
    }
}

#[async_trait::async_trait]
impl Source for FileSystemSource {
    #[tracing::instrument(level = "debug", skip_all, fields(%package))]
    async fn query(&self, package: &PackageSource) -> Result<Vec<PackageSummary>, QueryError> {
        let path = match package {
            PackageSource::Path(path) => {
                let path = std::path::PathBuf::from(path);
                path.canonicalize()
                    .with_context(|| {
                        format!(
                            "Unable to get the canonical form for \"{}\"",
                            path.display()
                        )
                    })
                    .map_err(|error| QueryError::new_other(error, package))?
            }
            _ => {
                return Err(QueryError::Unsupported {
                    query: package.clone(),
                })
            }
        };

        Self::load_path(&path)
            .await
            .map_err(|error| QueryError::new_other(error, package))
    }
}