use std::fs::{self, DirBuilder, File, OpenOptions};
use std::io::{self, Read, Seek, SeekFrom, Write};
use std::path::Path;
use std::process;
use std::time::{SystemTime, UNIX_EPOCH};
pub fn within(root: &Path, candidate: &Path) -> io::Result<bool> {
let mut existing = candidate;
loop {
match fs::symlink_metadata(existing) {
Ok(_) => break,
Err(error) if error.kind() == io::ErrorKind::NotFound => {
let Some(parent) = existing.parent() else {
return Ok(false);
};
existing = parent;
}
Err(error) => return Err(error),
}
}
match existing.canonicalize() {
Ok(resolved) => Ok(resolved
.join(candidate.strip_prefix(existing).unwrap_or(candidate))
.starts_with(root)),
Err(error) if error.kind() == io::ErrorKind::NotFound => Ok(false),
Err(error) => Err(error),
}
}
pub fn private_mkdir(directory: &Path) -> io::Result<()> {
let mut missing = Vec::new();
let mut current = directory;
while !current.exists() {
missing.push(current);
match current.parent() {
Some(parent) => current = parent,
None => break,
}
}
let builder = private_builder();
for level in missing.into_iter().rev() {
match builder.create(level) {
Err(error) if error.kind() != io::ErrorKind::AlreadyExists => return Err(error),
_ => {}
}
}
Ok(())
}
#[cfg(unix)]
pub(crate) fn private_builder() -> DirBuilder {
use std::os::unix::fs::DirBuilderExt;
let mut builder = DirBuilder::new();
builder.mode(0o700);
builder
}
#[cfg(not(unix))]
pub(crate) fn private_builder() -> DirBuilder {
DirBuilder::new()
}
#[cfg(unix)]
pub fn private() -> OpenOptions {
use std::os::unix::fs::OpenOptionsExt;
let mut options = OpenOptions::new();
options.mode(0o600);
options
}
#[cfg(not(unix))]
pub fn private() -> OpenOptions {
OpenOptions::new()
}
pub fn atomic_write(file: &Path, text: &str, sync: bool) -> io::Result<()> {
atomic_write_with(file, sync, |handle| handle.write_all(text.as_bytes()))
}
pub(super) fn atomic_write_with<E: From<io::Error>>(
file: &Path,
sync: bool,
write: impl FnOnce(&mut File) -> Result<(), E>,
) -> Result<(), E> {
let parent = file.parent().unwrap_or(Path::new("."));
private_mkdir(parent)?;
let mut attempt = SystemTime::now()
.duration_since(UNIX_EPOCH)
.map_or(0, |since| since.subsec_nanos());
let (temp, mut handle) = loop {
let temp = parent.join(format!(".neosian-tmp-{}-{attempt:08x}", process::id()));
match private()
.read(true)
.write(true)
.create_new(true)
.open(&temp)
{
Ok(handle) => break (temp, handle),
Err(error) if error.kind() == io::ErrorKind::AlreadyExists => attempt += 1,
Err(error) => return Err(error.into()),
}
};
let written = (|| {
write(&mut handle)?;
if sync {
handle.sync_all()?;
}
fs::rename(&temp, file)?;
Ok(())
})();
if written.is_err() {
let _ = fs::remove_file(&temp);
}
written
}
pub fn append_line(file: &Path, text: &str, sync: bool) -> io::Result<()> {
append(file, text, sync, false)
}
pub fn append_turn_line(file: &Path, text: &str, sync: bool) -> io::Result<()> {
append(file, text, sync, true)
}
fn append(file: &Path, text: &str, sync: bool, check_ending: bool) -> io::Result<()> {
if let Some(parent) = file.parent() {
private_mkdir(parent)?;
}
let mut handle = private()
.append(true)
.read(check_ending)
.create(true)
.open(file)?;
if check_ending {
let len = handle.metadata()?.len();
if needs_separator(&mut handle, len)? {
handle.write_all(b"\n")?;
}
}
handle.write_all(text.as_bytes())?;
if sync { handle.sync_all() } else { Ok(()) }
}
pub(super) fn needs_separator(file: &mut File, prefix: u64) -> io::Result<bool> {
if prefix == 0 {
file.rewind()?;
return Ok(false);
}
file.seek(SeekFrom::Start(prefix - 1))?;
let mut last = [0];
file.read_exact(&mut last)?;
Ok(last[0] != b'\n')
}
fn allow_absent(result: io::Result<()>) -> io::Result<()> {
match result {
Err(error) if error.kind() == io::ErrorKind::NotFound => Ok(()),
result => result,
}
}
pub fn remove_file(path: &Path) -> io::Result<()> {
allow_absent(fs::remove_file(path))
}
pub fn remove_tree(path: &Path) -> io::Result<()> {
allow_absent(fs::remove_dir_all(path))
}