watcher
watcher copied to clipboard
new file creation return a folder info
- i run watcher
- i run command to create a new file, but watcher read it as
DIRECTORY - i run point 2 command again (with same path) then watcher read it as
FILE
how do i get File info (as FILE ) for new file (like point 2), thank you !
The event struct contains all the information your need using event.FileInfo or
file := fileInfo{
Size: event.Size(),
Path: event.Path,
Name: event.Name(),
ModTime: event.ModTime(),
Mode: event.Mode(), IsDir: event.IsDir()}
package main
import (
"fmt"
"log"
"time"
"github.com/radovskyb/watcher"
)
func main() {
w := watcher.New()
// SetMaxEvents to 1 to allow at most 1 event's to be received
// on the Event channel per watching cycle.
//
// If SetMaxEvents is not set, the default is to send all events.
w.SetMaxEvents(1)
// Only notify rename and move events.
w.FilterOps(watcher.Rename, watcher.Move)
// Only files that match the regular expression during file listings
// will be watched.
r := regexp.MustCompile("^abc$")
w.AddFilterHook(watcher.RegexFilterHook(r, false))
go func() {
for {
select {
case event := <-w.Event:
// THIS ---> event
fmt.Println(event) // Print the event's info.
case err := <-w.Error:
log.Fatalln(err)
case <-w.Closed:
return
}
}
}()
// Watch this folder for changes.
if err := w.Add("."); err != nil {
log.Fatalln(err)
}
// Watch test_folder recursively for changes.
if err := w.AddRecursive("../test_folder"); err != nil {
log.Fatalln(err)
}
// Print a list of all of the files and folders currently
// being watched and their paths.
for path, f := range w.WatchedFiles() {
fmt.Printf("%s: %s\n", path, f.Name())
}
fmt.Println()
// Trigger 2 events after watcher started.
go func() {
w.Wait()
w.TriggerEvent(watcher.Create, nil)
w.TriggerEvent(watcher.Remove, nil)
}()
// Start the watching process - it'll check for changes every 100ms.
if err := w.Start(time.Millisecond * 100); err != nil {
log.Fatalln(err)
}
}
@Akumzy sir, you need to "edit" the file to get file information, if this is a first time you write a file, you just only get Folder info from Event value
# touch test/test/test/test-file.jpg (for the first time)
DIRECTORY "test" WRITE [/mnt/storage/wamon/test/test/test]
/mnt/storage/wamon/test/test/test
test
4096
drwxrwxr-x
# touch test/test/test/test-file.jpg (again)
FILE "test-file.jpg" WRITE [/mnt/storage/wamon/test/test/test/test-file.jpg]
/mnt/storage/wamon/test/test/test/test-file.jpg
test-file.jpg
0
-rw-rw-r--
Yeah is true when an event occurs at first two different events are being emitted one from the parent directory while the second one will be the real event source
So I used this to filter it out
if event.IsDir() && event.Op == watcher.Write {
continue
}
@Akumzy thank you
umm actually, i still need to "modify" the file first, then i got the file info, what i need is i get File info when in the first file creation
because currently you will get folder info in first file creation
@codenoid Create a demo
@Akumzy I've just been having a look into this library. I'm confused as to why the Create operation isn't being fired here instead of Write. If I touch or vim a file for the first time. It never fires a create event.
@liamsorsby did you set watcherInstance.SetMaxEvents(1) to one?
If so it will likely emit Write event from the source direct directory.
@Akumzy I’ve tried with and without. I’ve also tried on different OS’s as I’m aware of an issue on mac’s not working correctly with fsnotifier in node either. If I just listen on Create nothing gets logged ever.
Met the same problem: I want to get the FILE event but not DIRECTORY event.
Did anyone solve it? /cc @Akumzy
@FogDong if you're not using the CLI you can use this to filter that out
if event.IsDir() && event.Op == watcher.Write {
continue
}
but for CLI you might want to fork the repo and add this since the author hasn't been active throughout this year I hope he is fine