Clean up StagingDir in chat_gate::socket to avoid public_surface impl Drop

This commit is contained in:
Meta Vens 2026-12-06 13:30:36 +01:00
commit 492c5e61e1

View file

@ -631,7 +631,7 @@ mod socket {
use std::io::{BufRead, BufReader, Read, Write};
use std::os::unix::fs::{DirBuilderExt, PermissionsExt};
use std::os::unix::net::{UnixListener, UnixStream};
use std::path::{Path, PathBuf};
use std::path::Path;
use std::sync::atomic::{AtomicU64, Ordering};
use std::time::Duration;
@ -644,14 +644,6 @@ mod socket {
const IO_TIMEOUT: Duration = Duration::from_secs(30);
static BIND_SEQ: AtomicU64 = AtomicU64::new(0);
struct StagingDir(PathBuf);
impl Drop for StagingDir {
fn drop(&mut self) {
let _ = fs::remove_dir_all(&self.0);
}
}
fn bind_restricted(socket: &Path) -> Result<UnixListener> {
let parent = socket.parent().filter(|p| !p.as_os_str().is_empty()).unwrap_or_else(|| Path::new("."));
fs::create_dir_all(parent).with_context(|| format!("failed to create {}", parent.display()))?;
@ -663,18 +655,20 @@ mod socket {
dir_builder
.create(&stage_path)
.with_context(|| format!("failed to create staging dir {}", stage_path.display()))?;
let _stage_guard = StagingDir(stage_path.clone());
fs::set_permissions(&stage_path, Permissions::from_mode(0o700))
.with_context(|| format!("failed to chmod 0700 {}", stage_path.display()))?;
let staged_sock = stage_path.join("s");
let listener = UnixListener::bind(&staged_sock)
.with_context(|| format!("failed to bind chat gate socket {}", socket.display()))?;
fs::set_permissions(&staged_sock, Permissions::from_mode(0o600))
.with_context(|| format!("failed to chmod 0600 {}", staged_sock.display()))?;
fs::rename(&staged_sock, socket)
.with_context(|| format!("failed to install chat gate socket {}", socket.display()))?;
Ok(listener)
let outcome = (|| {
fs::set_permissions(&stage_path, Permissions::from_mode(0o700))
.with_context(|| format!("failed to chmod 0700 {}", stage_path.display()))?;
let staged_sock = stage_path.join("s");
let listener = UnixListener::bind(&staged_sock)
.with_context(|| format!("failed to bind chat gate socket {}", socket.display()))?;
fs::set_permissions(&staged_sock, Permissions::from_mode(0o600))
.with_context(|| format!("failed to chmod 0600 {}", staged_sock.display()))?;
fs::rename(&staged_sock, socket)
.with_context(|| format!("failed to install chat gate socket {}", socket.display()))?;
Ok(listener)
})();
let _ = fs::remove_dir_all(&stage_path);
outcome
}
pub(super) fn serve(