Only download ls-iommu if we do not have it or its outdated
This commit is contained in:
parent
4ffa9aaf35
commit
3b0731be1c
2 changed files with 58 additions and 8 deletions
|
@ -1,12 +1,16 @@
|
||||||
package ls_iommu_downloader
|
package ls_iommu_downloader
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"crypto/sha256"
|
||||||
|
"encoding/hex"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/HikariKnight/ls-iommu/pkg/errorcheck"
|
"github.com/HikariKnight/ls-iommu/pkg/errorcheck"
|
||||||
|
@ -119,8 +123,55 @@ func GetLsIOMMU() {
|
||||||
result.TagName,
|
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)
|
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()
|
grabClient := grab.NewClient()
|
||||||
req, _ := grab.NewRequest(fileName, downloadUrl)
|
req, _ := grab.NewRequest(fileName, downloadUrl)
|
||||||
|
|
||||||
|
@ -134,10 +185,4 @@ func GetLsIOMMU() {
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Printf("Download saved to ./%v \n", download.Filename)
|
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)
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,13 +6,18 @@ import (
|
||||||
"io"
|
"io"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
|
||||||
|
"github.com/HikariKnight/ls-iommu/pkg/errorcheck"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Source: https://medium.com/@skdomino/taring-untaring-files-in-go-6b07cf56bc07
|
// 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
|
// 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
|
// 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)
|
gzr, err := gzip.NewReader(r)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue