|(require file-watchers)||package: file-watchers|
Use file-watchers to audit and react to file activity in a system.
Use the following Racket definitions with your chosen directories.
(require file-watchers) (define watcher (watch-directories '("/path/to/dir")))
By default, lists describing file activity from the watched directory will appear via displayln.
(watch-directories [ directories on-activity on-status thread-maker]) → void?
directories : (listof directory-exists?) = (list (current-directory)) on-activity : (-> list? any) = displayln on-status : (-> list? any) = displayln
thread-maker : (-> path? thread?) = (suggest-approach #:apathetic #f)
If file change events are not supported on the operating system or if file-level monitoring is unavailable, then robust-watch is returned.
All file monitoring occurs in at least one thread. Activity and status information are each conveyed on a dedicated asynchronous channel. For more, see Buffered Asynchronous Channels.
Each channel message is a list that starts with a symbol for the associated file monitoring method, followed by a symbol indicating the kind of activity or status reported. For example, an apathetic-watch will convey that it is watching a directory and a change was detected somewhere inside it.
'(apathetic watching /path/to/dir)
file-activity-channel : (parameter/c async-channel?)
file-watcher-status-channel : (parameter/c async-channel?)
An apathetic thread reports a (list 'apathetic 'watching path) status on file-watcher-status-channel each time it starts waiting for a change. There are no other status messages and the thread will terminate when it can no longer access the directory located at the given path.
The below example starts an apathetic watch thread, waits for the thread to report that it is watching "dir", then deletes "dir". The apathetic watcher thread will report that the change occurred on file-activity-channel before terminating, since "dir" was the root path for the watching thread.
(define apathetic-watcher (apathetic-watch "dir")) (sync/enable-break (file-watcher-status-channel)) (delete-directory "dir") (displayln (sync/enable-break (file-activity-channel))) (thread-wait apathetic-watcher) (displayln (thread-dead? apathetic-watcher))
Furthermore, robust-watch only detects changes in file permissions and access time.
If a link file is accessed in a way that impacts the link’s target, both the link file and the target file will be marked as changed.
Status information appears on file-watcher-status-channel under the following rules:
(list 'intensive 'new-thread detected-path) appears when a new thread is created to monitor a created file.
(list 'intensive 'thread-done path) appears when a thread dies, meaning it is no longer monitoring the given path.
Activity information appears on file-activity-channel under the following rules: