@@ -305,16 +305,16 @@ fn get_modifications(
305
305
/// [`anyhow::Result`] containing a tuple of directory trees in the order:
306
306
///
307
307
/// 1. `pristine_etc_files` – Dirtree of the pristine etc state
308
- /// 2. `current_etc_files` – Dirtree of the current etc state
309
- /// 3. `new_etc_files` – Dirtree of the new etc state
308
+ /// 2. `current_etc_files` – Dirtree of the current etc state
309
+ /// 3. `new_etc_files` – Dirtree of the new etc state (if new_etc directory is passed)
310
310
pub fn traverse_etc (
311
311
pristine_etc : & CapStdDir ,
312
312
current_etc : & CapStdDir ,
313
- new_etc : & CapStdDir ,
313
+ new_etc : Option < & CapStdDir > ,
314
314
) -> anyhow:: Result < (
315
315
Directory < CustomMetadata > ,
316
316
Directory < CustomMetadata > ,
317
- Directory < CustomMetadata > ,
317
+ Option < Directory < CustomMetadata > > ,
318
318
) > {
319
319
let mut pristine_etc_files = Directory :: default ( ) ;
320
320
recurse_dir ( pristine_etc, & mut pristine_etc_files)
@@ -324,8 +324,16 @@ pub fn traverse_etc(
324
324
recurse_dir ( current_etc, & mut current_etc_files)
325
325
. context ( format ! ( "Recursing {current_etc:?}" ) ) ?;
326
326
327
- let mut new_etc_files = Directory :: default ( ) ;
328
- recurse_dir ( new_etc, & mut new_etc_files) . context ( format ! ( "Recursing {new_etc:?}" ) ) ?;
327
+ let new_etc_files = match new_etc {
328
+ Some ( new_etc) => {
329
+ let mut new_etc_files = Directory :: default ( ) ;
330
+ recurse_dir ( new_etc, & mut new_etc_files) . context ( format ! ( "Recursing {new_etc:?}" ) ) ?;
331
+
332
+ Some ( new_etc_files)
333
+ }
334
+
335
+ None => None ,
336
+ } ;
329
337
330
338
return Ok ( ( pristine_etc_files, current_etc_files, new_etc_files) ) ;
331
339
}
@@ -664,7 +672,7 @@ fn merge_modified_files(
664
672
665
673
let current_inode = dir
666
674
. lookup ( filename)
667
- . ok_or ( anyhow:: anyhow!( "{filename:?} not found" ) ) ?;
675
+ . ok_or_else ( || anyhow:: anyhow!( "{filename:?} not found" ) ) ?;
668
676
669
677
// This will error out if some directory in a chain does not exist
670
678
let res = new_etc_dirtree. split ( OsStr :: new ( & file) ) ;
@@ -734,30 +742,18 @@ pub fn merge(
734
742
for removed in diff. removed {
735
743
let stat = new_etc_fd. metadata_optional ( & removed) ?;
736
744
737
- let stat = match stat {
738
- Some ( s) => s,
739
-
740
- None => {
741
- // File/dir doesn't exist in new_etc
742
- // Basically a no-op
743
- continue ;
744
- }
745
+ let Some ( stat) = stat else {
746
+ // File/dir doesn't exist in new_etc
747
+ // Basically a no-op
748
+ continue ;
745
749
} ;
746
750
747
751
if stat. is_file ( ) || stat. is_symlink ( ) {
748
- match new_etc_fd. remove_file ( & removed) {
749
- Ok ( ..) => { /* no-op */ }
750
- Err ( e) => Err ( e) ?,
751
- }
752
- }
753
-
754
- if stat. is_dir ( ) {
752
+ new_etc_fd. remove_file ( & removed) ?;
753
+ } else if stat. is_dir ( ) {
755
754
// We only add the directory to the removed array, if the entire directory was deleted
756
755
// So `remove_dir_all` should be okay here
757
- match new_etc_fd. remove_dir_all ( & removed) {
758
- Ok ( ..) => { /* no-op */ }
759
- Err ( e) => Err ( e) ?,
760
- }
756
+ new_etc_fd. remove_dir_all ( & removed) ?;
761
757
}
762
758
}
763
759
@@ -826,7 +822,7 @@ mod tests {
826
822
c. remove_file ( deleted_files[ 0 ] ) ?;
827
823
c. remove_file ( deleted_files[ 1 ] ) ?;
828
824
829
- let ( pristine_etc_files, current_etc_files, _) = traverse_etc ( & p, & c, & n ) ?;
825
+ let ( pristine_etc_files, current_etc_files, _) = traverse_etc ( & p, & c, Some ( & n ) ) ?;
830
826
let res = compute_diff ( & pristine_etc_files, & current_etc_files) ?;
831
827
832
828
// Test added files
@@ -1010,9 +1006,10 @@ mod tests {
1010
1006
n. create_dir_all ( "dir/perms" ) ?;
1011
1007
n. write ( "dir/perms/some-file" , "Some-file" ) ?;
1012
1008
1013
- let ( pristine_etc_files, current_etc_files, new_etc_files) = traverse_etc ( & p, & c, & n) ?;
1009
+ let ( pristine_etc_files, current_etc_files, new_etc_files) =
1010
+ traverse_etc ( & p, & c, Some ( & n) ) ?;
1014
1011
let diff = compute_diff ( & pristine_etc_files, & current_etc_files) ?;
1015
- merge ( & c, & current_etc_files, & n, & new_etc_files, diff) ?;
1012
+ merge ( & c, & current_etc_files, & n, & new_etc_files. unwrap ( ) , diff) ?;
1016
1013
1017
1014
assert ! ( files_eq( & c, & n, "new_file.txt" ) ?) ;
1018
1015
assert ! ( files_eq( & c, & n, "a/new_file.txt" ) ?) ;
@@ -1082,10 +1079,11 @@ mod tests {
1082
1079
1083
1080
n. create_dir_all ( "file-to-dir" ) ?;
1084
1081
1085
- let ( pristine_etc_files, current_etc_files, new_etc_files) = traverse_etc ( & p, & c, & n) ?;
1082
+ let ( pristine_etc_files, current_etc_files, new_etc_files) =
1083
+ traverse_etc ( & p, & c, Some ( & n) ) ?;
1086
1084
let diff = compute_diff ( & pristine_etc_files, & current_etc_files) ?;
1087
1085
1088
- let merge_res = merge ( & c, & current_etc_files, & n, & new_etc_files, diff) ;
1086
+ let merge_res = merge ( & c, & current_etc_files, & n, & new_etc_files. unwrap ( ) , diff) ;
1089
1087
1090
1088
assert ! ( merge_res. is_err( ) ) ;
1091
1089
assert_eq ! (
0 commit comments