use std::path::Path;
use std::time::{SystemTime, UNIX_EPOCH};
fn invalid_input(msg: &'static str) -> std::io::Error {
std::io::Error::new(std::io::ErrorKind::InvalidInput, msg)
}
pub(crate) fn try_atomic_write(
path: &Path,
bytes: &[u8],
permissions: Option<&std::fs::Permissions>,
) -> std::io::Result<()> {
use std::io::Write;
let parent = path
.parent()
.ok_or_else(|| invalid_input("invalid path (no parent directory)"))?;
let filename = path
.file_name()
.ok_or_else(|| invalid_input("invalid path (no filename)"))?
.to_string_lossy();
cleanup_orphaned_temps(parent, &filename);
let pid = std::process::id();
let nanos = SystemTime::now()
.duration_since(UNIX_EPOCH)
.map_or(0, |d| d.as_nanos());
let tmp = parent.join(format!(".{filename}.lean-ctx.tmp.{pid}.{nanos}"));
{
let mut f = std::fs::OpenOptions::new()
.write(true)
.create_new(true)
.open(&tmp)?;
f.write_all(bytes)?;
let _ = f.flush();
let _ = f.sync_all();
}
if let Some(perms) = permissions {
let _ = std::fs::set_permissions(&tmp, perms.clone());
}
#[cfg(windows)]
if let Err(e) = windows_replace(&tmp, path) {
let _ = std::fs::remove_file(&tmp);
return Err(e);
}
#[cfg(not(windows))]
if let Err(e) = std::fs::rename(&tmp, path) {
let _ = std::fs::remove_file(&tmp);
return Err(e);
}
#[cfg(unix)]
fsync_dir(parent);
Ok(())
}
#[cfg(windows)]
fn windows_replace(tmp: &Path, path: &Path) -> std::io::Result<()> {
use std::os::windows::ffi::OsStrExt;
use windows_sys::Win32::Storage::FileSystem::{MOVEFILE_REPLACE_EXISTING, MoveFileExW};
fn to_wide(p: &Path) -> Vec<u16> {
p.as_os_str()
.encode_wide()
.chain(std::iter::once(0))
.collect()
}
let tmp_w = to_wide(tmp);
let path_w = to_wide(path);
let ok = unsafe { MoveFileExW(tmp_w.as_ptr(), path_w.as_ptr(), MOVEFILE_REPLACE_EXISTING) };
if ok == 0 {
return Err(std::io::Error::last_os_error());
}
Ok(())
}
#[cfg(unix)]
fn fsync_dir(dir: &Path) {
if let Ok(f) = std::fs::File::open(dir) {
let _ = f.sync_all();
}
}
fn cleanup_orphaned_temps(parent: &Path, filename: &str) {
const STALE_AGE: std::time::Duration = std::time::Duration::from_hours(1);
let prefix = format!(".{filename}.lean-ctx.tmp.");
let Ok(entries) = std::fs::read_dir(parent) else {
return;
};
let now = SystemTime::now();
for entry in entries.flatten() {
let name = entry.file_name();
let Some(rest) = name
.to_string_lossy()
.strip_prefix(&prefix)
.map(str::to_string)
else {
continue;
};
let pid: Option<u32> = rest.split('.').next().and_then(|s| s.parse().ok());
let pid_dead = pid.is_some_and(|p| !crate::ipc::process::is_alive(p));
let stale_by_age = entry
.metadata()
.and_then(|m| m.modified())
.ok()
.and_then(|m| now.duration_since(m).ok())
.is_some_and(|age| age > STALE_AGE);
if pid_dead || stale_by_age {
let _ = std::fs::remove_file(entry.path());
}
}
}
pub(crate) fn in_place_overwrite(
path: &Path,
bytes: &[u8],
permissions: Option<&std::fs::Permissions>,
) -> std::io::Result<()> {
use std::io::Write;
let mut opts = std::fs::OpenOptions::new();
opts.write(true).truncate(true);
#[cfg(unix)]
{
use std::os::unix::fs::OpenOptionsExt;
opts.custom_flags(libc::O_NOFOLLOW);
}
let mut f = opts.open(path)?;
f.write_all(bytes)?;
let _ = f.flush();
let _ = f.sync_all();
if let Some(perms) = permissions {
let _ = std::fs::set_permissions(path, perms.clone());
}
Ok(())
}
pub(crate) fn is_readonly_dir_error(e: &std::io::Error) -> bool {
if e.kind() == std::io::ErrorKind::PermissionDenied {
return true;
}
#[cfg(unix)]
{
matches!(
e.raw_os_error(),
Some(libc::EROFS | libc::EACCES | libc::EPERM)
)
}
#[cfg(not(unix))]
{
false
}
}
pub(crate) fn write_bytes_with_fallback(
path: &Path,
bytes: &[u8],
permissions: Option<&std::fs::Permissions>,
) -> Result<(), String> {
match try_atomic_write(path, bytes, permissions) {
Ok(()) => Ok(()),
Err(e) if is_readonly_dir_error(&e) && path.is_file() => {
in_place_overwrite(path, bytes, permissions).map_err(|fallback_err| {
format!(
"atomic write failed ({e}); in-place fallback also failed: {fallback_err} ({})",
path.display()
)
})
}
Err(e) => Err(format!("atomic write failed: {e} ({})", path.display())),
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn readonly_dir_error_classification() {
assert!(is_readonly_dir_error(&std::io::Error::from(
std::io::ErrorKind::PermissionDenied
)));
assert!(!is_readonly_dir_error(&std::io::Error::from(
std::io::ErrorKind::NotFound
)));
#[cfg(unix)]
{
assert!(is_readonly_dir_error(&std::io::Error::from_raw_os_error(
libc::EROFS
)));
assert!(is_readonly_dir_error(&std::io::Error::from_raw_os_error(
libc::EACCES
)));
assert!(is_readonly_dir_error(&std::io::Error::from_raw_os_error(
libc::EPERM
)));
}
}
#[cfg(unix)]
#[test]
fn fsync_dir_succeeds_on_a_real_directory() {
let dir = tempfile::tempdir().unwrap();
fsync_dir(dir.path());
}
#[test]
fn cleanup_orphaned_temps_removes_dead_pid_leftovers() {
let dir = tempfile::tempdir().unwrap();
let filename = "cfg.toml";
let dead_pid = 999_999_999u32;
let orphan = dir
.path()
.join(format!(".{filename}.lean-ctx.tmp.{dead_pid}.123"));
std::fs::write(&orphan, b"stale").unwrap();
cleanup_orphaned_temps(dir.path(), filename);
assert!(
!orphan.exists(),
"orphaned temp with a dead PID must be removed"
);
}
#[test]
fn cleanup_orphaned_temps_keeps_fresh_temp_from_a_live_pid() {
let dir = tempfile::tempdir().unwrap();
let filename = "cfg.toml";
let my_pid = std::process::id();
let in_progress = dir
.path()
.join(format!(".{filename}.lean-ctx.tmp.{my_pid}.123"));
std::fs::write(&in_progress, b"still writing").unwrap();
cleanup_orphaned_temps(dir.path(), filename);
assert!(
in_progress.exists(),
"a live writer's own in-progress temp must survive"
);
}
#[test]
fn cleanup_orphaned_temps_removes_old_temp_even_with_a_live_pid() {
let dir = tempfile::tempdir().unwrap();
let filename = "cfg.toml";
let my_pid = std::process::id();
let ancient = dir
.path()
.join(format!(".{filename}.lean-ctx.tmp.{my_pid}.123"));
std::fs::write(&ancient, b"ancient").unwrap();
filetime::set_file_mtime(&ancient, filetime::FileTime::from_unix_time(0, 0)).unwrap();
cleanup_orphaned_temps(dir.path(), filename);
assert!(
!ancient.exists(),
"ancient temp must be removed regardless of PID liveness"
);
}
#[test]
fn cleanup_orphaned_temps_ignores_unrelated_files() {
let dir = tempfile::tempdir().unwrap();
let filename = "cfg.toml";
let unrelated = dir.path().join("other-file.txt");
std::fs::write(&unrelated, b"keep me").unwrap();
cleanup_orphaned_temps(dir.path(), filename);
assert!(unrelated.exists(), "non-matching files must be left alone");
}
#[test]
fn try_atomic_write_creates_and_replaces() {
let dir = tempfile::tempdir().unwrap();
let path = dir.path().join("cfg.toml");
try_atomic_write(&path, b"first", None).unwrap();
assert_eq!(std::fs::read(&path).unwrap(), b"first");
let strays: Vec<_> = std::fs::read_dir(dir.path())
.unwrap()
.flatten()
.filter(|e| e.file_name().to_string_lossy().contains(".lean-ctx.tmp."))
.collect();
assert!(strays.is_empty(), "temp file must not linger");
try_atomic_write(&path, b"second", None).unwrap();
assert_eq!(std::fs::read(&path).unwrap(), b"second");
}
#[cfg(unix)]
#[test]
fn in_place_overwrite_truncates_existing_file() {
let dir = tempfile::tempdir().unwrap();
let path = dir.path().join("config.jsonc");
std::fs::write(&path, b"longer original content").unwrap();
in_place_overwrite(&path, b"short", None).unwrap();
assert_eq!(std::fs::read(&path).unwrap(), b"short");
}
#[cfg(unix)]
#[test]
fn fallback_overwrites_when_parent_dir_is_readonly() {
use std::os::unix::fs::PermissionsExt;
let dir = tempfile::tempdir().unwrap();
let path = dir.path().join("cfg.toml");
std::fs::write(&path, b"original").unwrap();
std::fs::set_permissions(dir.path(), std::fs::Permissions::from_mode(0o500)).unwrap();
let res = write_bytes_with_fallback(&path, b"updated", None);
let _ = std::fs::set_permissions(dir.path(), std::fs::Permissions::from_mode(0o700));
res.expect("read-only-dir fallback must succeed");
assert_eq!(std::fs::read(&path).unwrap(), b"updated");
}
}