lineageos-ota-server/main.go
Steven Polley 9a2e992964
All checks were successful
continuous-integration/drone/push Build is passing
lock cache during ROM move operation
2023-06-04 15:58:16 -06:00

259 lines
6.9 KiB
Go

package main
import (
"crypto/sha256"
"encoding/json"
"fmt"
"io"
"io/fs"
"log"
"net/http"
"os"
"strings"
"sync"
)
// Information for a LineageOS ROM available for download
type LineageOSROM struct {
Datetime int `json:"datetime"` // Unix timestamp - i.e. 1685907926
Filename string `json:"filename"` // .zip filename - i.e. lineage-20.0-20230604-UNOFFICIAL-sunfish.zip
ID string `json:"id"` // A unique identifier such as a SHA256 hash of the .zip - i.e. 603bfc02e403e5fd1bf9ed74383f1d6c9ec7fb228d03c4b37753033d79488e93
Romtype string `json:"romtype"` // i.e. nightly
Size int `json:"size"` // size of .zip file in bytes
URL string `json:"url"` // public URL where client could download the .zip file
Version string `json:"version"` // LineageOS version - i.e. 20.0
}
// The HTTP response JSON should be a JSON array of lineageOSROMS available for download
type HTTPResponseJSON struct {
Response []LineageOSROM `json:"response"`
}
// Caches data about available ROMs in memory so we don't need to reference the filesystem for each request
type ROMCache struct {
ROMs []LineageOSROM
Cached map[string]bool // to quickly lookup if a file is already cached
sync.Mutex // We have multiple goroutines that may be accessing this data simultaneously, so we much lock / unlock it to prevent race conditions
}
var ( // evil global variable
romCache ROMCache
)
func init() {
romCache = ROMCache{}
romCache.Cached = make(map[string]bool)
// Check if any new build artifacts and preload the romCache
moveBuildArtifacts("out", "public")
go updateROMCache("public")
}
// HTTP Server
func main() {
//Public static files
http.Handle("/public/", http.StripPrefix("/public/", http.FileServer(http.Dir("public"))))
// ROM list
http.HandleFunc("/", lineageOSROMListHandler)
log.Print("Service listening on :8080")
log.Fatal(http.ListenAndServe(":8080", nil))
}
// Reads the ROM files on the filesystem and populates a slice of linageOSROMs
func updateROMCache(romDirectory string) {
log.Printf("updating ROM cache")
f, err := os.Open(romDirectory)
if err != nil {
log.Printf("failed to open ROM directory: %v", err)
return
}
defer f.Close()
files, err := f.ReadDir(0)
if err != nil {
log.Printf("failed to read files in directory: %v", err)
return
}
for i, v := range files {
isLineageROM, splitName := isLineageROMZip(v)
if !isLineageROM {
continue
}
// skip already cached files
romCache.Lock()
if _, ok := romCache.Cached[v.Name()]; ok {
romCache.Unlock()
continue
}
romCache.Unlock()
go func(v fs.DirEntry) {
fInfo, err := v.Info()
if err != nil {
log.Printf("failed to get file info '%s': %v", v.Name(), err)
return
}
fileHash, err := hashFile(fmt.Sprintf("%s/%s", romDirectory, v.Name()))
if err != nil {
log.Printf("ingore zip file '%s', failed to get sha256 hash: %v", v.Name(), err)
return
}
lineageOSROM := LineageOSROM{
Datetime: int(fInfo.ModTime().Unix()),
Filename: v.Name(),
ID: fileHash,
Romtype: "nightly",
Size: int(fInfo.Size()),
URL: fmt.Sprintf("https://lineageos-ota.deadbeef.codes/public/%s", v.Name()),
Version: splitName[1],
}
romCache.Lock()
if _, ok := romCache.Cached[v.Name()]; ok { // it's possible another goroutine was already working to cache the file, so we check at the end
romCache.Unlock()
return
}
romCache.ROMs = append(romCache.ROMs, lineageOSROM)
romCache.Cached[v.Name()] = true
romCache.Unlock()
}(files[i])
}
}
// returns true if new builds were moved
func moveBuildArtifacts(outDirectory, romDirectory string) bool {
f, err := os.Open(outDirectory)
if err != nil {
log.Printf("failed to open ROM directory: %v", err)
return false
}
defer f.Close()
files, err := f.ReadDir(0)
if err != nil {
log.Printf("failed to read files in directory: %v", err)
return false
}
var newROMs bool
for _, v := range files {
if isLineageROM, _ := isLineageROMZip(v); !isLineageROM { // skip files that aren't LineageOS ROMs
continue
}
newROMs = true
log.Printf("new build found - moving file %s", v.Name())
romCache.Lock() // lock to prevent multiple concurrent goroutines moving the same file
err := moveBuildFile(fmt.Sprintf("%s/%s", outDirectory, v.Name()), fmt.Sprintf("%s/%s", romDirectory, v.Name()))
romCache.Unlock()
if err != nil {
log.Printf("failed to move file '%s' from out to rom directory: %v", v.Name(), err)
continue
}
}
return newROMs
}
// false if a file is not a LineageOS ROM .zip file
// no formal validation is performed - only file naming convention is checked
// also returns a lineage ROM's filename sliced and delimited by -'s
// Example filename: lineage-20.0-20230604-UNOFFICIAL-sunfish.zip
func isLineageROMZip(v fs.DirEntry) (bool, []string) {
// skip directories, non .zip files and files that don't begin with lineage-
if v.Type().IsDir() || !strings.HasSuffix(v.Name(), ".zip") || !strings.HasPrefix(v.Name(), "lineage-") {
return false, nil
}
splitName := strings.Split(v.Name(), "-")
// expect 5 dashes
return len(splitName) == 5, splitName
}
// http - GET /
// Writes JSON response for the updater app to know what versions are available to download
func lineageOSROMListHandler(w http.ResponseWriter, r *http.Request) {
go func() {
newBuilds := moveBuildArtifacts("out", "public")
if newBuilds {
updateROMCache("public")
}
}()
romCache.Lock()
httpResponseJSON := &HTTPResponseJSON{Response: romCache.ROMs}
romCache.Unlock()
b, err := json.Marshal(httpResponseJSON)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
log.Printf("failed to marshal lineageOSROMs to json: %v", err)
return
}
w.Write(b)
}
// Returns a sha256 hash of a file located at the path provided
func hashFile(filename string) (string, error) {
f, err := os.Open(filename)
if err != nil {
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)
}
return fmt.Sprintf("%x", h.Sum(nil)), nil
}
// A custom "move file" function because in docker container the mounted folders are different overlay filesystems
// Instead of os.Rename, we must copy and delete
func moveBuildFile(src, dst string) error {
sourceFileStat, err := os.Stat(src)
if err != nil {
return err
}
if !sourceFileStat.Mode().IsRegular() {
return fmt.Errorf("%s is not a regular file", src)
}
source, err := os.Open(src)
if err != nil {
return err
}
defer source.Close()
destination, err := os.Create(dst)
if err != nil {
return err
}
defer destination.Close()
_, err = io.Copy(destination, source)
if err != nil {
return fmt.Errorf("failed to copy file: %v", err)
}
err = os.Remove(src)
if err != nil {
return fmt.Errorf("failed to delete source file after copy: %v", err)
}
return nil
}