Skip to content

Commit

Permalink
machine: compute sha256 as we read the image file
Browse files Browse the repository at this point in the history
It avoids to have the full file in memory.

[NO TESTS NEEDED]

Signed-off-by: Guillaume Rose <[email protected]>
  • Loading branch information
guillaumerose authored and mheon committed Aug 20, 2021
1 parent a52b6bf commit b5e04ae
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 deletions pkg/machine/fcos.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,14 @@
package machine

import (
"crypto/sha256"
"io/ioutil"
url2 "net/url"
"os"
"path/filepath"
"runtime"
"strings"

digest "github.com/opencontainers/go-digest"
"github.com/sirupsen/logrus"
)

// These should eventually be moved into machine/qemu as
Expand Down Expand Up @@ -95,12 +94,19 @@ func UpdateAvailable(d *Download) (bool, error) {
if _, err := os.Stat(d.LocalPath); os.IsNotExist(err) {
return false, nil
}
b, err := ioutil.ReadFile(d.LocalPath)
fd, err := os.Open(d.LocalPath)
if err != nil {
return false, err
}
defer func() {
if err := fd.Close(); err != nil {
logrus.Error(err)
}
}()
sum, err := digest.SHA256.FromReader(fd)
if err != nil {
return false, err
}
s := sha256.Sum256(b)
sum := digest.NewDigestFromBytes(digest.SHA256, s[:])
return sum.Encoded() == d.Sha256sum, nil
}

Expand Down

0 comments on commit b5e04ae

Please sign in to comment.