Skip to main content

cosmwasm_vm/
filesystem.rs

1use std::{fs::create_dir_all, path::Path};
2
3#[derive(Debug)]
4pub struct MkdirPFailure;
5
6/// An implementation for `mkdir -p`.
7///
8/// This is a thin wrapper around fs::create_dir_all that
9/// hides all OS specific error messages to ensure they don't end up
10/// breaking consensus.
11pub fn mkdir_p(path: &Path) -> Result<(), MkdirPFailure> {
12    create_dir_all(path).map_err(|_e| MkdirPFailure)
13}
14
15#[cfg(test)]
16mod tests {
17    use tempfile::TempDir;
18
19    use super::*;
20
21    #[test]
22    fn mkdir_p_works() {
23        let tmp_root = TempDir::new().unwrap();
24
25        // Can create
26        let path = tmp_root.path().join("something");
27        assert!(!path.is_dir());
28        mkdir_p(&path).unwrap();
29        assert!(path.is_dir());
30
31        // Can be called on existing dir
32        let path = tmp_root.path().join("something else");
33        assert!(!path.is_dir());
34        mkdir_p(&path).unwrap();
35        assert!(path.is_dir());
36        mkdir_p(&path).unwrap(); // no-op
37        assert!(path.is_dir());
38
39        // Fails for dir with null
40        let path = tmp_root.path().join("something\0with NULL");
41        mkdir_p(&path).unwrap_err();
42    }
43}