1 // Copyright 2016 The Fuchsia Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include <stdio.h>
6 #include <unistd.h>
7 #include <fcntl.h>
8
9 #include <lib/fdio/watcher.h>
10
callback(int dirfd,int event,const char * fn,void * cookie)11 zx_status_t callback(int dirfd, int event, const char* fn, void* cookie) {
12 const char* path = cookie;
13
14 switch (event) {
15 case WATCH_EVENT_ADD_FILE:
16 fprintf(stderr, "watch: added '%s/%s'\n", path, fn);
17 break;
18 case WATCH_EVENT_REMOVE_FILE:
19 fprintf(stderr, "watch: removed '%s/%s'\n", path, fn);
20 break;
21 case WATCH_EVENT_IDLE:
22 fprintf(stderr, "watch: waiting...\n");
23 break;
24 }
25 return ZX_OK;
26 }
27
main(int argc,char ** argv)28 int main(int argc, char** argv) {
29 if (argc != 2) {
30 return -1;
31 }
32
33 int fd;
34 if ((fd = open(argv[1], O_DIRECTORY | O_RDONLY)) < 0) {
35 fprintf(stderr, "cannot open directory '%s'\n", argv[1]);
36 }
37
38 zx_status_t status;
39 if ((status = fdio_watch_directory(fd, callback, ZX_TIME_INFINITE, argv[1])) < 0) {
40 fprintf(stderr, "fdio watch directory failed: %d\n", status);
41 return -1;
42 }
43
44 return 0;
45 }
46