[lxc-devel] [distrobuilder/master] shared/net: Fix checksum check regression

monstermunchkin on Github lxc-bot at linuxcontainers.org
Wed May 20 06:47:26 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/20200519/3661858f/attachment.bin>
-------------- next part --------------
From 63b7a886b031b61880b5bb0bc4ba538c4e1782da Mon Sep 17 00:00:00 2001
From: Thomas Hipp <thomas.hipp at canonical.com>
Date: Wed, 20 May 2020 08:47:03 +0200
Subject: [PATCH] shared/net: Fix checksum check regression

Signed-off-by: Thomas Hipp <thomas.hipp at canonical.com>
---
 shared/net.go | 31 ++++++++++++++++++++++---------
 1 file changed, 22 insertions(+), 9 deletions(-)

diff --git a/shared/net.go b/shared/net.go
index 8ac9081..2c8be8e 100644
--- a/shared/net.go
+++ b/shared/net.go
@@ -21,7 +21,6 @@ func DownloadHash(def DefinitionImage, file, checksum string, hashFunc hash.Hash
 	var (
 		client http.Client
 		hashes []string
-		hash   string
 		err    error
 	)
 	targetDir := GetTargetDir(def)
@@ -69,6 +68,8 @@ func DownloadHash(def DefinitionImage, file, checksum string, hashFunc hash.Hash
 
 			result := fmt.Sprintf("%x", hashFunc.Sum(nil))
 
+			var hash string
+
 			for _, h := range hashes {
 				if result == h {
 					hash = h
@@ -94,15 +95,27 @@ func DownloadHash(def DefinitionImage, file, checksum string, hashFunc hash.Hash
 		fmt.Printf("%s\r", progress.Text)
 	}
 
-	if hashFunc != nil {
-		hashFunc.Reset()
-	}
-	_, err = lxd.DownloadFileHash(&client, "", progress, nil, imagePath, file, hash, hashFunc, image)
-	if err != nil {
-		if checksum == "" && strings.HasPrefix(err.Error(), "Hash mismatch") {
-			return targetDir, nil
+	if checksum == "" {
+		_, err = lxd.DownloadFileHash(&client, "", progress, nil, imagePath, file, "", nil, image)
+		if err != nil && !strings.HasPrefix(err.Error(), "Hash mismatch") {
+			return "", err
+		}
+	} else {
+		// Check all file hashes in case multiple have been provided.
+		for _, h := range hashes {
+			if hashFunc != nil {
+				hashFunc.Reset()
+			}
+
+			_, err = lxd.DownloadFileHash(&client, "", progress, nil, imagePath, file, h, hashFunc, image)
+			if err == nil {
+				break
+			}
+		}
+
+		if err != nil {
+			return "", err
 		}
-		return "", err
 	}
 
 	fmt.Println("")


More information about the lxc-devel mailing list