From b0d976c49b50f2d73621222c1adbf7da3563528f Mon Sep 17 00:00:00 2001 From: Bryce Kahle Date: Thu, 18 Jan 2024 10:21:42 -0800 Subject: [PATCH] ensure host platform are files and have contents In a containerized deployment, it is common to mount several files from /etc. Within the container, those files will be created regardless if they exist on the host or not. In those instances, the existing code would erroneously return empty platform information. --- host/host_linux.go | 16 ++++++++-------- internal/common/common.go | 2 +- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/host/host_linux.go b/host/host_linux.go index 86044d3d0..5d4c1a90f 100644 --- a/host/host_linux.go +++ b/host/host_linux.go @@ -229,47 +229,47 @@ func PlatformInformationWithContext(ctx context.Context) (platform string, famil version = contents[0] } } - } else if common.PathExists(common.HostEtcWithContext(ctx, "neokylin-release")) { + } else if common.PathExistsWithContents(common.HostEtcWithContext(ctx, "neokylin-release")) { contents, err := common.ReadLines(common.HostEtcWithContext(ctx, "neokylin-release")) if err == nil { version = getRedhatishVersion(contents) platform = getRedhatishPlatform(contents) } - } else if common.PathExists(common.HostEtcWithContext(ctx, "redhat-release")) { + } else if common.PathExistsWithContents(common.HostEtcWithContext(ctx, "redhat-release")) { contents, err := common.ReadLines(common.HostEtcWithContext(ctx, "redhat-release")) if err == nil { version = getRedhatishVersion(contents) platform = getRedhatishPlatform(contents) } - } else if common.PathExists(common.HostEtcWithContext(ctx, "system-release")) { + } else if common.PathExistsWithContents(common.HostEtcWithContext(ctx, "system-release")) { contents, err := common.ReadLines(common.HostEtcWithContext(ctx, "system-release")) if err == nil { version = getRedhatishVersion(contents) platform = getRedhatishPlatform(contents) } - } else if common.PathExists(common.HostEtcWithContext(ctx, "gentoo-release")) { + } else if common.PathExistsWithContents(common.HostEtcWithContext(ctx, "gentoo-release")) { platform = "gentoo" contents, err := common.ReadLines(common.HostEtcWithContext(ctx, "gentoo-release")) if err == nil { version = getRedhatishVersion(contents) } - } else if common.PathExists(common.HostEtcWithContext(ctx, "SuSE-release")) { + } else if common.PathExistsWithContents(common.HostEtcWithContext(ctx, "SuSE-release")) { contents, err := common.ReadLines(common.HostEtcWithContext(ctx, "SuSE-release")) if err == nil { version = getSuseVersion(contents) platform = getSusePlatform(contents) } // TODO: slackware detecion - } else if common.PathExists(common.HostEtcWithContext(ctx, "arch-release")) { + } else if common.PathExistsWithContents(common.HostEtcWithContext(ctx, "arch-release")) { platform = "arch" version = lsb.Release - } else if common.PathExists(common.HostEtcWithContext(ctx, "alpine-release")) { + } else if common.PathExistsWithContents(common.HostEtcWithContext(ctx, "alpine-release")) { platform = "alpine" contents, err := common.ReadLines(common.HostEtcWithContext(ctx, "alpine-release")) if err == nil && len(contents) > 0 && contents[0] != "" { version = contents[0] } - } else if common.PathExists(common.HostEtcWithContext(ctx, "os-release")) { + } else if common.PathExistsWithContents(common.HostEtcWithContext(ctx, "os-release")) { p, v, err := common.GetOSReleaseWithContext(ctx) if err == nil { platform = p diff --git a/internal/common/common.go b/internal/common/common.go index 99ed6a58e..5e25e507b 100644 --- a/internal/common/common.go +++ b/internal/common/common.go @@ -343,7 +343,7 @@ func PathExistsWithContents(filename string) bool { if err != nil { return false } - return info.Size() > 4 // at least 4 bytes + return info.Size() > 4 && !info.IsDir() // at least 4 bytes } // GetEnvWithContext retrieves the environment variable key. If it does not exist it returns the default.