gfilenotify.c 10.6 KB
Newer Older
1
/* Filesystem notifications support with glib API.
Paul Eggert's avatar
Paul Eggert committed
2
   Copyright (C) 2013-2019 Free Software Foundation, Inc.
3 4 5 6 7

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
8 9
the Free Software Foundation, either version 3 of the License, or (at
your option) any later version.
10 11 12 13 14 15 16

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
17
along with GNU Emacs.  If not, see <https://www.gnu.org/licenses/>.  */
18 19 20 21 22 23 24 25 26 27 28

#include <config.h>

#include <stdio.h>
#include <gio/gio.h>
#include "lisp.h"
#include "coding.h"
#include "termhooks.h"
#include "keyboard.h"


Michael Albinus's avatar
Michael Albinus committed
29
/* This is a list, elements are quadruples (DESCRIPTOR FILE FLAGS CALLBACK)  */
30 31 32 33 34 35
static Lisp_Object watch_list;

/* This is the callback function for arriving signals from
   g_file_monitor.  It shall create a Lisp event, and put it into
   Emacs input queue.  */
static gboolean
36 37 38
dir_monitor_callback (GFileMonitor *monitor,
		      GFile *file,
		      GFile *other_file,
39 40 41
		      GFileMonitorEvent event_type,
		      gpointer user_data)
{
42
  Lisp_Object symbol, monitor_object, watch_object, flags;
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
  char *name = g_file_get_parse_name (file);
  char *oname = other_file ? g_file_get_parse_name (other_file) : NULL;

  /* Determine event symbol.  */
  switch (event_type)
    {
    case G_FILE_MONITOR_EVENT_CHANGED:
      symbol = Qchanged;
      break;
    case G_FILE_MONITOR_EVENT_CHANGES_DONE_HINT:
      symbol = Qchanges_done_hint;
      break;
    case G_FILE_MONITOR_EVENT_DELETED:
      symbol = Qdeleted;
      break;
    case G_FILE_MONITOR_EVENT_CREATED:
      symbol = Qcreated;
      break;
    case G_FILE_MONITOR_EVENT_ATTRIBUTE_CHANGED:
      symbol = Qattribute_changed;
      break;
    case G_FILE_MONITOR_EVENT_PRE_UNMOUNT:
      symbol = Qpre_unmount;
      break;
    case G_FILE_MONITOR_EVENT_UNMOUNTED:
      symbol = Qunmounted;
      break;
    case G_FILE_MONITOR_EVENT_MOVED:
      symbol = Qmoved;
      break;
    default:
      goto cleanup;
    }

  /* Determine callback function.  */
78
  monitor_object = make_pointer_integer (monitor);
79
  watch_object = assq_no_quit (monitor_object, watch_list);
80

81
  if (CONSP (watch_object))
82 83
    {
      struct input_event event;
84
      Lisp_Object otail = oname ? list1 (build_string (oname)) : Qnil;
85 86 87

      /* Check, whether event_type is expected.  */
      flags = XCAR (XCDR (XCDR (watch_object)));
88 89 90 91 92
      if ((!NILP (Fmember (Qchange, flags))
	   && !NILP (Fmember (symbol, list5 (Qchanged, Qchanges_done_hint,
					     Qdeleted, Qcreated, Qmoved))))
	  || (!NILP (Fmember (Qattribute_change, flags))
	      && EQ (symbol, Qattribute_changed)))
93 94 95 96 97 98 99 100 101 102 103 104 105
	{
	  /* Construct an event.  */
	  EVENT_INIT (event);
	  event.kind = FILE_NOTIFY_EVENT;
	  event.frame_or_window = Qnil;
	  event.arg = list2 (Fcons (monitor_object,
				    Fcons (symbol,
					   Fcons (build_string (name),
						  otail))),
			     XCAR (XCDR (XCDR (XCDR (watch_object)))));

	  /* Store it into the input event queue.  */
	  kbd_buffer_store_event (&event);
106
	  /* XD_DEBUG_MESSAGE ("%s", XD_OBJECT_TO_STRING (event.arg));  */
107
	}
Michael Albinus's avatar
Michael Albinus committed
108 109

      /* Cancel monitor if file or directory is deleted.  */
110 111 112
      if (!NILP (Fmember (symbol, list2 (Qdeleted, Qmoved)))
	  && strcmp (name, SSDATA (XCAR (XCDR (watch_object)))) == 0
	  && !g_file_monitor_is_cancelled (monitor))
Michael Albinus's avatar
Michael Albinus committed
113
	g_file_monitor_cancel (monitor);
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
    }

  /* Cleanup.  */
 cleanup:
  g_free (name);
  g_free (oname);

  return TRUE;
}

DEFUN ("gfile-add-watch", Fgfile_add_watch, Sgfile_add_watch, 3, 3, 0,
       doc: /* Add a watch for filesystem events pertaining to FILE.

This arranges for filesystem events pertaining to FILE to be reported
to Emacs.  Use `gfile-rm-watch' to cancel the watch.

Value is a descriptor for the added watch.  If the file cannot be
131
watched for some reason, this function signals a `file-notify-error' error.
132 133 134 135

FLAGS is a list of conditions to set what will be watched for.  It can
include the following symbols:

136 137 138 139 140 141 142
  `change'           -- watch for file changes
  `attribute-change' -- watch for file attributes changes, like
                        permissions or modification time
  `watch-mounts'     -- watch for mount events
  `send-moved'       -- pair `deleted' and `created' events caused by
                        file renames and send a single `renamed' event
                        instead
143 144 145 146 147 148 149 150 151 152

When any event happens, Emacs will call the CALLBACK function passing
it a single argument EVENT, which is of the form

  (DESCRIPTOR ACTION FILE [FILE1])

DESCRIPTOR is the same object as the one returned by this function.
ACTION is the description of the event.  It could be any one of the
following:

153 154
  `changed'           -- FILE has changed
  `changes-done-hint' -- a hint that this was probably the last change
155
                         in a set of changes
156 157 158 159 160 161
  `deleted'           -- FILE was deleted
  `created'           -- FILE was created
  `attribute-changed' -- a FILE attribute was changed
  `pre-unmount'       -- the FILE location will soon be unmounted
  `unmounted'         -- the FILE location was unmounted
  `moved'             -- FILE was moved to FILE1
162 163

FILE is the name of the file whose event is being reported.  FILE1
164
will be reported only in case of the `moved' event.  */)
165 166
  (Lisp_Object file, Lisp_Object flags, Lisp_Object callback)
{
167
  Lisp_Object watch_object;
168
  GFile *gfile;
169
  GFileMonitor *monitor;
170
  GFileMonitorFlags gflags = G_FILE_MONITOR_NONE;
171
  GError *gerror = NULL;
172 173 174 175 176

  /* Check parameters.  */
  CHECK_STRING (file);
  file = Fdirectory_file_name (Fexpand_file_name (file, Qnil));
  if (NILP (Ffile_exists_p (file)))
177
    report_file_error ("File does not exist", file);
178 179 180 181 182 183 184 185 186 187

  if (!FUNCTIONP (callback))
    wrong_type_argument (Qinvalid_function, callback);

  /* Assemble flags.  */
  if (!NILP (Fmember (Qwatch_mounts, flags)))
    gflags |= G_FILE_MONITOR_WATCH_MOUNTS;
  if (!NILP (Fmember (Qsend_moved, flags)))
    gflags |= G_FILE_MONITOR_SEND_MOVED;

Paul Eggert's avatar
Paul Eggert committed
188 189 190
  /* Create GFile name.  */
  gfile = g_file_new_for_path (SSDATA (ENCODE_FILE (file)));

191
  /* Enable watch.  */
192
  monitor = g_file_monitor (gfile, gflags, NULL, &gerror);
193
  g_object_unref (gfile);
194 195 196 197 198 199 200
  if (gerror)
    {
      char msg[1024];
      strcpy (msg, gerror->message);
      g_error_free (gerror);
      xsignal1 (Qfile_notify_error, build_string (msg));
    }
201
  if (! monitor)
202
    xsignal2 (Qfile_notify_error, build_string ("Cannot watch file"), file);
203

204
  Lisp_Object watch_descriptor = make_pointer_integer_unsafe (monitor);
205

206 207
  if (! (FIXNUMP (watch_descriptor)
	 && XFIXNUMPTR (watch_descriptor) == monitor))
208 209
    {
      g_object_unref (monitor);
210 211
      xsignal2 (Qfile_notify_error, build_string ("Unsupported file watcher"),
		file);
212 213
    }

214 215 216 217
  /* The default rate limit is 800 msec.  We adapt this.  */
  g_file_monitor_set_rate_limit (monitor, 100);

  /* Subscribe to the "changed" signal.  */
218 219
  g_signal_connect (monitor, "changed",
		    (GCallback) dir_monitor_callback, NULL);
220 221

  /* Store watch object in watch list.  */
222
  watch_object = list4 (watch_descriptor, file, flags, callback);
223 224 225 226 227
  watch_list = Fcons (watch_object, watch_list);

  return watch_descriptor;
}

228
DEFUN ("gfile-rm-watch", Fgfile_rm_watch, Sgfile_rm_watch, 1, 1, 0,
229 230 231 232 233
       doc: /* Remove an existing WATCH-DESCRIPTOR.

WATCH-DESCRIPTOR should be an object returned by `gfile-add-watch'.  */)
     (Lisp_Object watch_descriptor)
{
234
  Lisp_Object watch_object = assq_no_quit (watch_descriptor, watch_list);
235

236
  if (! CONSP (watch_object))
237
    xsignal2 (Qfile_notify_error, build_string ("Not a watch descriptor"),
238
	      watch_descriptor);
239

240
  eassert (FIXNUMP (watch_descriptor));
Tom Tromey's avatar
Tom Tromey committed
241
  GFileMonitor *monitor = XFIXNUMPTR (watch_descriptor);
242 243 244 245
  if (!g_file_monitor_is_cancelled (monitor)
      && !g_file_monitor_cancel (monitor))
    xsignal2 (Qfile_notify_error, build_string ("Could not rm watch"),
	      watch_descriptor);
246

Michael Albinus's avatar
Michael Albinus committed
247
  /* Remove watch descriptor from watch list.  */
248
  watch_list = Fdelq (watch_object, watch_list);
249 250 251 252 253 254 255

  /* Cleanup.  */
  g_object_unref (monitor);

  return Qt;
}

Michael Albinus's avatar
Michael Albinus committed
256
DEFUN ("gfile-valid-p", Fgfile_valid_p, Sgfile_valid_p, 1, 1, 0,
Michael Albinus's avatar
Michael Albinus committed
257
       doc: /* Check a watch specified by its WATCH-DESCRIPTOR.
Michael Albinus's avatar
Michael Albinus committed
258 259 260 261 262 263 264 265 266

WATCH-DESCRIPTOR should be an object returned by `gfile-add-watch'.

A watch can become invalid if the file or directory it watches is
deleted, or if the watcher thread exits abnormally for any other
reason.  Removing the watch by calling `gfile-rm-watch' also makes it
invalid.  */)
     (Lisp_Object watch_descriptor)
{
267
  Lisp_Object watch_object = Fassoc (watch_descriptor, watch_list, Qnil);
Michael Albinus's avatar
Michael Albinus committed
268 269 270 271
  if (NILP (watch_object))
    return Qnil;
  else
    {
Tom Tromey's avatar
Tom Tromey committed
272
      GFileMonitor *monitor = XFIXNUMPTR (watch_descriptor);
Michael Albinus's avatar
Michael Albinus committed
273 274 275 276
      return g_file_monitor_is_cancelled (monitor) ? Qnil : Qt;
    }
}

Michael Albinus's avatar
Michael Albinus committed
277 278 279
DEFUN ("gfile-monitor-name", Fgfile_monitor_name, Sgfile_monitor_name, 1, 1, 0,
       doc: /* Return the internal monitor name for WATCH-DESCRIPTOR.

280 281
The result is a symbol, either `GInotifyFileMonitor',
`GKqueueFileMonitor', `GFamFileMonitor', or `GPollFileMonitor'.
Michael Albinus's avatar
Michael Albinus committed
282 283 284 285 286 287 288 289 290

WATCH-DESCRIPTOR should be an object returned by `gfile-add-watch'.
If WATCH-DESCRIPTOR is not valid, nil is returned.  */)
     (Lisp_Object watch_descriptor)
{
  if (NILP (Fgfile_valid_p (watch_descriptor)))
    return Qnil;
  else
    {
Tom Tromey's avatar
Tom Tromey committed
291
      GFileMonitor *monitor = XFIXNUMPTR (watch_descriptor);
292
      return intern (G_OBJECT_TYPE_NAME (monitor));
Michael Albinus's avatar
Michael Albinus committed
293 294 295
    }
}

296 297

void
298
globals_of_gfilenotify (void)
299
{
300
#if ! GLIB_CHECK_VERSION (2, 36, 0)
301
  g_type_init ();
302
#endif
303 304 305 306 307 308
  watch_list = Qnil;
}

void
syms_of_gfilenotify (void)
{
309 310
  defsubr (&Sgfile_add_watch);
  defsubr (&Sgfile_rm_watch);
Michael Albinus's avatar
Michael Albinus committed
311
  defsubr (&Sgfile_valid_p);
Michael Albinus's avatar
Michael Albinus committed
312
  defsubr (&Sgfile_monitor_name);
313

314
  /* Filter objects.  */
315 316
  DEFSYM (Qchange, "change");
  DEFSYM (Qattribute_change, "attribute-change");
317 318 319 320 321
  DEFSYM (Qwatch_mounts, "watch-mounts"); /* G_FILE_MONITOR_WATCH_MOUNTS  */
  DEFSYM (Qsend_moved, "send-moved");	/* G_FILE_MONITOR_SEND_MOVED  */

  /* Event types.  */
  DEFSYM (Qchanged, "changed");	/* G_FILE_MONITOR_EVENT_CHANGED  */
322
  DEFSYM (Qchanges_done_hint, "changes-done-hint");
323 324 325
				/* G_FILE_MONITOR_EVENT_CHANGES_DONE_HINT  */
  DEFSYM (Qdeleted, "deleted");	/* G_FILE_MONITOR_EVENT_DELETED  */
  DEFSYM (Qcreated, "created");	/* G_FILE_MONITOR_EVENT_CREATED  */
326
  DEFSYM (Qattribute_changed, "attribute-changed");
327 328 329 330
				/* G_FILE_MONITOR_EVENT_ATTRIBUTE_CHANGED  */
  DEFSYM (Qpre_unmount, "pre-unmount");	/* G_FILE_MONITOR_EVENT_PRE_UNMOUNT  */
  DEFSYM (Qunmounted, "unmounted");	/* G_FILE_MONITOR_EVENT_UNMOUNTED  */
  DEFSYM (Qmoved, "moved");	/* G_FILE_MONITOR_EVENT_MOVED  */
331 332 333 334 335

  staticpro (&watch_list);

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