do not recursively walk directories
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Steven Polley 2023-06-04 12:56:58 -06:00
parent bff4c107c1
commit 8ef8b3d1be

67
main.go
View File

@ -9,7 +9,6 @@ import (
"log" "log"
"net/http" "net/http"
"os" "os"
"path/filepath"
"strings" "strings"
"sync" "sync"
) )
@ -64,72 +63,74 @@ func main() {
// Reads the ROM files on the filesystem and populates a slice of linageOSROMs // Reads the ROM files on the filesystem and populates a slice of linageOSROMs
func updateROMCache(romDirectory string) { func updateROMCache(romDirectory string) {
if _, err := os.Stat(romDirectory); os.IsNotExist(err) { f, err := os.Open(romDirectory)
log.Printf("romDirectory '%s' does not exist", romDirectory) if err != nil {
log.Printf("failed to open ROM directory: %v", err)
return
}
files, err := f.ReadDir(0)
if err != nil {
log.Printf("failed to read files in directory: %v", err)
return return
} }
wg := sync.WaitGroup{}
err := filepath.WalkDir(romDirectory, func(s string, d fs.DirEntry, err error) error { for i, v := range files {
if err != nil {
return fmt.Errorf("walk error occured during file '%s': %v", d.Name(), err)
// skip directories
if v.Type().IsDir() {
continue
} }
if filepath.Ext(d.Name()) != ".zip" {
return nil // skip non .zip files
if !strings.HasSuffix(v.Name(), ".zip") {
continue
} }
// skip already cached files // skip already cached files
romCache.Lock() romCache.Lock()
if _, ok := romCache.Cached[d.Name()]; ok { if _, ok := romCache.Cached[v.Name()]; ok {
romCache.Unlock() romCache.Unlock()
return nil continue
} }
romCache.Unlock() romCache.Unlock()
// Get information about file and populate rom // Parse filename and skip files that can't be parsed
splitName := strings.Split(d.Name(), "-") splitName := strings.Split(v.Name(), "-")
if len(splitName) != 5 { if len(splitName) != 5 {
log.Printf("ignoring zip file '%s', name is not formatted correctly", d.Name()) continue
return nil
} }
go func(v fs.DirEntry) {
wg.Add(1) fInfo, err := v.Info()
go func(d fs.DirEntry, wg *sync.WaitGroup) {
defer wg.Done()
fInfo, err := d.Info()
if err != nil { if err != nil {
log.Printf("failed to get file info '%s': %v", d.Name(), err) log.Printf("failed to get file info '%s': %v", v.Name(), err)
return return
} }
fileHash, err := hashFile(fmt.Sprintf("%s/%s", romDirectory, d.Name())) fileHash, err := hashFile(fmt.Sprintf("%s/%s", romDirectory, v.Name()))
if err != nil { if err != nil {
log.Printf("ingore zip file '%s', failed to get sha256 hash: %v", d.Name(), err) log.Printf("ingore zip file '%s', failed to get sha256 hash: %v", v.Name(), err)
return return
} }
lineageOSROM := LineageOSROM{ lineageOSROM := LineageOSROM{
Datetime: int(fInfo.ModTime().Unix()), Datetime: int(fInfo.ModTime().Unix()),
Filename: d.Name(), Filename: v.Name(),
ID: fileHash, ID: fileHash,
Romtype: "nightly", Romtype: "nightly",
Size: int(fInfo.Size()), Size: int(fInfo.Size()),
URL: fmt.Sprintf("https://lineageos-ota.deadbeef.codes/public/%s", d.Name()), URL: fmt.Sprintf("https://lineageos-ota.deadbeef.codes/public/%s", v.Name()),
Version: splitName[1], Version: splitName[1],
} }
romCache.Lock() 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.ROMs = append(romCache.ROMs, lineageOSROM)
romCache.Cached[d.Name()] = true romCache.Cached[v.Name()] = true
romCache.Unlock() romCache.Unlock()
}(d, &wg) }(files[i])
return nil
})
if err != nil {
log.Printf("failed to walk romDirectory '%s': %v", romDirectory, err)
return
} }
} }