/
fswatcher.c
85 lines (71 loc) · 2.22 KB
/
fswatcher.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
#if __APPLE__
#include <CoreServices/CoreServices.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/dir.h>
static time_t global_last_mtime;
static void callback(ConstFSEventStreamRef stream,
void *userinfo,
size_t const N,
void *paths,
const FSEventStreamEventFlags flags[],
const FSEventStreamEventId ids[])
{
time_t last_mtime = global_last_mtime;
size_t i;
DIR* d;
struct dirent *dp;
for (i = 0; i < N; i++) {
const char* dname = ((char**)paths)[i];
if ((d = opendir(dname)) == NULL) {
perror(dname);
continue;
}
while ((dp = readdir(d)) != NULL) {
switch (dp->d_type) {
case DT_REG: case DT_LNK: break;
default: continue;
}
char path[dp->d_namlen + strlen(dname) + 1];
strcpy(path, dname);
strcat(path, dp->d_name);
struct stat sb;
if (lstat(path, &sb) < 0 && errno == EACCES)
perror(path);
else if (sb.st_mtime >= last_mtime) {
puts(path);
if (sb.st_mtime > global_last_mtime)
global_last_mtime = sb.st_mtime;
}
}
closedir(d);
}
fflush(stdout);
}
static inline CFArrayRef paths(const int N, char** in)
{
CFStringRef out[N];
int x;
for (x = 0; x < N; ++x)
out[x] = CFStringCreateWithCString(kCFAllocatorDefault, in[x], kCFStringEncodingUTF8);
return CFArrayCreate(NULL, (const void **)out, N, NULL);
}
int main(int argc, char** argv)
{
global_last_mtime = time(NULL);
FSEventStreamRef stream = FSEventStreamCreate(
NULL,
&callback,
NULL,
paths(--argc, ++argv),
kFSEventStreamEventIdSinceNow, // TODO previous event ID
5.0, // seconds after an event before calling our callback
kFSEventStreamCreateFlagNone);
FSEventStreamScheduleWithRunLoop(stream, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode);
FSEventStreamStart(stream);
CFRunLoopRun();
}
#elif WIN32
int wmain(int argc, wchar_t* argv[])
{}
#endif