From f40e1edc3398787b385aadde5bb7655f97884a07 Mon Sep 17 00:00:00 2001 From: William Chong Date: Sat, 1 Jun 2024 02:27:20 +0800 Subject: [PATCH] [preprocessor/folder] change fsnotify to rjeczalik/notify --- go.mod | 3 +- go.sum | 3 ++ preprocessor/folder/folder.go | 61 ++++++++++++++--------------------- 3 files changed, 29 insertions(+), 38 deletions(-) diff --git a/go.mod b/go.mod index 2c18045..75aa4ea 100644 --- a/go.mod +++ b/go.mod @@ -190,7 +190,8 @@ require ( ) require ( - github.com/fsnotify/fsnotify v1.7.0 + github.com/fsnotify/fsnotify v1.7.0 // indirect + github.com/rjeczalik/notify v0.9.3 github.com/x448/float16 v0.8.4 // indirect go.opencensus.io v0.24.0 // indirect go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.51.0 // indirect diff --git a/go.sum b/go.sum index bd78b04..2c0ab91 100644 --- a/go.sum +++ b/go.sum @@ -652,6 +652,8 @@ github.com/quic-go/webtransport-go v0.8.0 h1:HxSrwun11U+LlmwpgM1kEqIqH90IT4N8auv github.com/quic-go/webtransport-go v0.8.0/go.mod h1:N99tjprW432Ut5ONql/aUhSLT0YVSlwHohQsuac9WaM= github.com/raulk/go-watchdog v1.3.0 h1:oUmdlHxdkXRJlwfG0O9omj8ukerm8MEQavSiDTEtBsk= github.com/raulk/go-watchdog v1.3.0/go.mod h1:fIvOnLbF0b0ZwkB9YU4mOW9Did//4vPZtDqv66NfsMU= +github.com/rjeczalik/notify v0.9.3 h1:6rJAzHTGKXGj76sbRgDiDcYj/HniypXmSJo1SWakZeY= +github.com/rjeczalik/notify v0.9.3/go.mod h1:gF3zSOrafR9DQEWSE8TjfI9NkooDxbyT4UgRGKZA0lc= github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4= github.com/rogpeppe/go-internal v1.10.0 h1:TMyTOH3F/DB16zRVcYyreMH6GnZZrwQVAoYjRBZyWFQ= github.com/rogpeppe/go-internal v1.10.0/go.mod h1:UQnix2H7Ngw/k4C5ijL5+65zddjncjaFoBhdsK/akog= @@ -943,6 +945,7 @@ golang.org/x/sys v0.0.0-20180810173357-98c5dad5d1a0/go.mod h1:STP8DvDyc/dI5b8T5h golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20180926160741-c2ed4eda69e7/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20181029174526-d69651ed3497/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20181205085412-a5c9d58dba9a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= diff --git a/preprocessor/folder/folder.go b/preprocessor/folder/folder.go index 14490bf..764d0ca 100644 --- a/preprocessor/folder/folder.go +++ b/preprocessor/folder/folder.go @@ -5,7 +5,7 @@ import ( "os" "path/filepath" - "github.com/fsnotify/fsnotify" + "github.com/rjeczalik/notify" "github.com/starlinglab/integrity-v2/config" ) @@ -47,56 +47,43 @@ func Run(args []string) error { } // Init directory watcher - watcher, err := fsnotify.NewWatcher() + c := make(chan notify.EventInfo, 1) + scanRoot := config.GetConfig().FolderPreprocessor.SyncFolderRoot + err = notify.Watch(scanRoot+"/...", c, notify.Create, notify.Rename) if err != nil { return err } - defer watcher.Close() + defer notify.Stop(c) go func() { for { - select { - case event, ok := <-watcher.Events: - if !ok { - return + ei := <-c + event := ei.Event() + if event == notify.Rename || event == notify.Create { + filePath := ei.Path() + file, err := os.Open(filePath) + if err != nil { + // File may be moved away for fsnotify.Rename + continue } - if event.Has(fsnotify.Create) || event.Has(fsnotify.Rename) { - filePath := event.Name - file, err := os.Open(filePath) - if err != nil { - // File may be moved away for fsnotify.Rename - continue - } - defer file.Close() - fileInfo, err := file.Stat() + defer file.Close() + fileInfo, err := file.Stat() + if err != nil { + log.Println("error getting file info:", err) + continue + } + if shouldIncludeFile(fileInfo.Name()) { + cid, err := handleNewFile(filePath) if err != nil { - log.Println("error getting file info:", err) - continue - } - if shouldIncludeFile(fileInfo.Name()) { - cid, err := handleNewFile(filePath) - if err != nil { - log.Println(err) - } else { - log.Printf("File %s uploaded to webhook with CID %s\n", filePath, cid) - } + log.Println(err) + } else { + log.Printf("File %s uploaded to webhook with CID %s\n", filePath, cid) } } - case err, ok := <-watcher.Errors: - if !ok { - return - } - log.Println("error:", err) } } }() - scanRoot := config.GetConfig().FolderPreprocessor.SyncFolderRoot - err = watcher.Add(scanRoot) - if err != nil { - return err - } - // Block main goroutine forever. <-make(chan struct{}) return nil