Tuesday, May 5, 2015

File Monitor

Factor has a cross-platform file-system change monitor which supports detecting changes to file names, attributes and contents under a specified directory.

There is some minor platform differences between Mac OS X, Windows, and Linux which may be worth looking at if you are building on top of the io.monitors vocabulary. I was curious about what kind of events are generated for various test-cases and built a small utility to experiment with it.

Some code to monitor for changed paths recursively in a directory and print each one out:

: watch-loop ( monitor -- )
    dup next-change path>> print flush watch-loop ;

: watch-directory ( path -- )
    [ t [ watch-loop ] with-monitor ] with-monitors ;

I've committed this as the file-monitor tool (with support for an optional command-line argument to specify which directory to monitor as well as printing the change descriptors). You can run it very simply:

factor -run=file-monitor [path]

An example session on Linux, monitoring some simple changes to files in /tmp:

$ factor -run=file-monitor /tmp &
Monitoring /tmp

$ touch /tmp/a
{ +add-file+ } /tmp/a
{ +add-file+ } /tmp/a
{ +modify-file+ } /tmp/a

$ echo "test" > /tmp/a
{ +modify-file+ } /tmp/a

$ rm /tmp/a
{ +remove-file+ } /tmp/a

No comments: