use std::path::{Path, PathBuf};
use std::time::SystemTime;
use crate::ssh_config::model::SshConfigFile;
#[derive(Default)]
pub struct ReloadState {
pub config_path: PathBuf,
pub last_modified: Option<SystemTime>,
pub include_mtimes: Vec<(PathBuf, Option<SystemTime>)>,
pub include_dir_mtimes: Vec<(PathBuf, Option<SystemTime>)>,
pub keys_dir_mtime: Option<SystemTime>,
pub key_file_mtimes: Vec<(PathBuf, Option<SystemTime>)>,
}
#[derive(Default)]
pub struct ConflictState {
pub form_mtime: Option<SystemTime>,
pub form_include_mtimes: Vec<(PathBuf, Option<SystemTime>)>,
pub form_include_dir_mtimes: Vec<(PathBuf, Option<SystemTime>)>,
pub provider_form_mtime: Option<SystemTime>,
}
impl ReloadState {
pub fn from_config(config: &SshConfigFile) -> Self {
let config_path = config.path.clone();
let last_modified = get_mtime(&config_path);
let include_mtimes = snapshot_include_mtimes(config);
let include_dir_mtimes = snapshot_include_dir_mtimes(config);
Self {
config_path,
last_modified,
include_mtimes,
include_dir_mtimes,
keys_dir_mtime: None,
key_file_mtimes: Vec::new(),
}
}
}
pub fn get_mtime(path: &Path) -> Option<SystemTime> {
std::fs::metadata(path).ok()?.modified().ok()
}
pub fn snapshot_include_mtimes(config: &SshConfigFile) -> Vec<(PathBuf, Option<SystemTime>)> {
config
.include_paths()
.into_iter()
.map(|p| {
let mtime = get_mtime(&p);
(p, mtime)
})
.collect()
}
pub fn snapshot_include_dir_mtimes(config: &SshConfigFile) -> Vec<(PathBuf, Option<SystemTime>)> {
config
.include_glob_dirs()
.into_iter()
.map(|p| {
let mtime = get_mtime(&p);
(p, mtime)
})
.collect()
}
pub fn snapshot_key_mtimes(
ssh_dir: &Path,
keys: &[crate::ssh_keys::SshKeyInfo],
) -> Vec<(PathBuf, Option<SystemTime>)> {
keys.iter()
.map(|k| {
let pub_path = ssh_dir.join(format!("{}.pub", k.name));
let mtime = get_mtime(&pub_path);
(pub_path, mtime)
})
.collect()
}