1
0

Decided to implement an inotify watcher for a playlist file. Dunno if it works correctly, but it seems like, not tested yet.

This commit is contained in:
Alexander Andreev 2023-10-01 06:44:36 +04:00
parent 4faf2a0309
commit dc3658d6de
Signed by: Arav
GPG Key ID: D22A817D95815393

View File

@ -1,9 +1,12 @@
package radio
import (
"dwelling-radio/pkg/watcher"
"log"
"os"
"strings"
"sync"
"syscall"
)
// Playlist holds a list of paths to a song files.
@ -13,6 +16,8 @@ type Playlist struct {
playlist []string
cur int
repeat bool
watcher *watcher.InotifyWatcher
changed chan uint32
}
// NewPlaylist returns an instance of a Playlist struct with a loaded playlist.
@ -58,3 +63,34 @@ func (p *Playlist) load() error {
func (p *Playlist) Reload() error {
return p.load()
}
func (p *Playlist) Watch() (err error) {
p.watcher, err = watcher.NewInotifyWatcher()
if err != nil {
return err
}
err = p.watcher.AddWatch(p.filePath, watcher.ModIgnMask)
if err != nil {
return err
}
p.watcher.WatchForMask(p.changed, watcher.ModIgnMask)
go func() {
for {
mask := <-p.changed
if mask&syscall.IN_MODIFY > 0 {
if err := p.load(); err != nil {
log.Fatalln("cannot reload a changed playlist:", err)
}
} else if mask&syscall.IN_IGNORED > 0 {
p.watcher.Close()
p.Watch()
}
}
}()
return nil
}