chore: more robust xdg path handling

This commit is contained in:
atagen 2025-06-22 23:51:54 +10:00
parent 1318c51a44
commit 66d1dfacda
2 changed files with 20 additions and 4 deletions

View File

@ -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(())
}

View File

@ -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<Event>) -> Result<()> {
unreachable!("Listener loop ended");
}
fn get_run_path() -> Result<PathBuf> {
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<UnixListener> {
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?;