-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfammonitor.c
201 lines (164 loc) · 3.48 KB
/
fammonitor.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
/*
* Copyright 2008 James Peach
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/stat.h>
#include <poll.h>
#include <errno.h>
#include <time.h>
#include <fam.h>
#define arraysz(x) (sizeof(x)/sizeof(x[0])
static const char PROGNAME[] = "fammonitor";
void fam_print_event(const FAMEvent * ev)
{
static const char * evstr[] =
{
NULL,
"FAMChanged",
"FAMDeleted",
"FAMStartExecuting",
"FAMStopExecuting",
"FAMCreated",
"FAMMoved",
"FAMAcknowledge",
"FAMExists",
"FAMEndExist"
};
time_t now;
char tbuf[64];
now = time(NULL);
strftime(tbuf, sizeof(tbuf), "%T", localtime(&now));
printf("%s: %s: %s event on %s\n",
PROGNAME, tbuf, evstr[ev->code], ev->filename);
}
FAMConnection * fam_init(void)
{
FAMConnection * fam;
if ((fam = calloc(sizeof(FAMConnection), 1)) == NULL)
return(NULL);
if (FAMOpen2(fam, "jpeach") < 0)
{
free(fam);
return(NULL);
}
printf("%s: connected to fam\n", PROGNAME);
return(fam);
}
int fam_add_path(FAMConnection * fam, const char * path)
{
struct stat statbuf;
char * fullpath;
if (*path != '/')
{
char * cwd;
#ifdef _GNU_SOURCE
cwd = get_current_dir_name();
#else
cwd = getcwd(NULL, -1);
#endif
fullpath = malloc(strlen(cwd) + strlen(path) + 5);
sprintf(fullpath, "%s/%s", cwd, path);
free(cwd);
}
else
{
fullpath = strdup(path);
}
if (stat(fullpath, &statbuf) < 0)
{
fprintf(stderr, "%s: stat %s: %s\n", PROGNAME,
fullpath, strerror(errno));
free(fullpath);
return(-1);
}
printf("%s: monitoring %s\n", PROGNAME, fullpath);
if (S_ISDIR(statbuf.st_mode))
{
FAMRequest rq;
if (FAMMonitorDirectory(fam, fullpath, &rq, NULL) < 0)
{
fprintf(stderr, "%s: FAMMonitorDirectory %s failed\n",
PROGNAME, fullpath);
}
}
else
{
FAMRequest rq;
if (FAMMonitorFile(fam, fullpath, &rq, NULL) < 0)
{
fprintf(stderr, "%s: FAMMonitorFile %s failed\n",
PROGNAME, fullpath);
}
}
free(fullpath);
return(1);
}
int fam_monitor(FAMConnection * fam)
{
FAMEvent ev;
#if USE_POLL
for( ;; )
{
struct pollfd fds = {0};
fds.fd = FAMCONNECTION_GETFD(fam);
fds.events = POLLRDNORM | POLLWRNORM;
if (poll(&fds, 1, -1) < 0)
{
fprintf(stderr, "%s: poll: %s\n",
PROGNAME, strerror(errno));
exit(1);
}
if (FAMNextEvent(fam, &ev) >= 0)
{
fam_print_event(&ev);
}
}
#else
for ( ;; )
{
while (FAMPending(fam))
{
if (FAMNextEvent(fam, &ev) >= 0)
{
fam_print_event(&ev);
}
}
sleep(1);
}
#endif
}
int main(int argc, const char ** argv)
{
if (argc > 1)
{
int i;
FAMConnection * fam;
if ((fam = fam_init()) == NULL)
return(1);
for (i = 1; i < argc; ++i)
fam_add_path(fam, argv[i]);
fam_monitor(fam);
}
else
{
fprintf(stderr, "Usage: %s file [file ...]\n", PROGNAME);
return(1);
}
return(0);
}