kernel-rpi-os-32/cmd/check-update/main.go

194 lines
4 KiB
Go
Raw Permalink Normal View History

2022-03-23 16:08:12 +00:00
package main
import (
"archive/tar"
2022-03-23 16:08:12 +00:00
"bufio"
2022-03-26 13:25:22 +00:00
"compress/gzip"
2022-03-23 16:32:29 +00:00
"encoding/json"
2022-03-23 16:08:12 +00:00
"errors"
"fmt"
"io"
2022-03-23 16:08:12 +00:00
"log"
"net/http"
"os"
"os/exec"
"strings"
"github.com/ulikunitz/xz"
2022-03-23 16:08:12 +00:00
)
func main() {
if err := run(); err != nil {
log.Println(err)
os.Exit(1)
}
}
const baseURL = "https://archive.raspberrypi.org/debian/"
2022-03-26 13:25:22 +00:00
const packagesGzURL = baseURL + "dists/bullseye/main/binary-armhf/Packages.gz"
2022-03-23 16:08:12 +00:00
func run() error {
2022-03-26 13:25:22 +00:00
log.Println("checking:", packagesGzURL)
2022-03-23 16:08:12 +00:00
kernelPrefix := "Filename: pool/main/r/raspberrypi-firmware/raspberrypi-kernel_"
version := ""
versionPrefix := "Version: "
2022-03-23 19:51:09 +00:00
found := false
2022-03-26 13:25:22 +00:00
err := fetchAndScanGzTextFile(packagesGzURL, func(s string) bool {
2022-03-23 16:08:12 +00:00
if strings.HasPrefix(s, versionPrefix) {
version = s[len(versionPrefix):]
}
if strings.HasPrefix(s, kernelPrefix) {
2022-03-23 19:51:09 +00:00
found = true
2022-03-23 16:08:12 +00:00
return true
}
return false
})
2022-03-23 19:51:09 +00:00
if !found {
2022-03-23 16:08:12 +00:00
if err != nil {
return err
}
return errors.New("could not find kernel version in package list")
}
before, after, found := strings.Cut(version, ":")
if !found {
after = before
}
2022-03-23 19:49:32 +00:00
tagName, _, _ := strings.Cut(after, "-")
2022-03-23 19:57:46 +00:00
tagName, _, _ = strings.Cut(tagName, "~")
2022-03-23 16:08:12 +00:00
log.Println("latest version:", tagName)
latestSha, err := commitFromTag(tagName)
2022-03-23 16:08:12 +00:00
if err != nil {
return err
}
2022-03-23 16:32:29 +00:00
log.Println("latest commit:", latestSha)
currentSha, err := submoduleSha("linux-sources")
if err != nil {
return err
2022-03-23 16:08:12 +00:00
}
2022-03-23 16:32:29 +00:00
log.Println("submodule commit:", currentSha)
if latestSha == currentSha {
log.Println("already up to date")
return nil
}
fmt.Println(latestSha)
2022-03-23 16:08:12 +00:00
return nil
}
func commitFromTag(tagName string) (string, error) {
latestSha, err := githubCommitSha(tagName)
log.Println("checking https://github.com/raspberrypi/linux tags")
if err == nil {
return latestSha, nil
}
log.Println(err)
xzURL := "https://archive.raspberrypi.org/debian/pool/main/r/raspberrypi-firmware/raspberrypi-firmware_" + tagName + ".orig.tar.xz"
log.Println("checking", xzURL)
return debianSourceCommitSha(xzURL)
}
2022-03-23 16:32:29 +00:00
func githubCommitSha(tagName string) (string, error) {
req, err := http.NewRequest("GET", "https://api.github.com/repos/raspberrypi/linux/git/ref/tags/"+tagName, nil)
2022-03-23 16:08:12 +00:00
if err != nil {
2022-03-23 16:32:29 +00:00
return "", err
2022-03-23 16:08:12 +00:00
}
2022-03-23 16:32:29 +00:00
req.Header.Add("Accept", "application/vnd.github.v3+json")
resp, err := http.DefaultClient.Do(req)
if err != nil {
return "", err
}
defer resp.Body.Close()
2022-03-23 16:08:12 +00:00
2022-03-23 16:32:29 +00:00
type githubResponse struct {
Message string `json:"message"`
Object struct {
Sha string `json:"sha"`
} `json:"object"`
}
var gr githubResponse
err = json.NewDecoder(resp.Body).Decode(&gr)
2022-03-23 16:08:12 +00:00
if err != nil {
2022-03-23 16:32:29 +00:00
return "", err
2022-03-23 16:08:12 +00:00
}
2022-03-23 16:32:29 +00:00
if gr.Object.Sha == "" {
return "", fmt.Errorf("could not get sha for tag %q: %s", tagName, gr.Message)
2022-03-23 16:32:29 +00:00
}
return gr.Object.Sha, nil
}
func debianSourceCommitSha(xzURL string) (string, error) {
resp, err := http.Get(xzURL)
if err != nil {
return "", err
}
xzFile := resp.Body
// for local testing
// xzFile, err := os.Open("raspberrypi-firmware_1.20230317.orig.tar.xz")
// if err != nil {
// return "", err
// }
defer xzFile.Close()
tarFile, err := xz.NewReader(xzFile)
if err != nil {
return "", err
}
tr := tar.NewReader(tarFile)
for {
hdr, err := tr.Next()
if err != nil {
return "", fmt.Errorf("extra/git_hash not found: %w", err)
}
if !strings.HasSuffix(hdr.Name, "/extra/git_hash") {
continue
}
buf, err := io.ReadAll(tr)
if err != nil {
return "", err
}
return strings.TrimSpace(string(buf)), nil
}
}
2022-03-23 16:32:29 +00:00
func submoduleSha(submodule string) (string, error) {
cmd := exec.Command("git", "rev-parse", "HEAD:"+submodule)
2022-03-23 16:08:12 +00:00
cmd.Stderr = os.Stderr
out, err := cmd.Output()
if err != nil {
2022-03-23 16:32:29 +00:00
return "", err
2022-03-23 16:08:12 +00:00
}
2022-03-23 16:32:29 +00:00
return strings.TrimSpace(string(out)), nil
2022-03-23 16:08:12 +00:00
}
2022-03-26 13:25:22 +00:00
func fetchAndScanGzTextFile(url string, stopScanning func(string) bool) error {
2022-03-23 16:08:12 +00:00
resp, err := http.Get(url)
if err != nil {
return err
}
defer resp.Body.Close()
2022-03-26 13:25:22 +00:00
unzipped, err := gzip.NewReader(resp.Body)
if err != nil {
return err
}
scanner := bufio.NewScanner(unzipped)
2022-03-23 16:08:12 +00:00
for scanner.Scan() {
if stopScanning(scanner.Text()) {
break
}
}
return scanner.Err()
}