use std::path::Path;
use std::time::SystemTime;
#[cfg(unix)]
use std::os::unix::fs::{MetadataExt, PermissionsExt};
use tokio::fs;
pub struct EntryOptions {
pub mtime: SystemTime,
pub permissions: Option<u32>,
pub uid_gid: Option<(u32, u32)>,
pub comment: Option<String>,
}
impl Default for EntryOptions {
fn default() -> Self {
Self {
mtime: SystemTime::now(),
permissions: None,
uid_gid: None,
comment: None,
}
}
}
impl EntryOptions {
pub async fn from_path<T: AsRef<Path>>(path: T) -> std::io::Result<Self> {
let meta = fs::symlink_metadata(&path).await?;
Ok(Self {
mtime: meta.modified().unwrap_or_else(|_| SystemTime::now()),
#[cfg(unix)]
permissions: Some(meta.permissions().mode() & 0o7777),
#[cfg(not(unix))]
permissions: None,
uid_gid: {
#[cfg(unix)]
{
Some((meta.uid(), meta.gid()))
}
#[cfg(not(unix))]
{
None
}
},
comment: None,
})
}
pub fn file() -> Self {
Self {
mtime: SystemTime::now(),
permissions: Some(0o644),
..Default::default()
}
}
pub fn directory() -> Self {
Self {
mtime: SystemTime::now(),
permissions: Some(0o755),
..Default::default()
}
}
pub fn symlink() -> Self {
Self {
mtime: SystemTime::now(),
permissions: Some(0o777),
..Default::default()
}
}
}