3838/// ```
3939use futures_util:: TryStreamExt ;
4040use sha2:: { Digest , Sha256 } ;
41- use std:: path:: { Path , PathBuf } ;
41+ use std:: path:: PathBuf ;
4242use tokio_util:: bytes:: BytesMut ;
4343
4444use sigstore_protobuf_specs:: dev:: sigstore:: {
@@ -62,7 +62,7 @@ pub struct SigstoreTrustRoot {
6262
6363impl SigstoreTrustRoot {
6464 /// Constructs a new trust repository established by a [tough::Repository].
65- pub async fn new ( checkout_dir : Option < & Path > ) -> Result < Self > {
65+ pub async fn new ( checkout_dir : Option < PathBuf > ) -> Result < Self > {
6666 // These are statically defined and should always parse correctly.
6767 let metadata_base = url:: Url :: parse ( constants:: SIGSTORE_METADATA_BASE ) ?;
6868 let target_base = url:: Url :: parse ( constants:: SIGSTORE_TARGET_BASE ) ?;
@@ -77,8 +77,6 @@ impl SigstoreTrustRoot {
7777 . await
7878 . map_err ( Box :: new) ?;
7979
80- let checkout_dir = checkout_dir. map ( ToOwned :: to_owned) ;
81-
8280 let trusted_root = {
8381 let data = Self :: fetch_target ( & repository, & checkout_dir, "trusted_root.json" ) . await ?;
8482 serde_json:: from_slice ( & data[ ..] ) ?
@@ -105,12 +103,13 @@ impl SigstoreTrustRoot {
105103 }
106104 } ;
107105
108- // Try reading the target from disk cache.
106+ // First, try reading the target from disk cache.
109107 let data = if let Some ( Ok ( local_data) ) = local_path. as_ref ( ) . map ( std:: fs:: read) {
108+ debug ! ( "{}: reading from embedded resources" , name. raw( ) ) ;
110109 local_data. to_vec ( )
111110 // Try reading the target embedded into the binary.
112111 } else if let Some ( embedded_data) = constants:: static_resource ( name. raw ( ) ) {
113- debug ! ( "read embedded target {} " , name. raw( ) ) ;
112+ debug ! ( "{}: reading from remote " , name. raw( ) ) ;
114113 embedded_data. to_vec ( )
115114 // If all else fails, read the data from the TUF repo.
116115 } else if let Ok ( remote_data) = read_remote_target ( ) . await {
@@ -128,15 +127,15 @@ impl SigstoreTrustRoot {
128127 } ;
129128
130129 let data = if Sha256 :: digest ( & data) [ ..] != target. hashes . sha256 [ ..] {
130+ debug ! ( "{}: out of date" , name. raw( ) ) ;
131131 read_remote_target ( ) . await ?. to_vec ( )
132132 } else {
133133 data
134134 } ;
135135
136- // Write the up-to-date data back to the disk. This doesn't need to succeed, as we can
137- // always fetch the target again later.
136+ // Write our updated data back to the disk.
138137 if let Some ( local_path) = local_path {
139- let _ = std:: fs:: write ( local_path, & data) ;
138+ std:: fs:: write ( local_path, & data) ? ;
140139 }
141140
142141 Ok ( data)
0 commit comments