forked from facebook/watchman
-
Notifications
You must be signed in to change notification settings - Fork 0
/
root.c
2770 lines (2309 loc) · 75 KB
/
root.c
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
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* Copyright 2012-present Facebook, Inc.
* Licensed under the Apache License, Version 2.0 */
#include "watchman.h"
#ifdef __APPLE__
# include <sys/attr.h>
#endif
static struct watchman_ops *watcher_ops = NULL;
static watchman_global_watcher_t watcher = NULL;
static w_ht_t *watched_roots = NULL;
static volatile long live_roots = 0;
static pthread_mutex_t root_lock = PTHREAD_MUTEX_INITIALIZER;
// Each root gets a number that uniquely identifies it within the process. This
// helps avoid confusion if a root is removed and then added again.
static long next_root_number = 1;
/* Some error conditions will put us into a non-recoverable state where we
* can't guarantee that we will be operating correctly. Rather than suffering
* in silence and misleading our clients, we'll poison ourselves and advertise
* that we have done so and provide some advice on how the user can cure us. */
char *poisoned_reason = NULL;
static void crawler(w_root_t *root, struct watchman_pending_collection *coll,
w_string_t *dir_name, struct timeval now, bool recursive);
static void w_root_teardown(w_root_t *root);
static void delete_trigger(w_ht_val_t val)
{
struct watchman_trigger_command *cmd = w_ht_val_ptr(val);
w_trigger_command_free(cmd);
}
static const struct watchman_hash_funcs trigger_hash_funcs = {
w_ht_string_copy,
w_ht_string_del,
w_ht_string_equal,
w_ht_string_hash,
NULL,
delete_trigger
};
static void delete_dir(w_ht_val_t val)
{
struct watchman_dir *dir = w_ht_val_ptr(val);
w_log(W_LOG_DBG, "delete_dir(%.*s)\n", dir->path->len, dir->path->buf);
w_string_delref(dir->path);
dir->path = NULL;
if (dir->files) {
w_ht_free(dir->files);
dir->files = NULL;
}
if (dir->dirs) {
w_ht_free(dir->dirs);
dir->dirs = NULL;
}
free(dir);
}
static const struct watchman_hash_funcs dirname_hash_funcs = {
w_ht_string_copy,
w_ht_string_del,
w_ht_string_equal,
w_ht_string_hash,
NULL,
delete_dir
};
static void load_root_config(w_root_t *root, const char *path)
{
char cfgfilename[WATCHMAN_NAME_MAX];
json_error_t err;
snprintf(cfgfilename, sizeof(cfgfilename), "%s%c.watchmanconfig",
path, WATCHMAN_DIR_SEP);
if (!w_path_exists(cfgfilename)) {
if (errno == ENOENT) {
return;
}
w_log(W_LOG_ERR, "%s is not accessible: %s\n",
cfgfilename, strerror(errno));
return;
}
root->config_file = json_load_file(cfgfilename, 0, &err);
if (!root->config_file) {
w_log(W_LOG_ERR, "failed to parse json from %s: %s\n",
cfgfilename, err.text);
}
}
static size_t root_init_offset = offsetof(w_root_t, _init_sentinel_);
// internal initialization for root
static bool w_root_init(w_root_t *root, char **errmsg)
{
struct watchman_dir *dir;
struct watchman_dir_handle *osdir;
memset((char *)root + root_init_offset, 0,
sizeof(w_root_t) - root_init_offset);
osdir = w_dir_open(root->root_path->buf);
if (!osdir) {
ignore_result(asprintf(errmsg, "failed to opendir(%s): %s",
root->root_path->buf,
strerror(errno)));
return false;
}
w_dir_close(osdir);
if (!watcher_ops->root_init(watcher, root, errmsg)) {
return false;
}
root->number = __sync_fetch_and_add(&next_root_number, 1);
root->cursors = w_ht_new(2, &w_ht_string_funcs);
root->suffixes = w_ht_new(2, &w_ht_string_funcs);
root->dirname_to_dir = w_ht_new(HINT_NUM_DIRS, &dirname_hash_funcs);
root->ticks = 1;
// "manually" populate the initial dir, as the dir resolver will
// try to find its parent and we don't want it to for the root
dir = calloc(1, sizeof(*dir));
dir->path = root->root_path;
w_string_addref(dir->path);
w_ht_set(root->dirname_to_dir, w_ht_ptr_val(dir->path), w_ht_ptr_val(dir));
time(&root->last_cmd_timestamp);
return root;
}
static json_t *config_get_ignore_vcs(w_root_t *root)
{
json_t *ignores = cfg_get_json(root, "ignore_vcs");
if (ignores && !json_is_array(ignores)) {
return NULL;
}
if (ignores) {
// incref so that the caller can simply decref whatever we return
json_incref(ignores);
} else {
// default to a well-known set of vcs's
ignores = json_pack("[sss]", ".git", ".svn", ".hg");
}
return ignores;
}
static bool apply_ignore_vcs_configuration(w_root_t *root, char **errmsg)
{
w_string_t *name;
w_string_t *fullname;
uint8_t i;
json_t *ignores;
char hostname[256];
struct stat st;
ignores = config_get_ignore_vcs(root);
if (!ignores) {
ignore_result(asprintf(errmsg, "ignore_vcs must be an array of strings"));
return false;
}
for (i = 0; i < json_array_size(ignores); i++) {
const char *ignore = json_string_value(json_array_get(ignores, i));
if (!ignore) {
ignore_result(asprintf(errmsg,
"ignore_vcs must be an array of strings"));
json_decref(ignores);
return false;
}
name = w_string_new(ignore);
fullname = w_string_path_cat(root->root_path, name);
// if we are completely ignoring this dir, we have nothing more to
// do here
if (w_ht_get(root->ignore_dirs, w_ht_ptr_val(fullname))) {
w_string_delref(fullname);
w_string_delref(name);
continue;
}
w_ht_set(root->ignore_vcs, w_ht_ptr_val(fullname),
w_ht_ptr_val(fullname));
// While we're at it, see if we can find out where to put our
// query cookie information
if (root->query_cookie_dir == NULL &&
w_lstat(fullname->buf, &st, root->case_sensitive) == 0 &&
S_ISDIR(st.st_mode)) {
// root/{.hg,.git,.svn}
root->query_cookie_dir = w_string_path_cat(root->root_path, name);
}
w_string_delref(name);
w_string_delref(fullname);
}
json_decref(ignores);
if (root->query_cookie_dir == NULL) {
w_string_addref(root->root_path);
root->query_cookie_dir = root->root_path;
}
gethostname(hostname, sizeof(hostname));
hostname[sizeof(hostname) - 1] = '\0';
root->query_cookie_prefix = w_string_make_printf(
"%.*s%c" WATCHMAN_COOKIE_PREFIX "%s-%d-", root->query_cookie_dir->len,
root->query_cookie_dir->buf, WATCHMAN_DIR_SEP, hostname, (int)getpid());
return true;
}
static void apply_ignore_configuration(w_root_t *root)
{
w_string_t *name;
w_string_t *fullname;
uint8_t i;
json_t *ignores;
ignores = cfg_get_json(root, "ignore_dirs");
if (!ignores) {
return;
}
if (!json_is_array(ignores)) {
w_log(W_LOG_ERR, "ignore_dirs must be an array of strings\n");
return;
}
for (i = 0; i < json_array_size(ignores); i++) {
const char *ignore = json_string_value(json_array_get(ignores, i));
if (!ignore) {
w_log(W_LOG_ERR, "ignore_dirs must be an array of strings\n");
continue;
}
name = w_string_new(ignore);
fullname = w_string_path_cat(root->root_path, name);
w_ht_set(root->ignore_dirs, w_ht_ptr_val(fullname),
w_ht_ptr_val(fullname));
w_log(W_LOG_DBG, "ignoring %.*s recursively\n",
fullname->len, fullname->buf);
w_string_delref(fullname);
w_string_delref(name);
}
}
static bool is_case_sensitive_filesystem(const char *path) {
#ifdef __APPLE__
return pathconf(path, _PC_CASE_SENSITIVE);
#elif defined(_WIN32)
unused_parameter(path);
return false;
#else
unused_parameter(path);
return true;
#endif
}
static w_root_t *w_root_new(const char *path, char **errmsg)
{
w_root_t *root = calloc(1, sizeof(*root));
pthread_mutexattr_t attr;
assert(root != NULL);
root->refcnt = 1;
w_refcnt_add(&live_roots);
pthread_mutexattr_init(&attr);
pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
pthread_mutex_init(&root->lock, &attr);
pthread_mutexattr_destroy(&attr);
root->case_sensitive = is_case_sensitive_filesystem(path);
w_pending_coll_init(&root->pending);
root->root_path = w_string_new(path);
root->commands = w_ht_new(2, &trigger_hash_funcs);
root->query_cookies = w_ht_new(2, &w_ht_string_funcs);
root->ignore_vcs = w_ht_new(2, &w_ht_string_funcs);
root->ignore_dirs = w_ht_new(2, &w_ht_string_funcs);
load_root_config(root, path);
root->trigger_settle = (int)cfg_get_int(
root, "settle", DEFAULT_SETTLE_PERIOD);
root->gc_age = (int)cfg_get_int(root, "gc_age_seconds", DEFAULT_GC_AGE);
root->gc_interval = (int)cfg_get_int(root, "gc_interval_seconds",
DEFAULT_GC_INTERVAL);
root->idle_reap_age = (int)cfg_get_int(root, "idle_reap_age_seconds",
DEFAULT_REAP_AGE);
apply_ignore_configuration(root);
if (!apply_ignore_vcs_configuration(root, errmsg)) {
w_root_delref(root);
return NULL;
}
if (!w_root_init(root, errmsg)) {
w_root_delref(root);
return NULL;
}
return root;
}
void w_root_lock(w_root_t *root)
{
int err;
err = pthread_mutex_lock(&root->lock);
if (err != 0) {
w_log(W_LOG_FATAL, "lock [%.*s]: %s\n",
root->root_path->len,
root->root_path->buf,
strerror(err)
);
}
}
void w_root_unlock(w_root_t *root)
{
int err;
err = pthread_mutex_unlock(&root->lock);
if (err != 0) {
w_log(W_LOG_FATAL, "lock: [%.*s] %s\n",
root->root_path->len,
root->root_path->buf,
strerror(err)
);
}
}
void w_timeoutms_to_abs_timespec(int timeoutms, struct timespec *deadline) {
struct timeval now, delta, target;
/* compute deadline */
gettimeofday(&now, NULL);
delta.tv_sec = timeoutms / 1000;
delta.tv_usec = (timeoutms - (delta.tv_sec * 1000)) * 1000;
w_timeval_add(now, delta, &target);
w_timeval_to_timespec(target, deadline);
}
/* Ensure that we're synchronized with the state of the
* filesystem at the current time.
* We do this by touching a cookie file and waiting to
* observe it via inotify. When we see it we know that
* we've seen everything up to the point in time at which
* we're asking questions.
* Returns true if we observe the change within the requested
* time, false otherwise.
* Must be called with the root UNLOCKED. This function
* will acquire and release the root lock.
*/
bool w_root_sync_to_now(w_root_t *root, int timeoutms)
{
uint32_t tick;
struct watchman_query_cookie cookie;
w_string_t *path_str;
w_stm_t file;
int errcode = 0;
struct timespec deadline;
if (pthread_cond_init(&cookie.cond, NULL)) {
errcode = errno;
w_log(W_LOG_ERR, "sync_to_now: cond_init failed: %s\n", strerror(errcode));
errno = errcode;
return false;
}
cookie.seen = false;
/* generate a cookie name: cookie prefix + id */
w_root_lock(root);
tick = root->ticks++;
path_str = w_string_make_printf("%.*s%" PRIu32 "-%" PRIu32,
root->query_cookie_prefix->len,
root->query_cookie_prefix->buf,
root->number, tick);
/* insert our cookie in the map */
w_ht_set(root->query_cookies, w_ht_ptr_val(path_str),
w_ht_ptr_val(&cookie));
/* touch the file */
file = w_stm_open(path_str->buf, O_CREAT|O_TRUNC|O_WRONLY|O_CLOEXEC, 0700);
if (!file) {
errcode = errno;
w_log(W_LOG_ERR, "sync_to_now: creat(%s) failed: %s\n",
path_str->buf, strerror(errcode));
goto out;
}
w_stm_close(file);
/* compute deadline */
w_timeoutms_to_abs_timespec(timeoutms, &deadline);
w_log(W_LOG_DBG, "sync_to_now [%s] waiting\n", path_str->buf);
/* timed cond wait (unlocks root lock, reacquires) */
while (!cookie.seen) {
errcode = pthread_cond_timedwait(&cookie.cond, &root->lock, &deadline);
if (errcode && !cookie.seen) {
w_log(W_LOG_ERR,
"sync_to_now: %s timedwait failed: %d: istimeout=%d %s\n",
path_str->buf, errcode, errcode == ETIMEDOUT, strerror(errcode));
goto out;
}
}
w_log(W_LOG_DBG, "sync_to_now [%s] done\n", path_str->buf);
out:
// can't unlink the file until after the cookie has been observed because
// we don't know which file got changed until we look in the cookie dir
unlink(path_str->buf);
w_ht_del(root->query_cookies, w_ht_ptr_val(path_str));
w_root_unlock(root);
w_string_delref(path_str);
pthread_cond_destroy(&cookie.cond);
if (!cookie.seen) {
errno = errcode;
return false;
}
return true;
}
bool w_root_process_pending(w_root_t *root,
struct watchman_pending_collection *coll,
bool pull_from_root)
{
struct watchman_pending_fs *p, *pending;
if (pull_from_root) {
// You MUST own root->pending lock for this
w_pending_coll_append(coll, &root->pending);
}
if (!coll->pending) {
return false;
}
w_log(W_LOG_DBG, "processing %d events in %s\n",
w_ht_size(coll->pending_uniq), root->root_path->buf);
// Steal the contents
pending = coll->pending;
coll->pending = NULL;
w_ht_free_entries(coll->pending_uniq);
while (pending) {
p = pending;
pending = p->next;
if (!root->cancelled) {
w_root_process_path(root, coll, p->path, p->now, p->flags, NULL);
}
w_pending_fs_free(p);
}
return true;
}
struct watchman_dir *w_root_resolve_dir(w_root_t *root,
w_string_t *dir_name, bool create)
{
struct watchman_dir *dir, *parent;
w_string_t *parent_name;
dir = w_ht_val_ptr(w_ht_get(root->dirname_to_dir, w_ht_ptr_val(dir_name)));
if (dir || !create) {
return dir;
}
parent_name = w_string_dirname(dir_name);
parent = w_root_resolve_dir(root, parent_name, create);
w_string_delref(parent_name);
assert(parent != NULL);
dir = calloc(1, sizeof(*dir));
dir->path = dir_name;
w_string_addref(dir->path);
if (!parent->dirs) {
parent->dirs = w_ht_new(2, &w_ht_string_funcs);
}
assert(w_ht_set(parent->dirs, w_ht_ptr_val(dir_name), w_ht_ptr_val(dir)));
assert(w_ht_set(root->dirname_to_dir,
w_ht_ptr_val(dir_name), w_ht_ptr_val(dir)));
return dir;
}
static void apply_dir_size_hint(struct watchman_dir *dir,
uint32_t ndirs, uint32_t nfiles) {
if (nfiles > 0) {
if (!dir->files) {
dir->files = w_ht_new(nfiles, &w_ht_string_funcs);
}
}
if (!dir->dirs && ndirs > 0) {
dir->dirs = w_ht_new(ndirs, &w_ht_string_funcs);
}
}
static void watch_file(w_root_t *root, struct watchman_file *file)
{
watcher_ops->root_start_watch_file(watcher, root, file);
}
static void stop_watching_file(w_root_t *root, struct watchman_file *file)
{
watcher_ops->root_stop_watch_file(watcher, root, file);
}
static void remove_from_file_list(w_root_t *root, struct watchman_file *file)
{
if (root->latest_file == file) {
root->latest_file = file->next;
}
if (file->next) {
file->next->prev = file->prev;
}
if (file->prev) {
file->prev->next = file->next;
}
}
static void remove_from_suffix_list(w_root_t *root, struct watchman_file *file)
{
w_string_t *suffix = w_string_suffix(file->name);
struct watchman_file *sufhead;
if (!suffix) {
return;
}
sufhead = w_ht_val_ptr(w_ht_get(root->suffixes, w_ht_ptr_val(suffix)));
if (sufhead) {
if (file->suffix_prev) {
file->suffix_prev->suffix_next = file->suffix_next;
}
if (file->suffix_next) {
file->suffix_next->suffix_prev = file->suffix_prev;
}
if (sufhead == file) {
sufhead = file->suffix_next;
w_ht_replace(root->suffixes, w_ht_ptr_val(suffix),
w_ht_ptr_val(sufhead));
}
}
w_string_delref(suffix);
}
void w_root_mark_file_changed(w_root_t *root, struct watchman_file *file,
struct timeval now)
{
if (file->exists) {
watch_file(root, file);
} else {
stop_watching_file(root, file);
}
file->otime.tv = now;
file->otime.ticks = root->ticks;
if (root->latest_file != file) {
// unlink from list
remove_from_file_list(root, file);
// and move to the head
file->next = root->latest_file;
if (file->next) {
file->next->prev = file;
}
file->prev = NULL;
root->latest_file = file;
}
// Flag that we have pending trigger info
root->pending_trigger_tick = root->ticks;
root->pending_sub_tick = root->ticks;
}
struct watchman_file *w_root_resolve_file(w_root_t *root,
struct watchman_dir *dir, w_string_t *file_name,
struct timeval now)
{
struct watchman_file *file, *sufhead;
w_string_t *suffix;
if (dir->files) {
file = w_ht_val_ptr(w_ht_get(dir->files, w_ht_ptr_val(file_name)));
if (file) {
return file;
}
} else {
dir->files = w_ht_new(2, &w_ht_string_funcs);
}
file = calloc(1, sizeof(*file));
file->name = file_name;
w_string_addref(file->name);
file->parent = dir;
file->exists = true;
file->ctime.ticks = root->ticks;
file->ctime.tv = now;
suffix = w_string_suffix(file_name);
if (suffix) {
sufhead = w_ht_val_ptr(w_ht_get(root->suffixes, w_ht_ptr_val(suffix)));
file->suffix_next = sufhead;
if (sufhead) {
sufhead->suffix_prev = file;
}
w_ht_replace(root->suffixes, w_ht_ptr_val(suffix), w_ht_ptr_val(file));
w_string_delref(suffix);
}
w_ht_set(dir->files, w_ht_ptr_val(file->name), w_ht_ptr_val(file));
watch_file(root, file);
return file;
}
void stop_watching_dir(w_root_t *root, struct watchman_dir *dir)
{
w_ht_iter_t i;
w_log(W_LOG_DBG, "stop_watching_dir %.*s\n",
dir->path->len, dir->path->buf);
if (w_ht_first(dir->dirs, &i)) do {
struct watchman_dir *child = w_ht_val_ptr(i.value);
stop_watching_dir(root, child);
} while (w_ht_next(dir->dirs, &i));
watcher_ops->root_stop_watch_dir(watcher, root, dir);
}
static bool did_file_change(struct watchman_stat *saved,
struct watchman_stat *fresh)
{
/* we have to compare this way because the stat structure
* may contain fields that vary and that don't impact our
* understanding of the file */
#define FIELD_CHG(name) \
if (saved->name != fresh->name) { \
return true; \
}
// Can't compare with memcmp due to padding and garbage in the struct
// on OpenBSD, which has a 32-bit tv_sec + 64-bit tv_nsec
#define TIMESPEC_FIELD_CHG(wat) { \
struct timespec a = saved->wat##time; \
struct timespec b = fresh->wat##time; \
if (a.tv_sec != b.tv_sec || a.tv_nsec != b.tv_nsec) { \
return true; \
} \
}
FIELD_CHG(mode);
if (!S_ISDIR(saved->mode)) {
FIELD_CHG(size);
FIELD_CHG(nlink);
}
FIELD_CHG(dev);
FIELD_CHG(ino);
FIELD_CHG(uid);
FIELD_CHG(gid);
// Don't care about st_blocks
// Don't care about st_blksize
// Don't care about st_atimespec
TIMESPEC_FIELD_CHG(m);
TIMESPEC_FIELD_CHG(c);
return false;
}
// POSIX says open with O_NOFOLLOW should set errno to ELOOP if the path is a
// symlink. However, FreeBSD (which ironically originated O_NOFOLLOW) sets it to
// EMLINK.
#ifdef __FreeBSD__
#define ENOFOLLOWSYMLINK EMLINK
#else
#define ENOFOLLOWSYMLINK ELOOP
#endif
static void struct_stat_to_watchman_stat(const struct stat *st,
struct watchman_stat *target) {
target->size = (off_t)st->st_size;
target->mode = st->st_mode;
target->uid = st->st_uid;
target->gid = st->st_gid;
target->ino = st->st_ino;
target->dev = st->st_dev;
target->nlink = st->st_nlink;
memcpy(&target->atime, &st->WATCHMAN_ST_TIMESPEC(a),
sizeof(target->atime));
memcpy(&target->mtime, &st->WATCHMAN_ST_TIMESPEC(m),
sizeof(target->mtime));
memcpy(&target->ctime, &st->WATCHMAN_ST_TIMESPEC(c),
sizeof(target->ctime));
}
static void stat_path(w_root_t *root,
struct watchman_pending_collection *coll, w_string_t *full_path,
struct timeval now,
int flags,
struct watchman_dir_ent *pre_stat)
{
struct watchman_stat st;
int res, err;
char path[WATCHMAN_NAME_MAX];
struct watchman_dir *dir;
struct watchman_dir *dir_ent = NULL;
struct watchman_file *file = NULL;
w_string_t *dir_name;
w_string_t *file_name;
bool recursive = flags & W_PENDING_RECURSIVE;
bool via_notify = flags & W_PENDING_VIA_NOTIFY;
if (w_ht_get(root->ignore_dirs, w_ht_ptr_val(full_path))) {
w_log(W_LOG_DBG, "%.*s matches ignore_dir rules\n",
full_path->len, full_path->buf);
return;
}
if (full_path->len > sizeof(path)-1) {
w_log(W_LOG_FATAL, "path %.*s is too big\n",
full_path->len, full_path->buf);
}
memcpy(path, full_path->buf, full_path->len);
path[full_path->len] = 0;
dir_name = w_string_dirname(full_path);
file_name = w_string_basename(full_path);
dir = w_root_resolve_dir(root, dir_name, true);
if (dir->files) {
file = w_ht_val_ptr(w_ht_get(dir->files, w_ht_ptr_val(file_name)));
}
if (dir->dirs) {
dir_ent = w_ht_val_ptr(w_ht_get(dir->dirs, w_ht_ptr_val(full_path)));
}
if (pre_stat && pre_stat->has_stat) {
memcpy(&st, &pre_stat->stat, sizeof(st));
res = 0;
err = 0;
} else {
struct stat struct_stat;
res = w_lstat(path, &struct_stat, root->case_sensitive);
err = res == 0 ? 0 : errno;
w_log(W_LOG_DBG, "w_lstat(%s) file=%p dir=%p res=%d %s\n",
path, file, dir_ent, res, strerror(err));
if (err == 0) {
struct_stat_to_watchman_stat(&struct_stat, &st);
} else {
// To suppress warning on win32
memset(&st, 0, sizeof(st));
}
}
if (res && (err == ENOENT || err == ENOTDIR)) {
/* it's not there, update our state */
if (dir_ent) {
w_root_mark_deleted(root, dir_ent, now, true);
w_log(W_LOG_DBG, "w_lstat(%s) -> %s so stopping watch on %.*s\n", path,
strerror(err), dir_ent->path->len, dir_ent->path->buf);
stop_watching_dir(root, dir_ent);
}
if (file) {
if (file->exists) {
w_log(W_LOG_DBG, "w_lstat(%s) -> %s so marking %.*s deleted\n",
path, strerror(err), file->name->len, file->name->buf);
file->exists = false;
w_root_mark_file_changed(root, file, now);
}
} else {
// It was created and removed before we could ever observe it
// in the filesystem. We need to generate a deleted file
// representation of it now, so that subscription clients can
// be notified of this event
file = w_root_resolve_file(root, dir, file_name, now);
w_log(W_LOG_DBG, "w_lstat(%s) -> %s and file node was NULL. "
"Generating a deleted node.\n", path, strerror(err));
file->exists = false;
w_root_mark_file_changed(root, file, now);
}
if (!root->case_sensitive &&
!w_string_equal(dir_name, root->root_path)) {
/* If we rejected the name because it wasn't canonical,
* we need to ensure that we look in the parent dir to discover
* the new item(s) */
w_log(W_LOG_DBG, "we're case insensitive, and %s is ENOENT, "
"speculatively look at parent dir %.*s\n",
path, dir_name->len, dir_name->buf);
stat_path(root, coll, dir_name, now, 0, NULL);
}
} else if (res) {
w_log(W_LOG_ERR, "w_lstat(%s) %d %s\n",
path, err, strerror(err));
} else {
if (!file) {
file = w_root_resolve_file(root, dir, file_name, now);
}
if (!file->exists) {
/* we're transitioning from deleted to existing,
* so we're effectively new again */
file->ctime.ticks = root->ticks;
file->ctime.tv = now;
/* if a dir was deleted and now exists again, we want
* to crawl it again */
recursive = true;
}
if (!file->exists || via_notify || did_file_change(&file->stat, &st)) {
w_log(W_LOG_DBG,
"file changed exists=%d via_notify=%d stat-changed=%d isdir=%d %s\n",
(int)file->exists,
(int)via_notify,
(int)(file->exists && !via_notify),
S_ISDIR(st.mode),
path
);
file->exists = true;
w_root_mark_file_changed(root, file, now);
}
memcpy(&file->stat, &st, sizeof(file->stat));
if (S_ISDIR(st.mode)) {
if (dir_ent == NULL) {
recursive = true;
}
// Don't recurse if our parent is an ignore dir
if (!w_ht_get(root->ignore_vcs, w_ht_ptr_val(dir_name)) ||
// but do if we're looking at the cookie dir (stat_path is never
// called for the root itself)
w_string_equal(full_path, root->query_cookie_dir)) {
if (!watcher_ops->flags & WATCHER_HAS_PER_FILE_NOTIFICATIONS) {
/* we always need to crawl, but may not need to be fully recursive */
w_pending_coll_add(coll, full_path, now,
W_PENDING_CRAWL_ONLY | (recursive ? W_PENDING_RECURSIVE : 0));
} else {
/* we get told about changes on the child, so we only
* need to crawl if we've never seen the dir before.
* An exception is that fsevents will only report the root
* of a dir rename and not a rename event for all of its
* children. */
if (recursive) {
w_pending_coll_add(coll, full_path, now,
W_PENDING_RECURSIVE|W_PENDING_CRAWL_ONLY);
}
}
}
} else if (dir_ent) {
// We transitioned from dir to file (see fishy.php), so we should prune
// our former tree here
w_root_mark_deleted(root, dir_ent, now, true);
}
if ((watcher_ops->flags & WATCHER_HAS_PER_FILE_NOTIFICATIONS) &&
!S_ISDIR(st.mode) &&
!w_string_equal(dir_name, root->root_path)) {
/* Make sure we update the mtime on the parent directory. */
stat_path(root, coll, dir_name, now, flags & W_PENDING_VIA_NOTIFY, NULL);
}
}
// out is only used on some platforms, so on others compilers will complain
// about it being unused
goto out;
out:
w_string_delref(dir_name);
w_string_delref(file_name);
}
void w_root_process_path(w_root_t *root,
struct watchman_pending_collection *coll, w_string_t *full_path,
struct timeval now, int flags,
struct watchman_dir_ent *pre_stat)
{
/* From a particular query's point of view, there are four sorts of cookies we
* can observe:
* 1. Cookies that this query has created. This marks the end of this query's
* sync_to_now, so we hide it from the results.
* 2. Cookies that another query on the same watch by the same process has
* created. This marks the end of that other query's sync_to_now, so from
* the point of view of this query we turn a blind eye to it.
* 3. Cookies created by another process on the same watch. We're independent
* of other processes, so we report these.
* 4. Cookies created by a nested watch by the same or a different process.
* We're independent of other watches, so we report these.
*
* The below condition is true for cases 1 and 2 and false for 3 and 4.
*/
if (w_string_startswith(full_path, root->query_cookie_prefix)) {
struct watchman_query_cookie *cookie;
bool consider_cookie =
(watcher_ops->flags & WATCHER_HAS_PER_FILE_NOTIFICATIONS) ?
((flags & W_PENDING_VIA_NOTIFY) || !root->done_initial) : true;
if (!consider_cookie) {
// Never allow cookie files to show up in the tree
return;
}
cookie = w_ht_val_ptr(w_ht_get(root->query_cookies,
w_ht_ptr_val(full_path)));
w_log(W_LOG_DBG, "cookie! %.*s cookie=%p\n",
full_path->len, full_path->buf, cookie);
if (cookie) {
cookie->seen = true;
pthread_cond_signal(&cookie->cond);
}
// Never allow cookie files to show up in the tree
return;
}
if (w_string_equal(full_path, root->root_path)
|| (flags & W_PENDING_CRAWL_ONLY) == W_PENDING_CRAWL_ONLY) {
crawler(root, coll, full_path, now,
(flags & W_PENDING_RECURSIVE) == W_PENDING_RECURSIVE);
} else {
stat_path(root, coll, full_path, now, flags, pre_stat);
}
}
/* recursively mark the dir contents as deleted */
void w_root_mark_deleted(w_root_t *root, struct watchman_dir *dir,
struct timeval now, bool recursive)
{
w_ht_iter_t i;
if (w_ht_first(dir->files, &i)) do {
struct watchman_file *file = w_ht_val_ptr(i.value);
if (file->exists) {
w_log(W_LOG_DBG, "mark_deleted: %.*s%c%.*s\n",
dir->path->len, dir->path->buf,
WATCHMAN_DIR_SEP,
file->name->len, file->name->buf);
file->exists = false;
w_root_mark_file_changed(root, file, now);
}
} while (w_ht_next(dir->files, &i));
if (recursive && w_ht_first(dir->dirs, &i)) do {
struct watchman_dir *child = w_ht_val_ptr(i.value);
w_root_mark_deleted(root, child, now, true);
} while (w_ht_next(dir->dirs, &i));
}
void handle_open_errno(w_root_t *root, struct watchman_dir *dir,
struct timeval now, const char *syscall, int err, const char *reason)
{
w_string_t *dir_name = dir->path;
w_string_t *warn = NULL;
bool log_warning = true;
bool transient = false;
if (err == ENOENT || err == ENOTDIR || err == ENOFOLLOWSYMLINK) {
log_warning = false;
transient = false;
} else if (err == EACCES || err == EPERM) {