Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 91 additions & 2 deletions src-tauri/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4598,6 +4598,16 @@ mod win_job {
SetInformationJobObject(job, JobObjectExtendedLimitInformation, &info as *const _ as *const std::ffi::c_void, size)
}

/// Whether a process is still in the job: one the step started and left running, after the
/// step itself ended.
pub fn has_processes(job: &Job) -> bool {
use windows::Win32::System::JobObjects::{JobObjectBasicAccountingInformation, QueryInformationJobObject, JOBOBJECT_BASIC_ACCOUNTING_INFORMATION};
let mut info = JOBOBJECT_BASIC_ACCOUNTING_INFORMATION::default();
let size = std::mem::size_of::<JOBOBJECT_BASIC_ACCOUNTING_INFORMATION>() as u32;
let asked = unsafe { QueryInformationJobObject(Some(job.0), JobObjectBasicAccountingInformation, &mut info as *mut _ as *mut std::ffi::c_void, size, None) };
asked.is_ok() && info.ActiveProcesses > 0
}

/// None when Windows won't make the job: the process then runs as it did before, outside one.
pub fn kill_on_close(child: &std::process::Child) -> Option<Job> {
use std::os::windows::io::AsRawHandle;
Expand Down Expand Up @@ -4737,6 +4747,18 @@ fn spawn_just_written(c: &mut std::process::Command) -> std::io::Result<std::pro
/// Runs the recomp's setup step. `writing` is called once the step has started, when it can
/// write over the last build; a step that can't start (its program missing, no room, no Proton)
/// never calls it. `out` gets every line it prints (the full log the player can open or send).
/// Whether a process is left in a process group: the setup step's (process_group(0) made the
/// step's id the group's), after the step itself ended.
#[cfg(unix)]
fn group_has_processes(group: u32) -> bool {
extern "C" {
fn kill(pid: std::ffi::c_int, sig: std::ffi::c_int) -> std::ffi::c_int;
}
let Ok(group) = std::ffi::c_int::try_from(group) else { return false };
// Signal 0 sends nothing: it only says whether the group still has a process in it.
unsafe { kill(-group, 0) == 0 }
}

fn run_prepare(pr: &profile::Prepare, install: &Path, copy: Option<&Path>, extra: &[String], report: &dyn Fn(f32, String), stopped: &dyn Fn() -> bool, writing: &dyn Fn(), out: Option<&std::fs::File>) -> Result<(), String> {
use std::io::{BufRead, Write};
let exe = pr.program_in(install).ok_or("Its setup program is missing. Reinstall the game.")?;
Expand All @@ -4748,7 +4770,7 @@ fn run_prepare(pr: &profile::Prepare, install: &Path, copy: Option<&Path>, extra
return Err(setup_temp::NO_ROOM.into());
}
// A temp folder of its own on the game's drive, taken away however this returns.
let temp = setup_temp::SetupTemp::make(install)?;
let mut temp = setup_temp::SetupTemp::make(install)?;
let data = profile::data_dir(install);
let _ = std::fs::create_dir_all(&data);
// A Windows step on Linux (a Windows-only game's builder): through the same Proton the game
Expand Down Expand Up @@ -4803,7 +4825,7 @@ fn run_prepare(pr: &profile::Prepare, install: &Path, copy: Option<&Path>, extra
// On Windows the same, through a job that ends the step (and what it started) when the
// launcher is gone. Held until the step is.
#[cfg(windows)]
let _job = win_job::kill_on_close(&child);
let job = win_job::kill_on_close(&child);
let (tx, rx) = std::sync::mpsc::channel::<String>();
if let Some(o) = child.stdout.take() {
let tx = tx.clone();
Expand Down Expand Up @@ -4849,6 +4871,15 @@ fn run_prepare(pr: &profile::Prepare, install: &Path, copy: Option<&Path>, extra
loop {
read(&mut said, &mut too_long, &mut heard);
if let Ok(Some(s)) = child.try_wait() {
// What the step started and left running (a build it handed off to) goes on with the
// temp folder it was given, so the folder stays until the next setup run.
#[cfg(unix)]
let left_running = group_has_processes(child.id());
#[cfg(windows)]
let left_running = job.as_ref().is_some_and(win_job::has_processes);
if left_running {
temp.leave();
}
if s.success() {
return Ok(());
}
Expand Down Expand Up @@ -12329,6 +12360,64 @@ mod tests {
mod prepare_env_tests {
use super::*;

fn step_named(dir: &Path, name: &str, script: &str) -> profile::Prepare {
std::fs::write(dir.join(name), script).unwrap();
#[cfg(unix)]
{
use std::os::unix::fs::PermissionsExt;
std::fs::set_permissions(dir.join(name), std::fs::Permissions::from_mode(0o755)).unwrap();
}
profile::Prepare { program: vec![name.into()], args: vec![], label: "Step".into(), options: vec![], version: String::new(), done: vec![], env: BTreeMap::new(), keeps_copy: false }
}

fn run_step(dir: &Path, pr: &profile::Prepare) -> Result<(), String> {
run_prepare(pr, dir, None, &[], &|_, _| {}, &|| false, &|| {}, None)
}

/// What a program the step left running wrote when it looked for its temp folder a second
/// after the step ended ("kept" or "gone"), waiting up to ten seconds for it.
fn what_the_helper_found(dir: &Path) -> String {
let until = Instant::now() + std::time::Duration::from_secs(10);
while Instant::now() < until {
let said = std::fs::read_to_string(dir.join("helper.txt")).unwrap_or_default();
if !said.trim().is_empty() {
return said.trim().to_string();
}
std::thread::sleep(std::time::Duration::from_millis(100));
}
String::new()
}

#[test]
fn a_program_the_step_left_running_keeps_its_temp_folder() {
let dir = std::env::temp_dir().join(format!("rl-prep-helper-{}", std::process::id()));
let _ = std::fs::remove_dir_all(&dir);
std::fs::create_dir_all(&dir).unwrap();
#[cfg(windows)]
let pr = step_named(&dir, "handoff.bat", "@echo off\r\nstart \"\" /b cmd /c \"ping -n 2 127.0.0.1 >nul & if exist \"%TEMP%\\\" (echo kept>helper.txt) else (echo gone>helper.txt)\"\r\nexit /b 0\r\n");
#[cfg(not(windows))]
let pr = step_named(&dir, "handoff.sh", "#!/bin/sh\n( sleep 1; if [ -d \"$TMPDIR\" ]; then echo kept; else echo gone; fi > helper.txt ) &\nexit 0\n");
run_step(&dir, &pr).unwrap();
let found = what_the_helper_found(&dir);
let _ = std::fs::remove_dir_all(&dir);
assert_eq!(found, "kept");
}

#[test]
fn a_step_that_leaves_nothing_running_takes_its_temp_folder_away() {
let dir = std::env::temp_dir().join(format!("rl-prep-alone-{}", std::process::id()));
let _ = std::fs::remove_dir_all(&dir);
std::fs::create_dir_all(&dir).unwrap();
#[cfg(windows)]
let pr = step_named(&dir, "alone.bat", "@echo off\r\nexit /b 0\r\n");
#[cfg(not(windows))]
let pr = step_named(&dir, "alone.sh", "#!/bin/sh\nexit 0\n");
run_step(&dir, &pr).unwrap();
let gone = !dir.join(setup_temp::FOLDER).exists();
let _ = std::fs::remove_dir_all(&dir);
assert!(gone);
}

/// A setup step that writes down what it was given: its temp folder (and whether it was
/// there), the launcher's names and one of its own from prepare.env.
fn tattler(dir: &Path) -> profile::Prepare {
Expand Down
13 changes: 10 additions & 3 deletions src-tauri/src/setup_temp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,10 @@ pub const CANT_MAKE: &str = "Couldn't make a temp folder for setup in the game's
pub const TOO_LONG: &str = "A path in the game's folder is too long for Windows. Add a library folder with a shorter path in Settings → Games and Folders and move the game there, or turn on Windows' long paths setting (LongPathsEnabled), then try again. The launcher doesn't change that setting for you.";

/// One setup run's temp folder. Dropping it takes the folder away, whether the run worked, failed
/// or was stopped.
/// or was stopped, unless it was left for programs the run started (`leave`).
pub struct SetupTemp {
dir: PathBuf,
left: bool,
}

impl SetupTemp {
Expand All @@ -39,7 +40,13 @@ impl SetupTemp {
}
std::fs::create_dir_all(&dir).map_err(|_| CANT_MAKE)?;
std::fs::write(dir.join(MARK), b"").map_err(|_| CANT_MAKE)?;
Ok(SetupTemp { dir })
Ok(SetupTemp { dir, left: false })
}

/// Kept when the run ends: a program the step started (a build it handed off to) still runs and
/// still writes there. The next setup run empties it (`make`).
pub fn leave(&mut self) {
self.left = true;
}

pub fn path(&self) -> &Path {
Expand All @@ -54,7 +61,7 @@ impl SetupTemp {

impl Drop for SetupTemp {
fn drop(&mut self) {
if made_here(&self.dir) {
if !self.left && made_here(&self.dir) {
let _ = std::fs::remove_dir_all(&self.dir);
}
}
Expand Down
Loading