This commit is contained in:
+81
-5
@@ -1,15 +1,26 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"archive/zip"
|
||||
"bufio"
|
||||
"crypto/sha1"
|
||||
"crypto/sha256"
|
||||
"fmt"
|
||||
"io"
|
||||
"io/fs"
|
||||
"log"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type ZipMetadata struct {
|
||||
Timestamp int
|
||||
SDKLevel int
|
||||
PatchLevel string
|
||||
OTAPropertyFiles string
|
||||
}
|
||||
|
||||
// Searches the build toolchain output directory for new LineageOS builds.
|
||||
// Any new builds are moved into the public http server directory.
|
||||
// Returns true if new builds were moved
|
||||
@@ -50,18 +61,83 @@ func moveBuildArtifacts() bool {
|
||||
|
||||
// Returns a sha256 hash of a file located at the path provided
|
||||
func hashFile(filename string) (string, error) {
|
||||
_, sha256Val, err := hashFileSha1AndSha256(filename)
|
||||
return sha256Val, err
|
||||
}
|
||||
|
||||
// Returns sha1 and sha256 hashes of a file located at the path provided
|
||||
func hashFileSha1AndSha256(filename string) (string, string, error) {
|
||||
f, err := os.Open(filename)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("failed to open file '%s': %v: ", filename, err)
|
||||
return "", "", fmt.Errorf("failed to open file '%s': %v", filename, err)
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
h := sha256.New()
|
||||
if _, err := io.Copy(h, f); err != nil {
|
||||
return "", fmt.Errorf("failed to copy data from file to hash function: %v", err)
|
||||
h1 := sha1.New()
|
||||
h256 := sha256.New()
|
||||
|
||||
buf := make([]byte, 32768)
|
||||
for {
|
||||
n, err := f.Read(buf)
|
||||
if n > 0 {
|
||||
h1.Write(buf[:n])
|
||||
h256.Write(buf[:n])
|
||||
}
|
||||
if err == io.EOF {
|
||||
break
|
||||
}
|
||||
if err != nil {
|
||||
return "", "", fmt.Errorf("failed to read file '%s': %v", filename, err)
|
||||
}
|
||||
}
|
||||
|
||||
return fmt.Sprintf("%x", h.Sum(nil)), nil
|
||||
return fmt.Sprintf("%x", h1.Sum(nil)), fmt.Sprintf("%x", h256.Sum(nil)), nil
|
||||
}
|
||||
|
||||
// Reads metadata from META-INF/com/android/metadata inside the zip file
|
||||
func readZipMetadata(zipPath string) (ZipMetadata, error) {
|
||||
var meta ZipMetadata
|
||||
|
||||
r, err := zip.OpenReader(zipPath)
|
||||
if err != nil {
|
||||
return meta, err
|
||||
}
|
||||
defer r.Close()
|
||||
|
||||
for _, f := range r.File {
|
||||
if f.Name == "META-INF/com/android/metadata" {
|
||||
rc, err := f.Open()
|
||||
if err != nil {
|
||||
return meta, err
|
||||
}
|
||||
defer rc.Close()
|
||||
|
||||
scanner := bufio.NewScanner(rc)
|
||||
for scanner.Scan() {
|
||||
line := scanner.Text()
|
||||
parts := strings.SplitN(line, "=", 2)
|
||||
if len(parts) == 2 {
|
||||
key := strings.TrimSpace(parts[0])
|
||||
val := strings.TrimSpace(parts[1])
|
||||
switch key {
|
||||
case "post-timestamp":
|
||||
t, _ := strconv.Atoi(val)
|
||||
meta.Timestamp = t
|
||||
case "post-sdk-level":
|
||||
s, _ := strconv.Atoi(val)
|
||||
meta.SDKLevel = s
|
||||
case "security-patch-level":
|
||||
meta.PatchLevel = val
|
||||
case "ota-property-files":
|
||||
meta.OTAPropertyFiles = val
|
||||
}
|
||||
}
|
||||
}
|
||||
return meta, scanner.Err()
|
||||
}
|
||||
}
|
||||
|
||||
return meta, fmt.Errorf("metadata file not found in zip")
|
||||
}
|
||||
|
||||
// false if a file is not a LineageOS ROM .zip file
|
||||
|
||||
Reference in New Issue
Block a user