2020-01-16 22:53:48 +01:00
|
|
|
package scanner
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
2020-02-03 02:12:46 +01:00
|
|
|
"path/filepath"
|
2020-01-16 22:53:48 +01:00
|
|
|
"time"
|
|
|
|
|
2020-05-18 20:37:01 +02:00
|
|
|
"github.com/deluan/navidrome/consts"
|
2020-01-24 01:44:08 +01:00
|
|
|
"github.com/deluan/navidrome/log"
|
2020-01-16 22:53:48 +01:00
|
|
|
)
|
|
|
|
|
2020-01-18 17:00:20 +01:00
|
|
|
type dirInfo struct {
|
|
|
|
mdate time.Time
|
|
|
|
maybe bool
|
|
|
|
}
|
|
|
|
type dirInfoMap map[string]dirInfo
|
|
|
|
|
2020-01-16 22:53:48 +01:00
|
|
|
type ChangeDetector struct {
|
|
|
|
rootFolder string
|
2020-01-18 17:00:20 +01:00
|
|
|
dirMap dirInfoMap
|
2020-01-16 22:53:48 +01:00
|
|
|
}
|
|
|
|
|
2020-01-18 04:41:10 +01:00
|
|
|
func NewChangeDetector(rootFolder string) *ChangeDetector {
|
2020-01-16 22:53:48 +01:00
|
|
|
return &ChangeDetector{
|
|
|
|
rootFolder: rootFolder,
|
2020-01-18 17:00:20 +01:00
|
|
|
dirMap: dirInfoMap{},
|
2020-01-16 22:53:48 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-18 04:41:10 +01:00
|
|
|
func (s *ChangeDetector) Scan(lastModifiedSince time.Time) (changed []string, deleted []string, err error) {
|
2020-01-16 22:53:48 +01:00
|
|
|
start := time.Now()
|
2020-01-18 17:00:20 +01:00
|
|
|
newMap := make(dirInfoMap)
|
|
|
|
err = s.loadMap(newMap, s.rootFolder, lastModifiedSince, false)
|
2020-01-16 22:53:48 +01:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2020-01-18 04:41:10 +01:00
|
|
|
changed, deleted, err = s.checkForUpdates(lastModifiedSince, newMap)
|
2020-01-16 22:53:48 +01:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
elapsed := time.Since(start)
|
|
|
|
|
|
|
|
log.Trace("Folder analysis complete\n", "total", len(newMap), "changed", len(changed), "deleted", len(deleted), "elapsed", elapsed)
|
|
|
|
s.dirMap = newMap
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *ChangeDetector) loadDir(dirPath string) (children []string, lastUpdated time.Time, err error) {
|
|
|
|
dir, err := os.Open(dirPath)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2020-03-20 05:02:31 +01:00
|
|
|
defer dir.Close()
|
2020-01-16 22:53:48 +01:00
|
|
|
dirInfo, err := os.Stat(dirPath)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
lastUpdated = dirInfo.ModTime()
|
|
|
|
|
|
|
|
files, err := dir.Readdir(-1)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
for _, f := range files {
|
2020-05-18 21:06:33 +02:00
|
|
|
isDir, err := isDirOrSymlinkToDir(dirPath, f)
|
2020-04-24 02:12:32 +02:00
|
|
|
// Skip invalid symlinks
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
2020-05-18 21:06:33 +02:00
|
|
|
if isDir && !isDirIgnored(dirPath, f) {
|
2020-02-03 02:12:46 +01:00
|
|
|
children = append(children, filepath.Join(dirPath, f.Name()))
|
2020-01-16 22:53:48 +01:00
|
|
|
} else {
|
|
|
|
if f.ModTime().After(lastUpdated) {
|
|
|
|
lastUpdated = f.ModTime()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-05-18 21:06:33 +02:00
|
|
|
// isDirOrSymlinkToDir returns true if and only if the dirent represents a file
|
2020-05-18 20:37:01 +02:00
|
|
|
// system directory, or a symbolic link to a directory. Note that if the dirent
|
2020-04-24 02:12:32 +02:00
|
|
|
// is not a directory but is a symbolic link, this method will resolve by
|
|
|
|
// sending a request to the operating system to follow the symbolic link.
|
|
|
|
// Copied from github.com/karrick/godirwalk
|
2020-05-18 21:06:33 +02:00
|
|
|
func isDirOrSymlinkToDir(baseDir string, dirent os.FileInfo) (bool, error) {
|
2020-05-18 20:37:01 +02:00
|
|
|
if dirent.IsDir() {
|
2020-04-24 02:12:32 +02:00
|
|
|
return true, nil
|
|
|
|
}
|
2020-05-18 20:37:01 +02:00
|
|
|
if dirent.Mode()&os.ModeSymlink == 0 {
|
2020-04-24 02:12:32 +02:00
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
// Does this symlink point to a directory?
|
2020-05-18 20:37:01 +02:00
|
|
|
dirent, err := os.Stat(filepath.Join(baseDir, dirent.Name()))
|
2020-04-24 02:12:32 +02:00
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
2020-05-18 20:37:01 +02:00
|
|
|
return dirent.IsDir(), nil
|
|
|
|
}
|
|
|
|
|
2020-05-18 21:06:33 +02:00
|
|
|
func isDirIgnored(baseDir string, dirent os.FileInfo) bool {
|
2020-05-18 20:37:01 +02:00
|
|
|
_, err := os.Stat(filepath.Join(baseDir, dirent.Name(), consts.SkipScanFile))
|
|
|
|
return err == nil
|
2020-04-24 02:12:32 +02:00
|
|
|
}
|
|
|
|
|
2020-02-03 23:25:17 +01:00
|
|
|
func (s *ChangeDetector) loadMap(dirMap dirInfoMap, path string, since time.Time, maybe bool) error {
|
|
|
|
children, lastUpdated, err := s.loadDir(path)
|
2020-01-16 22:53:48 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-01-18 17:00:20 +01:00
|
|
|
maybe = maybe || lastUpdated.After(since)
|
2020-01-16 22:53:48 +01:00
|
|
|
for _, c := range children {
|
2020-01-18 17:00:20 +01:00
|
|
|
err := s.loadMap(dirMap, c, since, maybe)
|
2020-01-16 22:53:48 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-03 23:25:17 +01:00
|
|
|
dir := s.getRelativePath(path)
|
2020-01-18 17:00:20 +01:00
|
|
|
dirMap[dir] = dirInfo{mdate: lastUpdated, maybe: maybe}
|
2020-01-16 22:53:48 +01:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-02-03 23:25:17 +01:00
|
|
|
func (s *ChangeDetector) getRelativePath(subFolder string) string {
|
|
|
|
dir, _ := filepath.Rel(s.rootFolder, subFolder)
|
2020-01-16 22:53:48 +01:00
|
|
|
if dir == "" {
|
|
|
|
dir = "."
|
|
|
|
}
|
|
|
|
return dir
|
|
|
|
}
|
|
|
|
|
2020-01-18 17:00:20 +01:00
|
|
|
func (s *ChangeDetector) checkForUpdates(lastModifiedSince time.Time, newMap dirInfoMap) (changed []string, deleted []string, err error) {
|
|
|
|
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 22:53:48 +01:00
|
|
|
}
|
2020-02-03 23:25:17 +01:00
|
|
|
|
2020-01-16 22:53:48 +01:00
|
|
|
if lastUpdated.After(oldLastUpdated) {
|
|
|
|
changed = append(changed, dir)
|
|
|
|
}
|
|
|
|
}
|
2020-01-18 04:41:10 +01:00
|
|
|
for dir := range s.dirMap {
|
2020-01-16 22:53:48 +01:00
|
|
|
if _, ok := newMap[dir]; !ok {
|
|
|
|
deleted = append(deleted, dir)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|