fix concurrency race condition resulting in null json file
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Steven Polley 2023-06-26 10:34:18 -06:00
parent 3c5c99e340
commit aaf0abd08e

11
main.go
View File

@ -49,7 +49,7 @@ var ( // evil global variable
func init() {
// intialize and load ROMCache from file - so we don't have to rehash all the big files again
romCache = ROMCache{}
romCache.Cached = make(map[string]bool)
romCacheJson, err := os.ReadFile(cacheFile)
if err != nil {
if err != os.ErrNotExist { // don't care if it doesn't exist, just skip it
@ -60,8 +60,11 @@ func init() {
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)
@ -102,6 +105,8 @@ func updateROMCache() {
return
}
wg := sync.WaitGroup{}
for i, v := range files {
isLineageROM, splitName := isLineageROMZip(v)
@ -117,7 +122,9 @@ func updateROMCache() {
}
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)
@ -148,10 +155,12 @@ func updateROMCache() {
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()