use std::fs;
use std::io;
use std::path::Path;
use super::guard_destructive;
use crate::error::{Error, Result};
pub fn remove(path: impl AsRef<Path>) -> Result<bool> {
let path = path.as_ref();
guard_destructive(path)?;
let meta = match fs::symlink_metadata(path) {
Ok(meta) => meta,
Err(e) if e.kind() == io::ErrorKind::NotFound => return Ok(false),
Err(e) => return Err(Error::io("inspect", path, e)),
};
remove_entry(path, &meta)?;
Ok(true)
}
pub(super) fn remove_entry(path: &Path, meta: &fs::Metadata) -> Result<()> {
let result = if meta.file_type().is_symlink() {
remove_link(path, meta)
} else if meta.is_dir() {
fs::remove_dir_all(path)
} else {
fs::remove_file(path)
};
result.map_err(|e| Error::io("remove", path, e))
}
#[cfg(windows)]
pub(super) fn remove_link(path: &Path, meta: &fs::Metadata) -> io::Result<()> {
use std::os::windows::fs::FileTypeExt;
if meta.file_type().is_symlink_dir() {
fs::remove_dir(path)
} else {
fs::remove_file(path)
}
}
#[cfg(unix)]
pub(super) fn remove_link(path: &Path, _meta: &fs::Metadata) -> io::Result<()> {
fs::remove_file(path)
}