blob: f33509be9092f313e4125312b2653dcf14ab53cb (
plain) (
tree)
|
|
use std::io::ErrorKind;
use std::path::Path;
pub fn entry_exists(source: &Path) -> bool {
std::fs::metadata(source).is_ok()
}
// Delete a file or folder, 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,
}
}
}
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()
}
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()
}
}
|