[lxc-devel] [distrobuilder/master] Fix checksum matching

monstermunchkin on Github lxc-bot at linuxcontainers.org
Thu Apr 16 14:03:43 UTC 2020


A non-text attachment was scrubbed...
Name: not available
Type: text/x-mailbox
Size: 310 bytes
Desc: not available
URL: <http://lists.linuxcontainers.org/pipermail/lxc-devel/attachments/20200416/e1ec976b/attachment.bin>
-------------- next part --------------
From 5fbf4a9e635190375e69b0bc0f1a6cb87d6da4b9 Mon Sep 17 00:00:00 2001
From: Thomas Hipp <thomas.hipp at canonical.com>
Date: Thu, 16 Apr 2020 14:04:04 +0200
Subject: [PATCH 1/2] shared/util: Fix checksum matching

Signed-off-by: Thomas Hipp <thomas.hipp at canonical.com>
---
 shared/util.go | 42 ++++++++++++++++++++++++++++++++++++------
 1 file changed, 36 insertions(+), 6 deletions(-)

diff --git a/shared/util.go b/shared/util.go
index 3bf6eed..2b66c9f 100644
--- a/shared/util.go
+++ b/shared/util.go
@@ -371,16 +371,46 @@ func GetTargetDir(def DefinitionImage) string {
 func getChecksum(fname string, hashLen int, r io.Reader) string {
 	scanner := bufio.NewScanner(r)
 
-	for scanner.Scan() {
-		fields := strings.Split(scanner.Text(), " ")
+	var matches []string
 
-		// We need to special case CentOS, as they don't use the common
-		// "<hash> <filename>" syntax.
-		if strings.TrimSpace(fields[len(fields)-1]) != fname && !strings.Contains(scanner.Text(), fmt.Sprintf("(%s)", fname)) {
+	for scanner.Scan() {
+		if !strings.Contains(scanner.Text(), fname) {
 			continue
 		}
 
-		for _, s := range fields {
+		for _, s := range strings.Split(scanner.Text(), " ") {
+			m, _ := regexp.MatchString("[[:xdigit:]]+", s)
+			if !m {
+				continue
+			}
+
+			if hashLen == 0 || hashLen == len(strings.TrimSpace(s)) {
+				matches = append(matches, scanner.Text())
+			}
+		}
+	}
+
+	// Check common checksum file (pattern: "<hash> <filename>") with the exact filename
+	for _, m := range matches {
+		fields := strings.Split(m, " ")
+
+		if strings.TrimSpace(fields[len(fields)-1]) == fname {
+			return strings.TrimSpace(fields[0])
+		}
+	}
+
+	// Check common checksum file (pattern: "<hash> <filename>") which contains the filename
+	for _, m := range matches {
+		fields := strings.Split(m, " ")
+
+		if strings.Contains(strings.TrimSpace(fields[len(fields)-1]), fname) {
+			return strings.TrimSpace(fields[0])
+		}
+	}
+
+	// Special case: CentOS
+	for _, m := range matches {
+		for _, s := range strings.Split(m, " ") {
 			m, _ := regexp.MatchString("[[:xdigit:]]+", s)
 			if !m {
 				continue

From 8bc486037336c9ee30c13b59d085d147c3538b0f Mon Sep 17 00:00:00 2001
From: Thomas Hipp <thomas.hipp at canonical.com>
Date: Thu, 16 Apr 2020 16:02:51 +0200
Subject: [PATCH 2/2] test: Update checksum matching

Signed-off-by: Thomas Hipp <thomas.hipp at canonical.com>
---
 shared/util_test.go | 18 ++++++++++++++++++
 1 file changed, 18 insertions(+)

diff --git a/shared/util_test.go b/shared/util_test.go
index 955a3ab..635605d 100644
--- a/shared/util_test.go
+++ b/shared/util_test.go
@@ -227,6 +227,24 @@ func Test_getChecksum(t *testing.T) {
 		args args
 		want string
 	}{
+		{
+			"openwrt-x86-64-rootfs.tar.gz",
+			args{
+				"openwrt-x86-64-rootfs.tar.gz",
+				64,
+				bytes.NewBufferString(`8b194c619b65d675da15d190fe7c7d2ce6125debc98452e30890c16212aa7b1c *openwrt-imagebuilder-x86-64.Linux-x86_64.tar.xz
+d99669ef301129e6ba59417ff41814dd02b4bdbe7254e2c8535de5eae35801ad *openwrt-sdk-x86-64_gcc-8.4.0_musl.Linux-x86_64.tar.xz
+84be5c09beb3791c574a35b9e73dcb7b7637482f83ed61fbe07cd0af68987cf8 *openwrt-x86-64-generic-ext4-combined-efi.img.gz
+23d9ac551d0cd9c85458d4032ae030f33f5f6b44158866130c3065f2a121b641 *openwrt-x86-64-generic-ext4-combined.img.gz
+4462e51e9b325e107b57a3b44aef176837fcee0ae8ccc01c1e239e343c9666e0 *openwrt-x86-64-generic-ext4-rootfs.img.gz
+643ff73b119f3ecb36497a0c71213f9dd0129b64e803fa87d7e75b39c730e7fa *openwrt-x86-64-generic-kernel.bin
+770fa5a3e47ed12f46114aca6dca16a1a4ba2b6e89e53d5966839ffc5581dc53 *openwrt-x86-64-generic-squashfs-combined-efi.img.gz
+1a19c82c0614ad043fa0b854249bf6cc804550359ec453816ffbd426c31ab4a2 *openwrt-x86-64-generic-squashfs-combined.img.gz
+3b961a97e3105e02e07c1aba7671186efe559ce0ac078c370d5082a7a6999dbe *openwrt-x86-64-generic-squashfs-rootfs.img.gz
+76cc26429a61a516d348735a8d62bf3885d9d37489f20789a77c879dcf8a1025 *openwrt-x86-64-rootfs.tar.gz`),
+			},
+			"76cc26429a61a516d348735a8d62bf3885d9d37489f20789a77c879dcf8a1025",
+		},
 		{
 			"stage3-ppc64le-20200414T103003Z.tar.xz",
 			args{


More information about the lxc-devel mailing list