spin_factor_wasi/
spin.rs

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
use std::path::PathBuf;

use spin_common::{ui::quoted_path, url::parse_file_url};
use spin_factors::anyhow::{ensure, Context};

use crate::FilesMounter;

pub struct SpinFilesMounter {
    working_dir: PathBuf,
    allow_transient_writes: bool,
}

impl SpinFilesMounter {
    pub fn new(working_dir: impl Into<PathBuf>, allow_transient_writes: bool) -> Self {
        Self {
            working_dir: working_dir.into(),
            allow_transient_writes,
        }
    }
}

impl FilesMounter for SpinFilesMounter {
    fn mount_files(
        &self,
        app_component: &spin_factors::AppComponent,
        mut ctx: crate::MountFilesContext,
    ) -> spin_factors::anyhow::Result<()> {
        for content_dir in app_component.files() {
            let source_uri = content_dir
                .content
                .source
                .as_deref()
                .with_context(|| format!("Missing 'source' on files mount {content_dir:?}"))?;
            let source_path = self.working_dir.join(parse_file_url(source_uri)?);
            ensure!(
                source_path.is_dir(),
                "SpinFilesMounter only supports directory mounts; {} is not a directory",
                quoted_path(&source_path),
            );
            let guest_path = &content_dir.path;
            let guest_path = guest_path
                .to_str()
                .with_context(|| format!("guest path {guest_path:?} not valid UTF-8"))?;
            ctx.preopened_dir(source_path, guest_path, self.allow_transient_writes)?;
        }
        Ok(())
    }
}