16
16
package pkgs
17
17
18
18
import (
19
- "bytes"
20
19
"context"
21
20
"crypto/sha256"
22
21
"encoding/hex"
@@ -195,21 +194,8 @@ func (t *Tools) install(ctx context.Context, path, url, checksum string) (*tools
195
194
}
196
195
defer res .Body .Close ()
197
196
198
- var buffer bytes.Buffer
199
-
200
- // We copy the body of the response to a buffer to calculate the checksum
201
- _ , err = io .Copy (& buffer , res .Body )
202
- if err != nil {
203
- return nil , err
204
- }
205
-
206
- // Check the checksum
207
- sum := sha256 .Sum256 (buffer .Bytes ())
208
- sumString := "SHA-256:" + hex .EncodeToString (sum [:sha256 .Size ])
209
-
210
- if sumString != checksum {
211
- return nil , errors .New ("checksum of downloaded file doesn't match, expected: " + checksum + " got: " + sumString )
212
- }
197
+ h := sha256 .New ()
198
+ r := io .TeeReader (res .Body , h )
213
199
214
200
safePath , err := utilities .SafeJoin (t .folder , path )
215
201
if err != nil {
@@ -222,12 +208,18 @@ func (t *Tools) install(ctx context.Context, path, url, checksum string) (*tools
222
208
return nil , err
223
209
}
224
210
225
- err = extract .Archive (ctx , & buffer , t .folder , rename (path ))
211
+ err = extract .Archive (ctx , r , t .folder , rename (path ))
226
212
if err != nil {
227
213
os .RemoveAll (safePath )
228
214
return nil , err
229
215
}
230
216
217
+ // Check the checksum
218
+ sumString := "SHA-256:" + hex .EncodeToString (h .Sum (nil ))
219
+ if sumString != checksum {
220
+ return nil , errors .New ("checksum of downloaded file doesn't match, expected: " + checksum + " got: " + sumString )
221
+ }
222
+
231
223
// Write installed.json for retrocompatibility with v1
232
224
err = writeInstalled (t .folder , path )
233
225
if err != nil {
0 commit comments