Skip to content

Commit

Permalink
Reduce number of calls to lstat.
Browse files Browse the repository at this point in the history
Should make the scanner a bit faster, specially in networked filesystems
  • Loading branch information
deluan committed Jul 22, 2021
1 parent 86c0b42 commit 876dda8
Showing 1 changed file with 21 additions and 23 deletions.
44 changes: 21 additions & 23 deletions scanner/tag_scanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package scanner

import (
"context"
"io/fs"
"os"
"path/filepath"
"sort"
Expand Down Expand Up @@ -249,7 +250,7 @@ func (s *TagScanner) processChangedDir(ctx context.Context, dir string, fullScan
currentTracks[t.Path] = t
}

// Load tracks FileInfo from the folder
// Load track list from the folder
files, err := loadAllAudioFiles(dir)
if err != nil {
return err
Expand All @@ -268,15 +269,21 @@ func (s *TagScanner) processChangedDir(ctx context.Context, dir string, fullScan
// If track from folder is newer than the one in DB, select for update/insert in DB
log.Trace(ctx, "Processing changed folder", "dir", dir, "tracksInDB", len(currentTracks), "tracksInFolder", len(files))
var filesToUpdate []string
for filePath, info := range files {
c, ok := currentTracks[filePath]
if !ok {
for filePath, entry := range files {
c, inDB := currentTracks[filePath]
if !inDB || fullScan {
filesToUpdate = append(filesToUpdate, filePath)
s.cnt.added++
}
if ok && (info.ModTime().After(c.UpdatedAt) || fullScan) {
filesToUpdate = append(filesToUpdate, filePath)
s.cnt.updated++
} else {
info, err := entry.Info()
if err != nil {
log.Error("Could not stat file", "filePath", filePath, err)
continue
}
if info.ModTime().After(c.UpdatedAt) {
filesToUpdate = append(filesToUpdate, filePath)
s.cnt.updated++
}
}

// Force a refresh of the album and artist, to cater for cover art files
Expand Down Expand Up @@ -311,7 +318,7 @@ func (s *TagScanner) processChangedDir(ctx context.Context, dir string, fullScan

err = buffer.flush()
log.Info(ctx, "Finished processing changed folder", "dir", dir, "updated", numUpdatedTracks,
"purged", numPurgedTracks, "elapsed", time.Since(start))
"deleted", numPurgedTracks, "elapsed", time.Since(start))
return err
}

Expand Down Expand Up @@ -393,16 +400,12 @@ func (s *TagScanner) withAdminUser(ctx context.Context) context.Context {
return request.WithUser(ctx, *u)
}

func loadAllAudioFiles(dirPath string) (map[string]os.FileInfo, error) {
dir, err := os.Open(dirPath)
func loadAllAudioFiles(dirPath string) (map[string]fs.DirEntry, error) {
files, err := fs.ReadDir(os.DirFS(dirPath), ".")
if err != nil {
return nil, err
}
files, err := dir.Readdir(-1)
if err != nil {
return nil, err
}
audioFiles := make(map[string]os.FileInfo)
fileInfos := make(map[string]fs.DirEntry)
for _, f := range files {
if f.IsDir() {
continue
Expand All @@ -414,13 +417,8 @@ func loadAllAudioFiles(dirPath string) (map[string]os.FileInfo, error) {
if !utils.IsAudioFile(filePath) {
continue
}
fi, err := os.Stat(filePath)
if err != nil {
log.Error("Could not stat file", "filePath", filePath, err)
} else {
audioFiles[filePath] = fi
}
fileInfos[filePath] = f
}

return audioFiles, nil
return fileInfos, nil
}

0 comments on commit 876dda8

Please sign in to comment.