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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
use std::io::{self, ErrorKind, Result};
use std::{
    ffi::OsString,
    path::{Component, Path, PathBuf},
    sync::Arc,
};

use super::fs::{
    has_parent, resolve, resolve_parent, root_fs_mut, MemoryFd, Parent,
    PathTarget,
};

/// A builder for creating directories in various manners.
#[derive(Debug, Default)]
pub struct DirBuilder {
    /// Indicates whether to create parent directories
    /// if they are missing.
    recursive: bool,
}

impl DirBuilder {
    /// Creates a new set of options with default
    /// mode/security settings for all platforms and also non-recursive.
    pub fn new() -> Self {
        Default::default()
    }

    /// Indicates whether to create directories
    /// recursively (including all parent directories).
    /// Parents that do not exist are created with the
    /// same security and permissions settings.
    ///
    /// This option defaults to `false`.
    pub fn recursive(&mut self, recursive: bool) -> &mut Self {
        self.recursive = recursive;
        self
    }

    /// Creates the specified directory with the configured options.
    ///
    /// It is considered an error if the directory already exists unless
    /// recursive mode is enabled.
    ///
    /// # Errors
    ///
    /// An error will be returned under the following circumstances:
    ///
    /// * Path already points to an existing file.
    /// * Path already points to an existing directory and the mode is
    ///   non-recursive.
    /// * The calling process doesn't have permissions to create the directory
    ///   or its missing parents.
    /// * Other I/O error occurred.
    pub async fn create(&self, path: impl AsRef<Path>) -> Result<()> {
        if self.recursive {
            let mut current = Parent::Root(root_fs_mut());
            let mut buf = PathBuf::new();
            let mut file_name: Option<OsString> = None;
            for component in path.as_ref().components() {
                match component {
                    Component::RootDir => {
                        continue;
                    }
                    Component::Normal(name) => {
                        buf = buf.join(name);
                        file_name = Some(name.to_owned());
                    }
                    _ => unimplemented!(),
                }

                // Got a directory to make
                if let Some(file_name) = file_name {
                    // See if a folder already exists at the location
                    if let Some(target) = resolve(&buf).await {
                        match target {
                            PathTarget::Descriptor(parent) => {
                                let fd = parent.read().await;
                                match &*fd {
                                    MemoryFd::Dir(_dir) => {
                                        current = Parent::Folder(Arc::clone(
                                            &parent,
                                        ));
                                    }
                                    _ => {
                                        return Err(
                                            ErrorKind::PermissionDenied
                                                .into(),
                                        )
                                    }
                                }
                            }
                            PathTarget::Root(_) => {
                                return Err(ErrorKind::PermissionDenied.into())
                            }
                        }
                    } else {
                        let fd = current.mkdir(file_name).await?;
                        current = Parent::Folder(fd);
                    }
                }
            }
            Ok(())
        } else {
            let file_name = path.as_ref().file_name().ok_or_else(|| {
                let err: io::Error = ErrorKind::PermissionDenied.into();
                err
            })?;

            let has_parent = has_parent(path.as_ref());
            if has_parent {
                if let Some(target) = resolve_parent(path.as_ref()).await {
                    match target {
                        PathTarget::Descriptor(parent) => {
                            let is_dir = {
                                let fd = parent.read().await;
                                matches!(&*fd, MemoryFd::Dir(_))
                            };
                            if is_dir {
                                Parent::Folder(parent)
                                    .mkdir(file_name.to_owned())
                                    .await?;
                            } else {
                                return Err(
                                    ErrorKind::PermissionDenied.into()
                                );
                            }
                        }
                        PathTarget::Root(fs) => {
                            Parent::Root(fs)
                                .mkdir(file_name.to_owned())
                                .await?;
                        }
                    }
                    Ok(())
                } else {
                    Err(ErrorKind::NotFound.into())
                }
            } else {
                Parent::Root(root_fs_mut())
                    .mkdir(file_name.to_owned())
                    .await?;
                Ok(())
            }
        }
    }
}

/// Creates a new, empty directory at the provided path.
pub async fn create_dir(path: impl AsRef<Path>) -> Result<()> {
    DirBuilder::new().create(path).await
}

/// Recursively creates a directory and all of its parent
/// components if they are missing.
pub async fn create_dir_all(path: impl AsRef<Path>) -> Result<()> {
    DirBuilder::new().recursive(true).create(path).await
}