I'm looking for a way to check periodically if a files under a certain directory were changed from the last check (a functionality symilar to FAM daemon or to gio.monitor_directory). In emacs lisp.
(defun install-monitor (file secs)
(run-with-timer
0 secs
(lambda (f p)
(unless (< p (second (time-since (elt (file-attributes f) 5))))
(message "File %s changed!" f)))
file secs))
(defvar monitor-timer (install-monitor "/tmp" 5)
"Check if /tmp is changed every 5s.")
To cancel,
(cancel-timer monitor-timer)
As mentioned by mankoff, the above code snippet monitors file modification in the last 5 seconds, instead of since last check. To achieve the latter, we will need to save the attributes each time we do the checking. Hope this works:
(defvar monitor-attributes nil
"Cached file attributes to be monitored.")
(defun install-monitor (file secs)
(run-with-timer
0 secs
(lambda (f p)
(let ((att (file-attributes f)))
(unless (or (null monitor-attributes) (equalp monitor-attributes att))
(message "File %s changed!" f))
(setq monitor-attributes att)))
file secs))
(defvar monitor-timer (install-monitor "/tmp" 5)
"Check if /tmp is changed every 5s.")
I don't have a proper solution but maybe a couple of pointer to get you on the right direction.
According to some quick googling it seems that dbus has an inotify interface built-ins. Since latest version of emacs you can access to dbus interface via Emacs lisp (under Linux at least) maybe you can plug all of this together to make it works. See an example here about using dbus with Emacs :
http://emacs-fu.blogspot.com/2009/01/using-d-bus-example.html
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With