do not recursively walk directories
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
parent
bff4c107c1
commit
8ef8b3d1be
69
main.go
69
main.go
@ -9,7 +9,6 @@ import (
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"sync"
|
||||
)
|
||||
@ -64,73 +63,75 @@ 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
|
||||
}
|
||||
wg := sync.WaitGroup{}
|
||||
|
||||
err := filepath.WalkDir(romDirectory, func(s string, d fs.DirEntry, err error) error {
|
||||
files, err := f.ReadDir(0)
|
||||
if err != nil {
|
||||
return fmt.Errorf("walk error occured during file '%s': %v", d.Name(), err)
|
||||
|
||||
log.Printf("failed to read files in directory: %v", err)
|
||||
return
|
||||
}
|
||||
if filepath.Ext(d.Name()) != ".zip" {
|
||||
return nil
|
||||
|
||||
for i, v := range files {
|
||||
|
||||
// skip directories
|
||||
if v.Type().IsDir() {
|
||||
continue
|
||||
}
|
||||
|
||||
// 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()
|
||||
romCache.ROMs = append(romCache.ROMs, lineageOSROM)
|
||||
romCache.Cached[d.Name()] = true
|
||||
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()
|
||||
}(d, &wg)
|
||||
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
log.Printf("failed to walk romDirectory '%s': %v", romDirectory, err)
|
||||
return
|
||||
}
|
||||
romCache.ROMs = append(romCache.ROMs, lineageOSROM)
|
||||
romCache.Cached[v.Name()] = true
|
||||
romCache.Unlock()
|
||||
}(files[i])
|
||||
}
|
||||
}
|
||||
|
||||
// http - GET /
|
||||
|
Loading…
Reference in New Issue
Block a user