@@ -5,6 +5,7 @@ use std::path::{Path, PathBuf};
55
66use serde:: { Deserialize , Serialize } ;
77
8+ use crate :: error:: { BootspecError , SynthesizeError } ;
89use crate :: { Result , SpecialisationName , SystemConfigurationRoot } ;
910
1011/// The V1 bootspec schema version.
@@ -40,9 +41,9 @@ impl GenerationV1 {
4041 let specialisation = specialisation?;
4142 let name = specialisation
4243 . file_name ( )
43- . ok_or ( "Could not get name of specialisation dir" ) ?
44+ . ok_or ( BootspecError :: InvalidFileName ( specialisation. clone ( ) ) ) ?
4445 . to_str ( )
45- . ok_or ( "Specialisation dir name was invalid UTF8" ) ?;
46+ . ok_or ( BootspecError :: InvalidUtf8 ( specialisation . clone ( ) ) ) ?;
4647 let toplevel = fs:: canonicalize ( generation_path. join ( "specialisation" ) . join ( name) ) ?;
4748
4849 specialisations. insert (
@@ -92,29 +93,54 @@ impl BootSpecV1 {
9293 pub ( crate ) fn synthesize ( generation : & Path ) -> Result < Self > {
9394 let generation = generation
9495 . canonicalize ( )
95- . map_err ( |e| format ! ( "Failed to canonicalize generation dir:\n {}" , e) ) ?;
96-
97- let system_version = fs:: read_to_string ( generation. join ( "nixos-version" ) )
98- . map_err ( |e| format ! ( "Failed to read system version:\n {}" , e) ) ?;
99-
100- let system = fs:: read_to_string ( generation. join ( "system" ) )
101- . map_err ( |e| format ! ( "Failed to read system double:\n {}" , e) ) ?;
102-
103- let kernel = fs:: canonicalize ( generation. join ( "kernel-modules/bzImage" ) )
104- . map_err ( |e| format ! ( "Failed to canonicalize the kernel:\n {}" , e) ) ?;
105-
106- let kernel_modules = fs:: canonicalize ( generation. join ( "kernel-modules/lib/modules" ) )
107- . map_err ( |e| format ! ( "Failed to canonicalize the kernel modules dir:\n {}" , e) ) ?;
108- let versioned_kernel_modules = fs:: read_dir ( kernel_modules)
109- . map_err ( |e| format ! ( "Failed to read kernel modules dir:\n {}" , e) ) ?
96+ . map_err ( |e| SynthesizeError :: Canonicalize {
97+ path : generation. to_path_buf ( ) ,
98+ err : e,
99+ } ) ?;
100+
101+ let version_file = generation. join ( "nixos-version" ) ;
102+ let system_version =
103+ fs:: read_to_string ( version_file. clone ( ) ) . map_err ( |e| SynthesizeError :: ReadPath {
104+ path : version_file,
105+ err : e,
106+ } ) ?;
107+
108+ let system_file = generation. join ( "system" ) ;
109+ let system =
110+ fs:: read_to_string ( system_file. clone ( ) ) . map_err ( |e| SynthesizeError :: ReadPath {
111+ path : system_file,
112+ err : e,
113+ } ) ?;
114+
115+ let kernel_file = generation. join ( "kernel-modules/bzImage" ) ;
116+ let kernel =
117+ fs:: canonicalize ( kernel_file. clone ( ) ) . map_err ( |e| SynthesizeError :: Canonicalize {
118+ path : kernel_file,
119+ err : e,
120+ } ) ?;
121+
122+ let kernel_modules_path = generation. join ( "kernel-modules/lib/modules" ) ;
123+ let kernel_modules = fs:: canonicalize ( kernel_modules_path. clone ( ) ) . map_err ( |e| {
124+ SynthesizeError :: Canonicalize {
125+ path : kernel_modules_path,
126+ err : e,
127+ }
128+ } ) ?;
129+ let versioned_kernel_modules = fs:: read_dir ( kernel_modules. clone ( ) )
130+ . map_err ( |e| SynthesizeError :: ReadPath {
131+ path : kernel_modules. clone ( ) ,
132+ err : e,
133+ } ) ?
110134 . map ( |res| res. map ( |e| e. path ( ) ) )
111135 . next ( )
112- . ok_or ( "Could not find kernel version dir" ) ??;
136+ . ok_or ( SynthesizeError :: MissingKernelVersionDir ( kernel_modules ) ) ??;
113137 let kernel_version = versioned_kernel_modules
114138 . file_name ( )
115- . ok_or ( "Could not get name of kernel version dir" ) ?
139+ . ok_or ( BootspecError :: InvalidFileName (
140+ versioned_kernel_modules. clone ( ) ,
141+ ) ) ?
116142 . to_str ( )
117- . ok_or ( "Kernel version dir name was invalid UTF8" ) ?;
143+ . ok_or ( BootspecError :: InvalidUtf8 ( versioned_kernel_modules . clone ( ) ) ) ?;
118144
119145 let kernel_params: Vec < String > = fs:: read_to_string ( generation. join ( "kernel-params" ) ) ?
120146 . split ( ' ' )
@@ -125,10 +151,12 @@ impl BootSpecV1 {
125151
126152 let initrd_path = generation. join ( "initrd" ) ;
127153 let initrd = if initrd_path. exists ( ) {
128- Some (
129- fs:: canonicalize ( initrd_path)
130- . map_err ( |e| format ! ( "Failed to canonicalize the initrd:\n {}" , e) ) ?,
131- )
154+ Some ( fs:: canonicalize ( initrd_path. clone ( ) ) . map_err ( |e| {
155+ SynthesizeError :: Canonicalize {
156+ path : initrd_path,
157+ err : e,
158+ }
159+ } ) ?)
132160 } else {
133161 None
134162 } ;
0 commit comments