Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

new file creation return a folder info #73

Open
codenoid opened this issue May 10, 2019 · 14 comments
Open

new file creation return a folder info #73

codenoid opened this issue May 10, 2019 · 14 comments

Comments

@codenoid
Copy link

  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 !

@Akumzy
Copy link

Akumzy commented May 10, 2019

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)
	}
}

@codenoid
Copy link
Author

@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
Copy link
Author

# 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 codenoid changed the title watch new file while running new file creation return a folder info May 10, 2019
@Akumzy
Copy link

Akumzy commented May 10, 2019

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
Copy link

Akumzy commented May 10, 2019

So I used this to filter it out

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

@codenoid
Copy link
Author

codenoid commented May 10, 2019

@Akumzy thank you

@codenoid
Copy link
Author

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 codenoid reopened this May 10, 2019
@Akumzy
Copy link

Akumzy commented May 14, 2019

@codenoid
Create a demo

@liamsorsby
Copy link

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

@Akumzy
Copy link

Akumzy commented May 17, 2019

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

@Akumzy
Copy link

Akumzy commented May 17, 2019

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

@liamsorsby
Copy link

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

@FogDong
Copy link

FogDong commented Sep 24, 2020

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

Did anyone solve it? /cc @Akumzy

@Akumzy
Copy link

Akumzy commented Oct 1, 2020

@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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants