Skip to content

Commit

Permalink
Restructure directories setup a bit to improve readability
Browse files Browse the repository at this point in the history
  • Loading branch information
jherbel committed Nov 18, 2024
1 parent d5b1255 commit 591086f
Showing 1 changed file with 51 additions and 41 deletions.
92 changes: 51 additions & 41 deletions src/bin/scheduler/setup/directories.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,32 +25,65 @@ pub fn setup(
transfer_directory_ownership_recursive(&global_config.runtime_base_directory)?;
#[cfg(windows)]
reset_access(&global_config.runtime_base_directory)?;
create_dir_all(&global_config.working_directory)?;
create_dir_all(plans_working_directory(&global_config.working_directory))?;
for working_sub_dir in [
rcc_setup_working_directory(&global_config.working_directory),
environment_building_directory(&global_config.working_directory),
] {
if working_sub_dir.exists() {
remove_dir_all(&working_sub_dir)?;
}
create_dir_all(&working_sub_dir)?;
}

setup_working_directory(&global_config.working_directory, &plans)?;
setup_managed_directory(&global_config.managed_directory)?;
setup_results_directory(global_config, &plans)?;

Ok(run_setup_steps(global_config, plans))
}

fn setup_working_directory(working_directory: &Utf8Path, plans: &[Plan]) -> AnyhowResult<()> {
create_dir_all(working_directory)?;
create_dir_all(plans_working_directory(working_directory))?;
clean_up_file_system_entries(
plans.iter().map(|plan| &plan.working_directory),
top_level_directories(&plans_working_directory(&global_config.working_directory))?.iter(),
top_level_directories(&plans_working_directory(working_directory))?.iter(),
)?;
if global_config.managed_directory.exists() {
remove_dir_all(&global_config.managed_directory)?;
for dir_to_be_reset in [
rcc_setup_working_directory(working_directory),
environment_building_directory(working_directory),
] {
if dir_to_be_reset.exists() {
remove_dir_all(&dir_to_be_reset)?;
}
create_dir_all(&dir_to_be_reset)?;
}
create_dir_all(&global_config.managed_directory)?;
Ok(())
}

fn setup_managed_directory(managed_directory: &Utf8Path) -> AnyhowResult<()> {
if managed_directory.exists() {
remove_dir_all(managed_directory)?;
}
create_dir_all(managed_directory)?;
Ok(())
}

setup_results_directories(global_config, &plans)?;
fn setup_results_directory(global_config: &GlobalConfig, plans: &[Plan]) -> AnyhowResult<()> {
create_dir_all(&global_config.results_directory)?;
create_dir_all(plan_results_directory(&global_config.results_directory))?;
clean_up_results_directory(global_config, plans).context("Failed to clean up results directory")
}

Ok(run_setup(global_config, plans))
fn clean_up_results_directory(
global_config: &GlobalConfig,
plans: &[Plan],
) -> Result<(), Terminate> {
let results_directory_lock = global_config
.results_directory_locker
.wait_for_write_lock()?;
for path in top_level_files(&global_config.results_directory)? {
remove_file(path)?;
}
clean_up_file_system_entries(
plans.iter().map(|plan| &plan.results_file),
top_level_files(&plan_results_directory(&global_config.results_directory))?.iter(),
)?;
Ok(results_directory_lock.release()?)
}

fn run_setup(config: &GlobalConfig, mut plans: Vec<Plan>) -> (Vec<Plan>, Vec<SetupFailure>) {
fn run_setup_steps(config: &GlobalConfig, mut plans: Vec<Plan>) -> (Vec<Plan>, Vec<SetupFailure>) {
let gather_requirements = [
gather_managed_directories,
#[cfg(windows)]
Expand Down Expand Up @@ -278,12 +311,6 @@ fn gather_rcc_longpath_directory(config: &GlobalConfig, plans: Vec<Plan>) -> Vec
]
}

fn setup_results_directories(global_config: &GlobalConfig, plans: &[Plan]) -> AnyhowResult<()> {
create_dir_all(&global_config.results_directory)?;
create_dir_all(plan_results_directory(&global_config.results_directory))?;
clean_up_results_directory(global_config, plans).context("Failed to clean up results directory")
}

fn gather_managed_directories(
_global_config: &GlobalConfig,
plans: Vec<Plan>,
Expand All @@ -307,23 +334,6 @@ fn gather_managed_directories(
setup_steps
}

fn clean_up_results_directory(
global_config: &GlobalConfig,
plans: &[Plan],
) -> Result<(), Terminate> {
let results_directory_lock = global_config
.results_directory_locker
.wait_for_write_lock()?;
for path in top_level_files(&global_config.results_directory)? {
remove_file(path)?;
}
clean_up_file_system_entries(
plans.iter().map(|plan| &plan.results_file),
top_level_files(&plan_results_directory(&global_config.results_directory))?.iter(),
)?;
Ok(results_directory_lock.release()?)
}

#[cfg(unix)]
pub fn transfer_directory_ownership_recursive(target: &Utf8Path) -> AnyhowResult<()> {
let user_id = unsafe { libc::getuid() };
Expand Down

0 comments on commit 591086f

Please sign in to comment.