@@ -298,29 +298,30 @@ func (s *Store) Save(
298
298
// saveChunk saves the given chunkBody with the given index to its appropriate path on disk.
299
299
// The hash of the chunk is appended to the snapshot's metadata,
300
300
// and the overall snapshot hash is updated with the chunk content too.
301
- func (s * Store ) saveChunk (chunkBody io.ReadCloser , index uint32 , snapshot * types.Snapshot , chunkHasher , snapshotHasher hash.Hash ) error {
302
- defer chunkBody .Close ()
301
+ func (s * Store ) saveChunk (chunkBody io.ReadCloser , index uint32 , snapshot * types.Snapshot , chunkHasher , snapshotHasher hash.Hash ) (err error ) {
302
+ defer func () {
303
+ if cErr := chunkBody .Close (); cErr != nil {
304
+ err = errors .Wrapf (cErr , "failed to close snapshot chunk body %d" , index )
305
+ }
306
+ }()
303
307
304
308
path := s .PathChunk (snapshot .Height , snapshot .Format , index )
305
309
chunkFile , err := os .Create (path )
306
310
if err != nil {
307
311
return errors .Wrapf (err , "failed to create snapshot chunk file %q" , path )
308
312
}
309
- defer chunkFile .Close ()
313
+
314
+ defer func () {
315
+ if cErr := chunkFile .Close (); cErr != nil {
316
+ err = errors .Wrapf (cErr , "failed to close snapshot chunk file %d" , index )
317
+ }
318
+ }()
310
319
311
320
chunkHasher .Reset ()
312
321
if _ , err := io .Copy (io .MultiWriter (chunkFile , chunkHasher , snapshotHasher ), chunkBody ); err != nil {
313
322
return errors .Wrapf (err , "failed to generate snapshot chunk %d" , index )
314
323
}
315
324
316
- if err := chunkFile .Close (); err != nil {
317
- return errors .Wrapf (err , "failed to close snapshot chunk file %d" , index )
318
- }
319
-
320
- if err := chunkBody .Close (); err != nil {
321
- return errors .Wrapf (err , "failed to close snapshot chunk body %d" , index )
322
- }
323
-
324
325
snapshot .Metadata .ChunkHashes = append (snapshot .Metadata .ChunkHashes , chunkHasher .Sum (nil ))
325
326
return nil
326
327
}
0 commit comments