-
Notifications
You must be signed in to change notification settings - Fork 334
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Change filesystem errors to be consistent across operating systems
- Loading branch information
1 parent
33e0808
commit 13a16b9
Showing
5 changed files
with
79 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
use std::{fs::create_dir_all, path::PathBuf}; | ||
|
||
#[derive(Debug)] | ||
pub struct MkdirPFailure; | ||
|
||
/// An implementation for `mkdir -p`. | ||
/// | ||
/// This is a thin wrapper around fs::create_dir_all that | ||
/// hides all OS specific error messages to ensure they don't end up | ||
/// breaking consensus. | ||
pub fn mkdir_p(path: &PathBuf) -> Result<(), MkdirPFailure> { | ||
create_dir_all(path).map_err(|_e| MkdirPFailure {}) | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use tempfile::TempDir; | ||
|
||
use super::*; | ||
|
||
#[test] | ||
fn mkdir_p_works() { | ||
let tmp_root = TempDir::new().unwrap(); | ||
|
||
// Can create | ||
let path = tmp_root.path().join("something"); | ||
assert!(!path.is_dir()); | ||
mkdir_p(&path).unwrap(); | ||
assert!(path.is_dir()); | ||
|
||
// Can be called on existing dir | ||
let path = tmp_root.path().join("something else"); | ||
assert!(!path.is_dir()); | ||
mkdir_p(&path).unwrap(); | ||
assert!(path.is_dir()); | ||
mkdir_p(&path).unwrap(); // no-op | ||
assert!(path.is_dir()); | ||
|
||
// Fails for dir with null | ||
let path = tmp_root.path().join("something\0with NULL"); | ||
mkdir_p(&path).unwrap_err(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters