Skip to content

Commit

Permalink
scanner: use create time if we have it
Browse files Browse the repository at this point in the history
  • Loading branch information
sentriz committed May 15, 2024
1 parent f5893ea commit 9cc3a90
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 4 deletions.
12 changes: 12 additions & 0 deletions scanner/ctime/ctime.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
//go:build !linux

package ctime

import (
"os"
"time"
)

func CreateTime(info os.FileInfo) time.Time {
return info.ModTime()
}
14 changes: 14 additions & 0 deletions scanner/ctime/ctime_linux.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
//go:build linux

package ctime

import (
"os"
"syscall"
"time"
)

func CreateTime(info os.FileInfo) time.Time {
st := info.Sys().(*syscall.Stat_t)
return time.Unix(int64(st.Ctim.Sec), int64(st.Ctim.Nsec)) //nolint:unconvert
}
9 changes: 5 additions & 4 deletions scanner/scanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (

"go.senan.xyz/gonic/db"
"go.senan.xyz/gonic/fileutil"
"go.senan.xyz/gonic/scanner/ctime"
"go.senan.xyz/gonic/tags/tagcommon"
)

Expand Down Expand Up @@ -363,7 +364,7 @@ func (s *Scanner) populateTrackAndArtists(tx *db.DB, st *State, i int, album *db
return fmt.Errorf("populate track artists: %w", err)
}

if err := populateAlbum(tx, album, trags, stat.ModTime()); err != nil {
if err := populateAlbum(tx, album, trags, stat.ModTime(), ctime.CreateTime(stat)); err != nil {
return fmt.Errorf("populate album: %w", err)
}

Expand Down Expand Up @@ -402,7 +403,7 @@ func (s *Scanner) populateTrackAndArtists(tx *db.DB, st *State, i int, album *db
return nil
}

func populateAlbum(tx *db.DB, album *db.Album, trags tagcommon.Info, modTime time.Time) error {
func populateAlbum(tx *db.DB, album *db.Album, trags tagcommon.Info, modTime, createTime time.Time) error {
albumName := tagcommon.MustAlbum(trags)
album.TagTitle = albumName
album.TagTitleUDec = decoded(albumName)
Expand All @@ -411,8 +412,8 @@ func populateAlbum(tx *db.DB, album *db.Album, trags tagcommon.Info, modTime tim
album.TagYear = trags.Year()

album.ModifiedAt = modTime
if album.CreatedAt.After(modTime) {
album.CreatedAt = modTime // reset created at to match filesytem for new albums
if album.CreatedAt.After(createTime) {
album.CreatedAt = createTime // reset created at to match filesytem for new albums
}

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

0 comments on commit 9cc3a90

Please sign in to comment.