diff options
Diffstat (limited to 'src/ipc.rs')
| -rw-r--r-- | src/ipc.rs | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/src/ipc.rs b/src/ipc.rs new file mode 100644 index 00000000..aa0148a1 --- /dev/null +++ b/src/ipc.rs @@ -0,0 +1,46 @@ +use std::fs; +use std::io; +use std::os::unix::fs::PermissionsExt; +use std::os::unix::net::UnixStream; +use std::path::Path; + +pub(crate) fn prepare_socket_path( + path: &Path, + busy_message: impl FnOnce(&Path) -> String, +) -> io::Result<()> { + if let Some(parent) = path.parent() { + fs::create_dir_all(parent)?; + } + + if !path.exists() { + return Ok(()); + } + + match UnixStream::connect(path) { + Ok(_) => { + return Err(io::Error::new(io::ErrorKind::AddrInUse, busy_message(path))); + } + Err(err) + if matches!( + err.kind(), + io::ErrorKind::ConnectionRefused + | io::ErrorKind::NotFound + | io::ErrorKind::TimedOut + ) => {} + Err(err) => return Err(err), + } + + if let Err(err) = fs::remove_file(path) { + if err.kind() != io::ErrorKind::NotFound { + return Err(err); + } + } + + Ok(()) +} + +pub(crate) fn restrict_socket_permissions(path: &Path, mode: u32) -> io::Result<()> { + let mut permissions = fs::metadata(path)?.permissions(); + permissions.set_mode(mode); + fs::set_permissions(path, permissions) +} |
