From d0a03210efd08d2fbf61fa37e000fc1f4622b46a Mon Sep 17 00:00:00 2001 From: Irene Knapp Date: Tue, 24 Nov 2026 01:07:05 -0700 Subject: move get_username() into a new file Force-Push: yes Change-Id: Ied7051c063d5c3380efb2345e6d3889fa1fbe883 --- src/facility/i3.rs | 33 ++------------------------------- src/main.rs | 1 + src/unix.rs | 35 +++++++++++++++++++++++++++++++++++ 3 files changed, 38 insertions(+), 31 deletions(-) create mode 100644 src/unix.rs (limited to 'src') diff --git a/src/facility/i3.rs b/src/facility/i3.rs index 268d6bd..4b91b02 100644 --- a/src/facility/i3.rs +++ b/src/facility/i3.rs @@ -1,19 +1,14 @@ use crate::error::{ Error, Result }; use crate::facility::{ CreatableFacility, Facility, FacilityType }; +use crate::unix::get_username; use crate::xdg::xdg_runtime_directory; use std::any::Any; -use std::ffi::CStr; -use std::mem::{ size_of_val, MaybeUninit }; use std::path::{ Path, PathBuf }; -use std::ptr::null_mut; use async_fs::{ metadata, read_dir, File, OpenOptions }; use async_fs::unix::FileTypeExt; use futures_lite::StreamExt; -use libc::{ getpwuid_r, passwd }; -use rustix::io::Errno; -use rustix::process::{ getuid, Uid }; pub struct I3 { @@ -86,7 +81,7 @@ impl I3 { } /* As before, an absent environment variable is not an error. */ - let username = Self::get_username().await?; + let username = get_username().await?; let tmp_path: PathBuf = "/tmp/".into(); match Self::try_tmp_directory(&tmp_path, &username).await { Ok(Some(socket)) => { return Ok(socket); } @@ -98,30 +93,6 @@ impl I3 { Err(Error::custom("i3 window manager socket not found")) } - async fn get_username() -> Result { - let uid: Uid = getuid(); - - let mut buffer: [i8; 512] = [0; 512]; - let mut entry: MaybeUninit = MaybeUninit::uninit(); - let mut result = null_mut(); - - let error = unsafe { - getpwuid_r(uid.as_raw(), - &raw mut entry as *mut passwd, - &raw mut buffer as *mut i8, - size_of_val(&buffer), - &mut result) - }; - if error != 0 { - return Err(Errno::from_raw_os_error(error).into()); - } - - let name = unsafe { entry.assume_init() }.pw_name; - let name = unsafe { CStr::from_ptr(name) }; - - Ok(name.to_string_lossy().into_owned()) - } - /* One of the ways to find an i3 socket is to look within the tmp directory * for subdirectories with names having a specific pattern. Do that. */ diff --git a/src/main.rs b/src/main.rs index 8d3adda..c1e7323 100644 --- a/src/main.rs +++ b/src/main.rs @@ -48,6 +48,7 @@ mod geometry; mod layout; mod module; mod property_list; +mod unix; mod x; mod xdg; diff --git a/src/unix.rs b/src/unix.rs new file mode 100644 index 0000000..5561292 --- /dev/null +++ b/src/unix.rs @@ -0,0 +1,35 @@ +use crate::error::Result; + +use std::ffi::CStr; +use std::mem::{ size_of_val, MaybeUninit }; +use std::ptr::null_mut; + +use libc::{ getpwuid_r, passwd }; +use rustix::io::Errno; +use rustix::process::{ getuid, Uid }; + + +pub async fn get_username() -> Result { + let uid: Uid = getuid(); + + let mut buffer: [i8; 512] = [0; 512]; + let mut entry: MaybeUninit = MaybeUninit::uninit(); + let mut result = null_mut(); + + let error = unsafe { + getpwuid_r(uid.as_raw(), + &raw mut entry as *mut passwd, + &raw mut buffer as *mut i8, + size_of_val(&buffer), + &mut result) + }; + if error != 0 { + return Err(Errno::from_raw_os_error(error).into()); + } + + let name = unsafe { entry.assume_init() }.pw_name; + let name = unsafe { CStr::from_ptr(name) }; + + Ok(name.to_string_lossy().into_owned()) +} + -- cgit 1.4.1