Skip to content

Commit

Permalink
Fix directories functions for other platforms
Browse files Browse the repository at this point in the history
  • Loading branch information
Nukesor committed Oct 15, 2020
1 parent 47bf567 commit a5a0b9e
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 11 deletions.
20 changes: 12 additions & 8 deletions shared/platform/linux/directories.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,16 @@ use users::{get_current_uid, get_user_by_uid};
pub fn get_unix_socket_path() -> Result<String> {
// 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<PathBuf> {
Expand All @@ -35,25 +39,25 @@ pub fn default_pueue_path() -> Result<String> {
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())
}

#[cfg(test)]
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;

#[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(());
}
Expand Down
17 changes: 17 additions & 0 deletions shared/platform/macos/directories.rs
Original file line number Diff line number Diff line change
@@ -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<String> {
// 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<PathBuf> {
dirs::home_dir().ok_or_else(|| anyhow!("Couldn't resolve home dir"))
Expand Down
4 changes: 2 additions & 2 deletions shared/platform/unix/socket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ pub trait GenericListener: Sync + Send {
impl GenericListener for TcpListener {
async fn accept<'a>(&'a self) -> Result<Box<dyn GenericSocket>> {
let (socket, _) = self.accept().await?;
Ok((Box::new(socket)))
Ok(Box::new(socket))
}
}

#[async_trait]
impl GenericListener for UnixListener {
async fn accept<'a>(&'a self) -> Result<Box<dyn GenericSocket>> {
let (socket, _) = self.accept().await?;
Ok((Box::new(socket)))
Ok(Box::new(socket))
}
}

Expand Down
11 changes: 11 additions & 0 deletions shared/platform/windows/directories.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> {
// 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<PathBuf> {
dirs::home_dir().ok_or_else(|| anyhow!("Couldn't resolve home dir"))
}
Expand Down
2 changes: 1 addition & 1 deletion shared/platform/windows/socket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ pub trait GenericListener: Sync + Send {
impl GenericListener for TcpListener {
async fn accept<'a>(&'a self) -> Result<Box<dyn GenericSocket>> {
let (socket, _) = self.accept().await?;
Ok((Box::new(socket)))
Ok(Box::new(socket))
}
}

Expand Down

0 comments on commit a5a0b9e

Please sign in to comment.