backend: use fsnotify instead of the deprecated inotify

This commit is contained in:
nemunaire 2016-12-08 09:14:42 +01:00
parent 173dafa69e
commit d54bc80a59

View File

@ -11,16 +11,16 @@ import (
"strings"
"time"
"golang.org/x/exp/inotify"
"gopkg.in/fsnotify.v1"
"srs.epita.fr/fic-server/libfic"
)
var TeamsDir string
var SubmissionDir string
func watchsubdir(watcher *inotify.Watcher, pathname string) error {
func watchsubdir(watcher *fsnotify.Watcher, pathname string) error {
log.Println("Watch new directory:", pathname)
if err := watcher.AddWatch(pathname, inotify.IN_CLOSE_WRITE|inotify.IN_CREATE); err != nil {
if err := watcher.Add(pathname); err != nil {
return err
}
@ -70,10 +70,11 @@ func main() {
defer fic.DBClose()
log.Println("Registering directory events...")
watcher, err := inotify.NewWatcher()
watcher, err := fsnotify.NewWatcher()
if err != nil {
log.Fatal(err)
}
defer watcher.Close()
if err := watchsubdir(watcher, SubmissionDir); err != nil {
log.Fatal(err)
@ -86,18 +87,18 @@ func main() {
for {
select {
case ev := <-watcher.Event:
if ev.Mask&inotify.IN_CREATE == inotify.IN_CREATE {
case ev := <-watcher.Events:
if ev.Op & fsnotify.Create == fsnotify.Create {
// Register new subdirectory
if d, err := os.Stat(ev.Name); err == nil && d.IsDir() {
if err := watchsubdir(watcher, ev.Name); err != nil {
log.Println(err)
}
}
} else if ev.Mask&inotify.IN_CLOSE_WRITE == inotify.IN_CLOSE_WRITE {
} else if ev.Op & fsnotify.Write == fsnotify.Write {
go treat(ev.Name)
}
case err := <-watcher.Error:
case err := <-watcher.Errors:
log.Println("error:", err)
}
}