Failed to catch file modified event in the subtree(fsnotify)?

290 views
Skip to first unread message

angan...@gmail.com

unread,
Sep 3, 2014, 3:34:23 PM9/3/14
to golan...@googlegroups.com
Hello,

I have a monthly folder "201409", there are lots of sub-folders under it, each with one or two files in the sub-folder.
The sub-folders change rapidly everyday(delete, create). I want to monitor the file changes within the sub-folder, but can only
catch "write" event when creating files in it or rename the files. But cannot catch file content changes.

The sample program is here: http://play.golang.org/p/NMJvFovSkC

Suppose I want to monitor "C:/temp" folder. If I create a file "abc.txt" under "test" folder like:
"c:/temp/test/abc.txt". 

Then there will be "write" event when I create "abc.txt". I can also get "write" event when 
rename it to "aaa.txt". 

2014/09/03 12:16:35 event: "c:\\temp\\test": WRITE
2014/09/03 12:16:35 modified file: c:\temp\test

But there is no event when I just modify the content of "abc.txt". 

If I modify the "c:/temp/abc.txt", there will be a "write" event.

How can I get the "write" event if the content of the file "c:/temp/test/abc.txt" is changed?

Andrew



Jonathan Lawlor

unread,
Sep 3, 2014, 6:08:31 PM9/3/14
to golan...@googlegroups.com, angan...@gmail.com
From the go-fsnotify/fsnotify wiki:

When I watch a directory, are all subdirectories watched as well?

No, you must add watches for any directory you want to watch (a recursive watcher is in the works #56).

angan...@gmail.com

unread,
Sep 3, 2014, 7:30:15 PM9/3/14
to golan...@googlegroups.com, angan...@gmail.com
Thanks for the link. The recursive watcher is a very useful feature. I hope it can be used in Go 1.4.

Andrew

unread,
Sep 4, 2014, 11:41:08 AM9/4/14
to golan...@googlegroups.com, angan...@gmail.com

How about implement the Watch method should like this:
 
func (w *Watcher) Watch(path string, n int) error
if n>0,  Watch the path down to n levels
if n<=0, Watch the path all the way down

Suppose the following structure:
C:\temp\file1
C:\temp\dir1\file2
C:\temp\dir1\dir2\file3
C:\temp\dir1\dir2\dir3\file4

If Watch("C:/temp", 1), it should return create/rename/modify/delete event for the "file1" and "dir1"
If Watch("C:/temp", 2), it should return create/rename/modify/delete event for the "file1" and "dir1", also "file2" and "dir2"

The current implementation is like 1.5, which like Watch("C:/temp", 2), but lacks event for the "file2" midification.

Andrew


rjeczalik

unread,
Sep 4, 2014, 12:23:33 PM9/4/14
to Andrew, golang-nuts
On 4 September 2014 17:41, Andrew <angan...@gmail.com> wrote:
> How about implement the Watch method should like this:
>  
> func (w *Watcher) Watch(path string, n int) error

Current API is not yet feature-complete not well-designed in my opinion. Couple of questions that came to my mind while reading its godoc page:

- what is more performant, having multiple Watchers watching single path, of single Watcher watching multiple paths?
- why it tries to group watched paths at the API level?
- a Watcher is also a container, why? also, where's Get method? (especially when Get/Add/Remove does not feel very Go'ish)

I'd love if the actual API of the library that eventually hits std would have only two methods, Notify and Stop and be channel-based, exactly how os/signal is modeled.

I ended up writing my own wrapper for fsnotify in order to use it. The os/signal API that I'm talking about could look like the following:


(except the Notifier interface, this is for swapping implementations)
Reply all
Reply to author
Forward
0 new messages