use std::path::{Path, PathBuf};
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum HostPathKind {
Directory,
RegularFile,
UnixSocket,
Other,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct HostPathMetadata {
kind: HostPathKind,
owner_uid: u32,
mode: u32,
}
impl HostPathMetadata {
#[must_use]
pub const fn new(kind: HostPathKind, owner_uid: u32, mode: u32) -> Self {
Self {
kind,
owner_uid,
mode,
}
}
#[cfg(unix)]
pub fn inspect(path: &Path) -> std::io::Result<Self> {
use std::os::unix::fs::{FileTypeExt, MetadataExt};
let metadata = std::fs::metadata(path)?;
let file_type = metadata.file_type();
let kind = if file_type.is_socket() {
HostPathKind::UnixSocket
} else if file_type.is_file() {
HostPathKind::RegularFile
} else if file_type.is_dir() {
HostPathKind::Directory
} else {
HostPathKind::Other
};
Ok(Self::new(kind, metadata.uid(), metadata.mode()))
}
#[must_use]
pub const fn kind(self) -> HostPathKind {
self.kind
}
#[must_use]
pub const fn owner_uid(self) -> u32 {
self.owner_uid
}
#[must_use]
pub const fn mode(self) -> u32 {
self.mode
}
#[must_use]
pub fn is_owned_socket(self, current_uid: u32) -> bool {
self.kind == HostPathKind::UnixSocket && self.owner_uid == current_uid
}
}
#[must_use]
pub fn ssh_known_hosts(home: &Path) -> [PathBuf; 2] {
[
home.join(".ssh/known_hosts"),
home.join(".ssh/known_hosts2"),
]
}