cap_primitives/fs/
create_dir.rs1#[cfg(racy_asserts)]
5use crate::fs::{
6 canonicalize, create_dir_unchecked, map_result, stat_unchecked, FollowSymlinks, Metadata,
7};
8use crate::fs::{create_dir_impl, DirOptions};
9use std::path::Path;
10use std::{fs, io};
11
12#[cfg_attr(not(racy_asserts), allow(clippy::let_and_return))]
15#[inline]
16pub fn create_dir(start: &fs::File, path: &Path, options: &DirOptions) -> io::Result<()> {
17 #[cfg(racy_asserts)]
18 let stat_before = stat_unchecked(start, path, FollowSymlinks::No);
19
20 let result = create_dir_impl(start, path, options);
22
23 #[cfg(racy_asserts)]
24 let stat_after = stat_unchecked(start, path, FollowSymlinks::No);
25
26 #[cfg(racy_asserts)]
27 check_create_dir(start, path, options, &stat_before, &result, &stat_after);
28
29 result
30}
31
32#[cfg(racy_asserts)]
33#[allow(clippy::enum_glob_use)]
34fn check_create_dir(
35 start: &fs::File,
36 path: &Path,
37 options: &DirOptions,
38 stat_before: &io::Result<Metadata>,
39 result: &io::Result<()>,
40 stat_after: &io::Result<Metadata>,
41) {
42 use io::ErrorKind::*;
43
44 match (
45 map_result(stat_before),
46 map_result(result),
47 map_result(stat_after),
48 ) {
49 (Err((NotFound, _)), Ok(()), Ok(metadata)) => {
50 assert!(metadata.is_dir());
51 assert_same_file_metadata!(
52 &stat_unchecked(
53 start,
54 &canonicalize(start, path).unwrap(),
55 FollowSymlinks::No
56 )
57 .unwrap(),
58 &metadata
59 );
60 }
61
62 (Ok(metadata_before), Err((AlreadyExists, _)), Ok(metadata_after)) => {
63 assert_same_file_metadata!(&metadata_before, &metadata_after);
64 }
65
66 (_, Err((kind, message)), _) => {
67 if !path.to_string_lossy().ends_with(".") {
69 match map_result(&canonicalize(start, path)) {
70 Ok(canon) => match map_result(&create_dir_unchecked(start, &canon, options)) {
71 Err((unchecked_kind, unchecked_message)) => {
72 assert_eq!(
73 kind,
74 unchecked_kind,
75 "unexpected error kind from create_dir start='{:?}', \
76 path='{}':\nstat_before={:#?}\nresult={:#?}\nstat_after={:#?}",
77 start,
78 path.display(),
79 stat_before,
80 result,
81 stat_after
82 );
83 assert_eq!(message, unchecked_message);
84 }
85 _ => panic!("unsandboxed create_dir success"),
86 },
87 Err((_canon_kind, _canon_message)) => {
88 }
93 }
94 }
95 }
96
97 other => panic!(
98 "inconsistent create_dir checks: start='{:?}' path='{}':\n{:#?}",
99 start,
100 path.display(),
101 other,
102 ),
103 }
104}