@@ -14,10 +14,6 @@ use std::collections::{HashMap, HashSet};
14
14
use std:: fs:: { create_dir_all, remove_dir_all, remove_file} ;
15
15
16
16
pub fn setup ( global_config : & GlobalConfig , plans : Vec < Plan > ) -> AnyhowResult < Vec < Plan > > {
17
- if global_config. working_directory . exists ( ) {
18
- remove_dir_all ( & global_config. working_directory )
19
- . context ( "Failed to remove working directory" ) ?;
20
- }
21
17
create_dir_all ( & global_config. working_directory )
22
18
. context ( "Failed to create working directory" ) ?;
23
19
setup_results_directories ( global_config, & plans) ?;
@@ -31,6 +27,8 @@ fn setup_working_directories(
31
27
global_config : & GlobalConfig ,
32
28
plans : Vec < Plan > ,
33
29
) -> AnyhowResult < Vec < Plan > > {
30
+ clean_up_working_directories ( global_config, & plans) ?;
31
+
34
32
let ( surviving_plans, plan_failures) = setup_plans_working_directory ( plans) ;
35
33
let ( surviving_plans, rcc_failures) =
36
34
setup_rcc_working_directories ( & global_config. working_directory , surviving_plans) ;
@@ -49,6 +47,33 @@ fn setup_working_directories(
49
47
Ok ( surviving_plans)
50
48
}
51
49
50
+ fn clean_up_working_directories ( global_config : & GlobalConfig , plans : & [ Plan ] ) -> AnyhowResult < ( ) > {
51
+ for dir_to_be_removed in [
52
+ environment_building_working_directory ( & global_config. working_directory ) ,
53
+ rcc_setup_working_directory ( & global_config. working_directory ) ,
54
+ ] {
55
+ if dir_to_be_removed. exists ( ) {
56
+ remove_dir_all ( dir_to_be_removed) ?;
57
+ }
58
+ }
59
+
60
+ let plan_working_directory = & global_config. working_directory . join ( "plans" ) ;
61
+ if !plan_working_directory. is_dir ( ) {
62
+ return Ok ( ( ) ) ;
63
+ }
64
+
65
+ let plan_working_directories_to_keep =
66
+ HashSet :: < & Utf8PathBuf > :: from_iter ( plans. iter ( ) . map ( |plan| & plan. working_directory ) ) ;
67
+
68
+ for entry in top_level_directories ( plan_working_directory) ? {
69
+ if !plan_working_directories_to_keep. contains ( & entry) {
70
+ remove_dir_all ( & entry) ?;
71
+ }
72
+ }
73
+
74
+ Ok ( ( ) )
75
+ }
76
+
52
77
fn setup_plans_working_directory ( plans : Vec < Plan > ) -> ( Vec < Plan > , HashMap < String , String > ) {
53
78
let mut surviving_plans = Vec :: new ( ) ;
54
79
let mut failures = HashMap :: new ( ) ;
@@ -235,26 +260,34 @@ fn clean_up_plan_results_directory(
235
260
Ok ( ( ) )
236
261
}
237
262
238
- fn top_level_files ( directory : & Utf8Path ) -> AnyhowResult < Vec < Utf8PathBuf > > {
239
- let mut result_files = vec ! [ ] ;
263
+ fn top_level_directories ( directory : & Utf8Path ) -> anyhow:: Result < Vec < Utf8PathBuf > > {
264
+ Ok ( top_level_directory_entries ( directory) ?
265
+ . into_iter ( )
266
+ . filter ( |path| path. is_dir ( ) )
267
+ . collect ( ) )
268
+ }
240
269
241
- for dir_entry in directory. read_dir_utf8 ( ) . context ( format ! (
242
- "Failed to read entries of results directory {directory}" ,
243
- ) ) ? {
244
- let dir_entry = dir_entry. context ( format ! (
245
- "Failed to read entries of results directory {directory}" ,
246
- ) ) ?;
247
- if dir_entry
248
- . file_type ( )
249
- . context ( format ! (
250
- "Failed to determine file type of {}" ,
251
- dir_entry. path( )
252
- ) ) ?
253
- . is_file ( )
254
- {
255
- result_files. push ( dir_entry. path ( ) . to_path_buf ( ) )
256
- }
270
+ fn top_level_files ( directory : & Utf8Path ) -> anyhow:: Result < Vec < Utf8PathBuf > > {
271
+ Ok ( top_level_directory_entries ( directory) ?
272
+ . into_iter ( )
273
+ . filter ( |path| path. is_file ( ) )
274
+ . collect ( ) )
275
+ }
276
+
277
+ fn top_level_directory_entries ( directory : & Utf8Path ) -> anyhow:: Result < Vec < Utf8PathBuf > > {
278
+ let mut entries = vec ! [ ] ;
279
+
280
+ for dir_entry in directory
281
+ . read_dir_utf8 ( )
282
+ . context ( format ! ( "Failed to read entries of directory {directory}" , ) ) ?
283
+ {
284
+ entries. push (
285
+ dir_entry
286
+ . context ( format ! ( "Failed to read entries of directory {directory}" , ) ) ?
287
+ . path ( )
288
+ . to_path_buf ( ) ,
289
+ )
257
290
}
258
291
259
- Ok ( result_files )
292
+ Ok ( entries )
260
293
}
0 commit comments