Only download ls-iommu if we do not have it or its outdated

This commit is contained in:
HikariKnight 2023-04-07 03:16:27 +02:00
parent 4ffa9aaf35
commit 3b0731be1c
2 changed files with 58 additions and 8 deletions

View file

@ -1,12 +1,16 @@
package ls_iommu_downloader
import (
"crypto/sha256"
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"io"
"log"
"net/http"
"os"
"strings"
"time"
"github.com/HikariKnight/ls-iommu/pkg/errorcheck"
@ -119,8 +123,55 @@ func GetLsIOMMU() {
result.TagName,
)
// Create blank file
// Generate checksums.txt url
checkSumsUrl := fmt.Sprintf(
"https://github.com/HikariKnight/ls-iommu/releases/download/%s/checksums.txt",
result.TagName,
)
fileName := fmt.Sprintf("%s/ls-iommu_Linux_x86_64.tar.gz", path)
// Get the checksum data
checksums, err := http.Get(checkSumsUrl)
errorcheck.ErrorCheck(err)
defer checksums.Body.Close()
checksums_txt, err := io.ReadAll(checksums.Body)
errorcheck.ErrorCheck(err)
// Check if the tar.gz exists
_, err = os.Stat(fileName)
if errors.Is(err, os.ErrNotExist) {
downloadNewVersion(path, fileName, downloadUrl)
if checkSum(string(checksums_txt), fileName) {
err = untar.Untar(fmt.Sprintf("%s/", path), fileName)
errorcheck.ErrorCheck(err)
}
} else {
if !checkSum(string(checksums_txt), fileName) {
downloadNewVersion(path, fileName, downloadUrl)
err = untar.Untar(fmt.Sprintf("%s/", path), fileName)
errorcheck.ErrorCheck(err)
}
}
}
func checkSum(checksums string, fileName string) bool {
r, err := os.Open(fileName)
errorcheck.ErrorCheck(err)
defer r.Close()
hasher := sha256.New()
if _, err := io.Copy(hasher, r); err != nil {
log.Fatal(err)
}
value := hex.EncodeToString(hasher.Sum(nil))
return strings.Contains(checksums, value)
}
func downloadNewVersion(path, fileName, downloadUrl string) {
// Create a request
grabClient := grab.NewClient()
req, _ := grab.NewRequest(fileName, downloadUrl)
@ -134,10 +185,4 @@ func GetLsIOMMU() {
}
fmt.Printf("Download saved to ./%v \n", download.Filename)
r, err := os.Open(fileName)
errorcheck.ErrorCheck(err)
err = untar.Untar(fmt.Sprintf("%s/", path), r)
errorcheck.ErrorCheck(err)
}

View file

@ -6,13 +6,18 @@ import (
"io"
"os"
"path/filepath"
"github.com/HikariKnight/ls-iommu/pkg/errorcheck"
)
// Source: https://medium.com/@skdomino/taring-untaring-files-in-go-6b07cf56bc07
// Untar takes a destination path and a reader; a tar reader loops over the tarfile
// creating the file structure at 'dst' along the way, and writing any files
func Untar(dst string, r io.Reader) error {
func Untar(dst string, fileName string) error {
r, err := os.Open(fileName)
errorcheck.ErrorCheck(err)
defer r.Close()
gzr, err := gzip.NewReader(r)
if err != nil {