-
Notifications
You must be signed in to change notification settings - Fork 1
/
fs.rs
164 lines (132 loc) · 4.5 KB
/
fs.rs
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
use super::*;
use camino::Utf8Path;
use std::process::Command;
pub fn copy_sparse(from: impl AsRef<Utf8Path>, to: impl AsRef<Utf8Path>) -> std::io::Result<()> {
let from = from.as_ref();
let to = to.as_ref();
exec(Command::new("cp").args(["--sparse=always", from.as_str(), to.as_str()]))
.map_err(|e| std::io::Error::new(std::io::ErrorKind::Other, e))?;
Ok(())
}
pub fn rm_rf(path: impl AsRef<Utf8Path>) -> std::io::Result<()> {
let path = path.as_ref();
exec(Command::new("rm").args(["-rf", path.as_str()]))
.map_err(|e| std::io::Error::new(std::io::ErrorKind::Other, e))?;
Ok(())
}
pub fn mkdir_p(path: impl AsRef<Utf8Path>) -> std::io::Result<()> {
let path = path.as_ref();
exec(Command::new("mkdir").args(["-p", path.as_str()]))
.map_err(|e| std::io::Error::new(std::io::ErrorKind::Other, e))?;
Ok(())
}
pub fn mkfs_ext4(path: impl AsRef<Utf8Path>) -> std::io::Result<()> {
let path = path.as_ref();
exec(Command::new("mkfs.ext4").arg(path.as_str()))
.map_err(|e| std::io::Error::new(std::io::ErrorKind::Other, e))?;
Ok(())
}
pub fn dd(path: impl AsRef<Utf8Path>, size_in_mb: u64) -> std::io::Result<()> {
let path = path.as_ref();
exec(Command::new("dd").args([
"if=/dev/zero",
&format!("of={}", &path),
"conv=sparse",
"bs=1M",
&format!("count={}", size_in_mb),
]))
.map_err(|e| std::io::Error::new(std::io::ErrorKind::Other, e))?;
Ok(())
}
pub fn du(path: impl AsRef<Utf8Path>) -> std::io::Result<u64> {
let path = path.as_ref();
let du_output = exec(Command::new("du").args([&path.as_str()]))
.map(|o| String::from_utf8_lossy(&o.stdout).to_string())
.map_err(|e| std::io::Error::new(std::io::ErrorKind::Other, e))?;
let size = du_output
.split_whitespace()
.next()
.ok_or(std::io::Error::new(
std::io::ErrorKind::Other,
format!("Couldn not split '{:?}' into number and rest", du_output),
))?;
size.parse().map_err(|e| {
std::io::Error::new(
std::io::ErrorKind::Other,
format!("Could not parse '{:?}' to number: {}", size, e),
)
})
}
#[cfg(test)]
mod tests {
use super::*;
use std::os::unix::process::ExitStatusExt;
#[test]
fn test_copy_sparse() {
let _m = MTX.lock();
let ctx = mock_inner::internal_exec_context();
ctx.expect()
.withf(|c| inner::to_string(c) == "cp --sparse=always /foo.txt /bar.txt")
.returning(|_| {
Ok(std::process::Output {
status: std::process::ExitStatus::from_raw(0),
stdout: vec![],
stderr: vec![],
})
});
let result = copy_sparse("/foo.txt", "/bar.txt");
assert!(result.is_ok());
ctx.checkpoint();
}
#[test]
fn test_rm_rf() {
let _m = MTX.lock();
let ctx = mock_inner::internal_exec_context();
ctx.expect()
.withf(|c| inner::to_string(c) == "rm -rf /foo.txt")
.returning(|_| {
Ok(std::process::Output {
status: std::process::ExitStatus::from_raw(0),
stdout: vec![],
stderr: vec![],
})
});
let result = rm_rf("/foo.txt");
assert!(result.is_ok());
ctx.checkpoint();
}
#[test]
fn test_mkdir_p() {
let _m = MTX.lock();
let ctx = mock_inner::internal_exec_context();
ctx.expect()
.withf(|c| inner::to_string(c) == "mkdir -p /foo")
.returning(|_| {
Ok(std::process::Output {
status: std::process::ExitStatus::from_raw(0),
stdout: vec![],
stderr: vec![],
})
});
let result = mkdir_p("/foo");
assert!(result.is_ok());
ctx.checkpoint();
}
#[test]
fn test_mkfs_ext4() {
let _m = MTX.lock();
let ctx = mock_inner::internal_exec_context();
ctx.expect()
.withf(|c| inner::to_string(c) == "mkfs.ext4 /dev/sda1")
.returning(|_| {
Ok(std::process::Output {
status: std::process::ExitStatus::from_raw(0),
stdout: vec![],
stderr: vec![],
})
});
let result = mkfs_ext4("/dev/sda1");
assert!(result.is_ok());
ctx.checkpoint();
}
}