From 5610fbc5d534d37648ca12e1c4bccb7e3c7d3cf3 Mon Sep 17 00:00:00 2001 From: shirou Date: Fri, 15 Jul 2022 12:20:19 +0000 Subject: [PATCH 1/2] fix(host,linux): Check if path exists and is nonempty before reading host files --- host/host_linux.go | 12 +++++++----- internal/common/common.go | 17 +++++++++++++++++ internal/common/common_test.go | 19 +++++++++++++++++++ 3 files changed, 43 insertions(+), 5 deletions(-) diff --git a/host/host_linux.go b/host/host_linux.go index 4a21c3841..940415c9c 100644 --- a/host/host_linux.go +++ b/host/host_linux.go @@ -179,26 +179,26 @@ func PlatformInformationWithContext(ctx context.Context) (platform string, famil lsb = &lsbStruct{} } - if common.PathExists(common.HostEtc("oracle-release")) { + if common.PathExistsWithContents(common.HostEtc("oracle-release")) { platform = "oracle" contents, err := common.ReadLines(common.HostEtc("oracle-release")) if err == nil { version = getRedhatishVersion(contents) } - } else if common.PathExists(common.HostEtc("enterprise-release")) { + } else if common.PathExistsWithContents(common.HostEtc("enterprise-release")) { platform = "oracle" contents, err := common.ReadLines(common.HostEtc("enterprise-release")) if err == nil { version = getRedhatishVersion(contents) } - } else if common.PathExists(common.HostEtc("slackware-version")) { + } else if common.PathExistsWithContents(common.HostEtc("slackware-version")) { platform = "slackware" contents, err := common.ReadLines(common.HostEtc("slackware-version")) if err == nil { version = getSlackwareVersion(contents) } - } else if common.PathExists(common.HostEtc("debian_version")) { + } else if common.PathExistsWithContents(common.HostEtc("debian_version")) { if lsb.ID == "Ubuntu" { platform = "ubuntu" version = lsb.Release @@ -206,7 +206,7 @@ func PlatformInformationWithContext(ctx context.Context) (platform string, famil platform = "linuxmint" version = lsb.Release } else { - if common.PathExists("/usr/bin/raspi-config") { + if common.PathExistsWithContents("/usr/bin/raspi-config") { platform = "raspbian" } else { platform = "debian" @@ -279,6 +279,8 @@ func PlatformInformationWithContext(ctx context.Context) (platform string, famil version = lsb.Release } + platform = strings.Trim(platform, `"`) + switch platform { case "debian", "ubuntu", "linuxmint", "raspbian": family = "debian" diff --git a/internal/common/common.go b/internal/common/common.go index 4f6df9a3a..15872a8fa 100644 --- a/internal/common/common.go +++ b/internal/common/common.go @@ -311,6 +311,23 @@ func PathExists(filename string) bool { return false } +// PathExistsWithContents returns the filename exists and it is not empty +func PathExistsWithContents(filename string) bool { + if _, err := os.Stat(filename); err != nil { + return false + } + + f, err := os.Open(filename) + if err != nil { + return false + } + defer f.Close() + + r := bufio.NewReader(f) + _, err = r.Peek(4) // check first 4 bytes + return err == nil +} + // GetEnv retrieves the environment variable key. If it does not exist it returns the default. func GetEnv(key string, dfault string, combineWith ...string) string { value := os.Getenv(key) diff --git a/internal/common/common_test.go b/internal/common/common_test.go index 93f33ec39..27bcfac06 100644 --- a/internal/common/common_test.go +++ b/internal/common/common_test.go @@ -102,6 +102,25 @@ func TestPathExists(t *testing.T) { } } +func TestPathExistsWithContents(t *testing.T) { + if !PathExistsWithContents("common_test.go") { + t.Error("exists but return not exists") + } + if PathExistsWithContents("should_not_exists.go") { + t.Error("not exists but return exists") + } + + f, err := os.CreateTemp("", "empty_test.txt") + if err != nil { + t.Errorf("CreateTemp failed, %s", err) + } + defer os.Remove(f.Name()) // clean up + + if PathExistsWithContents(f.Name()) { + t.Error("exists but no content file return true") + } +} + func TestHostEtc(t *testing.T) { if runtime.GOOS == "windows" { t.Skip("windows doesn't have etc") From 839e8b731f3d89f5028e6bff65625ab3d914be9a Mon Sep 17 00:00:00 2001 From: shirou Date: Tue, 19 Jul 2022 12:43:41 +0000 Subject: [PATCH 2/2] fix(common): simplify size check --- internal/common/common.go | 12 ++---------- 1 file changed, 2 insertions(+), 10 deletions(-) diff --git a/internal/common/common.go b/internal/common/common.go index 15872a8fa..adc4922bd 100644 --- a/internal/common/common.go +++ b/internal/common/common.go @@ -313,19 +313,11 @@ func PathExists(filename string) bool { // PathExistsWithContents returns the filename exists and it is not empty func PathExistsWithContents(filename string) bool { - if _, err := os.Stat(filename); err != nil { - return false - } - - f, err := os.Open(filename) + info, err := os.Stat(filename) if err != nil { return false } - defer f.Close() - - r := bufio.NewReader(f) - _, err = r.Peek(4) // check first 4 bytes - return err == nil + return info.Size() > 4 // at least 4 bytes } // GetEnv retrieves the environment variable key. If it does not exist it returns the default.