forge
Explore
Log in

Don't you dare go Hollow

e278e871c357c4832f29629e4f65f57ff0d0ded0Ivan MolodetskikhParent: ab9d1aa
src/utils/mod.rsmodified+12 -4
@@ -1,7 +1,7 @@
1 use std::ffi::{CString, OsStr};1 use std::ffi::{CString, OsStr};
2 use std::io::Write;2 use std::io::Write;
3 use std::os::unix::prelude::OsStrExt;3 use std::os::unix::prelude::OsStrExt;
4-use std::path::PathBuf;4+use std::path::{Path, PathBuf};
5 use std::ptr::null_mut;5 use std::ptr::null_mut;
6 use std::sync::atomic::AtomicBool;6 use std::sync::atomic::AtomicBool;
7 use std::time::Duration;7 use std::time::Duration;
@@ -50,6 +50,15 @@ pub fn output_size(output: &Output) -> Size<i32, Logical> {
50 .to_logical(output_scale)50 .to_logical(output_scale)
51 }51 }
52 52
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+
53 pub fn make_screenshot_path(config: &Config) -> anyhow::Result<Option<PathBuf>> {62 pub fn make_screenshot_path(config: &Config) -> anyhow::Result<Option<PathBuf>> {
54 let Some(path) = &config.screenshot_path else {63 let Some(path) = &config.screenshot_path else {
55 return Ok(None);64 return Ok(None);
@@ -72,9 +81,8 @@ pub fn make_screenshot_path(config: &Config) -> anyhow::Result<Option<PathBuf>>
72 path = PathBuf::from(OsStr::from_bytes(&buf[..rv]));81 path = PathBuf::from(OsStr::from_bytes(&buf[..rv]));
73 }82 }
74 83
75- if let Ok(rest) = path.strip_prefix("~") {84+ if let Some(expanded) = expand_home(&path).context("error expanding ~")? {
76- let dirs = UserDirs::new().context("error retrieving home directory")?;85+ path = expanded;
77- path = [dirs.home_dir(), rest].iter().collect();
78 }86 }
79 87
80 Ok(Some(path))88 Ok(Some(path))
src/utils/spawning.rsmodified+14 -2
@@ -1,6 +1,7 @@
1 use std::ffi::OsStr;1 use std::ffi::OsStr;
2 use std::os::fd::{AsFd, AsRawFd, FromRawFd, OwnedFd};2 use std::os::fd::{AsFd, AsRawFd, FromRawFd, OwnedFd};
3 use std::os::unix::process::CommandExt;3 use std::os::unix::process::CommandExt;
4+use std::path::Path;
4 use std::process::{Command, Stdio};5 use std::process::{Command, Stdio};
5 use std::sync::atomic::{AtomicBool, Ordering};6 use std::sync::atomic::{AtomicBool, Ordering};
6 use std::{io, thread};7 use std::{io, thread};
@@ -10,6 +11,8 @@ use smithay::reexports::rustix;
10 use smithay::reexports::rustix::io::{close, read, retry_on_intr, write};11 use smithay::reexports::rustix::io::{close, read, retry_on_intr, write};
11 use smithay::reexports::rustix::pipe::{pipe_with, PipeFlags};12 use smithay::reexports::rustix::pipe::{pipe_with, PipeFlags};
12 13
14+use crate::utils::expand_home;
15+
13 pub static REMOVE_ENV_RUST_BACKTRACE: AtomicBool = AtomicBool::new(false);16 pub static REMOVE_ENV_RUST_BACKTRACE: AtomicBool = AtomicBool::new(false);
14 pub static REMOVE_ENV_RUST_LIB_BACKTRACE: AtomicBool = AtomicBool::new(false);17 pub static REMOVE_ENV_RUST_LIB_BACKTRACE: AtomicBool = AtomicBool::new(false);
15 18
@@ -37,7 +40,17 @@ pub fn spawn<T: AsRef<OsStr> + Send + 'static>(command: Vec<T>) {
37 fn spawn_sync(command: impl AsRef<OsStr>, args: impl IntoIterator<Item = impl AsRef<OsStr>>) {40 fn spawn_sync(command: impl AsRef<OsStr>, args: impl IntoIterator<Item = impl AsRef<OsStr>>) {
38 let _span = tracy_client::span!();41 let _span = tracy_client::span!();
39 42
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+ }
41 54
42 let mut process = Command::new(command);55 let mut process = Command::new(command);
43 process56 process
@@ -221,7 +234,6 @@ fn read_all(fd: impl AsFd, buf: &mut [u8]) -> rustix::io::Result<()> {
221 fn start_systemd_scope(name: &OsStr, intermediate_pid: u32, child_pid: u32) -> anyhow::Result<()> {234 fn start_systemd_scope(name: &OsStr, intermediate_pid: u32, child_pid: u32) -> anyhow::Result<()> {
222 use std::fmt::Write as _;235 use std::fmt::Write as _;
223 use std::os::unix::ffi::OsStrExt;236 use std::os::unix::ffi::OsStrExt;
224- use std::path::Path;
225 use std::sync::OnceLock;237 use std::sync::OnceLock;
226 238
227 use anyhow::Context;239 use anyhow::Context;