all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
blob 283354474f201f3e41e75ee8b8f3185c868f10e2 14335 bytes (raw)
name: src/filewatch.c 	 # note: path name is non-authoritative(*)

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
 
/* 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 <http://www.gnu.org/licenses/>.  */

#include <config.h>

#ifdef HAVE_FILEWATCH
#include <setjmp.h> /* Required for lisp.h.  */
#include "lisp.h"
#include "coding.h"
#include "process.h"
#include "keyboard.h"
#include "character.h"
#include "frame.h" /* Required for termhooks.h.  */
#include "termhooks.h"

static Lisp_Object Qmodify, Qmove, Qattrib, Qdelete, Qfrom, Qto, Qall_modify;
static Lisp_Object Qunknown_aspect, Qfile_watch;
static Lisp_Object Qaccess, Qclose_write, Qclose_nowrite, Qopen, Qall;

#ifdef HAVE_INOTIFY
#include <sys/inotify.h>
#include <sys/ioctl.h>

enum { uninitialized = -100 };
/* File handle for inotify.  */
static int inotifyfd = uninitialized;

/* Assoc list of files being watched.
 Format:
 (id . (filename ((subid callback flags) (subid callbacks flags) ...))) */
static Lisp_Object watch_list;

#define CADDR(x) Fcar (Fcdr (Fcdr (x)))
#define CADR(x) Fcar (Fcdr (x))

static Lisp_Object
append2 (Lisp_Object s1, Lisp_Object s2)
{
  Lisp_Object args[2];
  args[0] = s1;
  args[1] = s2;
  return Fappend (2, args);
}

/* Returns a list with all the elements from EVENTS the `watch_list' CELL
   is interested in.  */
static Lisp_Object
match_aspects (Lisp_Object cell, Lisp_Object events)
{
  Lisp_Object ret = Qnil;
  Lisp_Object aspects = CADDR (cell);
  Lisp_Object i;
  if (EQ (aspects, Qt) || EQ (aspects, Qall))
    return events;
  else if (EQ (aspects, Qall_modify))
    aspects = list4 (Qmodify, Qmove, Qattrib, Qdelete);

  i = Fcar (aspects);
  while (!NILP (i))
    {
      Lisp_Object e = Fcar (events);
      while (!NILP (e))
        {
          if (EQ (Fcar (e), i))
            ret = Fcons (e, ret);
          events = Fcdr (events);
          e = Fcar (events);
        }
      aspects = Fcdr (aspects);
      i = Fcar (aspects);
    }
  return ret;
}

static Lisp_Object
inotifyevent_to_aspects (Lisp_Object name, struct inotify_event *ev)
{
  Lisp_Object events = Qnil;
  if (ev->mask & (IN_MODIFY|IN_CREATE) )
    events = Fcons (Fcons (Qmodify, name), events);
  if (ev->mask & IN_MOVE_SELF)
    events = Fcons (Fcons (Qmove, name), events);
  if (ev->mask & IN_MOVED_FROM)
    events = Fcons (Fcons (Qmove,
                           Fcons (Qfrom,
                                  Fcons (name,
                                         make_number (ev->cookie)))),
                    events);
  if (ev->mask & IN_MOVED_TO)
    events = Fcons (Fcons (Qmove,
                           Fcons (Qto,
                                  Fcons (name,
                                         make_number (ev->cookie)))),
                    events);
  if (ev->mask & IN_ATTRIB)
    events = Fcons (Fcons (Qattrib, name), events);
  if (ev->mask & (IN_DELETE|IN_DELETE_SELF) )
    events = Fcons (Fcons (Qdelete, name), events);
  if (ev->mask & IN_ACCESS)
    events = Fcons (Fcons (Qaccess, name), events);
  if (ev->mask & IN_CLOSE_WRITE)
    events = Fcons (Fcons (Qclose_write, name), events);
  if (ev->mask & IN_CLOSE_NOWRITE)
    events = Fcons (Fcons (Qclose_nowrite, name), events);
  if (ev->mask & IN_OPEN)
    events = Fcons (Fcons (Qopen, name), events);
  return events;
}

/* This callback is called when the FD is available FOR_READ.  The inotify
   events are read from FD and converted into input_events.  */
static void
inotify_callback (int fd, void *_, int for_read)
{
  struct input_event event;
  int to_read;
  char *buffer;
  ssize_t n;
  size_t i;

  if (!for_read)
    return;

  to_read = 0;
  if (ioctl (fd, FIONREAD,  &to_read) == -1)
    report_file_error ("Error while trying to retrieve file system events",
                       Qnil);
  buffer = xmalloc (to_read);
  n = read (fd, buffer, to_read);
  if (n < 0)
    {
      xfree (buffer);
      report_file_error ("Error while trying to read file system events",
                         Qnil);
    }

  EVENT_INIT (event);
  event.kind = FILEWATCH_EVENT;
  event.arg = Qnil;

  i = 0;
  while (i < (size_t)n)
    {
      struct inotify_event *ev = (struct inotify_event*)&buffer[i];

      Lisp_Object watch_data = Fassoc (make_number (ev->wd), watch_list);
      if (!NILP (watch_data))
        {
          Lisp_Object name, events;

          /* If a directory is watched name contains the name
             of the file that was changed.  */
          if (ev->len > 0)
            {
              size_t const len = strlen (ev->name);
              name = make_unibyte_string (ev->name, min (len, ev->len));
              name = DECODE_FILE (name);
            }
          else
            {
              name = CADR (watch_data);
            }

          events = inotifyevent_to_aspects (name, ev);

          if (!NILP (events))
            {
              Lisp_Object x = CADDR (watch_data);
              while (!NILP (x))
                {
                  Lisp_Object cell = Fcar (x);
                  Lisp_Object aspects = match_aspects (cell, events);
                  if (!NILP (aspects))
                    {
                      Lisp_Object id = list3 (Qfile_watch, make_number (ev->wd),
                                              Fcar (cell));
                      Lisp_Object event_info = list1 (list3 (id, CADR (cell),
                                                             aspects));

                      if (NILP (event.arg))
                        event.arg = event_info;
                      else
                        event.arg = append2 (event.arg, event_info);
                    }
                  x = Fcdr (x);
                }
            }

          if (ev->mask & IN_IGNORED)
            {
              /* Event was removed automatically: Drop it from data list.  */
              add_to_log ("File-watch: \"%s\" will be ignored", name, Qnil);
              watch_list = Fdelete (watch_data, watch_list);
            }
          if (ev->mask & IN_Q_OVERFLOW)
            add_to_log ("File watch: Inotify Queue Overflow!", Qnil, Qnil);
        }

      i += sizeof (*ev) + ev->len;
    }

  if (!NILP (event.arg))
    kbd_buffer_store_event (&event);

  xfree (buffer);
}

static uint32_t
symbol_to_inotifymask (Lisp_Object symb, int in_list)
{
  if (EQ (symb, Qmodify))
    return IN_MODIFY | IN_CREATE;
  else if (EQ (symb, Qmove))
    return IN_MOVE_SELF | IN_MOVE;
  else if (EQ (symb, Qattrib))
    return IN_ATTRIB;
  else if (EQ (symb, Qdelete))
    return IN_DELETE_SELF | IN_DELETE;
  else if (!in_list && EQ (symb, Qall_modify))
    return IN_MODIFY | IN_CREATE | IN_MOVE_SELF | IN_MOVE | IN_ATTRIB
      | IN_DELETE_SELF | IN_DELETE;
  else if (EQ (symb, Qaccess))
    return IN_ACCESS;
  else if (EQ (symb, Qclose_write))
    return IN_CLOSE_WRITE;
  else if (EQ (symb, Qclose_nowrite))
    return IN_CLOSE_NOWRITE;
  else if (EQ (symb, Qopen))
    return IN_OPEN;
  else if (!in_list && (EQ (symb, Qt) || EQ (symb, Qall)))
    return IN_MODIFY | IN_CREATE | IN_MOVE_SELF | IN_MOVE | IN_ATTRIB
      | IN_DELETE_SELF | IN_DELETE | IN_ACCESS | IN_CLOSE_WRITE
      | IN_CLOSE_NOWRITE | IN_OPEN;
  else
    Fsignal (Qunknown_aspect, Fcons (symb, Qnil));
}

static uint32_t
aspect_to_inotifymask (Lisp_Object aspect)
{
  if (CONSP (aspect))
    {
      Lisp_Object x = aspect;
      uint32_t mask = 0;
      while (!NILP (x))
        {
          mask |= symbol_to_inotifymask (Fcar (x), 1);
          x = Fcdr (x);
        }
      return mask;
    }
  else
    return symbol_to_inotifymask (aspect, 0);
}

static Lisp_Object
get_watch_data_by_file_name (Lisp_Object file_name)
{
  Lisp_Object cell, file_name_data;
  Lisp_Object x = watch_list;
  CHECK_STRING (file_name);

  while (!NILP (x))
    {
      cell = Fcar (x);
      x = Fcdr (x);
      file_name_data = Fcdr (cell);
      if (!NILP (file_name_data) && STRINGP (Fcar (file_name_data))
          && !NILP (Fstring_equal (Fcar (file_name_data), file_name))
          && NUMBERP (Fcar (cell)))
        return cell;
    }
  return Qnil;
}

DEFUN ("file-watch", Ffile_watch, Sfile_watch, 3, 3, 0,
       doc: /* Arrange to call FUNC if ASPECT of FILENAME changes.

ASPECT might be one of the following symbols or a list of those symbols (except
for all-modify and t):

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-modify -- notify for all of the above (all modifying changes).

access -- notify when file was accessed/read.

close-write -- notify when a file opened for writing was closed.

close-nowrite -- notify when a file not opened for writing was closed.

open -- notify when a file was opened.

t -- 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.

Example:
(file-watch "foo" t #'(lambda (id event) (message "%s %s" id event)))

Use `file-unwatch' to stop watching.

usage: (file-watch FILENAME ASPECT CALLBACK) */)
  (Lisp_Object file_name, Lisp_Object aspect, Lisp_Object callback)
{
  static int intern_counter = 0; /* assign local ids */
  uint32_t mask;
  int watchdesc;
  Lisp_Object decoded_file_name;
  Lisp_Object data;
  Lisp_Object info;

  CHECK_STRING (file_name);

  if (inotifyfd == uninitialized)
    {
      inotifyfd = inotify_init1 (IN_NONBLOCK|IN_CLOEXEC);
      if (inotifyfd == -1)
        {
          inotifyfd = uninitialized;
          report_file_error ("File watching feature (inotify) is not available",
                             Qnil);
        }
      watch_list = Qnil;
      add_read_fd (inotifyfd, &inotify_callback, &watch_list);
    }

  mask = aspect_to_inotifymask (aspect);
  data = get_watch_data_by_file_name (file_name);
  if (!NILP (data))
    mask |= IN_MASK_ADD;

  decoded_file_name = ENCODE_FILE (file_name);
  watchdesc = inotify_add_watch (inotifyfd, SSDATA (decoded_file_name), mask);
  if (watchdesc == -1)
    report_file_error ("Could not watch file", Fcons (file_name, Qnil));

  info = list3 (make_number (intern_counter), callback, aspect);
  ++intern_counter;

  if (!NILP (data))
    Fsetcdr (Fcdr (data), Fcons (append2 (CADDR (data), Fcons (info, Qnil)),
                                 Qnil));
  else
    watch_list = Fcons (list3 (make_number (watchdesc),
                               file_name, Fcons (info, Qnil)),
                        watch_list);

  return list3 (Qfile_watch, make_number (watchdesc), Fcar (info));
}

static int
file_watch_objectp (Lisp_Object obj)
{
  return CONSP (obj) && XINT (Flength (obj)) == 3
    && EQ (Fcar (obj), Qfile_watch);
}

DEFUN ("file-unwatch", Ffile_unwatch, Sfile_unwatch, 1, 1, 0,
       doc: /* Stop watching a file or directory.  */)
  (Lisp_Object watch_object)
{
  Lisp_Object watch_data;
  Lisp_Object info;

  if (!file_watch_objectp (watch_object))
    wrong_type_argument (Qfile_watch, watch_object);

  if (inotifyfd == uninitialized)
    return Qnil;

  watch_data = Fassoc ( CADR (watch_object), watch_list );
  if (NILP (watch_data))
    return Qnil;

  info = Fassoc (CADDR (watch_object), CADDR (watch_data));
  if (NILP (info))
    return Qnil;

  Fsetcdr (Fcdr (watch_data), Fcons (Fdelete (info, CADDR (watch_data)), Qnil));

  if (NILP (CADDR (watch_data)))
    {
      int const magicno = XINT (CADR (watch_object));
      watch_list = Fdelete (watch_data, watch_list);

      if (inotify_rm_watch (inotifyfd, magicno) == -1)
        report_file_error ("Could not unwatch file",
                           Fcons (Fcar (watch_data), Qnil));

      /* Cleanup if watch_list is empty.  */
      if (NILP (watch_list))
        {
          close (inotifyfd);
          delete_read_fd (inotifyfd);
          inotifyfd = uninitialized;
        }
    }
  else
    {
      Lisp_Object decoded_file_name = ENCODE_FILE (CADR (watch_data));
      Lisp_Object x = CADDR (watch_data);
      uint32_t mask = 0;
      while (!NILP (x))
        {
          Lisp_Object cell = Fcar (x);
          mask |= aspect_to_inotifymask (CADDR (cell));
          x = Fcdr (x);
        }
      /* Reset watch mask */
      if (inotify_add_watch (inotifyfd, SSDATA (decoded_file_name), mask) == -1)
        report_file_error ("Could not unwatch file",
                           Fcons (Fcar (watch_data), Qnil));
    }

  return Qt;
}

#else /* HAVE_INOTIFY */
#error "Filewatch defined but no watch mechanism (inotify) available"
#endif /* HAVE_INOTIFY */

void
syms_of_filewatch (void)
{
  DEFSYM (Qmodify, "modify");
  DEFSYM (Qmove, "move");
  DEFSYM (Qattrib, "attrib");
  DEFSYM (Qdelete, "delete");
  DEFSYM (Qfrom, "from");
  DEFSYM (Qto, "to");
  DEFSYM (Qall_modify, "all-modify");

  DEFSYM (Qaccess, "access");
  DEFSYM (Qclose_write, "close-write");
  DEFSYM (Qclose_nowrite, "close-nowrite");
  DEFSYM (Qopen, "open");
  DEFSYM (Qall, "all");

  DEFSYM (Qunknown_aspect, "unknown-aspect");
  DEFSYM (Qfile_watch, "file-watch");

  Fput (Qunknown_aspect, Qerror_conditions,
        list2 (Qunknown_aspect, Qerror));
  Fput (Qunknown_aspect, Qerror_message,
        make_pure_c_string ("Unknown Aspect"));

  defsubr (&Sfile_watch);
  defsubr (&Sfile_unwatch);

  staticpro (&watch_list);

  Fprovide (intern_c_string ("filewatch"), Qnil);
}

#endif /* HAVE_FILEWATCH */

debug log:

solving 2833544 ...
found 2833544 in https://yhetil.org/emacs/201106240250.01247.ruediger@c-plusplus.de/

applying [1/1] https://yhetil.org/emacs/201106240250.01247.ruediger@c-plusplus.de/
diff --git a/src/filewatch.c b/src/filewatch.c
new file mode 100644
index 0000000..2833544

Checking patch src/filewatch.c...
Applied patch src/filewatch.c cleanly.

index at:
100644 283354474f201f3e41e75ee8b8f3185c868f10e2	src/filewatch.c

(*) Git path names are given by the tree(s) the blob belongs to.
    Blobs themselves have no identifier aside from the hash of its contents.^

Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.