lineageos-ota-server/main.go
Steven Polley aaf0abd08e
All checks were successful
continuous-integration/drone/push Build is passing
fix concurrency race condition resulting in null json file
2023-06-26 10:34:18 -06:00

311 lines
8.4 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 or UNOFFICIAL
Size int `json:"size"` // size of .zip file in bytes
URL string `json:"url"` // Accessible 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 `json:"roms"`
Cached map[string]bool `json:"-"` // to quickly lookup if a file is already cached
sync.Mutex `json:"-"` // We have multiple goroutines that may be accessing this data simultaneously, so we much lock / unlock it to prevent race conditions
}
const (
romDirectory = "public" // directory where ROMs are available for download
buildOutDirectory = "out" // directory from build system containing artifacts which we can move to romDirectory
cacheFile = "public/romcache.json"
)
var ( // evil global variable
romCache ROMCache
)
func init() {
// intialize and load ROMCache from file - so we don't have to rehash all the big files again
romCache = ROMCache{}
romCacheJson, err := os.ReadFile(cacheFile)
if err != nil {
if err != os.ErrNotExist { // don't care if it doesn't exist, just skip it
log.Printf("failed to read romCache file : %v", err)
}
} else { // if opening the file's successful, then load the contents
err = json.Unmarshal(romCacheJson, &romCache)
if err != nil {
log.Printf("failed to unmarshal romCacheJson to romCache struct: %v", err)
}
}
romCache.Cached = make(map[string]bool)
for _, rom := range romCache.ROMs {
romCache.Cached[rom.ID] = true
log.Printf("loaded cached file: %s", rom.Filename)
}
// Check if any new build artifacts and preload the romCache
moveBuildArtifacts()
go updateROMCache()
}
// 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() {
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
}
wg := sync.WaitGroup{}
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()
wg.Add(1)
go func(v fs.DirEntry) {
defer wg.Done()
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: splitName[3], // UNOFFICIAL
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])
}
// save file to disk for next startup so we don't have to rehash all the files again
wg.Wait()
romCache.Lock()
romCacheJson, err := json.Marshal(romCache)
romCache.Unlock()
if err != nil {
log.Printf("failed to marshal romCache to json: %v", err)
return
}
err = os.WriteFile(cacheFile, romCacheJson, 0644)
if err != nil {
log.Printf("failed to write '%s' file: %v", cacheFile, err)
log.Printf("attempting to remove '%s' to ensure integrity during next program startup...", cacheFile)
err = os.Remove(cacheFile)
if err != nil {
log.Printf("failed to remove file '%s': %v", cacheFile, err)
}
return
}
}
// returns true if new builds were moved
func moveBuildArtifacts() bool {
f, err := os.Open(buildOutDirectory)
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", buildOutDirectory, 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()
if newBuilds {
updateROMCache()
}
}()
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
}