1 /* Inotify support for Emacs
4 Free Software Foundation, Inc.
6 This file is part of GNU Emacs.
8 GNU Emacs is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
13 GNU Emacs is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
29 #include "character.h"
30 #include "frame.h" /* Required for termhooks.h. */
31 #include "termhooks.h"
33 static Lisp_Object Qaccess
; /* IN_ACCESS */
34 static Lisp_Object Qattrib
; /* IN_ATTRIB */
35 static Lisp_Object Qclose_write
; /* IN_CLOSE_WRITE */
36 static Lisp_Object Qclose_nowrite
; /* IN_CLOSE_NOWRITE */
37 static Lisp_Object Qcreate
; /* IN_CREATE */
38 static Lisp_Object Qdelete
; /* IN_DELETE */
39 static Lisp_Object Qdelete_self
; /* IN_DELETE_SELF */
40 static Lisp_Object Qmodify
; /* IN_MODIFY */
41 static Lisp_Object Qmove_self
; /* IN_MOVE_SELF */
42 static Lisp_Object Qmoved_from
; /* IN_MOVED_FROM */
43 static Lisp_Object Qmoved_to
; /* IN_MOVED_TO */
44 static Lisp_Object Qopen
; /* IN_OPEN */
46 static Lisp_Object Qall_events
; /* IN_ALL_EVENTS */
47 static Lisp_Object Qmove
; /* IN_MOVE */
48 static Lisp_Object Qclose
; /* IN_CLOSE */
50 static Lisp_Object Qdont_follow
; /* IN_DONT_FOLLOW */
51 static Lisp_Object Qexcl_unlink
; /* IN_EXCL_UNLINK */
52 static Lisp_Object Qmask_add
; /* IN_MASK_ADD */
53 static Lisp_Object Qoneshot
; /* IN_ONESHOT */
54 static Lisp_Object Qonlydir
; /* IN_ONLYDIR */
56 static Lisp_Object Qignored
; /* IN_IGNORED */
57 static Lisp_Object Qisdir
; /* IN_ISDIR */
58 static Lisp_Object Qq_overflow
; /* IN_Q_OVERFLOW */
59 static Lisp_Object Qunmount
; /* IN_UNMOUNT */
61 #include <sys/inotify.h>
62 #include <sys/ioctl.h>
64 /* Ignore bits that might be undefined on old GNU/Linux systems. */
65 #ifndef IN_EXCL_UNLINK
66 # define IN_EXCL_UNLINK 0
68 #ifndef IN_DONT_FOLLOW
69 # define IN_DONT_FOLLOW 0
75 enum { uninitialized
= -100 };
76 /* File handle for inotify. */
77 static int inotifyfd
= uninitialized
;
79 /* Assoc list of files being watched.
81 (watch-descriptor . callback)
83 static Lisp_Object watch_list
;
86 make_watch_descriptor (int wd
)
88 /* TODO replace this with a Misc Object! */
89 return make_number (wd
);
93 mask_to_aspects (uint32_t mask
) {
94 Lisp_Object aspects
= Qnil
;
96 aspects
= Fcons (Qaccess
, aspects
);
98 aspects
= Fcons (Qattrib
, aspects
);
99 if (mask
& IN_CLOSE_WRITE
)
100 aspects
= Fcons (Qclose_write
, aspects
);
101 if (mask
& IN_CLOSE_NOWRITE
)
102 aspects
= Fcons (Qclose_nowrite
, aspects
);
103 if (mask
& IN_CREATE
)
104 aspects
= Fcons (Qcreate
, aspects
);
105 if (mask
& IN_DELETE
)
106 aspects
= Fcons (Qdelete
, aspects
);
107 if (mask
& IN_DELETE_SELF
)
108 aspects
= Fcons (Qdelete_self
, aspects
);
109 if (mask
& IN_MODIFY
)
110 aspects
= Fcons (Qmodify
, aspects
);
111 if (mask
& IN_MOVE_SELF
)
112 aspects
= Fcons (Qmove_self
, aspects
);
113 if (mask
& IN_MOVED_FROM
)
114 aspects
= Fcons (Qmoved_from
, aspects
);
115 if (mask
& IN_MOVED_TO
)
116 aspects
= Fcons (Qmoved_to
, aspects
);
118 aspects
= Fcons (Qopen
, aspects
);
119 if (mask
& IN_IGNORED
)
120 aspects
= Fcons (Qignored
, aspects
);
122 aspects
= Fcons (Qisdir
, aspects
);
123 if (mask
& IN_Q_OVERFLOW
)
124 aspects
= Fcons (Qq_overflow
, aspects
);
125 if (mask
& IN_UNMOUNT
)
126 aspects
= Fcons (Qunmount
, aspects
);
131 inotifyevent_to_event (Lisp_Object watch_object
, struct inotify_event
const *ev
)
133 Lisp_Object name
= Qnil
;
136 size_t const len
= strlen (ev
->name
);
137 name
= make_unibyte_string (ev
->name
, min (len
, ev
->len
));
138 name
= DECODE_FILE (name
);
141 return list2 (list4 (make_watch_descriptor (ev
->wd
),
142 mask_to_aspects (ev
->mask
),
143 make_number (ev
->cookie
),
145 XCDR (watch_object
));
148 /* This callback is called when the FD is available for read. The inotify
149 events are read from FD and converted into input_events. */
151 inotify_callback (int fd
, void *_
)
153 struct input_event event
;
154 Lisp_Object watch_object
;
161 if (ioctl (fd
, FIONREAD
, &to_read
) == -1)
162 report_file_error ("Error while trying to retrieve file system events",
164 buffer
= xmalloc (to_read
);
165 n
= read (fd
, buffer
, to_read
);
169 report_file_error ("Error while trying to read file system events",
174 event
.kind
= FILE_NOTIFY_EVENT
;
178 while (i
< (size_t)n
)
180 struct inotify_event
*ev
= (struct inotify_event
*)&buffer
[i
];
182 watch_object
= Fassoc (make_watch_descriptor (ev
->wd
), watch_list
);
183 if (!NILP (watch_object
))
185 event
.arg
= inotifyevent_to_event (watch_object
, ev
);
187 /* If event was removed automatically: Drop it from watch list. */
188 if (ev
->mask
& IN_IGNORED
)
189 watch_list
= Fdelete (watch_object
, watch_list
);
192 i
+= sizeof (*ev
) + ev
->len
;
195 if (!NILP (event
.arg
))
196 kbd_buffer_store_event (&event
);
202 symbol_to_inotifymask (Lisp_Object symb
)
204 if (EQ (symb
, Qaccess
))
206 else if (EQ (symb
, Qattrib
))
208 else if (EQ (symb
, Qclose_write
))
209 return IN_CLOSE_WRITE
;
210 else if (EQ (symb
, Qclose_nowrite
))
211 return IN_CLOSE_NOWRITE
;
212 else if (EQ (symb
, Qcreate
))
214 else if (EQ (symb
, Qdelete
))
216 else if (EQ (symb
, Qdelete_self
))
217 return IN_DELETE_SELF
;
218 else if (EQ (symb
, Qmodify
))
220 else if (EQ (symb
, Qmove_self
))
222 else if (EQ (symb
, Qmoved_from
))
223 return IN_MOVED_FROM
;
224 else if (EQ (symb
, Qmoved_to
))
226 else if (EQ (symb
, Qopen
))
228 else if (EQ (symb
, Qmove
))
230 else if (EQ (symb
, Qclose
))
233 else if (EQ (symb
, Qdont_follow
))
234 return IN_DONT_FOLLOW
;
235 else if (EQ (symb
, Qexcl_unlink
))
236 return IN_EXCL_UNLINK
;
237 else if (EQ (symb
, Qmask_add
))
239 else if (EQ (symb
, Qoneshot
))
241 else if (EQ (symb
, Qonlydir
))
244 else if (EQ (symb
, Qt
) || EQ (symb
, Qall_events
))
245 return IN_ALL_EVENTS
;
247 signal_error ("Unknown aspect", symb
);
251 aspect_to_inotifymask (Lisp_Object aspect
)
255 Lisp_Object x
= aspect
;
259 mask
|= symbol_to_inotifymask (XCAR (x
));
265 return symbol_to_inotifymask (aspect
);
268 DEFUN ("inotify-add-watch", Finotify_add_watch
, Sinotify_add_watch
, 3, 3, 0,
269 doc
: /* Add a watch for FILE-NAME to inotify.
271 A WATCH-DESCRIPTOR is returned on success. ASPECT might be one of the following
272 symbols or a list of those symbols:
291 The following symbols can also be added to a list of aspects
299 Watching a directory is not recursive. CALLBACK gets called in case of an
300 event. It gets passed a single argument EVENT which contains an event structure
303 (WATCH-DESCRIPTOR ASPECTS COOKIE NAME)
305 WATCH-DESCRIPTOR is the same object that was returned by this function. It can
306 be tested for equality using `equal'. ASPECTS describes the event. It is a
307 list of ASPECT symbols described above and can also contain one of the following
315 COOKIE is an object that can be compared using `equal' to identify two matching
316 renames (moved-from and moved-to).
318 If a directory is watched then NAME is the name of file that caused the event.
320 See inotify(7) and inotify_add_watch(2) for further information. The inotify fd
321 is managed internally and there is no corresponding inotify_init. Use
322 `inotify-rm-watch' to remove a watch.
324 (Lisp_Object file_name
, Lisp_Object aspect
, Lisp_Object callback
)
327 Lisp_Object watch_object
;
328 Lisp_Object decoded_file_name
;
329 Lisp_Object watch_descriptor
;
332 CHECK_STRING (file_name
);
334 if (inotifyfd
== uninitialized
)
336 inotifyfd
= inotify_init1 (IN_NONBLOCK
|IN_CLOEXEC
);
339 inotifyfd
= uninitialized
;
340 report_file_error ("File watching feature (inotify) is not available",
344 add_read_fd (inotifyfd
, &inotify_callback
, NULL
);
347 mask
= aspect_to_inotifymask (aspect
);
348 decoded_file_name
= ENCODE_FILE (file_name
);
349 watchdesc
= inotify_add_watch (inotifyfd
, SSDATA (decoded_file_name
), mask
);
351 report_file_error ("Could not add watch for file", Fcons (file_name
, Qnil
));
353 watch_descriptor
= make_watch_descriptor (watchdesc
);
355 /* Delete existing watch object. */
356 watch_object
= Fassoc (watch_descriptor
, watch_list
);
357 if (!NILP (watch_object
))
358 watch_list
= Fdelete (watch_object
, watch_list
);
360 /* Store watch object in watch list. */
361 watch_object
= Fcons (watch_descriptor
, callback
);
362 watch_list
= Fcons (watch_object
, watch_list
);
364 return watch_descriptor
;
367 DEFUN ("inotify-rm-watch", Finotify_rm_watch
, Sinotify_rm_watch
, 1, 1, 0,
368 doc
: /* Remove an existing WATCH-DESCRIPTOR.
370 WATCH-DESCRIPTOR should be an object returned by `inotify-add-watch'.
372 See inotify_rm_watch(2) for more information.
374 (Lisp_Object watch_descriptor
)
376 Lisp_Object watch_object
;
377 int wd
= XINT (watch_descriptor
);
379 if (inotify_rm_watch (inotifyfd
, wd
) == -1)
380 report_file_error ("Could not rm watch", Fcons (watch_descriptor
,
383 /* Remove watch descriptor from watch list. */
384 watch_object
= Fassoc (watch_descriptor
, watch_list
);
385 if (!NILP (watch_object
))
386 watch_list
= Fdelete (watch_object
, watch_list
);
388 /* Cleanup if no more files are watched. */
389 if (NILP (watch_list
))
392 delete_read_fd (inotifyfd
);
393 inotifyfd
= uninitialized
;
400 syms_of_inotify (void)
402 DEFSYM (Qaccess
, "access");
403 DEFSYM (Qattrib
, "attrib");
404 DEFSYM (Qclose_write
, "close-write");
405 DEFSYM (Qclose_nowrite
, "close-nowrite");
406 DEFSYM (Qcreate
, "create");
407 DEFSYM (Qdelete
, "delete");
408 DEFSYM (Qdelete_self
, "delete-self");
409 DEFSYM (Qmodify
, "modify");
410 DEFSYM (Qmove_self
, "move-self");
411 DEFSYM (Qmoved_from
, "moved-from");
412 DEFSYM (Qmoved_to
, "moved-to");
413 DEFSYM (Qopen
, "open");
415 DEFSYM (Qall_events
, "all-events");
416 DEFSYM (Qmove
, "move");
417 DEFSYM (Qclose
, "close");
419 DEFSYM (Qdont_follow
, "dont-follow");
420 DEFSYM (Qexcl_unlink
, "excl-unlink");
421 DEFSYM (Qmask_add
, "mask-add");
422 DEFSYM (Qoneshot
, "oneshot");
423 DEFSYM (Qonlydir
, "onlydir");
425 DEFSYM (Qignored
, "ignored");
426 DEFSYM (Qisdir
, "isdir");
427 DEFSYM (Qq_overflow
, "q-overflow");
428 DEFSYM (Qunmount
, "unmount");
430 defsubr (&Sinotify_add_watch
);
431 defsubr (&Sinotify_rm_watch
);
433 staticpro (&watch_list
);
435 Fprovide (intern_c_string ("inotify"), Qnil
);
438 #endif /* HAVE_INOTIFY */