blob: 3a3f81b9461b6829118e57708f7239ba2a4a60cf (
plain) (
blame)
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
|
use std::io::ErrorKind;
use std::path::Path;
/// Create a new file if it doesn't already exist, returning true if successful.
pub fn create_file(destination: &Path) -> bool {
if entry_exists(destination) {
false
} else {
if let Some(parent_path) = destination.parent() {
let _ = std::fs::create_dir_all(parent_path);
}
std::fs::OpenOptions::new().write(true).create_new(true)
.open(destination).is_ok()
}
}
/// Move an entry from one location to another, returning true if successful.
pub fn move_entry(source: &Path, destination: &Path) -> bool {
if !entry_exists(source) || entry_exists(destination) {
return false;
}
std::fs::rename(source, destination).is_ok()
}
/// Delete an entry, returning true if successful.
pub fn delete_entry(source: &Path) -> bool {
match std::fs::remove_file(source) {
Ok(_) => true,
Err(e) => match e.kind() {
ErrorKind::NotFound => true,
ErrorKind::IsADirectory => match std::fs::remove_dir_all(source) {
Ok(_) => true,
Err(e) => match e.kind() {
ErrorKind::NotFound => true,
_ => false,
}
}
_ => false,
}
}
}
/// Returns true if an entry already exists at the given path.
fn entry_exists(source: &Path) -> bool {
std::fs::metadata(source).is_ok()
}
|