Skip to content

Commit c5b3a1c

Browse files
committed
scanner: use create time if we have it
1 parent f5893ea commit c5b3a1c

File tree

3 files changed

+19
-9
lines changed

3 files changed

+19
-9
lines changed

go.mod

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ require (
77
github.com/andybalholm/cascadia v1.3.2
88
github.com/dexterlb/mpvipc v0.0.0-20230829142118-145d6eabdc37
99
github.com/disintegration/imaging v1.6.2
10+
github.com/djherbis/times v1.6.0
1011
github.com/dustin/go-humanize v1.0.1
1112
github.com/fatih/structs v1.1.0
1213
github.com/fsnotify/fsnotify v1.7.0
@@ -28,6 +29,7 @@ require (
2829
go.senan.xyz/flagconf v0.1.8
2930
golang.org/x/net v0.24.0
3031
golang.org/x/sync v0.7.0
32+
golang.org/x/sys v0.19.0
3133
gopkg.in/gormigrate.v1 v1.6.0
3234
jaytaylor.com/html2text v0.0.0-20230321000545-74c2419ad056
3335
)
@@ -60,7 +62,6 @@ require (
6062
github.com/ssor/bom v0.0.0-20170718123548-6386211fdfcf // indirect
6163
golang.org/x/crypto v0.22.0 // indirect
6264
golang.org/x/image v0.15.0 // indirect
63-
golang.org/x/sys v0.19.0 // indirect
6465
golang.org/x/text v0.14.0 // indirect
6566
gopkg.in/natefinch/npipe.v2 v2.0.0-20160621034901-c1b8fa8bdcce // indirect
6667
gopkg.in/yaml.v2 v2.4.0 // indirect

go.sum

+3
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ github.com/dexterlb/mpvipc v0.0.0-20230829142118-145d6eabdc37 h1:s+qNFsO3VsdsKro
2121
github.com/dexterlb/mpvipc v0.0.0-20230829142118-145d6eabdc37/go.mod h1:CXCwawNJCtFDip7gvbaQVgw0cGjldpyHDIp7oA5prOg=
2222
github.com/disintegration/imaging v1.6.2 h1:w1LecBlG2Lnp8B3jk5zSuNqd7b4DXhcjwek1ei82L+c=
2323
github.com/disintegration/imaging v1.6.2/go.mod h1:44/5580QXChDfwIclfc/PCwrr44amcmDAg8hxG0Ewe4=
24+
github.com/djherbis/times v1.6.0 h1:w2ctJ92J8fBvWPxugmXIv7Nz7Q3iDMKNx9v5ocVH20c=
25+
github.com/djherbis/times v1.6.0/go.mod h1:gOHeRAz2h+VJNZ5Gmc/o7iD9k4wW7NMVqieYCY99oc0=
2426
github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY=
2527
github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+mFykh5fBlto=
2628
github.com/erikstmartin/go-testdb v0.0.0-20160219214506-8d10e4a1bae5 h1:Yzb9+7DPaBjB8zlTR87/ElzFsnQfuHnVUVqpZZIcV5Y=
@@ -174,6 +176,7 @@ golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7w
174176
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
175177
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
176178
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
179+
golang.org/x/sys v0.0.0-20220615213510-4f61da869c0c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
177180
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
178181
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
179182
golang.org/x/sys v0.7.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=

scanner/scanner.go

+14-8
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
"sync/atomic"
1818
"time"
1919

20+
"github.com/djherbis/times"
2021
"github.com/fsnotify/fsnotify"
2122
"github.com/jinzhu/gorm"
2223
"github.com/rainycape/unidecode"
@@ -314,17 +315,18 @@ func (s *Scanner) scanDir(tx *db.DB, st *State, absPath string) error {
314315
}
315316

316317
func (s *Scanner) populateTrackAndArtists(tx *db.DB, st *State, i int, album *db.Album, basename string, absPath string) error {
317-
stat, err := os.Stat(absPath)
318+
// useful to get the real create/birth time for filesystems and kernels which support it
319+
timeSpec, err := times.Stat(absPath)
318320
if err != nil {
319-
return fmt.Errorf("stating %q: %w", basename, err)
321+
return fmt.Errorf("get times %q: %w", basename, err)
320322
}
321323

322324
var track db.Track
323325
if err := tx.Where("album_id=? AND filename=?", album.ID, filepath.Base(basename)).First(&track).Error; err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
324326
return fmt.Errorf("query track: %w", err)
325327
}
326328

327-
if !st.isFull && track.ID != 0 && stat.ModTime().Before(track.UpdatedAt) {
329+
if !st.isFull && track.ID != 0 && timeSpec.ModTime().Before(track.UpdatedAt) {
328330
st.seenTracks[track.ID] = struct{}{}
329331
return nil
330332
}
@@ -363,7 +365,7 @@ func (s *Scanner) populateTrackAndArtists(tx *db.DB, st *State, i int, album *db
363365
return fmt.Errorf("populate track artists: %w", err)
364366
}
365367

366-
if err := populateAlbum(tx, album, trags, stat.ModTime()); err != nil {
368+
if err := populateAlbum(tx, album, trags, timeSpec.ChangeTime(), timeSpec.BirthTime()); err != nil {
367369
return fmt.Errorf("populate album: %w", err)
368370
}
369371

@@ -372,6 +374,10 @@ func (s *Scanner) populateTrackAndArtists(tx *db.DB, st *State, i int, album *db
372374
}
373375
}
374376

377+
stat, err := os.Stat(absPath)
378+
if err != nil {
379+
return fmt.Errorf("stating %q: %w", basename, err)
380+
}
375381
if err := populateTrack(tx, album, &track, trags, basename, int(stat.Size())); err != nil {
376382
return fmt.Errorf("process %q: %w", basename, err)
377383
}
@@ -402,17 +408,17 @@ func (s *Scanner) populateTrackAndArtists(tx *db.DB, st *State, i int, album *db
402408
return nil
403409
}
404410

405-
func populateAlbum(tx *db.DB, album *db.Album, trags tagcommon.Info, modTime time.Time) error {
411+
func populateAlbum(tx *db.DB, album *db.Album, trags tagcommon.Info, modifiedAt, createdAt time.Time) error {
406412
albumName := tagcommon.MustAlbum(trags)
407413
album.TagTitle = albumName
408414
album.TagTitleUDec = decoded(albumName)
409415
album.TagAlbumArtist = tagcommon.MustAlbumArtist(trags)
410416
album.TagBrainzID = trags.AlbumBrainzID()
411417
album.TagYear = trags.Year()
412418

413-
album.ModifiedAt = modTime
414-
if album.CreatedAt.After(modTime) {
415-
album.CreatedAt = modTime // reset created at to match filesytem for new albums
419+
album.ModifiedAt = modifiedAt
420+
if album.CreatedAt.After(createdAt) {
421+
album.CreatedAt = createdAt // reset created at to match filesytem for new albums
416422
}
417423

418424
if err := tx.Save(&album).Error; err != nil {

0 commit comments

Comments
 (0)