Expand ~ in spawn

This commit is contained in:
Ivan Molodetskikh
2024-02-24 09:16:44 +04:00
parent ab9d1aab4e
commit e278e871c3
2 changed files with 26 additions and 6 deletions
+12 -4
View File
@@ -1,7 +1,7 @@
use std::ffi::{CString, OsStr};
use std::io::Write;
use std::os::unix::prelude::OsStrExt;
use std::path::PathBuf;
use std::path::{Path, PathBuf};
use std::ptr::null_mut;
use std::sync::atomic::AtomicBool;
use std::time::Duration;
@@ -50,6 +50,15 @@ pub fn output_size(output: &Output) -> Size<i32, Logical> {
.to_logical(output_scale)
}
pub fn expand_home(path: &Path) -> anyhow::Result<Option<PathBuf>> {
if let Ok(rest) = path.strip_prefix("~") {
let dirs = UserDirs::new().context("error retrieving home directory")?;
Ok(Some([dirs.home_dir(), rest].iter().collect()))
} else {
Ok(None)
}
}
pub fn make_screenshot_path(config: &Config) -> anyhow::Result<Option<PathBuf>> {
let Some(path) = &config.screenshot_path else {
return Ok(None);
@@ -72,9 +81,8 @@ pub fn make_screenshot_path(config: &Config) -> anyhow::Result<Option<PathBuf>>
path = PathBuf::from(OsStr::from_bytes(&buf[..rv]));
}
if let Ok(rest) = path.strip_prefix("~") {
let dirs = UserDirs::new().context("error retrieving home directory")?;
path = [dirs.home_dir(), rest].iter().collect();
if let Some(expanded) = expand_home(&path).context("error expanding ~")? {
path = expanded;
}
Ok(Some(path))
+14 -2
View File
@@ -1,6 +1,7 @@
use std::ffi::OsStr;
use std::os::fd::{AsFd, AsRawFd, FromRawFd, OwnedFd};
use std::os::unix::process::CommandExt;
use std::path::Path;
use std::process::{Command, Stdio};
use std::sync::atomic::{AtomicBool, Ordering};
use std::{io, thread};
@@ -10,6 +11,8 @@ use smithay::reexports::rustix;
use smithay::reexports::rustix::io::{close, read, retry_on_intr, write};
use smithay::reexports::rustix::pipe::{pipe_with, PipeFlags};
use crate::utils::expand_home;
pub static REMOVE_ENV_RUST_BACKTRACE: AtomicBool = AtomicBool::new(false);
pub static REMOVE_ENV_RUST_LIB_BACKTRACE: AtomicBool = AtomicBool::new(false);
@@ -37,7 +40,17 @@ pub fn spawn<T: AsRef<OsStr> + Send + 'static>(command: Vec<T>) {
fn spawn_sync(command: impl AsRef<OsStr>, args: impl IntoIterator<Item = impl AsRef<OsStr>>) {
let _span = tracy_client::span!();
let command = command.as_ref();
let mut command = command.as_ref();
// Expand `~` at the start.
let expanded = expand_home(Path::new(command));
match &expanded {
Ok(Some(expanded)) => command = expanded.as_ref(),
Ok(None) => (),
Err(err) => {
warn!("error expanding ~: {err:?}");
}
}
let mut process = Command::new(command);
process
@@ -221,7 +234,6 @@ fn read_all(fd: impl AsFd, buf: &mut [u8]) -> rustix::io::Result<()> {
fn start_systemd_scope(name: &OsStr, intermediate_pid: u32, child_pid: u32) -> anyhow::Result<()> {
use std::fmt::Write as _;
use std::os::unix::ffi::OsStrExt;
use std::path::Path;
use std::sync::OnceLock;
use anyhow::Context;