From d54bc80a5925c9f0a8210d17d7efc3572f23d7c8 Mon Sep 17 00:00:00 2001 From: nemunaire Date: Thu, 8 Dec 2016 09:14:42 +0100 Subject: [PATCH] backend: use fsnotify instead of the deprecated inotify --- backend/main.go | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/backend/main.go b/backend/main.go index 532d3f78..76caea84 100644 --- a/backend/main.go +++ b/backend/main.go @@ -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) } }