From 0434571920939576b1708e905cfb156d9fde504b Mon Sep 17 00:00:00 2001 From: Guillaume Rose Date: Thu, 19 Aug 2021 16:14:06 +0200 Subject: machine: check for file exists instead of listing directory [NO TESTS NEEDED] Signed-off-by: Guillaume Rose --- pkg/machine/fcos.go | 23 ++++++++--------------- 1 file changed, 8 insertions(+), 15 deletions(-) (limited to 'pkg') diff --git a/pkg/machine/fcos.go b/pkg/machine/fcos.go index 11936aee7..943b9fd3c 100644 --- a/pkg/machine/fcos.go +++ b/pkg/machine/fcos.go @@ -6,6 +6,7 @@ import ( "crypto/sha256" "io/ioutil" url2 "net/url" + "os" "path/filepath" "runtime" "strings" @@ -91,24 +92,16 @@ func UpdateAvailable(d *Download) (bool, error) { // check the sha of the local image if it exists // get the sha of the remote image // == dont bother to pull - files, err := ioutil.ReadDir(filepath.Dir(d.LocalPath)) + if _, err := os.Stat(d.LocalPath); os.IsNotExist(err) { + return false, nil + } + b, err := ioutil.ReadFile(d.LocalPath) if err != nil { return false, err } - for _, file := range files { - if filepath.Base(d.LocalPath) == file.Name() { - b, err := ioutil.ReadFile(d.LocalPath) - if err != nil { - return false, err - } - s := sha256.Sum256(b) - sum := digest.NewDigestFromBytes(digest.SHA256, s[:]) - if sum.Encoded() == d.Sha256sum { - return true, nil - } - } - } - return false, nil + s := sha256.Sum256(b) + sum := digest.NewDigestFromBytes(digest.SHA256, s[:]) + return sum.Encoded() == d.Sha256sum, nil } func getFcosArch() string { -- cgit v1.2.3-54-g00ecf From b19812b5a245eec1f04da77f15d86004bd42d3fd Mon Sep 17 00:00:00 2001 From: Guillaume Rose Date: Thu, 19 Aug 2021 16:17:23 +0200 Subject: machine: compute sha256 as we read the image file It avoids to have the full file in memory. [NO TESTS NEEDED] Signed-off-by: Guillaume Rose --- pkg/machine/fcos.go | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) (limited to 'pkg') diff --git a/pkg/machine/fcos.go b/pkg/machine/fcos.go index 943b9fd3c..49ec01e67 100644 --- a/pkg/machine/fcos.go +++ b/pkg/machine/fcos.go @@ -3,8 +3,6 @@ package machine import ( - "crypto/sha256" - "io/ioutil" url2 "net/url" "os" "path/filepath" @@ -12,6 +10,7 @@ import ( "strings" digest "github.com/opencontainers/go-digest" + "github.com/sirupsen/logrus" ) // These should eventually be moved into machine/qemu as @@ -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 } -- cgit v1.2.3-54-g00ecf