use crate::fs::PathUpdate;
use std::ffi::OsStr;
use std::io::{Error, ErrorKind, Result};
use std::os::unix::ffi::OsStrExt;
use std::path::{Path, PathBuf};
pub fn hide<T: AsRef<Path>>(r: T) -> Result<PathUpdate<T>> {
let path = r.as_ref();
if !path.exists() {
return Err(Error::new(ErrorKind::NotFound, "file or directory found"));
}
if let Some(str) = path.file_name() {
let bytes = str.as_bytes();
if bytes[0] == b'.' {
return Ok(PathUpdate::Unchanged(r)); }
let mut vec = bytes.to_vec();
vec.insert(0, b'.');
let mut copy: PathBuf = path.into();
copy.set_file_name(OsStr::from_bytes(&vec));
return std::fs::rename(path, ©).map(|_| PathUpdate::Changed(copy));
}
Err(Error::new(
ErrorKind::InvalidInput,
"the path does not have a file name",
))
}
pub fn show<T: AsRef<Path>>(r: T) -> Result<PathUpdate<T>> {
let path = r.as_ref();
if !path.exists() {
return Err(Error::new(ErrorKind::NotFound, "file or directory found"));
}
if let Some(str) = path.file_name() {
let bytes = str.as_bytes();
if bytes[0] != b'.' {
return Ok(PathUpdate::Unchanged(r)); }
let mut vec = bytes.to_vec();
vec.remove(0); let mut copy: PathBuf = path.into();
copy.set_file_name(OsStr::from_bytes(&vec));
return std::fs::rename(path, ©).map(|_| PathUpdate::Changed(copy));
}
Err(Error::new(
ErrorKind::InvalidInput,
"the path does not have a file name",
))
}
pub fn get_absolute_path<T: AsRef<Path>>(path: T) -> Result<PathBuf> {
std::fs::canonicalize(path)
}
pub fn is_hidden<T: AsRef<Path>>(path: T) -> bool {
if let Some(str) = path.as_ref().file_name() {
let bytes = str.as_bytes();
if bytes[0] == b'.' {
return true;
}
}
false
}