From a5a0b9e38678ba6e602ed4ea18fb6b9cc8bb8703 Mon Sep 17 00:00:00 2001 From: Arne Beer Date: Thu, 15 Oct 2020 23:31:08 +0200 Subject: [PATCH] Fix directories functions for other platforms --- shared/platform/linux/directories.rs | 20 ++++++++++++-------- shared/platform/macos/directories.rs | 17 +++++++++++++++++ shared/platform/unix/socket.rs | 4 ++-- shared/platform/windows/directories.rs | 11 +++++++++++ shared/platform/windows/socket.rs | 2 +- 5 files changed, 43 insertions(+), 11 deletions(-) diff --git a/shared/platform/linux/directories.rs b/shared/platform/linux/directories.rs index ba4841b79..01dfc8b8b 100644 --- a/shared/platform/linux/directories.rs +++ b/shared/platform/linux/directories.rs @@ -7,12 +7,16 @@ use users::{get_current_uid, get_user_by_uid}; pub fn get_unix_socket_path() -> Result { // Get the user and their username let user = get_user_by_uid(get_current_uid()) - .ok_or(anyhow!("Couldn't find username for current user"))?; + .ok_or_else(|| anyhow!("Couldn't find username for current user"))?; let username = user.name().to_string_lossy(); - // Create the socket in the default /tmp/ directory - let path = Path::new("/tmp/").join(format!("pueue_{}.socket", username)); - Ok(path.to_string_lossy().into()) + // Create the socket in the default pueue path + let pueue_path = PathBuf::from(default_pueue_path()?); + let path = pueue_path.join(format!("pueue_{}.socket", username)); + Ok(path + .to_str() + .ok_or_else(|| anyhow!("Failed to parse log path (Weird characters?)"))? + .to_string()) } fn get_home_dir() -> Result { @@ -35,7 +39,7 @@ pub fn default_pueue_path() -> Result { let path = get_home_dir()?.join(".local/share/pueue"); Ok(path .to_str() - .ok_or(anyhow!("Failed to parse log path (Weird characters?)"))? + .ok_or_else(|| anyhow!("Failed to parse log path (Weird characters?)"))? .to_string()) } @@ -43,7 +47,7 @@ pub fn default_pueue_path() -> Result { mod tests { use super::*; - use std::fs::{remove_file, File}; + use std::fs::{create_dir_all, remove_file, File}; use std::io::prelude::*; use anyhow::Result; @@ -51,9 +55,9 @@ mod tests { #[test] fn test_create_unix_socket() -> Result<()> { let path = get_unix_socket_path()?; + create_dir_all(default_pueue_path()?)?; - // If pueue is currently running on the system, - // simply accept that we found the correct path + // If pueue is currently running on the system, simply accept that we found the correct path if PathBuf::from(&path).exists() { return Ok(()); } diff --git a/shared/platform/macos/directories.rs b/shared/platform/macos/directories.rs index 9da76e078..3a8515158 100644 --- a/shared/platform/macos/directories.rs +++ b/shared/platform/macos/directories.rs @@ -1,6 +1,23 @@ use std::path::{Path, PathBuf}; use anyhow::{anyhow, Result}; +use users::{get_current_uid, get_user_by_uid}; + +/// Get the default unix socket path for the current user +pub fn get_unix_socket_path() -> Result { + // Get the user and their username + let user = get_user_by_uid(get_current_uid()) + .ok_or(anyhow!("Couldn't find username for current user"))?; + let username = user.name().to_string_lossy(); + + // Create the socket in the default pueue path + let pueue_path = PathBuf::from(default_pueue_path()?); + let path = pueue_path.join(format!("pueue_{}.socket", username)); + Ok(path + .to_str() + .ok_or(anyhow!("Failed to parse log path (Weird characters?)"))? + .to_string()) +} fn get_home_dir() -> Result { dirs::home_dir().ok_or_else(|| anyhow!("Couldn't resolve home dir")) diff --git a/shared/platform/unix/socket.rs b/shared/platform/unix/socket.rs index d7a1712c7..e747a4844 100644 --- a/shared/platform/unix/socket.rs +++ b/shared/platform/unix/socket.rs @@ -16,7 +16,7 @@ pub trait GenericListener: Sync + Send { impl GenericListener for TcpListener { async fn accept<'a>(&'a self) -> Result> { let (socket, _) = self.accept().await?; - Ok((Box::new(socket))) + Ok(Box::new(socket)) } } @@ -24,7 +24,7 @@ impl GenericListener for TcpListener { impl GenericListener for UnixListener { async fn accept<'a>(&'a self) -> Result> { let (socket, _) = self.accept().await?; - Ok((Box::new(socket))) + Ok(Box::new(socket)) } } diff --git a/shared/platform/windows/directories.rs b/shared/platform/windows/directories.rs index 554735089..a914122ac 100644 --- a/shared/platform/windows/directories.rs +++ b/shared/platform/windows/directories.rs @@ -2,6 +2,17 @@ use std::path::{Path, PathBuf}; use anyhow::{anyhow, Result}; +/// Get the default unix socket path for the current user +pub fn get_unix_socket_path() -> Result { + // Create the socket in the default pueue path + let pueue_path = PathBuf::from(default_pueue_path()?); + let path = pueue_path.join("pueue.socket"); + Ok(path + .to_str() + .ok_or(anyhow!("Failed to parse log path (Weird characters?)"))? + .to_string()) +} + fn get_home_dir() -> Result { dirs::home_dir().ok_or_else(|| anyhow!("Couldn't resolve home dir")) } diff --git a/shared/platform/windows/socket.rs b/shared/platform/windows/socket.rs index cdf3f40f3..b3ed55b82 100644 --- a/shared/platform/windows/socket.rs +++ b/shared/platform/windows/socket.rs @@ -15,7 +15,7 @@ pub trait GenericListener: Sync + Send { impl GenericListener for TcpListener { async fn accept<'a>(&'a self) -> Result> { let (socket, _) = self.accept().await?; - Ok((Box::new(socket))) + Ok(Box::new(socket)) } }