@@ -97,7 +97,8 @@ pub struct SplitStreamWriter<ObjectID: FsVerityHashValue> {
9797 total_size : u64 ,
9898 writer : Encoder < ' static , Vec < u8 > > ,
9999 pub content_type : u64 ,
100- pub sha256 : Option < ( Sha256 , Sha256Digest ) > ,
100+ pub sha256 : Option < Sha256 > ,
101+ pub expected_sha256 : Option < Sha256Digest > ,
101102}
102103
103104impl < ObjectID : FsVerityHashValue > std:: fmt:: Debug for SplitStreamWriter < ObjectID > {
@@ -106,6 +107,7 @@ impl<ObjectID: FsVerityHashValue> std::fmt::Debug for SplitStreamWriter<ObjectID
106107 f. debug_struct ( "SplitStreamWriter" )
107108 . field ( "repo" , & self . repo )
108109 . field ( "inline_content" , & self . inline_content )
110+ . field ( "expected_sha256" , & self . expected_sha256 )
109111 . field ( "sha256" , & self . sha256 )
110112 . finish ( )
111113 }
@@ -115,7 +117,8 @@ impl<ObjectID: FsVerityHashValue> SplitStreamWriter<ObjectID> {
115117 pub fn new (
116118 repo : & Arc < Repository < ObjectID > > ,
117119 content_type : u64 ,
118- sha256 : Option < Sha256Digest > ,
120+ compute_sha256 : bool ,
121+ expected_sha256 : Option < Sha256Digest > ,
119122 ) -> Self {
120123 // SAFETY: we surely can't get an error writing the header to a Vec<u8>
121124 let writer = Encoder :: new ( vec ! [ ] , 0 ) . unwrap ( ) ;
@@ -128,7 +131,12 @@ impl<ObjectID: FsVerityHashValue> SplitStreamWriter<ObjectID> {
128131 total_size : 0 ,
129132 mappings : DigestMap :: new ( ) ,
130133 writer,
131- sha256 : sha256. map ( |x| ( Sha256 :: new ( ) , x) ) ,
134+ sha256 : if compute_sha256 || expected_sha256. is_some ( ) {
135+ Some ( Sha256 :: new ( ) )
136+ } else {
137+ None
138+ } ,
139+ expected_sha256,
132140 }
133141 }
134142
@@ -177,7 +185,7 @@ impl<ObjectID: FsVerityHashValue> SplitStreamWriter<ObjectID> {
177185 /// really, "add inline content to the buffer"
178186 /// you need to call .flush_inline() later
179187 pub fn write_inline ( & mut self , data : & [ u8 ] ) {
180- if let Some ( ( ref mut sha256, .. ) ) = self . sha256 {
188+ if let Some ( ref mut sha256) = self . sha256 {
181189 sha256. update ( data) ;
182190 }
183191 self . inline_content . extend ( data) ;
@@ -195,7 +203,7 @@ impl<ObjectID: FsVerityHashValue> SplitStreamWriter<ObjectID> {
195203 }
196204
197205 pub fn write_external ( & mut self , data : & [ u8 ] , padding : Vec < u8 > ) -> Result < ( ) > {
198- if let Some ( ( ref mut sha256, ..) ) = self . sha256 {
206+ if let Some ( ref mut sha256, ..) = self . sha256 {
199207 sha256. update ( data) ;
200208 sha256. update ( & padding) ;
201209 }
@@ -207,7 +215,7 @@ impl<ObjectID: FsVerityHashValue> SplitStreamWriter<ObjectID> {
207215 }
208216
209217 pub async fn write_external_async ( & mut self , data : Vec < u8 > , padding : Vec < u8 > ) -> Result < ( ) > {
210- if let Some ( ( ref mut sha256, ..) ) = self . sha256 {
218+ if let Some ( ref mut sha256, ..) = self . sha256 {
211219 sha256. update ( & data) ;
212220 sha256. update ( & padding) ;
213221 }
@@ -217,14 +225,20 @@ impl<ObjectID: FsVerityHashValue> SplitStreamWriter<ObjectID> {
217225 self . write_reference ( & id, padding)
218226 }
219227
220- pub fn done ( mut self ) -> Result < ObjectID > {
228+ pub fn done ( mut self ) -> Result < ( ObjectID , Option < Sha256Digest > ) > {
221229 self . flush_inline ( vec ! [ ] ) ?;
222230
223- if let Some ( ( context, expected) ) = self . sha256 {
224- if Into :: < Sha256Digest > :: into ( context. finalize ( ) ) != expected {
225- bail ! ( "Content doesn't have expected SHA256 hash value!" ) ;
231+ let sha256_digest = if let Some ( sha256) = self . sha256 {
232+ let actual = Into :: < Sha256Digest > :: into ( sha256. finalize ( ) ) ;
233+ if let Some ( expected) = self . expected_sha256 {
234+ if actual != expected {
235+ bail ! ( "Content doesn't have expected SHA256 hash value!" ) ;
236+ }
226237 }
227- }
238+ Some ( actual)
239+ } else {
240+ None
241+ } ;
228242
229243 let mut buf = vec ! [ ] ;
230244 let header = SplitstreamHeader {
@@ -252,7 +266,7 @@ impl<ObjectID: FsVerityHashValue> SplitStreamWriter<ObjectID> {
252266
253267 buf. extend_from_slice ( & self . writer . finish ( ) ?) ;
254268
255- self . repo . ensure_object ( & buf)
269+ Ok ( ( self . repo . ensure_object ( & buf) ? , sha256_digest ) )
256270 }
257271}
258272
0 commit comments