From 8ef8b3d1be07db05f76c52518acd6a3dfc2fc495 Mon Sep 17 00:00:00 2001 From: Steven Polley Date: Sun, 4 Jun 2023 12:56:58 -0600 Subject: [PATCH] do not recursively walk directories --- main.go | 67 +++++++++++++++++++++++++++++---------------------------- 1 file changed, 34 insertions(+), 33 deletions(-) diff --git a/main.go b/main.go index e63f937..da890f3 100644 --- a/main.go +++ b/main.go @@ -9,7 +9,6 @@ import ( "log" "net/http" "os" - "path/filepath" "strings" "sync" ) @@ -64,72 +63,74 @@ func main() { // Reads the ROM files on the filesystem and populates a slice of linageOSROMs func updateROMCache(romDirectory string) { - if _, err := os.Stat(romDirectory); os.IsNotExist(err) { - log.Printf("romDirectory '%s' does not exist", romDirectory) + f, err := os.Open(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 } - wg := sync.WaitGroup{} - err := filepath.WalkDir(romDirectory, func(s string, d fs.DirEntry, err error) error { - if err != nil { - return fmt.Errorf("walk error occured during file '%s': %v", d.Name(), err) + for i, v := range files { + // 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 romCache.Lock() - if _, ok := romCache.Cached[d.Name()]; ok { + if _, ok := romCache.Cached[v.Name()]; ok { romCache.Unlock() - return nil + continue } romCache.Unlock() - // Get information about file and populate rom - splitName := strings.Split(d.Name(), "-") + // Parse filename and skip files that can't be parsed + splitName := strings.Split(v.Name(), "-") if len(splitName) != 5 { - log.Printf("ignoring zip file '%s', name is not formatted correctly", d.Name()) - return nil + continue } - - wg.Add(1) - go func(d fs.DirEntry, wg *sync.WaitGroup) { - defer wg.Done() - fInfo, err := d.Info() + go func(v fs.DirEntry) { + fInfo, err := v.Info() 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 } - fileHash, err := hashFile(fmt.Sprintf("%s/%s", romDirectory, d.Name())) + 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", d.Name(), err) + log.Printf("ingore zip file '%s', failed to get sha256 hash: %v", v.Name(), err) return } lineageOSROM := LineageOSROM{ Datetime: int(fInfo.ModTime().Unix()), - Filename: d.Name(), + Filename: v.Name(), ID: fileHash, Romtype: "nightly", 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], } 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[d.Name()] = true + romCache.Cached[v.Name()] = true romCache.Unlock() - }(d, &wg) - - return nil - }) - if err != nil { - log.Printf("failed to walk romDirectory '%s': %v", romDirectory, err) - return + }(files[i]) } }