/* Watching file system changes.
Copyright (C) 2011
Free Software Foundation, Inc.
This file is part of GNU Emacs.
GNU Emacs is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
GNU Emacs is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with GNU Emacs. If not, see . */
#include
#ifdef HAVE_FILEWATCH
#include
#include "lisp.h"
#include "process.h"
static Lisp_Object QCmodify, QCmove, QCattrib, QCdelete, QCfrom, QCto, QCall;
#ifdef HAVE_INOTIFY
#include
#include
enum { uninitialized = -100 };
static int inotifyfd = uninitialized;
// Assoc list of files being watched.
static Lisp_Object data;
static void
inotify_callback(int fd, void *_, int for_read) {
eassert(for_read);
eassert(data);
int to_read = 0;
if(ioctl(fd, FIONREAD, &to_read) == -1)
report_file_error("ioctl(2) on inotify", Qnil);
char *const buffer = xmalloc(to_read);
ssize_t const n = read(fd, buffer, to_read);
if(n < 0)
report_file_error("read from inotify", Qnil);
else if(n < to_read)
{
// TODO
message1("n < to_read");
}
size_t i = 0;
while(i < (size_t)n)
{
struct inotify_event *ev = (struct inotify_event*)&buffer[i];
Lisp_Object callback = Fassoc(make_number(ev->wd), data);
if(!NILP(callback))
{
Lisp_Object call[3];
call[0] = Fcar(Fcdr(Fcdr(callback))); /* callback */
call[1] = Fcar(Fcdr(callback)); /* path */
/* TODO check how to handle UTF-8 in file names:
make_string_from_bytes NCHARS vs. NBYTES */
/* ev->len seems to be an arbitrary large number
and not the exact length of ev->name */
size_t const len = strlen(ev->name);
/* if a directory is watched name contains the name
of the file that was changed */
Lisp_Object name = make_string_from_bytes (ev->name, len, len);
Lisp_Object events = Qnil;
if(ev->mask & (IN_MODIFY|IN_CREATE) )
events = Fcons(Fcons(QCmodify, name), events);
if(ev->mask & IN_MOVE_SELF)
events = Fcons(Fcons(QCmove, name), events);
if(ev->mask & IN_MOVED_FROM)
events = Fcons(Fcons(QCmove, Fcons(QCfrom, Fcons(name, make_number(ev->cookie)))), events);
if(ev->mask & IN_MOVED_TO)
events = Fcons(Fcons(QCmove, Fcons(QCto, Fcons(name, make_number(ev->cookie)))), events);
if(ev->mask & IN_ATTRIB)
events = Fcons(Fcons(QCattrib, name), events);
if(ev->mask & (IN_DELETE|IN_DELETE_SELF) )
events = Fcons(Fcons(QCdelete, name), events);
if(!NILP(events))
{
call[2] = events;
Ffuncall(3, call);
}
if(ev->mask & IN_IGNORED)
{
/* Event was removed automatically: Drop it from data list. */
message("File-watch: \"%s\" will be ignored", SSDATA(call[1]));
data = Fdelete(callback, data);
}
if(ev->mask & IN_Q_OVERFLOW)
message1("File watch: Inotify Queue Overflow!");
}
i += sizeof(*ev) + ev->len;
}
free(buffer);
}
DEFUN ("file-watch", Ffile_watch, Sfile_watch, 3, MANY, 0,
doc: /* Watch a file or directory.
file-watch watches the file or directory given in PATH. If a change occurs
CALLBACK is called with PATH as first argument and a list of changes as second
argument. FLAGS can be
:modify -- notify when a file is modified or created.
:move -- notify when a file/directory is moved.
:attrib -- notify when attributes change.
:delete -- notify when a file/directory is deleted.
:all -- notify for all of the above.
Watching a directory is not recursive. CALLBACK receives the events as a list
with each list element being a list containing information about an event. The
first element is a flag symbol. If a directory is watched the second element is
the name of the file that changed. If a file is moved from or to the directory
the second element is either :from or :to and the third element is the file
name. A fourth element contains a numeric identifier (cookie) that can be used
to identify matching move operations if a file is moved inside the directory.
Use `file-unwatch' to stop watching.
usage: (file-watch PATH CALLBACK &rest FLAGS) */)
(size_t nargs, Lisp_Object *args)
{
if(nargs < 3)
return Qnil;
CHECK_STRING(args[0]);
if(inotifyfd == uninitialized)
{
inotifyfd = inotify_init1(IN_NONBLOCK|IN_CLOEXEC);
if(inotifyfd == -1)
report_file_error("Initializing file watching", Qnil);
data = Qnil;
add_read_fd(inotifyfd, &inotify_callback, &data);
}
uint32_t mask = 0;
int i;
for(i = 2; i < nargs; ++i)
{
if(EQ(args[i], QCmodify))
mask |= IN_MODIFY | IN_CREATE;
else if(EQ(args[i], QCmove))
mask |= IN_MOVE_SELF | IN_MOVE;
else if(EQ(args[i], QCattrib))
mask |= IN_ATTRIB;
else if(EQ(args[i], QCdelete))
mask |= IN_DELETE_SELF | IN_DELETE;
else if(EQ(args[i], QCall))
mask |= IN_MODIFY | IN_CREATE | IN_MOVE_SELF | IN_MOVE | IN_ATTRIB
| IN_DELETE_SELF | IN_DELETE;
else /* TODO: should this be an error? */
message("Unkown parameter %s (ignored)", SSDATA(Fprin1_to_string(args[i], Qnil)));
}
int watchdesc = inotify_add_watch(inotifyfd, SSDATA(args[0]), mask);
if(watchdesc == -1) {
report_file_error("Watching file", Qnil);
}
data = Fcons(Fcons(make_number(watchdesc), Flist(2, args)), data);
return Qt;
}
DEFUN ("file-unwatch", Ffile_unwatch, Sfile_unwatch, 1, 1, 0,
doc: /* Stop watching a file or directory. */)
(Lisp_Object path)
{
if(inotifyfd == uninitialized)
return Qnil;
CHECK_STRING(path);
Lisp_Object x = data;
Lisp_Object cell, path_data;
while(!NILP(x))
{
cell = Fcar(x);
x = Fcdr(x);
path_data = Fcdr(cell);
if(!NILP(path_data) && STRINGP(Fcar(path_data))
&& Fstring_equal(Fcar(path_data), path)
&& NUMBERP(Fcar(cell)))
{
int const magicno = XINT(Fcar(cell));
data = Fdelete(cell, data);
if(inotify_rm_watch(inotifyfd, magicno) == -1)
report_file_error("Unwatch path", Qnil);
return Qt;
}
}
return Qnil;
}
#else /* HAVE_INOTIFY */
#error "Filewatch defined but no watch mechanism (inotify) available"
#endif /* HAVE_INOTIFY */
void
syms_of_filewatch (void)
{
QCmodify = intern_c_string (":modify");
staticpro (&QCmodify);
QCmove = intern_c_string (":move");
staticpro (&QCmove);
QCattrib = intern_c_string (":attrib");
staticpro (&QCattrib);
QCdelete = intern_c_string (":delete");
staticpro (&QCdelete);
QCfrom = intern_c_string (":from");
staticpro (&QCfrom);
QCto = intern_c_string (":to");
staticpro (&QCto);
QCall = intern_c_string (":all");
staticpro (&QCall);
defsubr (&Sfile_watch);
defsubr (&Sfile_unwatch);
}
#endif /* HAVE_FILEWATCH */