-
Notifications
You must be signed in to change notification settings - Fork 8
/
fsw.cpp
613 lines (533 loc) · 14.1 KB
/
fsw.cpp
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
/*
* Copyright (C) 2014, Enrico M. Crisostomo
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include "fsw.h"
#include "fsw_log.h"
#include <iostream>
#include <sstream>
#include <csignal>
#include <cstdlib>
#include <cmath>
#include <ctime>
#include <cerrno>
#include <vector>
#include "libfsw/c++/monitor.h"
#ifdef HAVE_GETOPT_LONG
# include <getopt.h>
#endif
using namespace std;
static const unsigned int TIME_FORMAT_BUFF_SIZE = 128;
static fsw::monitor *active_monitor = nullptr;
static vector<monitor_filter> filters;
static bool _0flag = false;
static bool _1flag = false;
static bool Eflag = false;
static bool fflag = false;
static bool Iflag = false;
static bool kflag = false;
static bool lflag = false;
static bool Lflag = false;
static bool nflag = false;
static bool oflag = false;
static bool pflag = false;
static bool rflag = false;
static bool tflag = false;
static bool uflag = false;
static bool vflag = false;
static bool xflag = false;
static double lvalue = 1.0;
static string tformat = "%c";
bool is_verbose()
{
return vflag;
}
static void usage(ostream& stream)
{
#ifdef HAVE_GETOPT_LONG
stream << PACKAGE_STRING << "\n\n";
stream << "Usage:\n";
stream << PACKAGE_NAME << " [OPTION] ... path ...\n";
stream << "\n";
stream << "Options:\n";
stream
<< " -0, --print0 Use the ASCII NUL character (0) as line separator.\n";
stream
<< " -1, --one-event Exit fsw after the first set of events is received.\n";
# ifdef HAVE_REGCOMP
stream << " -e, --exclude=REGEX Exclude paths matching REGEX.\n";
stream << " -E, --extended Use extended regular expressions.\n";
# endif
stream
<< " -f, --format-time Print the event time using the specified format.\n";
stream << " -h, --help Show this message.\n";
# ifdef HAVE_REGCOMP
stream << " -i, --include=REGEX Include paths matching REGEX.\n";
stream << " -I, --insensitive Use case insensitive regular expressions.\n";
# endif
stream << " -k, --kqueue Use the kqueue monitor.\n";
stream << " -l, --latency=DOUBLE Set the latency.\n";
stream << " -L, --follow-links Follow symbolic links.\n";
stream << " -n, --numeric Print a numeric event mask.\n";
stream << " -o, --one-per-batch Print a single message with the number of change events.\n";
stream << " in the current batch.\n";
stream << " -p, --poll Use the poll monitor.\n";
stream << " -r, --recursive Recurse subdirectories.\n";
stream << " -t, --timestamp Print the event timestamp.\n";
stream << " -u, --utc-time Print the event time as UTC time.\n";
stream << " -v, --verbose Print verbose output.\n";
stream << " -x, --event-flags Print the event flags.\n";
stream << "\n";
stream << "See the man page for more information.";
stream << endl;
#else
string option_string = "[";
option_string += "01";
# ifdef HAVE_REGCOMP
option_string += "eE";
# endif
option_string += "fh";
# ifdef HAVE_REGCOMP
option_string += "i";
# endif
option_string += "k";
option_string += "lLnoprtuvx";
option_string += "]";
stream << PACKAGE_STRING << "\n\n";
stream << "Syntax:\n";
stream << PACKAGE_NAME << " " << option_string << " path ...\n";
stream << "\n";
stream << "Usage:\n";
stream << " -0 Use the ASCII NUL character (0) as line separator.\n";
stream << " -1 Exit fsw after the first set of events is received.\n";
# ifdef HAVE_REGCOMP
stream << " -e Exclude paths matching REGEX.\n";
stream << " -E Use extended regular expressions.\n";
# endif
stream << " -f Print the event time stamp with the specified format.\n";
stream << " -h Show this message.\n";
# ifdef HAVE_REGCOMP
stream << " -i Use case insensitive regular expressions.\n";
stream << " -i Include paths matching REGEX.\n";
# endif
stream << " -k Use the kqueue monitor.\n";
stream << " -l Set the latency.\n";
stream << " -L Follow symbolic links.\n";
stream << " -n Print a numeric event masks.\n";
stream << " -o Print a single message with the number of change events in the current\n";
stream << " batch.\n";
stream << " -p Use the poll monitor.\n";
stream << " -r Recurse subdirectories.\n";
stream << " -t Print the event timestamp.\n";
stream << " -u Print the event time as UTC time.\n";
stream << " -v Print verbose output.\n";
stream << " -x Print the event flags.\n";
stream << "\n";
stream << "See the man page for more information.";
stream << endl;
#endif
exit(FSW_EXIT_USAGE);
}
static void close_stream()
{
if (active_monitor)
{
delete active_monitor;
active_monitor = nullptr;
}
}
static void close_handler(int signal)
{
close_stream();
fsw_log("Done.\n");
exit(FSW_EXIT_OK);
}
static bool validate_latency(double latency, ostream &ost, ostream &est)
{
if (lvalue == 0.0)
{
est << "Invalid value: " << optarg << endl;
return false;
}
if (errno == ERANGE || lvalue == HUGE_VAL)
{
est << "Value out of range: " << optarg << endl;
return false;
}
if (is_verbose())
{
ost << "Latency set to: " << lvalue << endl;
}
return true;
}
static void register_signal_handlers()
{
struct sigaction action;
action.sa_handler = close_handler;
sigemptyset(&action.sa_mask);
action.sa_flags = 0;
if (sigaction(SIGTERM, &action, nullptr) == 0)
{
fsw_log("SIGTERM handler registered.\n");
}
else
{
cerr << "SIGTERM handler registration failed." << endl;
}
if (sigaction(SIGABRT, &action, nullptr) == 0)
{
fsw_log("SIGABRT handler registered.\n");
}
else
{
cerr << "SIGABRT handler registration failed." << endl;
}
if (sigaction(SIGINT, &action, nullptr) == 0)
{
fsw_log("SIGINT handler registered.\n");
}
else
{
cerr << "SIGINT handler registration failed" << endl;
}
}
static vector<string> decode_event_flag_name(vector<fsw_event_flag> flags)
{
vector<string> names;
for (fsw_event_flag flag : flags)
{
switch (flag)
{
case fsw_event_flag::PlatformSpecific:
names.push_back("PlatformSpecific");
break;
case fsw_event_flag::Created:
names.push_back("Created");
break;
case fsw_event_flag::Updated:
names.push_back("Updated");
break;
case fsw_event_flag::Removed:
names.push_back("Removed");
break;
case fsw_event_flag::Renamed:
names.push_back("Renamed");
break;
case fsw_event_flag::OwnerModified:
names.push_back("OwnerModified");
break;
case fsw_event_flag::AttributeModified:
names.push_back("AttributeModified");
break;
case fsw_event_flag::IsFile:
names.push_back("IsFile");
break;
case fsw_event_flag::IsDir:
names.push_back("IsDir");
break;
case fsw_event_flag::IsSymLink:
names.push_back("IsSymLink");
break;
case fsw_event_flag::Link:
names.push_back("Link");
break;
default:
names.push_back("<Unknown>");
break;
}
}
return names;
}
static void print_event_timestamp(const time_t &evt_time)
{
char time_format_buffer[TIME_FORMAT_BUFF_SIZE];
struct tm * tm_time = uflag ? gmtime(&evt_time) : localtime(&evt_time);
string date =
strftime(
time_format_buffer,
TIME_FORMAT_BUFF_SIZE,
tformat.c_str(),
tm_time) ? string(time_format_buffer) : string("<date format error>");
cout << date << " ";
}
static void print_event_flags(const vector<fsw_event_flag> &flags)
{
if (nflag)
{
int mask = 0;
for (const fsw_event_flag &flag : flags)
{
mask += static_cast<int> (flag);
}
cout << " " << mask;
}
else
{
vector<string> flag_names = decode_event_flag_name(flags);
for (string &name : flag_names)
{
cout << " " << name;
}
}
}
static void end_event_record()
{
if (_0flag)
{
cout << '\0';
cout.flush();
}
else
{
cout << endl;
}
}
static void write_one_batch_event(const vector<event> &events)
{
cout << events.size();
end_event_record();
}
static void write_events(const vector<event> &events)
{
for (const event &evt : events)
{
if (tflag) print_event_timestamp(evt.get_time());
cout << evt.get_path();
if (xflag)
{
print_event_flags(evt.get_flags());
}
end_event_record();
}
if (_1flag)
{
::exit(FSW_EXIT_OK);
}
}
static void process_events(const vector<event> &events, void * context)
{
if (oflag)
write_one_batch_event(events);
else
write_events(events);
}
static void start_monitor(int argc, char ** argv, int optind)
{
// parsing paths
vector<string> paths;
for (auto i = optind; i < argc; ++i)
{
char *real_path = ::realpath(argv[i], nullptr);
string path(real_path ? real_path : argv[i]);
if (real_path)
{
::free(real_path);
}
fsw_log("Adding path: ");
fsw_log(path.c_str());
fsw_log("\n");
paths.push_back(path);
}
if (pflag)
{
active_monitor = fsw::monitor::create_monitor(poll_monitor_type, paths, process_events);
}
else if (kflag)
{
active_monitor = fsw::monitor::create_monitor(kqueue_monitor_type, paths, process_events);
}
else
{
active_monitor = fsw::monitor::create_default_monitor(paths, process_events);
}
/*
* libfsw supports case sensitivity and extended flags to be set on any
* filter but fsw does not. For the time being, we apply the same flags to
* every filter.
*/
for (auto & filter : filters)
{
filter.case_sensitive = !Iflag;
filter.extended = Eflag;
}
active_monitor->set_latency(lvalue);
active_monitor->set_recursive(rflag);
active_monitor->set_filters(filters);
active_monitor->set_follow_symlinks(Lflag);
active_monitor->start();
}
static void parse_opts(int argc, char ** argv)
{
int ch;
ostringstream short_options;
short_options << "01f:hkl:Lnoprtuvx";
#ifdef HAVE_REGCOMP
short_options << "e:Ei:I";
#endif
short_options << "k";
#ifdef HAVE_GETOPT_LONG
int option_index = 0;
static struct option long_options[] = {
{ "print0", no_argument, nullptr, '0'},
{ "one-event", no_argument, nullptr, '1'},
# ifdef HAVE_REGCOMP
{ "exclude", required_argument, nullptr, 'e'},
{ "extended", no_argument, nullptr, 'E'},
# endif
{ "format-time", required_argument, nullptr, 'f'},
{ "help", no_argument, nullptr, 'h'},
# ifdef HAVE_REGCOMP
{ "include", required_argument, nullptr, 'i'},
{ "insensitive", no_argument, nullptr, 'I'},
# endif
{ "kqueue", no_argument, nullptr, 'k'},
{ "latency", required_argument, nullptr, 'l'},
{ "follow-links", no_argument, nullptr, 'L'},
{ "numeric", no_argument, nullptr, 'n'},
{ "one-per-batch", no_argument, nullptr, 'o'},
{ "poll", no_argument, nullptr, 'p'},
{ "recursive", no_argument, nullptr, 'r'},
{ "timestamp", no_argument, nullptr, 't'},
{ "utc-time", no_argument, nullptr, 'u'},
{ "verbose", no_argument, nullptr, 'v'},
{ "event-flags", no_argument, nullptr, 'x'},
{ nullptr, 0, nullptr, 0}
};
while ((ch = getopt_long(argc,
argv,
short_options.str().c_str(),
long_options,
&option_index)) != -1)
{
#else
while ((ch = getopt(argc, argv, short_options.str().c_str())) != -1)
{
#endif
switch (ch)
{
case '0':
_0flag = true;
break;
case '1':
_1flag = true;
break;
#ifdef HAVE_REGCOMP
case 'e':
filters.push_back({optarg, fsw_filter_type::filter_exclude});
break;
case 'E':
Eflag = true;
break;
#endif
case 'f':
fflag = true;
tformat = string(optarg);
break;
case 'h':
usage(cout);
exit(FSW_EXIT_USAGE);
#ifdef HAVE_REGCOMP
case 'i':
filters.push_back({optarg, fsw_filter_type::filter_include});
break;
case 'I':
Iflag = true;
break;
#endif
case 'k':
kflag = true;
break;
case 'l':
lflag = true;
lvalue = strtod(optarg, nullptr);
if (!validate_latency(lvalue, cout, cerr))
{
exit(FSW_EXIT_LATENCY);
}
break;
case 'L':
Lflag = true;
break;
case 'n':
nflag = true;
xflag = true;
break;
case 'o':
oflag = true;
break;
case 'p':
pflag = true;
break;
case 'r':
rflag = true;
break;
case 't':
tflag = true;
break;
case 'u':
uflag = true;
break;
case 'v':
vflag = true;
break;
case 'x':
xflag = true;
break;
default:
usage(cerr);
exit(FSW_EXIT_UNK_OPT);
}
}
}
int main(int argc, char ** argv)
{
parse_opts(argc, argv);
// validate options
if (optind == argc)
{
cerr << "Invalid number of arguments." << endl;
::exit(FSW_EXIT_UNK_OPT);
}
// only one kind of monitor can be used at a time
if (pflag && kflag)
{
cerr << "-k and -p are mutually exclusive." << endl;
::exit(FSW_EXIT_OPT);
}
// configure and start the monitor
try
{
// registering handlers to clean up resources
register_signal_handlers();
::atexit(close_stream);
// configure and start the monitor loop
start_monitor(argc, argv, optind);
}
catch (exception & conf)
{
cerr << "An error occurred and the program will be terminated.\n";
cerr << conf.what() << endl;
return FSW_EXIT_ERROR;
}
catch (...)
{
cerr << "An unknown error occurred and the program will be terminated." << endl;
return FSW_EXIT_ERROR;
}
return FSW_EXIT_OK;
}