pub mod cipher;
mod keystore;
pub(crate) use keystore::{
encrypt_data, encryption_key_path, generate_random_key, load_key_from_file,
load_key_from_keyring, save_key, try_decrypt_data,
};
use std::path::Path;
use crate::error::AuthError;
pub(crate) fn atomic_write(path: &Path, data: &[u8], mode: u32) -> Result<(), AuthError> {
let parent = path
.parent()
.ok_or_else(|| AuthError::Storage(format!("无效文件路径: {}", path.display())))?;
std::fs::create_dir_all(parent)
.map_err(|e| AuthError::Storage(format!("创建目录 {} 失败: {e}", parent.display())))?;
let mut tmp = tempfile::NamedTempFile::new_in(parent)
.map_err(|e| AuthError::Storage(format!("在 {parent:?} 中创建临时文件失败: {e}")))?;
#[cfg(unix)]
{
use std::io::Write as _;
use std::os::unix::fs::PermissionsExt;
tmp.as_file()
.set_permissions(std::fs::Permissions::from_mode(mode))
.map_err(|e| AuthError::Storage(format!("设置临时文件权限失败: {e}")))?;
tmp.write_all(data)
.map_err(|e| AuthError::Storage(format!("写入临时文件失败: {e}")))?;
}
#[cfg(not(unix))]
{
use std::io::Write as _;
tmp.write_all(data)
.map_err(|e| AuthError::Storage(format!("写入临时文件失败: {e}")))?;
}
tmp.as_file()
.sync_all()
.map_err(|e| AuthError::Storage(format!("同步临时文件失败: {e}")))?;
tmp.persist(path)
.map_err(|e| AuthError::Storage(format!("原子写入 {} 失败: {e}", path.display())))?;
Ok(())
}