@@ -277,7 +277,7 @@ impl CgroupConfiguration {
277
277
// the file no longer being empty, regardless of who actually got to populated its contents.
278
278
279
279
fn inherit_from_parent_aux (
280
- path : & mut PathBuf ,
280
+ path : & Path ,
281
281
file_name : & str ,
282
282
retry_depth : u16 ,
283
283
) -> Result < ( ) , JailerError > {
@@ -296,7 +296,7 @@ fn inherit_from_parent_aux(
296
296
297
297
// Trying to avoid the race condition described above. We don't care about the result,
298
298
// because we check once more if line.is_empty() after the end of this block.
299
- let _ = inherit_from_parent_aux ( & mut parent. to_path_buf ( ) , file_name, retry_depth - 1 ) ;
299
+ let _ = inherit_from_parent_aux ( parent, file_name, retry_depth - 1 ) ;
300
300
line = readln_special ( & parent_file) ?;
301
301
}
302
302
@@ -308,16 +308,12 @@ fn inherit_from_parent_aux(
308
308
}
309
309
}
310
310
311
- path. push ( file_name) ;
312
- writeln_special ( & path, & line) ?;
313
- path. pop ( ) ;
311
+ writeln_special ( & path. join ( file_name) , & line) ?;
314
312
315
313
Ok ( ( ) )
316
314
}
317
315
318
- // The path reference is &mut here because we do a push to get the destination file name. However,
319
- // a pop follows shortly after (see fn inherit_from_parent_aux), reverting to the original value.
320
- fn inherit_from_parent ( path : & mut PathBuf , file_name : & str , depth : u16 ) -> Result < ( ) , JailerError > {
316
+ fn inherit_from_parent ( path : & Path , file_name : & str , depth : u16 ) -> Result < ( ) , JailerError > {
321
317
inherit_from_parent_aux ( path, file_name, depth)
322
318
}
323
319
@@ -362,19 +358,15 @@ impl Cgroup for CgroupV1 {
362
358
}
363
359
364
360
fn write_values ( & self ) -> Result < ( ) , JailerError > {
365
- let location = & mut self . base . location . clone ( ) ;
366
-
367
361
// Create the cgroup directory for the controller.
368
362
fs:: create_dir_all ( & self . base . location )
369
363
. map_err ( |err| JailerError :: CreateDir ( self . base . location . clone ( ) , err) ) ?;
370
364
371
365
for property in self . base . properties . iter ( ) {
372
- let file_location = & mut location. clone ( ) ;
373
366
// Write the corresponding cgroup value. inherit_from_parent is used to
374
367
// correctly propagate the value if not defined.
375
- inherit_from_parent ( location, & property. file , self . cg_parent_depth ) ?;
376
- file_location. push ( & property. file ) ;
377
- writeln_special ( file_location, & property. value ) ?;
368
+ inherit_from_parent ( & self . base . location , & property. file , self . cg_parent_depth ) ?;
369
+ writeln_special ( & self . base . location . join ( & property. file ) , & property. value ) ?;
378
370
}
379
371
380
372
Ok ( ( ) )
@@ -466,15 +458,14 @@ impl Cgroup for CgroupV2 {
466
458
}
467
459
468
460
fn write_values ( & self ) -> Result < ( ) , JailerError > {
469
- let location = & mut self . base . location . clone ( ) ;
470
461
let mut enabled_controllers: HashSet < & str > = HashSet :: new ( ) ;
471
462
472
463
// Create the cgroup directory for the controller.
473
464
fs:: create_dir_all ( & self . base . location )
474
465
. map_err ( |err| JailerError :: CreateDir ( self . base . location . clone ( ) , err) ) ?;
475
466
476
467
// Ok to unwrap since the path was just created.
477
- let parent = location. parent ( ) . unwrap ( ) ;
468
+ let parent = self . base . location . parent ( ) . unwrap ( ) ;
478
469
479
470
for property in self . base . properties . iter ( ) {
480
471
let controller = get_controller_from_filename ( & property. file ) ?;
@@ -484,9 +475,7 @@ impl Cgroup for CgroupV2 {
484
475
CgroupV2 :: write_all_subtree_control ( parent, controller) ?;
485
476
enabled_controllers. insert ( controller) ;
486
477
}
487
- let file_location = & mut location. clone ( ) ;
488
- file_location. push ( & property. file ) ;
489
- writeln_special ( file_location, & property. value ) ?;
478
+ writeln_special ( & self . base . location . join ( & property. file ) , & property. value ) ?;
490
479
}
491
480
492
481
Ok ( ( ) )
@@ -833,8 +822,8 @@ mod tests {
833
822
let dir = TempDir :: new ( ) . expect ( "Cannot create temporary directory." ) ;
834
823
// This is /A/B/C .
835
824
let dir2 = TempDir :: new_in ( dir. as_path ( ) ) . expect ( "Cannot create temporary directory." ) ;
836
- let mut path2 = PathBuf :: from ( dir2. as_path ( ) ) ;
837
- let result = inherit_from_parent ( & mut PathBuf :: from ( & path2) , "inexistent" , 1 ) ;
825
+ let path2 = PathBuf :: from ( dir2. as_path ( ) ) ;
826
+ let result = inherit_from_parent ( & path2, "inexistent" , 1 ) ;
838
827
assert ! (
839
828
matches!( result, Err ( JailerError :: ReadToString ( _, _) ) ) ,
840
829
"{:?}" ,
@@ -844,11 +833,7 @@ mod tests {
844
833
// 2. If parent file exists and is empty, will go one level up, and return error because
845
834
// the grandparent file does not exist.
846
835
let named_file = TempFile :: new_in ( dir. as_path ( ) ) . expect ( "Cannot create named file." ) ;
847
- let result = inherit_from_parent (
848
- & mut path2. clone ( ) ,
849
- named_file. as_path ( ) . to_str ( ) . unwrap ( ) ,
850
- 1 ,
851
- ) ;
836
+ let result = inherit_from_parent ( & path2, named_file. as_path ( ) . to_str ( ) . unwrap ( ) , 1 ) ;
852
837
assert ! (
853
838
matches!( result, Err ( JailerError :: CgroupInheritFromParent ( _, _) ) ) ,
854
839
"{:?}" ,
@@ -861,7 +846,7 @@ mod tests {
861
846
// contents.
862
847
let some_line = "Parent line" ;
863
848
writeln ! ( named_file. as_file( ) , "{}" , some_line) . expect ( "Cannot write to file." ) ;
864
- let result = inherit_from_parent ( & mut path2, named_file. as_path ( ) . to_str ( ) . unwrap ( ) , 1 ) ;
849
+ let result = inherit_from_parent ( & path2, named_file. as_path ( ) . to_str ( ) . unwrap ( ) , 1 ) ;
865
850
result. unwrap ( ) ;
866
851
let res = readln_special ( & child_file) . expect ( "Cannot read from file." ) ;
867
852
assert ! ( res == some_line) ;
0 commit comments