Rename option to shell-command-dont-erase-buffer
[emacs.git] / src / w32notify.c
blobd4a113aaa4df2d9467041ccf895e6d4ff7fdea6c
1 /* Filesystem notifications support for GNU Emacs on the Microsoft Windows API.
2 Copyright (C) 2012-2016 Free Software Foundation, Inc.
4 This file is part of GNU Emacs.
6 GNU Emacs is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or (at
9 your option) any later version.
11 GNU Emacs is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
19 /* Written by Eli Zaretskii <eliz@gnu.org>.
21 Design overview:
23 For each watch request, we launch a separate worker thread. The
24 worker thread runs the watch_worker function, which issues an
25 asynchronous call to ReadDirectoryChangesW, and then calls
26 WaitForSingleObjectEx to wait that an event be signaled
27 to terminate the thread.
28 Waiting with WaitForSingleObjectEx puts the thread in an
29 "alertable" state, so it wakes up when either (a) the call to
30 ReadDirectoryChangesW completes, or (b) the main thread instructs
31 the worker thread to terminate by signaling an event, see below.
33 When the ReadDirectoryChangesW call completes, its completion
34 routine watch_completion is automatically called. watch_completion
35 stashes the received file events in a linked list used to
36 communicate them to the main thread (using a critical section, so
37 that several threads could alter the same linked list), posts a
38 special message, WM_EMACS_FILENOTIFY, to the Emacs's message queue,
39 and returns. That causes the WaitForSingleObjectEx function call
40 inside watch_worker to return, but the thread won't terminate until
41 the event telling to do so will be signaled. The completion
42 routine issued another call to ReadDirectoryChangesW as quickly as
43 possible. (Except when it does not, see below.)
45 In a GUI session, the WM_EMACS_FILENOTIFY message posted to the
46 message queue gets dispatched to the main Emacs window procedure,
47 which queues it for processing by w32_read_socket. When
48 w32_read_socket sees this message, it accesses the linked list with file
49 notifications (using a critical section), extracts the information,
50 converts it to a series of FILE_NOTIFY_EVENT events, and stuffs
51 them into the input event queue to be processed by keyboard.c input
52 machinery (read_char via a call to kbd_buffer_get_event).
54 In a non-GUI session, we send the WM_EMACS_FILENOTIFY message to
55 the main (a.k.a. "Lisp") thread instead, since there are no window
56 procedures in console programs. That message wakes up
57 MsgWaitForMultipleObjects inside sys_select, which then signals to
58 its caller that some keyboard input is available. This causes
59 w32_console_read_socket to be called, which accesses the linked list
60 with file notifications and stuffs them into the input event queue
61 for keyboard.c to process.
63 When the FILE_NOTIFY_EVENT event is processed by keyboard.c's
64 kbd_buffer_get_event, it is converted to a Lispy event that can be
65 bound to a command. The default binding is file-notify-handle-event,
66 defined on subr.el.
68 Routines w32_read_socket or w32_console_read_socket process notifications
69 sets as long as some are available.
71 When the watch is removed by a call to w32notify-rm-watch, the main
72 thread requests that the worker thread terminates by signaling the
73 appropriate event and queuing an APC for the worker thread. The
74 APC specifies the watch_end function to be called. watch_end calls
75 CancelIo on the outstanding ReadDirectoryChangesW call. When
76 watch_end returns, the watch_completion function is called one last
77 time with the ERROR_OPERATION_ABORTED status, which causes it to
78 clean up and set a flag telling watch_worker to exit without
79 issuing another ReadDirectoryChangesW call. Since watch_worker is
80 the thread procedure of the worker thread, exiting it causes the
81 thread to exit. The main thread waits for some time for the worker
82 thread to exit, and if it doesn't, terminates it forcibly. */
84 #include <stddef.h>
85 #include <errno.h>
87 /* must include CRT headers *before* config.h */
88 #include <config.h>
90 #include <windows.h>
92 #include "lisp.h"
93 #include "w32term.h" /* for enter_crit/leave_crit and WM_EMACS_FILENOTIFY */
94 #include "w32common.h" /* for OS version data */
95 #include "w32.h" /* for w32_strerror */
96 #include "coding.h"
97 #include "keyboard.h"
98 #include "frame.h" /* needed by termhooks.h */
99 #include "termhooks.h" /* for FILE_NOTIFY_EVENT */
101 #define DIRWATCH_BUFFER_SIZE 16384
102 #define DIRWATCH_SIGNATURE 0x01233210
104 struct notification {
105 BYTE *buf; /* buffer for ReadDirectoryChangesW */
106 OVERLAPPED *io_info; /* the OVERLAPPED structure for async I/O */
107 BOOL subtree; /* whether to watch subdirectories */
108 DWORD filter; /* bit mask for events to watch */
109 char *watchee; /* the file we are interested in, UTF-8 encoded */
110 HANDLE dir; /* handle to the watched directory */
111 HANDLE thr; /* handle to the thread that watches */
112 HANDLE terminate; /* event signaling the thread to terminate */
113 unsigned signature;
116 /* Used for communicating notifications to the main thread. */
117 struct notifications_set *notifications_set_head;
119 static Lisp_Object watch_list;
121 /* Signal to the main thread that we have file notifications for it to
122 process. */
123 static void
124 send_notifications (struct notifications_set *ns)
126 struct frame *f = SELECTED_FRAME ();
128 /* We add the current notification set to the linked list. Use the
129 critical section to make sure only one thread will access the
130 linked list. */
131 enter_crit ();
132 ns->next = notifications_set_head;
133 ns->prev = notifications_set_head->prev;
134 ns->prev->next = ns;
135 notifications_set_head->prev = ns;
136 leave_crit();
138 /* If PostMessage fails, the message queue is full. If that
139 happens, the last thing they will worry about is file
140 notifications. So we effectively discard the notification in
141 that case. */
142 if (FRAME_TERMCAP_P (f))
143 /* We send the message to the main (a.k.a. "Lisp") thread, where
144 it will wake up MsgWaitForMultipleObjects inside sys_select,
145 causing it to report that there's some keyboard input
146 available. This will in turn cause w32_console_read_socket to
147 be called, which will pick up the file notifications. */
148 PostThreadMessage (dwMainThreadId, WM_EMACS_FILENOTIFY, 0, 0);
149 else if (FRAME_W32_P (f))
150 PostMessage (FRAME_W32_WINDOW (f),
151 WM_EMACS_FILENOTIFY, 0, 0);
152 /* When we are running in batch mode, there's no one to send a
153 message, so we just signal the data is available and hope
154 sys_select will be called soon and will read the data. */
155 #if 0
156 else if (FRAME_INITIAL_P (f) && noninteractive)
158 #endif
161 /* An APC routine to cancel outstanding directory watch. Invoked by
162 the main thread via QueueUserAPC. This is needed because only the
163 thread that issued the ReadDirectoryChangesW call can call CancelIo
164 to cancel that. (CancelIoEx is only available since Vista, so we
165 cannot use it on XP.) */
166 VOID CALLBACK watch_end (ULONG_PTR);
168 VOID CALLBACK
169 watch_end (ULONG_PTR arg)
171 HANDLE hdir = (HANDLE)arg;
173 if (hdir && hdir != INVALID_HANDLE_VALUE)
174 CancelIo (hdir);
177 /* A completion routine (a.k.a. "APC function") for handling events
178 read by ReadDirectoryChangesW. Called by the OS when the thread
179 which issued the asynchronous ReadDirectoryChangesW call is in the
180 "alertable state", i.e. waiting inside SleepEx call. */
181 VOID CALLBACK watch_completion (DWORD, DWORD, OVERLAPPED *);
183 VOID CALLBACK
184 watch_completion (DWORD status, DWORD bytes_ret, OVERLAPPED *io_info)
186 struct notification *dirwatch;
187 DWORD _bytes;
188 struct notifications_set *ns = NULL;
189 BOOL terminate = FALSE;
191 /* Who knows what happened? Perhaps the OVERLAPPED structure was
192 freed by someone already? In any case, we cannot do anything
193 with this request, so just punt and skip it. FIXME: should we
194 raise the 'terminate' flag in this case? */
195 if (!io_info)
197 DebPrint(("watch_completion: io_info is null.\n"));
198 return;
201 /* We have a pointer to our dirwatch structure conveniently stashed
202 away in the hEvent member of the OVERLAPPED struct. According to
203 MSDN documentation of ReadDirectoryChangesW: "The hEvent member
204 of the OVERLAPPED structure is not used by the system, so you can
205 use it yourself." */
206 dirwatch = (struct notification *)io_info->hEvent;
208 if (status == ERROR_OPERATION_ABORTED)
210 /* We've been called because the main thread told us to issue
211 CancelIo on the directory we watch, and watch_end did so.
212 We must exit, without issuing another call to
213 ReadDirectoryChangesW. */
214 return;
217 /* We allocate a new set of notifications to be linked to the linked
218 list of notifications set. This will be processed by Emacs event
219 loop in the main thread. We need to duplicate the notifications
220 buffer, but not the dirwatch structure. */
222 /* Implementation note: In general, allocating memory in non-main
223 threads is a no-no in Emacs. We certainly cannot call xmalloc
224 and friends, because it can longjmp when allocation fails, which
225 will crash Emacs because the jmp_buf is set up to a location on
226 the main thread's stack. However, we can call 'malloc' directly,
227 since that is redirected to HeapAlloc that uses our private heap,
228 see w32heap.c, and that is thread-safe. */
229 ns = malloc (sizeof(struct notifications_set));
230 if (ns)
232 memset (ns, 0, sizeof(struct notifications_set));
233 ns->notifications = malloc (bytes_ret);
234 if (ns->notifications)
236 memcpy (ns->notifications, dirwatch->buf, bytes_ret);
237 ns->size = bytes_ret;
238 ns->desc = dirwatch;
240 else
242 free (ns);
243 ns = NULL;
246 if (ns == NULL)
247 DebPrint(("Out of memory. Notifications lost."));
249 /* Calling ReadDirectoryChangesW quickly to watch again for new
250 notifications. */
251 if (!ReadDirectoryChangesW (dirwatch->dir, dirwatch->buf,
252 DIRWATCH_BUFFER_SIZE, dirwatch->subtree,
253 dirwatch->filter, &_bytes, dirwatch->io_info,
254 watch_completion))
256 DebPrint (("ReadDirectoryChangesW error: %lu\n", GetLastError ()));
257 /* If this call fails, it means that the directory is not
258 watchable any more. We need to terminate the worker thread.
259 Still, we will wait until the current notifications have been
260 sent to the main thread. */
261 terminate = TRUE;
264 if (ns)
265 send_notifications(ns);
267 /* If we were asked to terminate the thread, then fire the event. */
268 if (terminate)
269 SetEvent(dirwatch->terminate);
272 /* Worker routine for the watch thread. */
273 static DWORD WINAPI
274 watch_worker (LPVOID arg)
276 struct notification *dirwatch = (struct notification *)arg;
277 BOOL bErr;
278 DWORD _bytes = 0;
279 DWORD status;
281 if (dirwatch->dir)
283 bErr = ReadDirectoryChangesW (dirwatch->dir, dirwatch->buf,
284 DIRWATCH_BUFFER_SIZE, dirwatch->subtree,
285 dirwatch->filter, &_bytes,
286 dirwatch->io_info, watch_completion);
287 if (!bErr)
289 DebPrint (("ReadDirectoryChangesW: %lu\n", GetLastError ()));
290 /* We cannot remove the dirwatch object from watch_list,
291 because we are in a separate thread. For the same
292 reason, we also cannot free memory consumed by the
293 buffers allocated for the dirwatch object. So we close
294 the directory handle, but do not free the object itself
295 or its buffers. We also don't touch the signature. This
296 way, remove_watch can still identify the object, remove
297 it, and free its memory. */
298 CloseHandle (dirwatch->dir);
299 dirwatch->dir = NULL;
300 return 1;
304 do {
305 status = WaitForSingleObjectEx(dirwatch->terminate, INFINITE, TRUE);
306 } while (status == WAIT_IO_COMPLETION);
308 /* The thread is about to terminate, so we clean up the dir handle. */
309 CloseHandle (dirwatch->dir);
310 dirwatch->dir = NULL;
312 return 0;
314 /* Launch a thread to watch changes to FILE in a directory open on
315 handle HDIR. */
316 static struct notification *
317 start_watching (const char *file, HANDLE hdir, BOOL subdirs, DWORD flags)
319 struct notification *dirwatch = xzalloc (sizeof (struct notification));
321 dirwatch->signature = DIRWATCH_SIGNATURE;
322 dirwatch->buf = xmalloc (DIRWATCH_BUFFER_SIZE);
323 dirwatch->io_info = xzalloc (sizeof(OVERLAPPED));
324 /* Stash a pointer to dirwatch structure for use by the completion
325 routine. According to MSDN documentation of ReadDirectoryChangesW:
326 "The hEvent member of the OVERLAPPED structure is not used by the
327 system, so you can use it yourself." */
328 dirwatch->io_info->hEvent = dirwatch;
329 dirwatch->subtree = subdirs;
330 dirwatch->filter = flags;
331 dirwatch->watchee = xstrdup (file);
333 dirwatch->terminate = CreateEvent(NULL, FALSE, FALSE, NULL);
335 dirwatch->dir = hdir;
337 /* See w32proc.c where it calls CreateThread for the story behind
338 the 2nd and 5th argument in the call to CreateThread. */
339 dirwatch->thr = CreateThread (NULL, 64 * 1024, watch_worker, (void *)dirwatch,
340 0x00010000, NULL);
342 if (!dirwatch->thr)
344 CloseHandle(dirwatch->terminate);
345 xfree (dirwatch->buf);
346 xfree (dirwatch->io_info);
347 xfree (dirwatch->watchee);
348 xfree (dirwatch);
350 return dirwatch;
353 /* Called from the main thread to start watching FILE in PARENT_DIR,
354 subject to FLAGS. If SUBDIRS is TRUE, watch the subdirectories of
355 PARENT_DIR as well. Value is a pointer to 'struct notification'
356 used by the thread that watches the changes. */
357 static struct notification *
358 add_watch (const char *parent_dir, const char *file, BOOL subdirs, DWORD flags)
360 HANDLE hdir;
361 struct notification *dirwatch = NULL;
363 if (!file)
364 return NULL;
366 if (w32_unicode_filenames)
368 wchar_t dir_w[MAX_PATH], file_w[MAX_PATH];
370 filename_to_utf16 (parent_dir, dir_w);
371 if (*file)
372 filename_to_utf16 (file, file_w);
373 else
374 file_w[0] = 0;
376 hdir = CreateFileW (dir_w,
377 FILE_LIST_DIRECTORY,
378 /* FILE_SHARE_DELETE doesn't preclude other
379 processes from deleting files inside
380 parent_dir. */
381 FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE,
382 NULL, OPEN_EXISTING,
383 FILE_FLAG_BACKUP_SEMANTICS | FILE_FLAG_OVERLAPPED,
384 NULL);
386 else
388 char dir_a[MAX_PATH], file_a[MAX_PATH];
390 filename_to_ansi (parent_dir, dir_a);
391 if (*file)
392 filename_to_ansi (file, file_a);
393 else
394 file_a[0] = '\0';
396 hdir = CreateFileA (dir_a,
397 FILE_LIST_DIRECTORY,
398 FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE,
399 NULL, OPEN_EXISTING,
400 FILE_FLAG_BACKUP_SEMANTICS | FILE_FLAG_OVERLAPPED,
401 NULL);
403 if (hdir == INVALID_HANDLE_VALUE)
404 return NULL;
406 if ((dirwatch = start_watching (file, hdir, subdirs, flags)) == NULL)
408 CloseHandle (hdir);
409 dirwatch->dir = NULL;
412 return dirwatch;
415 /* Stop watching a directory specified by a pointer to its dirwatch object. */
416 static int
417 remove_watch (struct notification *dirwatch)
419 if (dirwatch && dirwatch->signature == DIRWATCH_SIGNATURE)
421 int i;
422 BOOL status;
423 DWORD exit_code = 0, err;
425 /* Only the thread that issued the outstanding I/O call can call
426 CancelIo on it. (CancelIoEx is available only since Vista.)
427 So we need to queue an APC for the worker thread telling it
428 to terminate. */
429 if (!QueueUserAPC (watch_end, dirwatch->thr, (ULONG_PTR)dirwatch->dir))
430 DebPrint (("QueueUserAPC failed (%lu)!\n", GetLastError ()));
432 /* We also signal the thread that it can terminate. */
433 SetEvent(dirwatch->terminate);
435 /* Wait for the thread to exit. FIXME: is there a better method
436 that is not overly complex? */
437 for (i = 0; i < 50; i++)
439 if (!((status = GetExitCodeThread (dirwatch->thr, &exit_code))
440 && exit_code == STILL_ACTIVE))
441 break;
442 Sleep (10);
445 if ((status == FALSE && (err = GetLastError ()) == ERROR_INVALID_HANDLE)
446 || exit_code == STILL_ACTIVE)
448 if (!(status == FALSE && err == ERROR_INVALID_HANDLE))
450 DebPrint(("Forcing thread termination.\n"));
451 TerminateThread (dirwatch->thr, 0);
452 if (dirwatch->dir)
453 CloseHandle (dirwatch->dir);
457 /* Clean up. */
458 if (dirwatch->thr)
460 CloseHandle (dirwatch->thr);
461 dirwatch->thr = NULL;
463 CloseHandle(dirwatch->terminate);
464 xfree (dirwatch->buf);
465 xfree (dirwatch->io_info);
466 xfree (dirwatch->watchee);
467 xfree (dirwatch);
468 return 0;
470 else
472 DebPrint (("Unknown dirwatch object!\n"));
473 return -1;
477 static DWORD
478 filter_list_to_flags (Lisp_Object filter_list)
480 DWORD flags = 0;
482 if (NILP (filter_list))
483 return flags;
485 if (!NILP (Fmember (Qfile_name, filter_list)))
486 flags |= FILE_NOTIFY_CHANGE_FILE_NAME;
487 if (!NILP (Fmember (Qdirectory_name, filter_list)))
488 flags |= FILE_NOTIFY_CHANGE_DIR_NAME;
489 if (!NILP (Fmember (Qattributes, filter_list)))
490 flags |= FILE_NOTIFY_CHANGE_ATTRIBUTES;
491 if (!NILP (Fmember (Qsize, filter_list)))
492 flags |= FILE_NOTIFY_CHANGE_SIZE;
493 if (!NILP (Fmember (Qlast_write_time, filter_list)))
494 flags |= FILE_NOTIFY_CHANGE_LAST_WRITE;
495 if (!NILP (Fmember (Qlast_access_time, filter_list)))
496 flags |= FILE_NOTIFY_CHANGE_LAST_ACCESS;
497 if (!NILP (Fmember (Qcreation_time, filter_list)))
498 flags |= FILE_NOTIFY_CHANGE_CREATION;
499 if (!NILP (Fmember (Qsecurity_desc, filter_list)))
500 flags |= FILE_NOTIFY_CHANGE_SECURITY;
502 return flags;
505 DEFUN ("w32notify-add-watch", Fw32notify_add_watch,
506 Sw32notify_add_watch, 3, 3, 0,
507 doc: /* Add a watch for filesystem events pertaining to FILE.
509 This arranges for filesystem events pertaining to FILE to be reported
510 to Emacs. Use `w32notify-rm-watch' to cancel the watch.
512 Value is a descriptor for the added watch. If the file cannot be
513 watched for some reason, this function signals a `file-error' error.
515 FILTER is a list of conditions for reporting an event. It can include
516 the following symbols:
518 'file-name' -- report file creation, deletion, or renaming
519 'directory-name' -- report directory creation, deletion, or renaming
520 'attributes' -- report changes in attributes
521 'size' -- report changes in file-size
522 'last-write-time' -- report changes in last-write time
523 'last-access-time' -- report changes in last-access time
524 'creation-time' -- report changes in creation time
525 'security-desc' -- report changes in security descriptor
527 If FILE is a directory, and FILTER includes 'subtree', then all the
528 subdirectories will also be watched and changes in them reported.
530 When any event happens that satisfies the conditions specified by
531 FILTER, Emacs will call the CALLBACK function passing it a single
532 argument EVENT, which is of the form
534 (DESCRIPTOR ACTION FILE)
536 DESCRIPTOR is the same object as the one returned by this function.
537 ACTION is the description of the event. It could be any one of the
538 following:
540 'added' -- FILE was added
541 'removed' -- FILE was deleted
542 'modified' -- FILE's contents or its attributes were modified
543 'renamed-from' -- a file was renamed whose old name was FILE
544 'renamed-to' -- a file was renamed and its new name is FILE
546 FILE is the name of the file whose event is being reported.
548 Note that some networked filesystems, such as Samba-mounted Unix
549 volumes, might not send notifications about file changes. In these
550 cases, this function will return a valid descriptor, but notifications
551 will never come in. Volumes shared from remote Windows machines do
552 generate notifications correctly, though. */)
553 (Lisp_Object file, Lisp_Object filter, Lisp_Object callback)
555 Lisp_Object dirfn, basefn, watch_object, watch_descriptor;
556 DWORD flags;
557 BOOL subdirs = FALSE;
558 struct notification *dirwatch = NULL;
559 Lisp_Object lisp_errstr;
560 char *errstr;
562 CHECK_LIST (filter);
564 /* The underlying features are available only since XP. */
565 if (os_subtype == OS_9X
566 || (w32_major_version == 5 && w32_minor_version < 1))
568 errno = ENOSYS;
569 report_file_notify_error ("Watching filesystem events is not supported",
570 Qnil);
573 /* filenotify.el always passes us a directory, either the parent
574 directory of a file to be watched, or the directory to be
575 watched. */
576 file = Fdirectory_file_name (Fexpand_file_name (file, Qnil));
577 if (NILP (Ffile_directory_p (file)))
579 /* This should only happen if we are called directly, not via
580 filenotify.el. If BASEFN is empty, the argument was the root
581 directory on its drive. */
582 dirfn = ENCODE_FILE (Ffile_name_directory (file));
583 basefn = ENCODE_FILE (Ffile_name_nondirectory (file));
584 if (*SDATA (basefn) == '\0')
585 subdirs = TRUE;
587 else
589 dirfn = ENCODE_FILE (file);
590 basefn = Qnil;
593 if (!NILP (Fmember (Qsubtree, filter)))
594 subdirs = TRUE;
596 flags = filter_list_to_flags (filter);
598 dirwatch = add_watch (SSDATA (dirfn), NILP (basefn) ? "" : SSDATA (basefn),
599 subdirs, flags);
600 if (!dirwatch)
602 DWORD err = GetLastError ();
604 errno = EINVAL;
605 if (err)
607 errstr = w32_strerror (err);
608 if (!NILP (Vlocale_coding_system))
609 lisp_errstr
610 = code_convert_string_norecord (build_unibyte_string (errstr),
611 Vlocale_coding_system, 0);
612 else
613 lisp_errstr = build_string (errstr);
614 report_file_notify_error ("Cannot watch file",
615 Fcons (lisp_errstr, Fcons (file, Qnil)));
617 else
618 report_file_notify_error ("Cannot watch file", Fcons (file, Qnil));
620 /* Store watch object in watch list. */
621 watch_descriptor = make_pointer_integer (dirwatch);
622 watch_object = Fcons (watch_descriptor, callback);
623 watch_list = Fcons (watch_object, watch_list);
625 return watch_descriptor;
628 DEFUN ("w32notify-rm-watch", Fw32notify_rm_watch,
629 Sw32notify_rm_watch, 1, 1, 0,
630 doc: /* Remove an existing watch specified by its WATCH-DESCRIPTOR.
632 WATCH-DESCRIPTOR should be an object returned by `w32notify-add-watch'. */)
633 (Lisp_Object watch_descriptor)
635 Lisp_Object watch_object;
636 struct notification *dirwatch;
637 int status = -1;
639 /* Remove the watch object from watch list. Do this before freeing
640 the object, do that even if we fail to free it, watch_list is
641 kept free of junk. */
642 watch_object = Fassoc (watch_descriptor, watch_list);
643 if (!NILP (watch_object))
645 watch_list = Fdelete (watch_object, watch_list);
646 dirwatch = (struct notification *)XINTPTR (watch_descriptor);
647 if (w32_valid_pointer_p (dirwatch, sizeof(struct notification)))
648 status = remove_watch (dirwatch);
651 if (status == -1)
652 report_file_notify_error ("Invalid watch descriptor",
653 Fcons (watch_descriptor, Qnil));
655 return Qnil;
658 Lisp_Object
659 w32_get_watch_object (void *desc)
661 Lisp_Object descriptor = make_pointer_integer (desc);
663 /* This is called from the input queue handling code, inside a
664 critical section, so we cannot possibly QUIT if watch_list is not
665 in the right condition. */
666 return NILP (watch_list) ? Qnil : assoc_no_quit (descriptor, watch_list);
669 DEFUN ("w32notify-valid-p", Fw32notify_valid_p, Sw32notify_valid_p, 1, 1, 0,
670 doc: /* "Check a watch specified by its WATCH-DESCRIPTOR for validity.
672 WATCH-DESCRIPTOR should be an object returned by `w32notify-add-watch'.
674 A watch can become invalid if the directory it watches is deleted, or if
675 the watcher thread exits abnormally for any other reason. Removing the
676 watch by calling `w32notify-rm-watch' also makes it invalid. */)
677 (Lisp_Object watch_descriptor)
679 Lisp_Object watch_object = Fassoc (watch_descriptor, watch_list);
681 if (!NILP (watch_object))
683 struct notification *dirwatch =
684 (struct notification *)XINTPTR (watch_descriptor);
685 if (w32_valid_pointer_p (dirwatch, sizeof(struct notification))
686 && dirwatch->dir != NULL)
687 return Qt;
690 return Qnil;
693 void
694 globals_of_w32notify (void)
696 watch_list = Qnil;
699 void
700 syms_of_w32notify (void)
702 DEFSYM (Qfile_name, "file-name");
703 DEFSYM (Qdirectory_name, "directory-name");
704 DEFSYM (Qattributes, "attributes");
705 DEFSYM (Qlast_write_time, "last-write-time");
706 DEFSYM (Qlast_access_time, "last-access-time");
707 DEFSYM (Qcreation_time, "creation-time");
708 DEFSYM (Qsecurity_desc, "security-desc");
709 DEFSYM (Qsubtree, "subtree");
711 defsubr (&Sw32notify_add_watch);
712 defsubr (&Sw32notify_rm_watch);
713 defsubr (&Sw32notify_valid_p);
715 staticpro (&watch_list);
717 Fprovide (intern_c_string ("w32notify"), Qnil);