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
extern crate getopts;
#[macro_use]
extern crate uucore;
use std::fs;
use std::path::Path;
static NAME: &str = "mkdir";
static VERSION: &str = env!("CARGO_PKG_VERSION");
pub fn uumain(args: Vec<String>) -> i32 {
let mut opts = getopts::Options::new();
opts.optopt("m", "mode", "set file mode", "755");
opts.optflag("p", "parents", "make parent directories as needed");
opts.optflag("v", "verbose", "print a message for each printed directory");
opts.optflag("h", "help", "display this help");
opts.optflag("V", "version", "display this version");
let matches = match opts.parse(&args[1..]) {
Ok(m) => m,
Err(f) => crash!(1, "Invalid options\n{}", f),
};
if args.len() == 1 || matches.opt_present("help") {
print_help(&opts);
return 0;
}
if matches.opt_present("version") {
println!("{} {}", NAME, VERSION);
return 0;
}
let verbose = matches.opt_present("verbose");
let recursive = matches.opt_present("parents");
let mode_match = matches.opts_str(&["mode".to_owned()]);
let mode: u16 = match mode_match {
Some(m) => {
let res: Option<u16> = u16::from_str_radix(&m, 8).ok();
match res {
Some(r) => r,
_ => crash!(1, "no mode given"),
}
}
_ => 0o755 as u16,
};
let dirs = matches.free;
if dirs.is_empty() {
crash!(1, "missing operand");
}
exec(dirs, recursive, mode, verbose)
}
fn print_help(opts: &getopts::Options) {
println!("{} {}", NAME, VERSION);
println!();
println!("Usage:");
print!(
"{}",
opts.usage("Create the given DIRECTORY(ies) if they do not exist")
);
}
fn exec(dirs: Vec<String>, recursive: bool, mode: u16, verbose: bool) -> i32 {
let mut status = 0;
let empty = Path::new("");
for dir in &dirs {
let path = Path::new(dir);
if !recursive {
if let Some(parent) = path.parent() {
if parent != empty && !parent.exists() {
show_info!(
"cannot create directory '{}': No such file or directory",
path.display()
);
status = 1;
continue;
}
}
}
status |= mkdir(path, recursive, mode, verbose);
}
status
}
fn mkdir(path: &Path, recursive: bool, mode: u16, verbose: bool) -> i32 {
let create_dir = if recursive {
fs::create_dir_all
} else {
fs::create_dir
};
if let Err(e) = create_dir(path) {
show_info!("{}: {}", path.display(), e.to_string());
return 1;
}
if verbose {
show_info!("created directory '{}'", path.display());
}
#[cfg(any(unix, target_os = "redox"))]
fn chmod(path: &Path, mode: u16) -> i32 {
use std::fs::{set_permissions, Permissions};
use std::os::unix::fs::PermissionsExt;
let mode = Permissions::from_mode(u32::from(mode));
if let Err(err) = set_permissions(path, mode) {
show_error!("{}: {}", path.display(), err);
return 1;
}
0
}
#[cfg(windows)]
#[allow(unused_variables)]
fn chmod(path: &Path, mode: u16) -> i32 {
0
}
chmod(path, mode)
}