-
Notifications
You must be signed in to change notification settings - Fork 0
/
veil.c
175 lines (167 loc) · 3.96 KB
/
veil.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
#include <CoreServices/CoreServices.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>
#include <string.h>
#include <libgen.h>
#include <sys/stat.h>
#include <sys/queue.h>
#include <sys/types.h>
#include <sys/mman.h>
typedef struct sub_entry {
long modtime;
char *path;
char *dir;
LIST_ENTRY(sub_entry) entry;
Byte isfile;
int txid;
} SubEntry;
typedef LIST_HEAD(sub_registry, sub_entry) SubRegistry;
static SubRegistry *registry;
void usage()
{
fprintf(stderr,
"Usage: veil [-h] [-c CONF] WATCHDIR\n"
"Monitor FS events from WATCHDIR down. Print changes to STDOUT\n"
"-h display this help message\n"
"-c CONF is path to a newline delimited list of paths\n"
"to whitelist. If CONF is not specified, we watch for all\n"
"changes.\n");
exit(2);
}
int loadconf(SubRegistry * registry, char *conf)
{
SubEntry *ent;
int lines = 0;
char *line = NULL;
struct stat statbuf;
size_t linecap = 0;
FILE *conffile;
if ((conffile = fopen(conf, "r")) == NULL) {
fprintf(stderr, "file %s can't be opened\n", conf);
return -1;
}
while (getline(&line, &linecap, conffile) > 0) {
ent = malloc(sizeof(*ent));
bzero(ent, sizeof(*ent));
ent->path = strdup(line);
char *tmp = strchr(ent->path, '\n');
*tmp = '\0';
if (stat(ent->path, &statbuf)) {
fprintf(stderr, "can't stat %s, skipping\n", ent->path);
goto DONE;
}
if (S_ISREG(statbuf.st_mode)) {
ent->isfile = 0x01;
ent->modtime = statbuf.st_mtime;
ent->dir = strdup(dirname(ent->path));
} else if (S_ISDIR(statbuf.st_mode)) {
ent->dir = strdup(ent->path);
} else {
fprintf(stderr, "%s not file or directory, skipping\n",
ent->path);
goto DONE;
}
LIST_INSERT_HEAD(registry, ent, entry);
lines++;
DONE:
continue;
// free(line);
}
fclose(conffile);
return lines;
}
void checkevent(SubRegistry * registry, int txid, char *epath,
FSEventStreamEventFlags eflags)
{
if (registry == NULL) {
printf("%s\n", epath);
return;
}
SubEntry *sub;
sub = malloc(sizeof(*sub));
LIST_FOREACH(sub, registry, entry) {
if (strstr(epath, sub->dir)) {
int doprint = 1;
if (sub->isfile) {
struct stat statbuf;
stat(sub->path, &statbuf);
if (sub->modtime >= statbuf.st_mtime) {
doprint = 0;
} else {
sub->modtime = statbuf.st_mtime;
}
}
if (eflags & kFSEventStreamEventFlagItemRemoved)
doprint = 0;
if (sub->txid == txid)
doprint = 0;
if (doprint) {
// fprintf(stderr, "txid: %d ", txid);
printf("%s\n", sub->path);
fflush(NULL);
sub->txid = txid;
}
}
}
free(sub);
}
void cbprint(ConstFSEventStreamRef streamRef,
void *cbctx,
size_t numEvents,
void *eventPaths,
const FSEventStreamEventFlags eventFlags[],
const FSEventStreamEventId eventIds[])
{
int i;
static int s = 0;
SubEntry *sub;
char **paths = eventPaths;
s++;
for (i = 0; i < numEvents; i++)
checkevent(registry, s, paths[i], eventFlags[i]);
}
int main(int argc, char *argv[])
{
int lines;
char opt;
char *conf;
char *watchdir;
SubEntry *sub;
while ((opt = getopt(argc, argv, ":hc:")) != -1) {
switch (opt) {
case 'h':
usage();
break;
case 'c':
conf = strdup(optarg);
break;
}
}
if (optind < argc)
watchdir = argv[optind];
else
usage();
registry = malloc(sizeof(*registry));
LIST_INIT(registry);
if ((lines = loadconf(registry, conf)) < 1) {
free(registry);
registry = NULL;
}
CFArrayRef cpaths;
FSEventStreamRef stream;
CFAbsoluteTime latency = 1.0;
cpaths = CFArrayCreateMutable(NULL, lines, &kCFTypeArrayCallBacks);
CFStringRef cfpath =
CFStringCreateWithCString(NULL, watchdir, kCFStringEncodingUTF8);
CFArrayAppendValue(cpaths, cfpath);
stream = FSEventStreamCreate(NULL,
&cbprint, NULL, cpaths,
kFSEventStreamEventIdSinceNow, latency,
kFSEventStreamCreateFlagNone);
FSEventStreamScheduleWithRunLoop(stream, CFRunLoopGetCurrent(),
kCFRunLoopDefaultMode);
FSEventStreamStart(stream);
CFRunLoopRun();
}