hyperlocal_with_windows/server_helpers.rs
1use std::{io, path::Path};
2
3/// Helper function to delete a Unix socket if it exists.
4/// This function correctly handles Unix sockets on Windows which need to be
5/// handled specially.
6///
7/// # Errors
8/// Refer to [`tokio::fs::remove_file`](https://docs.rs/tokio/latest/tokio/fs/fn.remove_file.html).
9pub async fn remove_unix_socket_if_present(path: impl AsRef<Path>) -> io::Result<()> {
10 // We need to delete the unix socket if it exists so we can listen on a
11 // new one.
12 // On Windows, metadata() returns an Uncategorized error if the file
13 // exists as a unix socket.
14 let must_delete = match tokio::fs::metadata(&path).await {
15 Ok(_) => true,
16 Err(err) => err.kind() != std::io::ErrorKind::NotFound,
17 };
18 if must_delete {
19 tokio::fs::remove_file(&path).await?;
20 }
21 Ok(())
22}