watcher icon indicating copy to clipboard operation
watcher copied to clipboard

new file creation return a folder info

Open codenoid opened this issue 6 years ago • 14 comments

  1. i run watcher
  2. i run command to create a new file, but watcher read it as DIRECTORY
  3. 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 !

codenoid avatar May 10 '19 14:05 codenoid

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 avatar May 10 '19 14:05 Akumzy

@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

codenoid avatar May 10 '19 14:05 codenoid

# 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--                                                                        

codenoid avatar May 10 '19 14:05 codenoid

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

Akumzy avatar May 10 '19 15:05 Akumzy

So I used this to filter it out

if event.IsDir() && event.Op == watcher.Write {
	continue
}

Akumzy avatar May 10 '19 15:05 Akumzy

@Akumzy thank you

codenoid avatar May 10 '19 15:05 codenoid

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 avatar May 10 '19 15:05 codenoid

@codenoid Create a demo

Akumzy avatar May 14 '19 11:05 Akumzy

@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 avatar May 17 '19 13:05 liamsorsby

@liamsorsby did you set watcherInstance.SetMaxEvents(1) to one?

Akumzy avatar May 17 '19 13:05 Akumzy

If so it will likely emit Write event from the source direct directory.

Akumzy avatar May 17 '19 14:05 Akumzy

@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.

liamsorsby avatar May 17 '19 18:05 liamsorsby

Met the same problem: I want to get the FILE event but not DIRECTORY event.

Did anyone solve it? /cc @Akumzy

FogDong avatar Sep 24 '20 07:09 FogDong

@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

Akumzy avatar Oct 01 '20 19:10 Akumzy