forge
Explore
Log in

Don't you dare go Hollow

e278e871c357c4832f29629e4f65f57ff0d0ded0Ivan MolodetskikhParent: ab9d1aa
src/utils/mod.rsmodified+12 -4
@@ -1,7 +1,7 @@
11 use std::ffi::{CString, OsStr};
22 use std::io::Write;
33 use std::os::unix::prelude::OsStrExt;
4-use std::path::PathBuf;
4+use std::path::{Path, PathBuf};
55 use std::ptr::null_mut;
66 use std::sync::atomic::AtomicBool;
77 use std::time::Duration;
@@ -50,6 +50,15 @@ pub fn output_size(output: &Output) -> Size<i32, Logical> {
5050 .to_logical(output_scale)
5151 }
5252
53+pub fn expand_home(path: &Path) -> anyhow::Result<Option<PathBuf>> {
54+ if let Ok(rest) = path.strip_prefix("~") {
55+ let dirs = UserDirs::new().context("error retrieving home directory")?;
56+ Ok(Some([dirs.home_dir(), rest].iter().collect()))
57+ } else {
58+ Ok(None)
59+ }
60+}
61+
5362 pub fn make_screenshot_path(config: &Config) -> anyhow::Result<Option<PathBuf>> {
5463 let Some(path) = &config.screenshot_path else {
5564 return Ok(None);
@@ -72,9 +81,8 @@ pub fn make_screenshot_path(config: &Config) -> anyhow::Result<Option<PathBuf>>
7281 path = PathBuf::from(OsStr::from_bytes(&buf[..rv]));
7382 }
7483
75- if let Ok(rest) = path.strip_prefix("~") {
76- let dirs = UserDirs::new().context("error retrieving home directory")?;
77- path = [dirs.home_dir(), rest].iter().collect();
84+ if let Some(expanded) = expand_home(&path).context("error expanding ~")? {
85+ path = expanded;
7886 }
7987
8088 Ok(Some(path))
src/utils/spawning.rsmodified+14 -2
@@ -1,6 +1,7 @@
11 use std::ffi::OsStr;
22 use std::os::fd::{AsFd, AsRawFd, FromRawFd, OwnedFd};
33 use std::os::unix::process::CommandExt;
4+use std::path::Path;
45 use std::process::{Command, Stdio};
56 use std::sync::atomic::{AtomicBool, Ordering};
67 use std::{io, thread};
@@ -10,6 +11,8 @@ use smithay::reexports::rustix;
1011 use smithay::reexports::rustix::io::{close, read, retry_on_intr, write};
1112 use smithay::reexports::rustix::pipe::{pipe_with, PipeFlags};
1213
14+use crate::utils::expand_home;
15+
1316 pub static REMOVE_ENV_RUST_BACKTRACE: AtomicBool = AtomicBool::new(false);
1417 pub static REMOVE_ENV_RUST_LIB_BACKTRACE: AtomicBool = AtomicBool::new(false);
1518
@@ -37,7 +40,17 @@ pub fn spawn<T: AsRef<OsStr> + Send + 'static>(command: Vec<T>) {
3740 fn spawn_sync(command: impl AsRef<OsStr>, args: impl IntoIterator<Item = impl AsRef<OsStr>>) {
3841 let _span = tracy_client::span!();
3942
40- let command = command.as_ref();
43+ let mut command = command.as_ref();
44+
45+ // Expand `~` at the start.
46+ let expanded = expand_home(Path::new(command));
47+ match &expanded {
48+ Ok(Some(expanded)) => command = expanded.as_ref(),
49+ Ok(None) => (),
50+ Err(err) => {
51+ warn!("error expanding ~: {err:?}");
52+ }
53+ }
4154
4255 let mut process = Command::new(command);
4356 process
@@ -221,7 +234,6 @@ fn read_all(fd: impl AsFd, buf: &mut [u8]) -> rustix::io::Result<()> {
221234 fn start_systemd_scope(name: &OsStr, intermediate_pid: u32, child_pid: u32) -> anyhow::Result<()> {
222235 use std::fmt::Write as _;
223236 use std::os::unix::ffi::OsStrExt;
224- use std::path::Path;
225237 use std::sync::OnceLock;
226238
227239 use anyhow::Context;