quintodrome/scanner/change_detector.go

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

129 lines
2.9 KiB
Go
Raw Normal View History

2020-01-16 12:53:48 -09:00
package scanner
import (
2020-07-12 09:30:03 -08:00
"context"
"io/ioutil"
2020-01-16 12:53:48 -09:00
"os"
"path/filepath"
2020-01-16 12:53:48 -09:00
"time"
2020-01-23 15:44:08 -09:00
"github.com/deluan/navidrome/log"
2020-01-16 12:53:48 -09:00
)
2020-01-18 07:00:20 -09:00
type dirInfo struct {
mdate time.Time
maybe bool
}
type dirInfoMap map[string]dirInfo
type changeDetector struct {
2020-01-16 12:53:48 -09:00
rootFolder string
2020-01-18 07:00:20 -09:00
dirMap dirInfoMap
2020-01-16 12:53:48 -09:00
}
func newChangeDetector(rootFolder string) *changeDetector {
return &changeDetector{
2020-01-16 12:53:48 -09:00
rootFolder: rootFolder,
2020-01-18 07:00:20 -09:00
dirMap: dirInfoMap{},
2020-01-16 12:53:48 -09:00
}
}
func (s *changeDetector) Scan(ctx context.Context, lastModifiedSince time.Time) (changed []string, deleted []string, err error) {
2020-01-16 12:53:48 -09:00
start := time.Now()
2020-01-18 07:00:20 -09:00
newMap := make(dirInfoMap)
2020-07-12 09:30:03 -08:00
err = s.loadMap(ctx, newMap, s.rootFolder, lastModifiedSince, false)
2020-01-16 12:53:48 -09:00
if err != nil {
return
}
changed, deleted, err = s.checkForUpdates(lastModifiedSince, newMap)
2020-01-16 12:53:48 -09:00
if err != nil {
return
}
elapsed := time.Since(start)
2020-07-13 07:49:06 -08:00
log.Trace(ctx, "Folder analysis complete", "total", len(newMap), "changed", len(changed), "deleted", len(deleted), "elapsed", elapsed)
2020-01-16 12:53:48 -09:00
s.dirMap = newMap
return
}
func (s *changeDetector) loadDir(ctx context.Context, dirPath string) (children []string, lastUpdated time.Time, err error) {
2020-01-16 12:53:48 -09:00
dirInfo, err := os.Stat(dirPath)
if err != nil {
2020-07-12 16:42:38 -08:00
log.Error(ctx, "Error stating dir", "path", dirPath, err)
2020-01-16 12:53:48 -09:00
return
}
lastUpdated = dirInfo.ModTime()
files, err := ioutil.ReadDir(dirPath)
2020-01-16 12:53:48 -09:00
if err != nil {
2020-07-12 16:42:38 -08:00
log.Error(ctx, "Error reading dir", "path", dirPath, err)
2020-01-16 12:53:48 -09:00
return
}
for _, f := range files {
2020-05-18 11:06:33 -08:00
isDir, err := isDirOrSymlinkToDir(dirPath, f)
// Skip invalid symlinks
if err != nil {
continue
}
if isDir && !isDirIgnored(dirPath, f) && isDirReadable(dirPath, f) {
children = append(children, filepath.Join(dirPath, f.Name()))
2020-01-16 12:53:48 -09:00
} else {
if f.ModTime().After(lastUpdated) {
lastUpdated = f.ModTime()
}
}
}
return
}
func (s *changeDetector) loadMap(ctx context.Context, dirMap dirInfoMap, path string, since time.Time, maybe bool) error {
2020-07-12 09:30:03 -08:00
children, lastUpdated, err := s.loadDir(ctx, path)
2020-01-16 12:53:48 -09:00
if err != nil {
return err
}
2020-01-18 07:00:20 -09:00
maybe = maybe || lastUpdated.After(since)
2020-01-16 12:53:48 -09:00
for _, c := range children {
2020-07-12 09:30:03 -08:00
err := s.loadMap(ctx, dirMap, c, since, maybe)
2020-01-16 12:53:48 -09:00
if err != nil {
return err
}
}
dir := s.getRelativePath(path)
2020-01-18 07:00:20 -09:00
dirMap[dir] = dirInfo{mdate: lastUpdated, maybe: maybe}
2020-01-16 12:53:48 -09:00
return nil
}
func (s *changeDetector) getRelativePath(subFolder string) string {
dir, _ := filepath.Rel(s.rootFolder, subFolder)
2020-01-16 12:53:48 -09:00
if dir == "" {
dir = "."
}
return dir
}
func (s *changeDetector) checkForUpdates(lastModifiedSince time.Time, newMap dirInfoMap) (changed []string, deleted []string, err error) {
2020-01-18 07:00:20 -09:00
for dir, newEntry := range newMap {
lastUpdated := newEntry.mdate
oldLastUpdated := lastModifiedSince
if oldEntry, ok := s.dirMap[dir]; ok {
oldLastUpdated = oldEntry.mdate
} else {
if newEntry.maybe {
oldLastUpdated = time.Time{}
}
2020-01-16 12:53:48 -09:00
}
2020-01-16 12:53:48 -09:00
if lastUpdated.After(oldLastUpdated) {
changed = append(changed, dir)
}
}
for dir := range s.dirMap {
2020-01-16 12:53:48 -09:00
if _, ok := newMap[dir]; !ok {
deleted = append(deleted, dir)
}
}
return
}