@@ -97,7 +97,8 @@ pub struct SplitStreamWriter<ObjectID: FsVerityHashValue> {
97
97
total_size : u64 ,
98
98
writer : Encoder < ' static , Vec < u8 > > ,
99
99
pub content_type : u64 ,
100
- pub sha256 : Option < ( Sha256 , Sha256Digest ) > ,
100
+ pub sha256 : Option < Sha256 > ,
101
+ pub expected_sha256 : Option < Sha256Digest > ,
101
102
}
102
103
103
104
impl < ObjectID : FsVerityHashValue > std:: fmt:: Debug for SplitStreamWriter < ObjectID > {
@@ -106,6 +107,7 @@ impl<ObjectID: FsVerityHashValue> std::fmt::Debug for SplitStreamWriter<ObjectID
106
107
f. debug_struct ( "SplitStreamWriter" )
107
108
. field ( "repo" , & self . repo )
108
109
. field ( "inline_content" , & self . inline_content )
110
+ . field ( "expected_sha256" , & self . expected_sha256 )
109
111
. field ( "sha256" , & self . sha256 )
110
112
. finish ( )
111
113
}
@@ -115,7 +117,8 @@ impl<ObjectID: FsVerityHashValue> SplitStreamWriter<ObjectID> {
115
117
pub fn new (
116
118
repo : & Arc < Repository < ObjectID > > ,
117
119
content_type : u64 ,
118
- sha256 : Option < Sha256Digest > ,
120
+ compute_sha256 : bool ,
121
+ expected_sha256 : Option < Sha256Digest > ,
119
122
) -> Self {
120
123
// SAFETY: we surely can't get an error writing the header to a Vec<u8>
121
124
let writer = Encoder :: new ( vec ! [ ] , 0 ) . unwrap ( ) ;
@@ -128,7 +131,12 @@ impl<ObjectID: FsVerityHashValue> SplitStreamWriter<ObjectID> {
128
131
total_size : 0 ,
129
132
mappings : DigestMap :: new ( ) ,
130
133
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,
132
140
}
133
141
}
134
142
@@ -177,7 +185,7 @@ impl<ObjectID: FsVerityHashValue> SplitStreamWriter<ObjectID> {
177
185
/// really, "add inline content to the buffer"
178
186
/// you need to call .flush_inline() later
179
187
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 {
181
189
sha256. update ( data) ;
182
190
}
183
191
self . inline_content . extend ( data) ;
@@ -195,7 +203,7 @@ impl<ObjectID: FsVerityHashValue> SplitStreamWriter<ObjectID> {
195
203
}
196
204
197
205
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 {
199
207
sha256. update ( data) ;
200
208
sha256. update ( & padding) ;
201
209
}
@@ -207,7 +215,7 @@ impl<ObjectID: FsVerityHashValue> SplitStreamWriter<ObjectID> {
207
215
}
208
216
209
217
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 {
211
219
sha256. update ( & data) ;
212
220
sha256. update ( & padding) ;
213
221
}
@@ -217,14 +225,20 @@ impl<ObjectID: FsVerityHashValue> SplitStreamWriter<ObjectID> {
217
225
self . write_reference ( & id, padding)
218
226
}
219
227
220
- pub fn done ( mut self ) -> Result < ObjectID > {
228
+ pub fn done ( mut self ) -> Result < ( ObjectID , Option < Sha256Digest > ) > {
221
229
self . flush_inline ( vec ! [ ] ) ?;
222
230
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
+ }
226
237
}
227
- }
238
+ Some ( actual)
239
+ } else {
240
+ None
241
+ } ;
228
242
229
243
let mut buf = vec ! [ ] ;
230
244
let header = SplitstreamHeader {
@@ -252,7 +266,7 @@ impl<ObjectID: FsVerityHashValue> SplitStreamWriter<ObjectID> {
252
266
253
267
buf. extend_from_slice ( & self . writer . finish ( ) ?) ;
254
268
255
- self . repo . ensure_object ( & buf)
269
+ Ok ( ( self . repo . ensure_object ( & buf) ? , sha256_digest ) )
256
270
}
257
271
}
258
272
0 commit comments