#[cfg(unix)]
use super::unix_impl;
#[cfg(windows)]
use super::windows_impl;
use super::{io, Path, PathBuf, WalpinBeacon, WalpinHeartbeat};
pub fn ensure_sidecar_dir(dir: &Path) -> io::Result<()> {
#[cfg(unix)]
{
unix_impl::SidecarDirHandle::open_or_create(dir)?;
Ok(())
}
#[cfg(windows)]
{
windows_impl::ensure_sidecar_dir(dir)
}
}
pub fn write_heartbeat(dir: &Path, heartbeat: &WalpinHeartbeat) -> io::Result<()> {
let body =
serde_json::to_vec(heartbeat).map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e))?;
let target = format!("{}.json", heartbeat.pid);
let tmp = format!(".{}.json.tmp", heartbeat.pid);
#[cfg(unix)]
{
let handle = unix_impl::SidecarDirHandle::open_or_create(dir)?;
handle.write_atomic(&target, &tmp, &body)
}
#[cfg(windows)]
{
windows_impl::write_atomic(dir, &target, &tmp, &body)
}
}
pub fn touch_heartbeat(dir: &Path, pid: u32) -> io::Result<()> {
let name = format!("{pid}.json");
#[cfg(unix)]
{
let handle = unix_impl::SidecarDirHandle::open_or_create(dir)?;
handle.touch_mtime(&name)
}
#[cfg(windows)]
{
windows_impl::touch_mtime(dir, &name)
}
}
pub fn remove_beacon(dir: &Path, pid: u32) -> io::Result<()> {
let target = format!("{pid}.beacon");
#[cfg(unix)]
{
match unix_impl::SidecarDirHandle::open_if_exists(dir)? {
Some(handle) => handle.remove_checked(&target),
None => Ok(()),
}
}
#[cfg(windows)]
{
windows_impl::remove_checked(dir, &target)
}
}
pub fn remove_heartbeat(dir: &Path, pid: u32) -> io::Result<()> {
let target = format!("{pid}.json");
#[cfg(unix)]
{
match unix_impl::SidecarDirHandle::open_if_exists(dir)? {
Some(handle) => handle.remove_checked(&target),
None => Ok(()),
}
}
#[cfg(windows)]
{
windows_impl::remove_checked(dir, &target)
}
}
pub fn write_beacon(dir: &Path, beacon: &WalpinBeacon) -> io::Result<()> {
let body =
serde_json::to_vec(beacon).map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e))?;
let target = format!("{}.beacon", beacon.pid);
let tmp = format!(".{}.beacon.tmp", beacon.pid);
#[cfg(unix)]
{
let handle = unix_impl::SidecarDirHandle::open_or_create(dir)?;
handle.write_atomic(&target, &tmp, &body)
}
#[cfg(windows)]
{
windows_impl::write_atomic(dir, &target, &tmp, &body)
}
}
pub fn touch_beacon(dir: &Path, pid: u32) -> io::Result<()> {
let name = format!("{pid}.beacon");
#[cfg(unix)]
{
let handle = unix_impl::SidecarDirHandle::open_or_create(dir)?;
handle.touch_mtime(&name)
}
#[cfg(windows)]
{
windows_impl::touch_mtime(dir, &name)
}
}
pub fn beacon_path(dir: &Path, pid: u32) -> PathBuf {
dir.join(format!("{pid}.beacon"))
}