//! Crash report directory handling (port of `crash/dir.zig`).
use std::{
fs,
io,
path::{Path, PathBuf},
time::UNIX_EPOCH,
};
/// The file extension of crash reports written by the handler.
pub const REPORT_EXTENSION: &str = "ghosttycrash";
/// Returns a [`Dir`] for the default directory
/// (`$XDG_STATE_HOME/ghostty/crash`).
pub fn default_dir() -> io::Result
{
let path = ghostty_os::xdg::state(&ghostty_os::xdg::Options {
subdir: Some("ghostty/crash"),
home: None,
})
.map_err(|e| io::Error::other(e.to_string()))?;
Ok(Dir {
// The XDG helpers return the encoded form of the OS string.
path: path.to_path(),
})
}
/// A directory where crash reports are stored.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Dir {
pub path: PathBuf,
}
impl Dir {
/// Returns an iterator over the crash reports in this directory. The
/// iterator may have no reports (including when the directory can't be
/// opened).
#[must_use]
pub fn iterator(&self) -> ReportIterator {
ReportIterator {
dir: self.path.clone(),
it: fs::read_dir(&self.path).ok(),
}
}
}
/// Iterates the regular files of a crash directory.
pub struct ReportIterator {
dir: PathBuf,
it: Option,
}
impl ReportIterator {
/// Next report, or `Ok(None)` at the end.
pub fn next_report(&mut self) -> io::Result