machine: check for file exists instead of listing directory

[NO TESTS NEEDED]

Signed-off-by: Guillaume Rose <gurose@redhat.com>
This commit is contained in:
Guillaume Rose
2021-08-19 16:14:06 +02:00
parent 23804d95f6
commit 0434571920

View File

@ -6,6 +6,7 @@ import (
"crypto/sha256" "crypto/sha256"
"io/ioutil" "io/ioutil"
url2 "net/url" url2 "net/url"
"os"
"path/filepath" "path/filepath"
"runtime" "runtime"
"strings" "strings"
@ -91,24 +92,16 @@ func UpdateAvailable(d *Download) (bool, error) {
// check the sha of the local image if it exists // check the sha of the local image if it exists
// get the sha of the remote image // get the sha of the remote image
// == dont bother to pull // == dont bother to pull
files, err := ioutil.ReadDir(filepath.Dir(d.LocalPath)) if _, err := os.Stat(d.LocalPath); os.IsNotExist(err) {
if err != nil { return false, nil
return false, err
} }
for _, file := range files {
if filepath.Base(d.LocalPath) == file.Name() {
b, err := ioutil.ReadFile(d.LocalPath) b, err := ioutil.ReadFile(d.LocalPath)
if err != nil { if err != nil {
return false, err return false, err
} }
s := sha256.Sum256(b) s := sha256.Sum256(b)
sum := digest.NewDigestFromBytes(digest.SHA256, s[:]) sum := digest.NewDigestFromBytes(digest.SHA256, s[:])
if sum.Encoded() == d.Sha256sum { return sum.Encoded() == d.Sha256sum, nil
return true, nil
}
}
}
return false, nil
} }
func getFcosArch() string { func getFcosArch() string {