Expand ~ in spawn
Browse files at this commite278e871c357c4832f29629e4f65f57ff0d0ded0Parent: ab9d1aa2 changed files +26 -6
- src/utils/mod.rs +12 -4
- src/utils/spawning.rs +14 -2
src/utils/mod.rsmodified+12 -4
| @@ -1,7 +1,7 @@ | ||
| 1 | 1 | use std::ffi::{CString, OsStr}; |
| 2 | 2 | use std::io::Write; |
| 3 | 3 | use std::os::unix::prelude::OsStrExt; |
| 4 | -use std::path::PathBuf; | |
| 4 | +use std::path::{Path, PathBuf}; | |
| 5 | 5 | use std::ptr::null_mut; |
| 6 | 6 | use std::sync::atomic::AtomicBool; |
| 7 | 7 | use std::time::Duration; |
| @@ -50,6 +50,15 @@ pub fn output_size(output: &Output) -> Size<i32, Logical> { | ||
| 50 | 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 | 62 | pub fn make_screenshot_path(config: &Config) -> anyhow::Result<Option<PathBuf>> { |
| 54 | 63 | let Some(path) = &config.screenshot_path else { |
| 55 | 64 | return Ok(None); |
| @@ -72,9 +81,8 @@ pub fn make_screenshot_path(config: &Config) -> anyhow::Result<Option<PathBuf>> | ||
| 72 | 81 | path = PathBuf::from(OsStr::from_bytes(&buf[..rv])); |
| 73 | 82 | } |
| 74 | 83 | |
| 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; | |
| 78 | 86 | } |
| 79 | 87 | |
| 80 | 88 | Ok(Some(path)) |
src/utils/spawning.rsmodified+14 -2
| @@ -1,6 +1,7 @@ | ||
| 1 | 1 | use std::ffi::OsStr; |
| 2 | 2 | use std::os::fd::{AsFd, AsRawFd, FromRawFd, OwnedFd}; |
| 3 | 3 | use std::os::unix::process::CommandExt; |
| 4 | +use std::path::Path; | |
| 4 | 5 | use std::process::{Command, Stdio}; |
| 5 | 6 | use std::sync::atomic::{AtomicBool, Ordering}; |
| 6 | 7 | use std::{io, thread}; |
| @@ -10,6 +11,8 @@ use smithay::reexports::rustix; | ||
| 10 | 11 | use smithay::reexports::rustix::io::{close, read, retry_on_intr, write}; |
| 11 | 12 | use smithay::reexports::rustix::pipe::{pipe_with, PipeFlags}; |
| 12 | 13 | |
| 14 | +use crate::utils::expand_home; | |
| 15 | + | |
| 13 | 16 | pub static REMOVE_ENV_RUST_BACKTRACE: AtomicBool = AtomicBool::new(false); |
| 14 | 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 | 40 | fn spawn_sync(command: impl AsRef<OsStr>, args: impl IntoIterator<Item = impl AsRef<OsStr>>) { |
| 38 | 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 | 55 | let mut process = Command::new(command); |
| 43 | 56 | process |
| @@ -221,7 +234,6 @@ fn read_all(fd: impl AsFd, buf: &mut [u8]) -> rustix::io::Result<()> { | ||
| 221 | 234 | fn start_systemd_scope(name: &OsStr, intermediate_pid: u32, child_pid: u32) -> anyhow::Result<()> { |
| 222 | 235 | use std::fmt::Write as _; |
| 223 | 236 | use std::os::unix::ffi::OsStrExt; |
| 224 | - use std::path::Path; | |
| 225 | 237 | use std::sync::OnceLock; |
| 226 | 238 | |
| 227 | 239 | use anyhow::Context; |