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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
use std::path::{Path, PathBuf};
use thiserror::Error;
use tracing::{error, info, warn};

#[derive(Error, Debug)]
pub enum RunDirError {
    #[error("Unable to initialize rundir.")]
    Initialize { path: PathBuf },
    #[error("The path provided is not a directory.")]
    PathIsNotDir { path: PathBuf },
    #[error("The directory is not empty.")]
    DirIsNotEmpty { path: PathBuf, child_path: PathBuf },
    #[error("Unable to create directory: {}", path.display())]
    CreateDir {
        path: PathBuf,
        #[source]
        source: std::io::Error,
    },
    #[error("Unable to scan directory: {}", path.display())]
    ScanDir {
        path: PathBuf,
        #[source]
        source: std::io::Error,
    },
    #[error("Unable to recreate directory: {}", path.display())]
    RecreateDir {
        path: PathBuf,
        #[source]
        source: std::io::Error,
    },
    #[error("Unable to remove subdirectory: {}", path.display())]
    RemoveSubDir {
        path: PathBuf,
        #[source]
        source: std::io::Error,
    },
    #[error("Invalid subdirectory name: {}", name)]
    InvalidSubDirName {
        name: String,
        inner_error: Option<std::io::Error>,
    },
}

type Result<T, E = RunDirError> = std::result::Result<T, E>;

pub struct RunDir {
    path: PathBuf,
    allow_cleaning: bool,
}

impl RunDir {
    pub fn new<T: Into<PathBuf>>(path: T) -> RunDir {
        RunDir {
            path: path.into(),
            allow_cleaning: false,
        }
    }

    /// Set whether the directory can perform cleanup operations.
    ///
    /// Use with caution. This will clear existing directories on initialization
    /// and cleanup.
    pub fn allow_cleaning(mut self, allow_cleaning: bool) -> RunDir {
        self.allow_cleaning = allow_cleaning;

        self
    }

    /// Creates the initial RunDir.
    ///
    /// # Examples
    ///
    /// ```
    /// use collective::rundir::RunDir;
    ///
    /// let rundir = RunDir::new("tests/rundir").allow_cleaning(true);
    ///
    /// rundir.initialize().unwrap();
    ///
    /// rundir.cleanup().unwrap();
    /// ```
    pub fn initialize(&self) -> Result<()> {
        if Path::exists(&self.path) {
            info!("RunDir already exists: {}", self.path.display());

            if !Path::is_dir(&self.path) {
                error!("RunDir is not a directory: {}", self.path.display());

                return Err(RunDirError::PathIsNotDir {
                    path: self.path.clone(),
                });
            } else {
                // Scan dir
                let mut dir_iterator =
                    std::fs::read_dir(&self.path).map_err(|source| RunDirError::ScanDir {
                        path: self.path.clone(),
                        source,
                    })?;

                let existing_child_path = dir_iterator
                    .next()
                    .map(|entry_result| entry_result.map(|entry| entry.path()));

                if let Some(child_path) = existing_child_path {
                    let child_path = child_path.map_err(|source| RunDirError::ScanDir {
                        path: self.path.clone(),
                        source,
                    })?;

                    if self.allow_cleaning {
                        info!("Recreating RunDir.");

                        std::fs::remove_dir_all(&self.path).map_err(|source| {
                            RunDirError::RecreateDir {
                                path: self.path.clone(),
                                source,
                            }
                        })?;

                        std::fs::create_dir_all(&self.path).map_err(|source| {
                            RunDirError::RecreateDir {
                                path: self.path.clone(),
                                source,
                            }
                        })?;
                    } else {
                        return Err(RunDirError::DirIsNotEmpty {
                            path: self.path.clone(),
                            child_path,
                        });
                    }
                }
            }
        } else {
            info!("Creating new RunDir: {}", self.path.display());

            std::fs::create_dir_all(&self.path).map_err(|source| RunDirError::CreateDir {
                path: self.path.clone(),
                source,
            })?
        }

        Ok(())
    }

    pub fn cleanup(&self) -> Result<()> {
        if self.allow_cleaning {
            std::fs::remove_dir_all(&self.path).map_err(|source| RunDirError::RecreateDir {
                path: self.path.clone(),
                source,
            })?;

            info!("Cleaned up RunDir: {}", self.path.display());
        } else {
            warn!("Leaving RunDir unmodified. Manual cleanup may be needed.");
        }

        Ok(())
    }

    /// Creates a subdir within the RunDir.
    pub fn create_subdir(&self, name: &str) -> Result<PathBuf> {
        let pathbuf = self.validate_subdir_name(name)?;

        std::fs::create_dir(&pathbuf).map_err(|source| RunDirError::CreateDir {
            path: pathbuf.clone(),
            source,
        })?;

        Ok(pathbuf)
    }

    /// Removes a subdir and all its contents from the RunDir.
    pub fn remove_subdir_all(&self, name: &str) -> Result<()> {
        let pathbuf = self.validate_subdir_name(name)?;

        std::fs::remove_dir_all(&pathbuf).map_err(|source| RunDirError::RemoveSubDir {
            path: pathbuf,
            source,
        })?;

        Ok(())
    }

    /// Checks if a subdir exists within the RunDir.
    pub fn subdir_exists(&self, name: &str) -> Result<bool> {
        let pathbuf = self.validate_subdir_name(name)?;

        Ok(pathbuf.exists())
    }

    fn validate_subdir_name(&self, name: &str) -> Result<PathBuf> {
        // Check that the name results in a dir exactly one level below the current one.
        let mut pathbuf = self.path.clone();

        pathbuf.push(name);

        if let Some(parent) = pathbuf.parent() {
            if parent != self.path.as_path() {
                return Err(RunDirError::InvalidSubDirName {
                    name: String::from(name),
                    inner_error: None,
                });
            }
        } else {
            return Err(RunDirError::InvalidSubDirName {
                name: String::from(name),
                inner_error: None,
            });
        }

        Ok(pathbuf)
    }
}

impl AsRef<Path> for RunDir {
    fn as_ref(&self) -> &Path {
        &self.path
    }
}

#[cfg(test)]
mod tests {
    use super::{RunDir, RunDirError};
    use std::path::PathBuf;

    #[test]
    fn test_initialize() {
        // Create dir for the first time.
        let result = RunDir::new("tests/rundir").initialize();

        assert!(result.is_ok());

        // Use existing empty dir.
        let result = RunDir::new("tests/rundir").initialize();

        assert!(result.is_ok());

        // Fail when dir is not empty and allow_cleaning is not set.
        std::fs::write("tests/rundir/hello.world", "test").unwrap();
        let result = RunDir::new("tests/rundir").initialize();

        match result {
            Err(RunDirError::DirIsNotEmpty { path, child_path }) => {
                assert_eq!(path, PathBuf::from("tests/rundir"));
                assert_eq!(child_path, PathBuf::from("tests/rundir/hello.world"));
            }
            _ => panic!("Expected an error."),
        }

        // Clean existing dir.
        let result = RunDir::new("tests/rundir")
            .allow_cleaning(true)
            .initialize();

        assert!(result.is_ok());

        std::fs::remove_dir("tests/rundir").unwrap();

        // Fail when dir is not a directory.
        std::fs::write("tests/rundir", "hello").unwrap();
        let result = RunDir::new("tests/rundir").initialize();

        match result {
            Err(RunDirError::PathIsNotDir { path }) => {
                assert_eq!(path, PathBuf::from("tests/rundir"));
            }
            _ => panic!("Expected an error."),
        }

        std::fs::remove_file("tests/rundir").unwrap();
    }

    #[test]
    fn test_subdirs() {
        let rundir = RunDir::new("tests/rundir2").allow_cleaning(true);

        rundir.initialize().unwrap();

        assert!(!PathBuf::from("tests/rundir2/subtest").exists());
        assert!(!rundir.subdir_exists("subtest").unwrap());

        rundir.create_subdir("subtest").unwrap();
        assert!(PathBuf::from("tests/rundir2/subtest").exists());
        assert!(rundir.subdir_exists("subtest").unwrap());

        rundir.remove_subdir_all("subtest").unwrap();
        assert!(!PathBuf::from("tests/rundir2/subtest").exists());
        assert!(!rundir.subdir_exists("subtest").unwrap());

        rundir.cleanup().unwrap();
    }
}