]> git.ipfire.org Git - thirdparty/git.git/blob - compat/fsmonitor/fsm-listen-win32.c
submodule-config.c: strengthen URL fsck check
[thirdparty/git.git] / compat / fsmonitor / fsm-listen-win32.c
1 #include "git-compat-util.h"
2 #include "config.h"
3 #include "fsmonitor-ll.h"
4 #include "fsm-listen.h"
5 #include "fsmonitor--daemon.h"
6 #include "gettext.h"
7 #include "trace2.h"
8
9 /*
10 * The documentation of ReadDirectoryChangesW() states that the maximum
11 * buffer size is 64K when the monitored directory is remote.
12 *
13 * Larger buffers may be used when the monitored directory is local and
14 * will help us receive events faster from the kernel and avoid dropped
15 * events.
16 *
17 * So we try to use a very large buffer and silently fallback to 64K if
18 * we get an error.
19 */
20 #define MAX_RDCW_BUF_FALLBACK (65536)
21 #define MAX_RDCW_BUF (65536 * 8)
22
23 struct one_watch
24 {
25 char buffer[MAX_RDCW_BUF];
26 DWORD buf_len;
27 DWORD count;
28
29 struct strbuf path;
30 wchar_t wpath_longname[MAX_PATH + 1];
31 DWORD wpath_longname_len;
32
33 HANDLE hDir;
34 HANDLE hEvent;
35 OVERLAPPED overlapped;
36
37 /*
38 * Is there an active ReadDirectoryChangesW() call pending. If so, we
39 * need to later call GetOverlappedResult() and possibly CancelIoEx().
40 */
41 BOOL is_active;
42
43 /*
44 * Are shortnames enabled on the containing drive? This is
45 * always true for "C:/" drives and usually never true for
46 * other drives.
47 *
48 * We only set this for the worktree because we only need to
49 * convert shortname paths to longname paths for items we send
50 * to clients. (We don't care about shortname expansion for
51 * paths inside a GITDIR because we never send them to
52 * clients.)
53 */
54 BOOL has_shortnames;
55 BOOL has_tilde;
56 wchar_t dotgit_shortname[16]; /* for 8.3 name */
57 };
58
59 struct fsm_listen_data
60 {
61 struct one_watch *watch_worktree;
62 struct one_watch *watch_gitdir;
63
64 HANDLE hEventShutdown;
65
66 HANDLE hListener[3]; /* we don't own these handles */
67 #define LISTENER_SHUTDOWN 0
68 #define LISTENER_HAVE_DATA_WORKTREE 1
69 #define LISTENER_HAVE_DATA_GITDIR 2
70 int nr_listener_handles;
71 };
72
73 /*
74 * Convert the WCHAR path from the event into UTF8 and normalize it.
75 *
76 * `wpath_len` is in WCHARS not bytes.
77 */
78 static int normalize_path_in_utf8(wchar_t *wpath, DWORD wpath_len,
79 struct strbuf *normalized_path)
80 {
81 int reserve;
82 int len = 0;
83
84 strbuf_reset(normalized_path);
85 if (!wpath_len)
86 goto normalize;
87
88 /*
89 * Pre-reserve enough space in the UTF8 buffer for
90 * each Unicode WCHAR character to be mapped into a
91 * sequence of 2 UTF8 characters. That should let us
92 * avoid ERROR_INSUFFICIENT_BUFFER 99.9+% of the time.
93 */
94 reserve = 2 * wpath_len + 1;
95 strbuf_grow(normalized_path, reserve);
96
97 for (;;) {
98 len = WideCharToMultiByte(CP_UTF8, 0,
99 wpath, wpath_len,
100 normalized_path->buf,
101 strbuf_avail(normalized_path) - 1,
102 NULL, NULL);
103 if (len > 0)
104 goto normalize;
105 if (GetLastError() != ERROR_INSUFFICIENT_BUFFER) {
106 error(_("[GLE %ld] could not convert path to UTF-8: '%.*ls'"),
107 GetLastError(), (int)wpath_len, wpath);
108 return -1;
109 }
110
111 strbuf_grow(normalized_path,
112 strbuf_avail(normalized_path) + reserve);
113 }
114
115 normalize:
116 strbuf_setlen(normalized_path, len);
117 return strbuf_normalize_path(normalized_path);
118 }
119
120 /*
121 * See if the worktree root directory has shortnames enabled.
122 * This will help us decide if we need to do an expensive shortname
123 * to longname conversion on every notification event.
124 *
125 * We do not want to create a file to test this, so we assume that the
126 * root directory contains a ".git" file or directory. (Our caller
127 * only calls us for the worktree root, so this should be fine.)
128 *
129 * Remember the spelling of the shortname for ".git" if it exists.
130 */
131 static void check_for_shortnames(struct one_watch *watch)
132 {
133 wchar_t buf_in[MAX_PATH + 1];
134 wchar_t buf_out[MAX_PATH + 1];
135 wchar_t *last;
136 wchar_t *p;
137
138 /* build L"<wt-root-path>/.git" */
139 swprintf(buf_in, ARRAY_SIZE(buf_in) - 1, L"%ls.git",
140 watch->wpath_longname);
141
142 if (!GetShortPathNameW(buf_in, buf_out, ARRAY_SIZE(buf_out)))
143 return;
144
145 /*
146 * Get the final filename component of the shortpath.
147 * We know that the path does not have a final slash.
148 */
149 for (last = p = buf_out; *p; p++)
150 if (*p == L'/' || *p == '\\')
151 last = p + 1;
152
153 if (!wcscmp(last, L".git"))
154 return;
155
156 watch->has_shortnames = 1;
157 wcsncpy(watch->dotgit_shortname, last,
158 ARRAY_SIZE(watch->dotgit_shortname));
159
160 /*
161 * The shortname for ".git" is usually of the form "GIT~1", so
162 * we should be able to avoid shortname to longname mapping on
163 * every notification event if the source string does not
164 * contain a "~".
165 *
166 * However, the documentation for GetLongPathNameW() says
167 * that there are filesystems that don't follow that pattern
168 * and warns against this optimization.
169 *
170 * Lets test this.
171 */
172 if (wcschr(watch->dotgit_shortname, L'~'))
173 watch->has_tilde = 1;
174 }
175
176 enum get_relative_result {
177 GRR_NO_CONVERSION_NEEDED,
178 GRR_HAVE_CONVERSION,
179 GRR_SHUTDOWN,
180 };
181
182 /*
183 * Info notification paths are relative to the root of the watch.
184 * If our CWD is still at the root, then we can use relative paths
185 * to convert from shortnames to longnames. If our process has a
186 * different CWD, then we need to construct an absolute path, do
187 * the conversion, and then return the root-relative portion.
188 *
189 * We use the longname form of the root as our basis and assume that
190 * it already has a trailing slash.
191 *
192 * `wpath_len` is in WCHARS not bytes.
193 */
194 static enum get_relative_result get_relative_longname(
195 struct one_watch *watch,
196 const wchar_t *wpath, DWORD wpath_len,
197 wchar_t *wpath_longname, size_t bufsize_wpath_longname)
198 {
199 wchar_t buf_in[2 * MAX_PATH + 1];
200 wchar_t buf_out[MAX_PATH + 1];
201 DWORD root_len;
202 DWORD out_len;
203
204 /*
205 * Build L"<wt-root-path>/<event-rel-path>"
206 * Note that the <event-rel-path> might not be null terminated
207 * so we avoid swprintf() constructions.
208 */
209 root_len = watch->wpath_longname_len;
210 if (root_len + wpath_len >= ARRAY_SIZE(buf_in)) {
211 /*
212 * This should not happen. We cannot append the observed
213 * relative path onto the end of the worktree root path
214 * without overflowing the buffer. Just give up.
215 */
216 return GRR_SHUTDOWN;
217 }
218 wcsncpy(buf_in, watch->wpath_longname, root_len);
219 wcsncpy(buf_in + root_len, wpath, wpath_len);
220 buf_in[root_len + wpath_len] = 0;
221
222 /*
223 * We don't actually know if the source pathname is a
224 * shortname or a longname. This Windows routine allows
225 * either to be given as input.
226 */
227 out_len = GetLongPathNameW(buf_in, buf_out, ARRAY_SIZE(buf_out));
228 if (!out_len) {
229 /*
230 * The shortname to longname conversion can fail for
231 * various reasons, for example if the file has been
232 * deleted. (That is, if we just received a
233 * delete-file notification event and the file is
234 * already gone, we can't ask the file system to
235 * lookup the longname for it. Likewise, for moves
236 * and renames where we are given the old name.)
237 *
238 * Since deleting or moving a file or directory by its
239 * shortname is rather obscure, I'm going ignore the
240 * failure and ask the caller to report the original
241 * relative path. This seems kinder than failing here
242 * and forcing a resync. Besides, forcing a resync on
243 * every file/directory delete would effectively
244 * cripple monitoring.
245 *
246 * We might revisit this in the future.
247 */
248 return GRR_NO_CONVERSION_NEEDED;
249 }
250
251 if (!wcscmp(buf_in, buf_out)) {
252 /*
253 * The path does not have a shortname alias.
254 */
255 return GRR_NO_CONVERSION_NEEDED;
256 }
257
258 if (wcsncmp(buf_in, buf_out, root_len)) {
259 /*
260 * The spelling of the root directory portion of the computed
261 * longname has changed. This should not happen. Basically,
262 * it means that we don't know where (without recomputing the
263 * longname of just the root directory) to split out the
264 * relative path. Since this should not happen, I'm just
265 * going to let this fail and force a shutdown (because all
266 * subsequent events are probably going to see the same
267 * mismatch).
268 */
269 return GRR_SHUTDOWN;
270 }
271
272 if (out_len - root_len >= bufsize_wpath_longname) {
273 /*
274 * This should not happen. We cannot copy the root-relative
275 * portion of the path into the provided buffer without an
276 * overrun. Just give up.
277 */
278 return GRR_SHUTDOWN;
279 }
280
281 /* Return the worktree root-relative portion of the longname. */
282
283 wcscpy(wpath_longname, buf_out + root_len);
284 return GRR_HAVE_CONVERSION;
285 }
286
287 void fsm_listen__stop_async(struct fsmonitor_daemon_state *state)
288 {
289 SetEvent(state->listen_data->hListener[LISTENER_SHUTDOWN]);
290 }
291
292 static struct one_watch *create_watch(const char *path)
293 {
294 struct one_watch *watch = NULL;
295 DWORD desired_access = FILE_LIST_DIRECTORY;
296 DWORD share_mode =
297 FILE_SHARE_WRITE | FILE_SHARE_READ | FILE_SHARE_DELETE;
298 HANDLE hDir;
299 DWORD len_longname;
300 wchar_t wpath[MAX_PATH + 1];
301 wchar_t wpath_longname[MAX_PATH + 1];
302
303 if (xutftowcs_path(wpath, path) < 0) {
304 error(_("could not convert to wide characters: '%s'"), path);
305 return NULL;
306 }
307
308 hDir = CreateFileW(wpath,
309 desired_access, share_mode, NULL, OPEN_EXISTING,
310 FILE_FLAG_BACKUP_SEMANTICS | FILE_FLAG_OVERLAPPED,
311 NULL);
312 if (hDir == INVALID_HANDLE_VALUE) {
313 error(_("[GLE %ld] could not watch '%s'"),
314 GetLastError(), path);
315 return NULL;
316 }
317
318 len_longname = GetLongPathNameW(wpath, wpath_longname,
319 ARRAY_SIZE(wpath_longname));
320 if (!len_longname) {
321 error(_("[GLE %ld] could not get longname of '%s'"),
322 GetLastError(), path);
323 CloseHandle(hDir);
324 return NULL;
325 }
326
327 if (wpath_longname[len_longname - 1] != L'/' &&
328 wpath_longname[len_longname - 1] != L'\\') {
329 wpath_longname[len_longname++] = L'/';
330 wpath_longname[len_longname] = 0;
331 }
332
333 CALLOC_ARRAY(watch, 1);
334
335 watch->buf_len = sizeof(watch->buffer); /* assume full MAX_RDCW_BUF */
336
337 strbuf_init(&watch->path, 0);
338 strbuf_addstr(&watch->path, path);
339
340 wcscpy(watch->wpath_longname, wpath_longname);
341 watch->wpath_longname_len = len_longname;
342
343 watch->hDir = hDir;
344 watch->hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
345
346 return watch;
347 }
348
349 static void destroy_watch(struct one_watch *watch)
350 {
351 if (!watch)
352 return;
353
354 strbuf_release(&watch->path);
355 if (watch->hDir != INVALID_HANDLE_VALUE)
356 CloseHandle(watch->hDir);
357 if (watch->hEvent != INVALID_HANDLE_VALUE)
358 CloseHandle(watch->hEvent);
359
360 free(watch);
361 }
362
363 static int start_rdcw_watch(struct one_watch *watch)
364 {
365 DWORD dwNotifyFilter =
366 FILE_NOTIFY_CHANGE_FILE_NAME |
367 FILE_NOTIFY_CHANGE_DIR_NAME |
368 FILE_NOTIFY_CHANGE_ATTRIBUTES |
369 FILE_NOTIFY_CHANGE_SIZE |
370 FILE_NOTIFY_CHANGE_LAST_WRITE |
371 FILE_NOTIFY_CHANGE_CREATION;
372
373 ResetEvent(watch->hEvent);
374
375 memset(&watch->overlapped, 0, sizeof(watch->overlapped));
376 watch->overlapped.hEvent = watch->hEvent;
377
378 /*
379 * Queue an async call using Overlapped IO. This returns immediately.
380 * Our event handle will be signalled when the real result is available.
381 *
382 * The return value here just means that we successfully queued it.
383 * We won't know if the Read...() actually produces data until later.
384 */
385 watch->is_active = ReadDirectoryChangesW(
386 watch->hDir, watch->buffer, watch->buf_len, TRUE,
387 dwNotifyFilter, &watch->count, &watch->overlapped, NULL);
388
389 if (watch->is_active)
390 return 0;
391
392 error(_("ReadDirectoryChangedW failed on '%s' [GLE %ld]"),
393 watch->path.buf, GetLastError());
394 return -1;
395 }
396
397 static int recv_rdcw_watch(struct one_watch *watch)
398 {
399 DWORD gle;
400
401 watch->is_active = FALSE;
402
403 /*
404 * The overlapped result is ready. If the Read...() was successful
405 * we finally receive the actual result into our buffer.
406 */
407 if (GetOverlappedResult(watch->hDir, &watch->overlapped, &watch->count,
408 TRUE))
409 return 0;
410
411 gle = GetLastError();
412 if (gle == ERROR_INVALID_PARAMETER &&
413 /*
414 * The kernel throws an invalid parameter error when our
415 * buffer is too big and we are pointed at a remote
416 * directory (and possibly for other reasons). Quietly
417 * set it down and try again.
418 *
419 * See note about MAX_RDCW_BUF at the top.
420 */
421 watch->buf_len > MAX_RDCW_BUF_FALLBACK) {
422 watch->buf_len = MAX_RDCW_BUF_FALLBACK;
423 return -2;
424 }
425
426 /*
427 * GetOverlappedResult() fails if the watched directory is
428 * deleted while we were waiting for an overlapped IO to
429 * complete. The documentation did not list specific errors,
430 * but I observed ERROR_ACCESS_DENIED (0x05) errors during
431 * testing.
432 *
433 * Note that we only get notificaiton events for events
434 * *within* the directory, not *on* the directory itself.
435 * (These might be properies of the parent directory, for
436 * example).
437 *
438 * NEEDSWORK: We might try to check for the deleted directory
439 * case and return a better error message, but I'm not sure it
440 * is worth it.
441 *
442 * Shutdown if we get any error.
443 */
444
445 error(_("GetOverlappedResult failed on '%s' [GLE %ld]"),
446 watch->path.buf, gle);
447 return -1;
448 }
449
450 static void cancel_rdcw_watch(struct one_watch *watch)
451 {
452 DWORD count;
453
454 if (!watch || !watch->is_active)
455 return;
456
457 /*
458 * The calls to ReadDirectoryChangesW() and GetOverlappedResult()
459 * form a "pair" (my term) where we queue an IO and promise to
460 * hang around and wait for the kernel to give us the result.
461 *
462 * If for some reason after we queue the IO, we have to quit
463 * or otherwise not stick around for the second half, we must
464 * tell the kernel to abort the IO. This prevents the kernel
465 * from writing to our buffer and/or signalling our event
466 * after we free them.
467 *
468 * (Ask me how much fun it was to track that one down).
469 */
470 CancelIoEx(watch->hDir, &watch->overlapped);
471 GetOverlappedResult(watch->hDir, &watch->overlapped, &count, TRUE);
472 watch->is_active = FALSE;
473 }
474
475 /*
476 * Process a single relative pathname event.
477 * Return 1 if we should shutdown.
478 */
479 static int process_1_worktree_event(
480 struct string_list *cookie_list,
481 struct fsmonitor_batch **batch,
482 const struct strbuf *path,
483 enum fsmonitor_path_type t,
484 DWORD info_action)
485 {
486 const char *slash;
487
488 switch (t) {
489 case IS_INSIDE_DOT_GIT_WITH_COOKIE_PREFIX:
490 /* special case cookie files within .git */
491
492 /* Use just the filename of the cookie file. */
493 slash = find_last_dir_sep(path->buf);
494 string_list_append(cookie_list,
495 slash ? slash + 1 : path->buf);
496 break;
497
498 case IS_INSIDE_DOT_GIT:
499 /* ignore everything inside of "<worktree>/.git/" */
500 break;
501
502 case IS_DOT_GIT:
503 /* "<worktree>/.git" was deleted (or renamed away) */
504 if ((info_action == FILE_ACTION_REMOVED) ||
505 (info_action == FILE_ACTION_RENAMED_OLD_NAME)) {
506 trace2_data_string("fsmonitor", NULL,
507 "fsm-listen/dotgit",
508 "removed");
509 return 1;
510 }
511 break;
512
513 case IS_WORKDIR_PATH:
514 /* queue normal pathname */
515 if (!*batch)
516 *batch = fsmonitor_batch__new();
517 fsmonitor_batch__add_path(*batch, path->buf);
518 break;
519
520 case IS_GITDIR:
521 case IS_INSIDE_GITDIR:
522 case IS_INSIDE_GITDIR_WITH_COOKIE_PREFIX:
523 default:
524 BUG("unexpected path classification '%d' for '%s'",
525 t, path->buf);
526 }
527
528 return 0;
529 }
530
531 /*
532 * Process filesystem events that happen anywhere (recursively) under the
533 * <worktree> root directory. For a normal working directory, this includes
534 * both version controlled files and the contents of the .git/ directory.
535 *
536 * If <worktree>/.git is a file, then we only see events for the file
537 * itself.
538 */
539 static int process_worktree_events(struct fsmonitor_daemon_state *state)
540 {
541 struct fsm_listen_data *data = state->listen_data;
542 struct one_watch *watch = data->watch_worktree;
543 struct strbuf path = STRBUF_INIT;
544 struct string_list cookie_list = STRING_LIST_INIT_DUP;
545 struct fsmonitor_batch *batch = NULL;
546 const char *p = watch->buffer;
547 wchar_t wpath_longname[MAX_PATH + 1];
548
549 /*
550 * If the kernel gets more events than will fit in the kernel
551 * buffer associated with our RDCW handle, it drops them and
552 * returns a count of zero.
553 *
554 * Yes, the call returns WITHOUT error and with length zero.
555 * This is the documented behavior. (My testing has confirmed
556 * that it also sets the last error to ERROR_NOTIFY_ENUM_DIR,
557 * but we do not rely on that since the function did not
558 * return an error and it is not documented.)
559 *
560 * (The "overflow" case is not ambiguous with the "no data" case
561 * because we did an INFINITE wait.)
562 *
563 * This means we have a gap in coverage. Tell the daemon layer
564 * to resync.
565 */
566 if (!watch->count) {
567 trace2_data_string("fsmonitor", NULL, "fsm-listen/kernel",
568 "overflow");
569 fsmonitor_force_resync(state);
570 return LISTENER_HAVE_DATA_WORKTREE;
571 }
572
573 /*
574 * On Windows, `info` contains an "array" of paths that are
575 * relative to the root of whichever directory handle received
576 * the event.
577 */
578 for (;;) {
579 FILE_NOTIFY_INFORMATION *info = (void *)p;
580 wchar_t *wpath = info->FileName;
581 DWORD wpath_len = info->FileNameLength / sizeof(WCHAR);
582 enum fsmonitor_path_type t;
583 enum get_relative_result grr;
584
585 if (watch->has_shortnames) {
586 if (!wcscmp(wpath, watch->dotgit_shortname)) {
587 /*
588 * This event exactly matches the
589 * spelling of the shortname of
590 * ".git", so we can skip some steps.
591 *
592 * (This case is odd because the user
593 * can "rm -rf GIT~1" and we cannot
594 * use the filesystem to map it back
595 * to ".git".)
596 */
597 strbuf_reset(&path);
598 strbuf_addstr(&path, ".git");
599 t = IS_DOT_GIT;
600 goto process_it;
601 }
602
603 if (watch->has_tilde && !wcschr(wpath, L'~')) {
604 /*
605 * Shortnames on this filesystem have tildes
606 * and the notification path does not have
607 * one, so we assume that it is a longname.
608 */
609 goto normalize_it;
610 }
611
612 grr = get_relative_longname(watch, wpath, wpath_len,
613 wpath_longname,
614 ARRAY_SIZE(wpath_longname));
615 switch (grr) {
616 case GRR_NO_CONVERSION_NEEDED: /* use info buffer as is */
617 break;
618 case GRR_HAVE_CONVERSION:
619 wpath = wpath_longname;
620 wpath_len = wcslen(wpath);
621 break;
622 default:
623 case GRR_SHUTDOWN:
624 goto force_shutdown;
625 }
626 }
627
628 normalize_it:
629 if (normalize_path_in_utf8(wpath, wpath_len, &path) == -1)
630 goto skip_this_path;
631
632 t = fsmonitor_classify_path_workdir_relative(path.buf);
633
634 process_it:
635 if (process_1_worktree_event(&cookie_list, &batch, &path, t,
636 info->Action))
637 goto force_shutdown;
638
639 skip_this_path:
640 if (!info->NextEntryOffset)
641 break;
642 p += info->NextEntryOffset;
643 }
644
645 fsmonitor_publish(state, batch, &cookie_list);
646 batch = NULL;
647 string_list_clear(&cookie_list, 0);
648 strbuf_release(&path);
649 return LISTENER_HAVE_DATA_WORKTREE;
650
651 force_shutdown:
652 fsmonitor_batch__free_list(batch);
653 string_list_clear(&cookie_list, 0);
654 strbuf_release(&path);
655 return LISTENER_SHUTDOWN;
656 }
657
658 /*
659 * Process filesystem events that happened anywhere (recursively) under the
660 * external <gitdir> (such as non-primary worktrees or submodules).
661 * We only care about cookie files that our client threads created here.
662 *
663 * Note that we DO NOT get filesystem events on the external <gitdir>
664 * itself (it is not inside something that we are watching). In particular,
665 * we do not get an event if the external <gitdir> is deleted.
666 *
667 * Also, we do not care about shortnames within the external <gitdir>, since
668 * we never send these paths to clients.
669 */
670 static int process_gitdir_events(struct fsmonitor_daemon_state *state)
671 {
672 struct fsm_listen_data *data = state->listen_data;
673 struct one_watch *watch = data->watch_gitdir;
674 struct strbuf path = STRBUF_INIT;
675 struct string_list cookie_list = STRING_LIST_INIT_DUP;
676 const char *p = watch->buffer;
677
678 if (!watch->count) {
679 trace2_data_string("fsmonitor", NULL, "fsm-listen/kernel",
680 "overflow");
681 fsmonitor_force_resync(state);
682 return LISTENER_HAVE_DATA_GITDIR;
683 }
684
685 for (;;) {
686 FILE_NOTIFY_INFORMATION *info = (void *)p;
687 const char *slash;
688 enum fsmonitor_path_type t;
689
690 if (normalize_path_in_utf8(
691 info->FileName,
692 info->FileNameLength / sizeof(WCHAR),
693 &path) == -1)
694 goto skip_this_path;
695
696 t = fsmonitor_classify_path_gitdir_relative(path.buf);
697
698 switch (t) {
699 case IS_INSIDE_GITDIR_WITH_COOKIE_PREFIX:
700 /* special case cookie files within gitdir */
701
702 /* Use just the filename of the cookie file. */
703 slash = find_last_dir_sep(path.buf);
704 string_list_append(&cookie_list,
705 slash ? slash + 1 : path.buf);
706 break;
707
708 case IS_INSIDE_GITDIR:
709 goto skip_this_path;
710
711 default:
712 BUG("unexpected path classification '%d' for '%s'",
713 t, path.buf);
714 }
715
716 skip_this_path:
717 if (!info->NextEntryOffset)
718 break;
719 p += info->NextEntryOffset;
720 }
721
722 fsmonitor_publish(state, NULL, &cookie_list);
723 string_list_clear(&cookie_list, 0);
724 strbuf_release(&path);
725 return LISTENER_HAVE_DATA_GITDIR;
726 }
727
728 void fsm_listen__loop(struct fsmonitor_daemon_state *state)
729 {
730 struct fsm_listen_data *data = state->listen_data;
731 DWORD dwWait;
732 int result;
733
734 state->listen_error_code = 0;
735
736 if (start_rdcw_watch(data->watch_worktree) == -1)
737 goto force_error_stop;
738
739 if (data->watch_gitdir &&
740 start_rdcw_watch(data->watch_gitdir) == -1)
741 goto force_error_stop;
742
743 for (;;) {
744 dwWait = WaitForMultipleObjects(data->nr_listener_handles,
745 data->hListener,
746 FALSE, INFINITE);
747
748 if (dwWait == WAIT_OBJECT_0 + LISTENER_HAVE_DATA_WORKTREE) {
749 result = recv_rdcw_watch(data->watch_worktree);
750 if (result == -1) {
751 /* hard error */
752 goto force_error_stop;
753 }
754 if (result == -2) {
755 /* retryable error */
756 if (start_rdcw_watch(data->watch_worktree) == -1)
757 goto force_error_stop;
758 continue;
759 }
760
761 /* have data */
762 if (process_worktree_events(state) == LISTENER_SHUTDOWN)
763 goto force_shutdown;
764 if (start_rdcw_watch(data->watch_worktree) == -1)
765 goto force_error_stop;
766 continue;
767 }
768
769 if (dwWait == WAIT_OBJECT_0 + LISTENER_HAVE_DATA_GITDIR) {
770 result = recv_rdcw_watch(data->watch_gitdir);
771 if (result == -1) {
772 /* hard error */
773 goto force_error_stop;
774 }
775 if (result == -2) {
776 /* retryable error */
777 if (start_rdcw_watch(data->watch_gitdir) == -1)
778 goto force_error_stop;
779 continue;
780 }
781
782 /* have data */
783 if (process_gitdir_events(state) == LISTENER_SHUTDOWN)
784 goto force_shutdown;
785 if (start_rdcw_watch(data->watch_gitdir) == -1)
786 goto force_error_stop;
787 continue;
788 }
789
790 if (dwWait == WAIT_OBJECT_0 + LISTENER_SHUTDOWN)
791 goto clean_shutdown;
792
793 error(_("could not read directory changes [GLE %ld]"),
794 GetLastError());
795 goto force_error_stop;
796 }
797
798 force_error_stop:
799 state->listen_error_code = -1;
800
801 force_shutdown:
802 /*
803 * Tell the IPC thead pool to stop (which completes the await
804 * in the main thread (which will also signal this thread (if
805 * we are still alive))).
806 */
807 ipc_server_stop_async(state->ipc_server_data);
808
809 clean_shutdown:
810 cancel_rdcw_watch(data->watch_worktree);
811 cancel_rdcw_watch(data->watch_gitdir);
812 }
813
814 int fsm_listen__ctor(struct fsmonitor_daemon_state *state)
815 {
816 struct fsm_listen_data *data;
817
818 CALLOC_ARRAY(data, 1);
819
820 data->hEventShutdown = CreateEvent(NULL, TRUE, FALSE, NULL);
821
822 data->watch_worktree = create_watch(state->path_worktree_watch.buf);
823 if (!data->watch_worktree)
824 goto failed;
825
826 check_for_shortnames(data->watch_worktree);
827
828 if (state->nr_paths_watching > 1) {
829 data->watch_gitdir = create_watch(state->path_gitdir_watch.buf);
830 if (!data->watch_gitdir)
831 goto failed;
832 }
833
834 data->hListener[LISTENER_SHUTDOWN] = data->hEventShutdown;
835 data->nr_listener_handles++;
836
837 data->hListener[LISTENER_HAVE_DATA_WORKTREE] =
838 data->watch_worktree->hEvent;
839 data->nr_listener_handles++;
840
841 if (data->watch_gitdir) {
842 data->hListener[LISTENER_HAVE_DATA_GITDIR] =
843 data->watch_gitdir->hEvent;
844 data->nr_listener_handles++;
845 }
846
847 state->listen_data = data;
848 return 0;
849
850 failed:
851 CloseHandle(data->hEventShutdown);
852 destroy_watch(data->watch_worktree);
853 destroy_watch(data->watch_gitdir);
854
855 return -1;
856 }
857
858 void fsm_listen__dtor(struct fsmonitor_daemon_state *state)
859 {
860 struct fsm_listen_data *data;
861
862 if (!state || !state->listen_data)
863 return;
864
865 data = state->listen_data;
866
867 CloseHandle(data->hEventShutdown);
868 destroy_watch(data->watch_worktree);
869 destroy_watch(data->watch_gitdir);
870
871 FREE_AND_NULL(state->listen_data);
872 }