forked from twitter/libwatchman
-
Notifications
You must be signed in to change notification settings - Fork 0
/
watchman.c
1577 lines (1415 loc) · 46 KB
/
watchman.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
#include "watchman.h"
#include <assert.h>
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <limits.h>
#include <stdlib.h>
#include <string.h>
#include <sys/select.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <sys/un.h>
#include <sys/wait.h>
#include <unistd.h>
#include <jansson.h>
#include "bser_write.h"
#include "proto.h"
static int use_bser_encoding = 0;
/* It's safe to have a small buffer here because watchman's socket name
* is guaranteed to be under 108 bytes (see sockaddr_un). The JSON only has
* sockname and version fields.
*/
#define WATCHMAN_GET_SOCKNAME_MAX 1024
static void watchman_err(struct watchman_error *error,
enum watchman_error_code code,
const char *message, ...)
__attribute__ ((format(printf, 3, 4)));
static void
watchman_err(struct watchman_error *error, enum watchman_error_code code,
const char *message, ...)
{
if (!error)
return;
va_list argptr;
va_start(argptr, message);
char c;
int len = vsnprintf(&c, 1, message, argptr);
va_end(argptr);
error->message = malloc(len + 1);
error->code = code;
error->err_no = errno;
va_start(argptr, message);
vsnprintf(error->message, len + 1, message, argptr);
va_end(argptr);
}
static int unix_stream_socket(void)
{
int fd = socket(AF_UNIX, SOCK_STREAM, 0);
return fd;
}
static int chdir_len(const char *orig, int len)
{
char *path = malloc(len + 1);
memcpy(path, orig, len);
path[len] = 0;
int r = chdir(path);
free(path);
return r;
}
struct unix_sockaddr_context {
char *orig_dir;
};
static char *getcwd_alloc(void)
{
int buflen = PATH_MAX;
char *buf = NULL;
while (1) {
buf = realloc(buf, buflen);
if (getcwd(buf, buflen)) {
break;
}
if (errno != ERANGE) {
return NULL;
}
buflen *= 2;
}
return buf;
}
static int unix_sockaddr_init(struct sockaddr_un *sa, const char *path,
struct unix_sockaddr_context *ctx)
{
size_t size = strlen(path) + 1;
ctx->orig_dir = NULL;
if (size > sizeof(sa->sun_path)) {
const char *slash = strrchr(path, '/');
const char *dir;
if (!slash) {
errno = ENAMETOOLONG;
return -1;
}
dir = path;
path = slash + 1;
size = strlen(path) + 1;
if (size > sizeof(sa->sun_path)) {
errno = ENAMETOOLONG;
return -1;
}
char *cwd = getcwd_alloc();
if (!cwd) {
return -1;
}
ctx->orig_dir = cwd;
if (chdir_len(dir, slash - dir) < 0) {
free(cwd);
return -1;
}
}
memset(sa, 0, sizeof(*sa));
sa->sun_family = AF_UNIX;
memcpy(sa->sun_path, path, size);
return 0;
}
static int unix_sockaddr_cleanup(struct unix_sockaddr_context *ctx, struct watchman_error *error)
{
int ret = 0;
if (!ctx->orig_dir) {
return 0;
}
/*
* If we fail, we have moved the cwd of the whole process, which
* could confuse calling code. But we don't want to just die, because
* libraries shouldn't do that. So we'll return an error but be
* sad about it.
*/
if (chdir(ctx->orig_dir) < 0) {
watchman_err(error, WATCHMAN_ERR_CWD,
"unable to restore original working directory");
ret = -1;
}
free(ctx->orig_dir);
return ret;
}
static int unix_stream_connect(const char *path, struct watchman_error *error)
{
struct sockaddr_un sa;
struct unix_sockaddr_context ctx;
if (unix_sockaddr_init(&sa, path, &ctx) < 0) {
return -1;
}
int fd = unix_stream_socket();
if (fd < 0) {
return -1;
}
if (connect(fd, (struct sockaddr *)&sa, sizeof(sa)) < 0) {
watchman_err(error, WATCHMAN_ERR_CONNECT, "Connect error %s",
strerror(errno));
if (unix_sockaddr_cleanup(&ctx, error)) {
error->code |= WATCHMAN_ERR_CWD;
}
close(fd);
return -1;
}
if (unix_sockaddr_cleanup(&ctx, error)) {
close(fd);
return -1;
}
return fd;
}
static struct watchman_connection *
watchman_sock_connect(const char *sockname, struct timeval timeout, struct watchman_error *error)
{
use_bser_encoding = getenv("LIBWATCHMAN_USE_JSON_PROTOCOL") == NULL;
if (getenv("LIBWATCHMAN_TRACE_WATCHMAN") != NULL) {
fprintf(stderr, "Using bser encoding: %s\n", use_bser_encoding ? "yes" : "no");
}
int fd;
error->message = NULL;
fd = unix_stream_connect(sockname, error);
if (fd < 0 && !error->message) {
watchman_err(error, WATCHMAN_ERR_CONNECT, "Connect error %s",
strerror(errno));
return NULL;
}
if (setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout))) {
watchman_err(error, WATCHMAN_ERR_CONNECT, "Failed to set timeout %s",
strerror(errno));
return NULL;
}
if (setsockopt(fd, SOL_SOCKET, SO_SNDTIMEO, &timeout, sizeof(timeout))) {
watchman_err(error, WATCHMAN_ERR_CONNECT, "Failed to set timeout %s",
strerror(errno));
return NULL;
}
FILE *sockfp = fdopen(fd, "r+");
if (!sockfp) {
close(fd);
watchman_err(error, WATCHMAN_ERR_OTHER,
"Failed to connect to watchman socket %s: %s.",
sockname, strerror(errno));
return NULL;
}
setlinebuf(sockfp);
struct watchman_connection *conn = malloc(sizeof(*conn));
conn->fp = sockfp;
return conn;
}
struct watchman_popen {
int fd;
int pid;
};
#define WATCHMAN_EXEC_FAILED 241
#define WATCHMAN_EXEC_INTERNAL_ERROR 242
static const char* get_sockname_msg = "Could not run watchman get-sockname: %s";
/* Runs watchman get-sockname and returns a FILE from which the output
can be read. */
static struct watchman_popen *watchman_popen_getsockname(struct watchman_error *error)
{
int pipefd[2];
static struct watchman_popen ret = {0, 0};
if (pipe(pipefd) < 0) {
goto fail;
}
pid_t pid = fork();
if (pid < 0) {
goto fail;
} else if (pid == 0) {
if (dup2(pipefd[1], 1) < 0) {
exit(WATCHMAN_EXEC_INTERNAL_ERROR);
}
int devnull_fh = open("/dev/null", O_RDWR);
if (devnull_fh < 0) {
exit(WATCHMAN_EXEC_INTERNAL_ERROR);
}
if (dup2(devnull_fh, 2) < 0) {
exit(WATCHMAN_EXEC_INTERNAL_ERROR);
}
execlp("watchman", "watchman", "get-sockname", (char *) NULL);
exit(WATCHMAN_EXEC_FAILED);
} else {
close(pipefd[1]);
ret.fd = pipefd[0];
ret.pid = pid;
return &ret;
}
fail:
watchman_err(error, WATCHMAN_ERR_OTHER, get_sockname_msg, strerror(errno));
return NULL;
}
int watchman_pclose(struct watchman_error *error, struct watchman_popen *popen)
{
close(popen->fd);
int status;
int pid = waitpid(popen->pid, &status, 0);
if (pid < 0) {
watchman_err(error, WATCHMAN_ERR_RUN_WATCHMAN, get_sockname_msg,
strerror(errno));
return -1;
}
switch(WEXITSTATUS(status)) {
case 0:
return 0;
case WATCHMAN_EXEC_FAILED:
watchman_err(error, WATCHMAN_ERR_RUN_WATCHMAN, get_sockname_msg,
strerror(errno));
return -1;
case WATCHMAN_EXEC_INTERNAL_ERROR:
watchman_err(error, WATCHMAN_ERR_OTHER, get_sockname_msg,
strerror(errno));
return -1;
default:
watchman_err(error, WATCHMAN_ERR_WATCHMAN_BROKEN, get_sockname_msg,
strerror(errno));
return -1;
}
}
/**
* Calls select() on the given fd until it's ready to read from or
* until the timeout expires. Updates timeout to indicate the
* remaining time. Returns 1 if the socket is readable, 0 if the
* socket is not readable, and -1 on error.
*/
static int block_on_read(int fd, struct timeval *timeout)
{
struct timeval now, start, elapsed, orig_timeout;
fd_set fds;
int ret = 0;
FD_ZERO(&fds);
FD_SET(fd, &fds);
if (timeout != NULL) {
gettimeofday(&start, NULL);
memcpy(&orig_timeout, timeout, sizeof(orig_timeout));
}
ret = select(fd + 1, &fds, NULL, NULL, timeout);
if (timeout != NULL) {
gettimeofday(&now, NULL);
timersub(&now, &start, &elapsed);
timersub(&orig_timeout, &elapsed, timeout);
}
return ret;
}
/*
* Read from fd into buf until either `bytes` bytes have been read, or
* EOF, or the data that has been read can be parsed as JSON. In the
* event of a timeout or read error, returns NULL.
*/
static proto_t read_with_timeout(int fd, char* buf, size_t bytes, struct timeval timeout)
{
size_t read_so_far = 0;
ssize_t bytes_read = 0;
while (read_so_far < bytes) {
if (timeout.tv_sec || timeout.tv_usec) {
if (timeout.tv_sec < 0 || timeout.tv_usec < 0) {
/* Timeout */
return proto_null();
}
if (1 == block_on_read(fd, &timeout)) {
bytes_read = read(fd, buf, bytes - read_so_far);
} else {
return proto_null(); /* timeout or error */
}
} else {
bytes_read = read(fd, buf, bytes - read_so_far);
}
if (bytes_read < 0) {
return proto_null();
}
if (bytes_read == 0) {
/* EOF, but we couldn't parse the JSON we have so far */
return proto_null();
}
read_so_far += bytes_read;
/* try to parse this */
buf[read_so_far] = 0;
proto_t proto;
if (buf[0] <= 0x0b) {
bser_t* bser = bser_parse_buffer((uint8_t*)buf, read_so_far, NULL);
proto = proto_from_bser(bser);
} else {
json_error_t jerror;
json_t* json = json_loads(buf, JSON_DISABLE_EOF_CHECK, &jerror);
proto = proto_from_json(json);
}
return proto;
}
return proto_null();
}
/*
* Connect to watchman's socket. Sets a socket send and receive
* timeout of `timeout`. Pass a {0} for no-timeout. On error,
* returns NULL and, if `error` is non-NULL, fills it in.
*/
struct watchman_connection *
watchman_connect(struct timeval timeout, struct watchman_error *error)
{
struct watchman_connection *conn = NULL;
/* If an environment variable WATCHMAN_SOCK is set, establish a connection
to that address. Otherwise, run `watchman get-sockname` to start the
daemon and retrieve its address. */
const char *sockname_env = getenv("WATCHMAN_SOCK");
if (sockname_env) {
conn = watchman_sock_connect(sockname_env, timeout, error);
goto done;
}
struct watchman_popen *p = watchman_popen_getsockname(error);
if (p == NULL) {
return NULL;
}
char buf[WATCHMAN_GET_SOCKNAME_MAX + 1];
proto_t proto = read_with_timeout(p->fd, buf, WATCHMAN_GET_SOCKNAME_MAX, timeout);
if (watchman_pclose(error, p)) {
goto done;
}
if (proto_is_null(proto)) {
watchman_err(error, WATCHMAN_ERR_WATCHMAN_BROKEN,
"Got bad or no JSON/BSER from watchman get-sockname");
goto done;
}
if (!proto_is_object(proto)) {
watchman_err(error, WATCHMAN_ERR_WATCHMAN_BROKEN,
"Got bad JSON/BSER from watchman get-sockname: object expected");
goto bad_proto;
}
proto_t sockname_obj = proto_object_get(proto, "sockname");
if (proto_is_null(sockname_obj)) {
watchman_err(error, WATCHMAN_ERR_WATCHMAN_BROKEN,
"Got bad JSON/BSER from watchman get-sockname: "
"sockname element expected");
goto bad_proto;
}
if (!proto_is_string(sockname_obj)) {
watchman_err(error, WATCHMAN_ERR_WATCHMAN_BROKEN,
"Got bad JSON/BSER from watchman get-sockname:"
" sockname is not string");
goto bad_proto;
}
const char *sockname = proto_strdup(sockname_obj);
conn = watchman_sock_connect(sockname, timeout, error);
bad_proto:
proto_free(proto);
done:
return conn;
}
static int
watchman_send_simple_command(struct watchman_connection *conn,
struct watchman_error *error, ...)
{
int result = 0;
json_t *cmd_array = json_array();
va_list argptr;
va_start(argptr, error);
char *arg;
while ((arg = va_arg(argptr, char *))) {
json_array_append_new(cmd_array, json_string(arg));
}
if (use_bser_encoding) {
result = bser_write_to_file(cmd_array, conn->fp) == 0;
} else {
result = json_dumpf(cmd_array, conn->fp, JSON_COMPACT);
fputc('\n', conn->fp);
}
if (result) {
if (errno == EAGAIN || errno == EWOULDBLOCK) {
watchman_err(error, WATCHMAN_ERR_TIMEOUT,
"Timeout sending simple watchman command");
} else {
watchman_err(error, WATCHMAN_ERR_WATCHMAN_BROKEN,
"Failed to send simple watchman command");
}
result = 1;
}
json_decref(cmd_array);
return result;
}
static proto_t
watchman_read_with_timeout(struct watchman_connection *conn, struct timeval *timeout, struct watchman_error *error)
{
proto_t result;
json_error_t jerror;
int ret = 1;
if (!timeout || timeout->tv_sec || timeout->tv_usec)
ret = block_on_read(fileno(conn->fp), timeout);
if (ret == -1) {
watchman_err(error, WATCHMAN_ERR_WATCHMAN_BROKEN,
"Error encountered blocking on watchman");
return proto_null();
}
if (ret != 1) {
watchman_err(error, WATCHMAN_ERR_TIMEOUT,
"timed out waiting for watchman");
return proto_null();
}
if (use_bser_encoding) {
bser_t* bser = bser_parse_from_file(conn->fp, NULL);
result = proto_from_bser(bser);
} else {
json_t* json = json_loadf(conn->fp, JSON_DISABLE_EOF_CHECK, &jerror);
result = proto_from_json(json);
if (fgetc(conn->fp) != '\n') {
if (errno == EAGAIN || errno == EWOULDBLOCK) {
watchman_err(error, WATCHMAN_ERR_TIMEOUT,
"Timeout reading EOL from watchman");
} else {
watchman_err(error, WATCHMAN_ERR_WATCHMAN_BROKEN,
"No newline at end of reply");
}
json_decref(json);
return proto_null();
}
}
if (proto_is_null(result)) {
if (errno == EAGAIN) {
watchman_err(error, WATCHMAN_ERR_TIMEOUT,
"Timeout:EAGAIN reading from watchman.");
}
else if (errno == EWOULDBLOCK) {
watchman_err(error, WATCHMAN_ERR_TIMEOUT,
"Timeout:EWOULDBLOCK reading from watchman");
} else {
watchman_err(error, WATCHMAN_ERR_WATCHMAN_BROKEN,
"Can't parse result from watchman: %s",
jerror.text);
}
return proto_null();
}
return result;
}
static proto_t
watchman_read(struct watchman_connection *conn, struct watchman_error *error)
{
return watchman_read_with_timeout(conn, NULL, error);
}
static int
watchman_read_and_handle_errors(struct watchman_connection *conn,
struct watchman_error *error)
{
proto_t obj = watchman_read(conn, error);
if (proto_is_null(obj)) {
return 1;
}
if (!proto_is_object(obj)) {
char *bogus_text = proto_dumps(obj, 0);
watchman_err(error, WATCHMAN_ERR_WATCHMAN_BROKEN,
"Got non-object result from watchman : %s",
bogus_text);
free(bogus_text);
proto_free(obj);
return 1;
}
proto_t error_node = proto_object_get(obj, "error");
if (!proto_is_null(error_node)) {
watchman_err(error, WATCHMAN_ERR_OTHER,
"Got error result from watchman : %s",
proto_strdup(error_node));
proto_free(obj);
return 1;
}
proto_free(obj);
return 0;
}
int
watchman_watch(struct watchman_connection *conn,
const char *path, struct watchman_error *error)
{
if (watchman_send_simple_command(conn, error, "watch", path, NULL)) {
return 1;
}
if (watchman_read_and_handle_errors(conn, error)) {
return 1;
}
return 0;
}
int
watchman_recrawl(struct watchman_connection *conn,
const char *path, struct watchman_error *error)
{
if (watchman_send_simple_command(conn, error, "debug-recrawl", path, NULL)) {
return 1;
}
if (watchman_read_and_handle_errors(conn, error)) {
return 1;
}
return 0;
}
int
watchman_watch_del(struct watchman_connection *conn,
const char *path, struct watchman_error *error)
{
if (watchman_send_simple_command(conn, error, "watch-del", path, NULL)) {
return 1;
}
if (watchman_read_and_handle_errors(conn, error)) {
return 1;
}
return 0;
}
static struct watchman_expression *
alloc_expr(enum watchman_expression_type ty)
{
struct watchman_expression *expr;
expr = calloc(1, sizeof(*expr));
expr->ty = ty;
return expr;
}
struct watchman_expression *
watchman_since_expression(const char *since, enum watchman_clockspec spec)
{
assert(since);
struct watchman_expression *expr = alloc_expr(WATCHMAN_EXPR_TY_SINCE);
expr->e.since_expr.is_str = 1;
expr->e.since_expr.t.since = strdup(since);
expr->e.since_expr.clockspec = spec;
return expr;
}
struct watchman_expression *
watchman_since_expression_time_t(time_t time, enum watchman_clockspec
spec)
{
struct watchman_expression *expr = alloc_expr(WATCHMAN_EXPR_TY_SINCE);
expr->e.since_expr.is_str = 0;
expr->e.since_expr.t.time = time;
expr->e.since_expr.clockspec = spec;
return expr;
}
/* corresponds to enum watchman_expression_type */
static char *ty_str[] = {
"allof",
"anyof",
"not",
"true",
"false",
"since",
"suffix",
"match",
"imatch",
"pcre",
"ipcre",
"name",
"iname",
"type",
"empty",
"exists"
};
/* corresponds to enum watchman_clockspec */
static char *clockspec_str[] = {
NULL,
"oclock",
"cclock",
"mtime",
"ctime"
};
/* corresponds to enum watchman_basename */
static char *basename_str[] = {
NULL,
"basename",
"wholename"
};
static json_t *
json_string_from_char(char c)
{
char str[2] = { c, 0 };
return json_string(str);
}
static json_t *
json_string_or_array(int nr, char **items)
{
if (nr == 1) {
return json_string(items[0]);
}
json_t *result = json_array();
int i;
for (i = 0; i < nr; ++i) {
json_array_append_new(result, json_string(items[i]));
}
return result;
}
static void
since_to_json(json_t *result, const struct watchman_expression *expr)
{
if (expr->e.since_expr.is_str) {
json_array_append_new(result, json_string(expr->e.since_expr.t.since));
} else {
json_array_append_new(result, json_integer(expr->e.since_expr.t.time));
}
if (expr->e.since_expr.clockspec) {
char *clockspec = clockspec_str[expr->e.since_expr.clockspec];
json_array_append_new(result, json_string(clockspec));
}
}
static json_t *
to_json(const struct watchman_expression *expr)
{
json_t *result = json_array();
json_t *arg;
json_array_append_new(result, json_string(ty_str[expr->ty]));
int i;
switch (expr->ty) {
case WATCHMAN_EXPR_TY_ALLOF:
/*-fallthrough*/
case WATCHMAN_EXPR_TY_ANYOF:
for (i = 0; i < expr->e.union_expr.nr; ++i) {
json_array_append_new(result,
to_json(expr->e.union_expr.clauses[i]));
}
break;
case WATCHMAN_EXPR_TY_NOT:
json_array_append_new(result, to_json(expr->e.not_expr.clause));
break;
case WATCHMAN_EXPR_TY_TRUE:
/*-fallthrough*/
case WATCHMAN_EXPR_TY_FALSE:
/*-fallthrough*/
case WATCHMAN_EXPR_TY_EMPTY:
/*-fallthrough*/
case WATCHMAN_EXPR_TY_EXISTS:
/* Nothing to do */
break;
case WATCHMAN_EXPR_TY_SINCE:
since_to_json(result, expr);
break;
case WATCHMAN_EXPR_TY_SUFFIX:
json_array_append_new(result,
json_string(expr->e.suffix_expr.suffix));
break;
case WATCHMAN_EXPR_TY_MATCH:
/*-fallthrough*/
case WATCHMAN_EXPR_TY_IMATCH:
/*-fallthrough*/
case WATCHMAN_EXPR_TY_PCRE:
/*-fallthrough*/
case WATCHMAN_EXPR_TY_IPCRE:
json_array_append_new(result,
json_string(expr->e.match_expr.match));
if (expr->e.match_expr.basename) {
char *base = basename_str[expr->e.match_expr.basename];
json_array_append_new(result, json_string(base));
}
break;
case WATCHMAN_EXPR_TY_NAME:
/*-fallthrough*/
case WATCHMAN_EXPR_TY_INAME:
arg =
json_string_or_array(expr->e.name_expr.nr,
expr->e.name_expr.names);
json_array_append_new(result, arg);
if (expr->e.name_expr.basename) {
char *base = basename_str[expr->e.name_expr.basename];
json_array_append_new(result, json_string(base));
}
break;
case WATCHMAN_EXPR_TY_TYPE:
json_array_append_new(result,
json_string_from_char(expr->e.
type_expr.type));
}
return result;
}
/* corresponds to enum watchman_fields */
static char *fields_str[] = {
"name",
"exists",
"cclock",
"oclock",
"ctime",
"ctime_ms",
"ctime_us",
"ctime_ns",
"ctime_f",
"mtime",
"mtime_ms",
"mtime_us",
"mtime_ns",
"mtime_f",
"size",
"uid",
"gid",
"ino",
"dev",
"nlink",
"new",
"mode"
};
json_t *
fields_to_json(int fields)
{
json_t *result = json_array();
int i = 0;
int mask;
for (mask = 1; mask < WATCHMAN_FIELD_END; mask *= 2) {
if (fields & mask) {
json_array_append_new(result, json_string(fields_str[i]));
}
++i;
}
return result;
}
#define PROTO_ASSERT(cond, condarg, msg) \
if (!cond(condarg)) { \
char *dump = proto_dumps(condarg, 0); \
watchman_err(error, WATCHMAN_ERR_WATCHMAN_BROKEN, msg, dump); \
free(dump); \
goto done; \
}
struct watchman_watch_list *
watchman_watch_list(struct watchman_connection *conn,
struct watchman_error *error)
{
struct watchman_watch_list *res = NULL;
struct watchman_watch_list *result = NULL;
if (watchman_send_simple_command(conn, error, "watch-list", NULL)) {
return NULL;
}
proto_t obj = watchman_read(conn, error);
if (proto_is_null(obj)) {
return NULL;
}
PROTO_ASSERT(proto_is_object, obj, "Got bogus value from watch-list %s");
proto_t roots = proto_object_get(obj, "roots");
PROTO_ASSERT(proto_is_array, roots, "Got bogus value from watch-list %s");
res = malloc(sizeof(*res));
int nr = proto_array_size(roots);
res->nr = 0;
res->roots = calloc(nr, sizeof(*res->roots));
int i;
for (i = 0; i < nr; ++i) {
proto_t root = proto_array_get(roots, i);
PROTO_ASSERT(proto_is_string, root,
"Got non-string root from watch-list %s");
res->nr++;
res->roots[i] = proto_strdup(root);
}
result = res;
res = NULL;
done:
if (res) {
watchman_free_watch_list(res);
}
proto_free(obj);
return result;
}
#define WRITE_BOOL_STAT(stat, statobj, attr) \
proto_t attr = proto_object_get(statobj, #attr); \
if (!proto_is_null(attr)) { \
PROTO_ASSERT(proto_is_boolean, attr, #attr " is not boolean: %s"); \
stat->attr = proto_is_true(attr); \
}
#define WRITE_INT_STAT(stat, statobj, attr) \
proto_t attr = proto_object_get(statobj, #attr); \
if (!proto_is_null(attr)) { \
PROTO_ASSERT(proto_is_integer, attr, #attr " is not an int: %s"); \
stat->attr = proto_integer_value(attr); \
}
#define WRITE_STR_STAT(stat, statobj, attr) \
proto_t attr = proto_object_get(statobj, #attr); \
if (!proto_is_null(attr)) { \
PROTO_ASSERT(proto_is_string, attr, #attr " is not a string: %s"); \
stat->attr = proto_strdup(attr); \
}
#define WRITE_FLOAT_STAT(stat, statobj, attr) \
proto_t attr = proto_object_get(statobj, #attr); \
if (!proto_is_null(attr)) { \
PROTO_ASSERT(proto_is_real, attr, #attr " is not a float: %s"); \
stat->attr = proto_real_value(attr); \
}
static int
watchman_send(struct watchman_connection *conn,
json_t *query, struct watchman_error *error)
{
int result;
if (use_bser_encoding) {
result = bser_write_to_file(query, conn->fp) == 0;
} else {
result = json_dumpf(query, conn->fp, JSON_COMPACT);
fputc('\n', conn->fp);
}
if (result) {
if (errno == EAGAIN || errno == EWOULDBLOCK) {
watchman_err(error, WATCHMAN_ERR_TIMEOUT,
"Timeout sending to watchman");
} else {
char *dump = json_dumps(query, 0);
watchman_err(error, WATCHMAN_ERR_OTHER,
"Failed to send watchman query %s", dump);
free(dump);
}
return 1;
}
return 0;
}
char *
watchman_clock(struct watchman_connection *conn,
const char *path,
unsigned int sync_timeout,
struct watchman_error *error)
{
char *result = NULL;
json_t *query = json_array();
json_array_append_new(query, json_string("clock"));
json_array_append_new(query, json_string(path));
if (sync_timeout) {
json_t *options = json_object();
json_object_set_new(options, "sync_timeout", json_integer(sync_timeout));
json_array_append_new(query, options);
}
int ret = watchman_send(conn, query, error);
json_decref(query);
if (ret) {
return NULL;
}
proto_t obj = watchman_read(conn, error);
if (proto_is_null(obj)) {
return NULL;
}
PROTO_ASSERT(proto_is_object, obj, "Got bogus value from clock %s");
proto_t clock = proto_object_get(obj, "clock");
PROTO_ASSERT(proto_is_string, clock, "Bad clock %s");
result = proto_strdup(clock);
done:
proto_free(obj);
return result;
}
static struct watchman_query_result *
watchman_query_json(struct watchman_connection *conn,
json_t *query,
struct timeval *timeout,
struct watchman_error *error)
{
struct watchman_query_result *result = NULL;
struct watchman_query_result *res = NULL;
if (watchman_send(conn, query, error)) {
return NULL;
}
/* parse the result */
proto_t obj = watchman_read_with_timeout(conn, timeout, error);
if (proto_is_null(obj)) {
return NULL;
}
PROTO_ASSERT(proto_is_object, obj, "Failed to send watchman query %s");
proto_t jerror = proto_object_get(obj, "error");
if (!proto_is_null(jerror)) {
watchman_err(error, WATCHMAN_ERR_WATCHMAN_REPORTED,
"Error result from watchman: %s",
proto_strdup(jerror));
goto done;
}
res = calloc(1, sizeof(*res));
proto_t files = proto_object_get(obj, "files");
PROTO_ASSERT(proto_is_array, files, "Bad files %s");