From 66d1dfacdaa16257987c6c8a86aa1f4ef3ebd75f Mon Sep 17 00:00:00 2001 From: atagen Date: Sun, 22 Jun 2025 23:51:54 +1000 Subject: [PATCH] chore: more robust xdg path handling --- cli/main.rs | 11 ++++++++--- daemon/ipc.rs | 13 ++++++++++++- 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/cli/main.rs b/cli/main.rs index 1c1da73..d346a2d 100644 --- a/cli/main.rs +++ b/cli/main.rs @@ -1,8 +1,9 @@ use anyhow::{Context, Result, anyhow}; use clap::{Parser, Subcommand}; +use microxdg::Xdg; use niri_tag::TagCmd; use nix::unistd::geteuid; -use std::{io::Write, os::unix::net::UnixStream}; +use std::{io::Write, os::unix::net::UnixStream, path::PathBuf, str::FromStr}; #[derive(Parser)] #[command(name = "tagctl")] @@ -51,8 +52,12 @@ fn main() -> Result<()> { let cmd = TagCmd::from(cli.cmd); - let mut ipc = UnixStream::connect(format!("/run/user/{}/niri-tag.sock", geteuid())) - .context("Connecting to niri-tag ipc socket")?; + let xdg = Xdg::new()?; + let mut path = xdg + .runtime()? + .unwrap_or(PathBuf::from_str(&format!("/run/user/{}", geteuid()))?); + path.push("niri-tag.sock"); + let mut ipc = UnixStream::connect(path).context("Connecting to niri-tag ipc socket")?; ipc.write_all(serde_json::to_string(&cmd)?.as_bytes())?; Ok(()) } diff --git a/daemon/ipc.rs b/daemon/ipc.rs index 7d7c6bf..80fb95b 100644 --- a/daemon/ipc.rs +++ b/daemon/ipc.rs @@ -2,10 +2,13 @@ use std::{ collections::{BTreeMap, HashMap}, net::SocketAddr, os::linux::net::SocketAddrExt, + path::PathBuf, + str::FromStr, }; use crate::socket::{create_niri_socket, tell}; use anyhow::{Error, Result, anyhow}; +use microxdg::Xdg; use niri_ipc::{Event, Request}; use niri_tag::{TagCmd, TagEvent, TagState}; use nix::unistd::geteuid; @@ -35,8 +38,16 @@ pub async fn event_consumer(tx: channel::Sender) -> Result<()> { unreachable!("Listener loop ended"); } +fn get_run_path() -> Result { + let xdg = Xdg::new()?; + Ok(xdg + .runtime()? + .unwrap_or(PathBuf::from_str(&format!("/run/user/{}", geteuid()))?)) +} + async fn create_provider_socket(name: &'static str, socket: &'static str) -> Result { - let sock_path = format!("/run/user/{}/{}.sock", geteuid(), socket); + let mut sock_path = get_run_path()?; + sock_path.push(format!("{}.sock", socket)); if smol::fs::metadata(&sock_path).await.is_ok() { tracing::debug!("removing old {} socket", name); smol::fs::remove_file(&sock_path).await?;