"Fossies" - the Fresh Open Source Software Archive  

Source code changes of the file "src/inotifywatch.c" between
inotify-tools-3.20.2.2.tar.gz and inotify-tools-3.20.11.0.tar.gz

About: inotify-tools offers commandline utilities relating to inotify (wait for or watch particular events on files).

inotifywatch.c  (inotify-tools-3.20.2.2):inotifywatch.c  (inotify-tools-3.20.11.0)
// FIXME this is cheating! Make this use only the public API.
#include "../config.h" #include "../config.h"
#include "../libinotifytools/src/inotifytools_p.h" #include "../libinotifytools/src/inotifytools_p.h"
#include "common.h" #include "common.h"
#include <sys/select.h> #include <sys/select.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <sys/time.h> #include <sys/time.h>
#include <sys/types.h> #include <sys/types.h>
#include <assert.h> #include <assert.h>
skipping to change at line 34 skipping to change at line 33
#include <inotifytools/inotifytools.h> #include <inotifytools/inotifytools.h>
extern char *optarg; extern char *optarg;
extern int optind, opterr, optopt; extern int optind, opterr, optopt;
#define nasprintf(...) niceassert(-1 != asprintf(__VA_ARGS__), "out of memory") #define nasprintf(...) niceassert(-1 != asprintf(__VA_ARGS__), "out of memory")
// METHODS // METHODS
bool parse_opts(int *argc, char ***argv, int *events, long int *timeout, bool parse_opts(int *argc, char ***argv, int *events, long int *timeout,
int *verbose, int *zero, int *sort, int *recursive, int *verbose, int *zero, int *sort, int *recursive,
char **fromfile, char **exc_regex, char **exc_iregex, int *no_dereference, char **fromfile, char **exc_regex,
char **inc_regex, char **inc_iregex); char **exc_iregex, char **inc_regex, char **inc_iregex);
void print_help(); void print_help();
static bool done; static bool done;
void handle_impatient_user(int signal __attribute__((unused))) { void handle_impatient_user(int signal __attribute__((unused))) {
static int times_called = 0; static int times_called = 0;
if (times_called) { if (times_called) {
fprintf(stderr, "No statistics collected, asked to abort before all " fprintf(stderr, "No statistics collected, asked to abort before all "
"watches could be established.\n"); "watches could be established.\n");
skipping to change at line 77 skipping to change at line 76
int events; int events;
int sort; int sort;
int zero; int zero;
int main(int argc, char **argv) { int main(int argc, char **argv) {
events = 0; events = 0;
long int timeout = BLOCKING_TIMEOUT; long int timeout = BLOCKING_TIMEOUT;
int verbose = 0; int verbose = 0;
zero = 0; zero = 0;
int recursive = 0; int recursive = 0;
int no_dereference = 0;
char *fromfile = 0; char *fromfile = 0;
sort = -1; sort = -1;
done = false; done = false;
char *exc_regex = NULL; char *exc_regex = NULL;
char *exc_iregex = NULL; char *exc_iregex = NULL;
char *inc_regex = NULL; char *inc_regex = NULL;
char *inc_iregex = NULL; char *inc_iregex = NULL;
signal(SIGINT, handle_impatient_user); signal(SIGINT, handle_impatient_user);
// Parse commandline options, aborting if something goes wrong // Parse commandline options, aborting if something goes wrong
if (!parse_opts(&argc, &argv, &events, &timeout, &verbose, &zero, &sort, if (!parse_opts(&argc, &argv, &events, &timeout, &verbose, &zero, &sort,
&recursive, &fromfile, &exc_regex, &exc_iregex, &inc_regex, &recursive, &no_dereference, &fromfile, &exc_regex,
&inc_iregex)) { &exc_iregex, &inc_regex, &inc_iregex)) {
return EXIT_FAILURE; return EXIT_FAILURE;
} }
if ((exc_regex && if ((exc_regex &&
!inotifytools_ignore_events_by_regex(exc_regex, REG_EXTENDED)) || !inotifytools_ignore_events_by_regex(exc_regex, REG_EXTENDED)) ||
(exc_iregex && (exc_iregex &&
!inotifytools_ignore_events_by_regex(exc_iregex, !inotifytools_ignore_events_by_regex(exc_iregex,
REG_EXTENDED | REG_ICASE))) { REG_EXTENDED | REG_ICASE))) {
fprintf(stderr, "Error in `exclude' regular expression.\n"); fprintf(stderr, "Error in `exclude' regular expression.\n");
return EXIT_FAILURE; return EXIT_FAILURE;
skipping to change at line 122 skipping to change at line 122
if (!inotifytools_initialize()) { if (!inotifytools_initialize()) {
warn_inotify_init_error(); warn_inotify_init_error();
return EXIT_FAILURE; return EXIT_FAILURE;
} }
// Attempt to watch file // Attempt to watch file
// If events is still 0, make it all events. // If events is still 0, make it all events.
if (!events) if (!events)
events = IN_ALL_EVENTS; events = IN_ALL_EVENTS;
if (no_dereference)
events = events | IN_DONT_FOLLOW;
FileList list = construct_path_list(argc, argv, fromfile); FileList list = construct_path_list(argc, argv, fromfile);
if (0 == list.watch_files[0]) { if (0 == list.watch_files[0]) {
fprintf(stderr, "No files specified to watch!\n"); fprintf(stderr, "No files specified to watch!\n");
return EXIT_FAILURE; return EXIT_FAILURE;
} }
unsigned int num_watches = 0; unsigned int num_watches = 0;
unsigned int status; unsigned int status;
skipping to change at line 365 skipping to change at line 367
printf("%s\n", w->filename); printf("%s\n", w->filename);
w = (watch *)rbreadlist(rblist); w = (watch *)rbreadlist(rblist);
} }
rbcloselist(rblist); rbcloselist(rblist);
rbdestroy(tree); rbdestroy(tree);
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }
bool parse_opts(int *argc, char ***argv, int *events, long int *timeout, bool parse_opts(int *argc, char ***argv, int *e, long int *timeout,
int *verbose, int *zero, int *sort, int *recursive, int *verbose, int *z, int *s, int *recursive,
char **fromfile, char **exc_regex, char **exc_iregex, int *no_dereference, char **fromfile, char **exc_regex,
char **inc_regex, char **inc_iregex) { char **exc_iregex, char **inc_regex, char **inc_iregex) {
assert(argc); assert(argc);
assert(argv); assert(argv);
assert(events); assert(e);
assert(timeout); assert(timeout);
assert(verbose); assert(verbose);
assert(zero); assert(z);
assert(sort); assert(s);
assert(recursive); assert(recursive);
assert(no_dereference);
assert(fromfile); assert(fromfile);
assert(exc_regex); assert(exc_regex);
assert(exc_iregex); assert(exc_iregex);
assert(inc_regex); assert(inc_regex);
assert(inc_iregex); assert(inc_iregex);
// Settings for options // Settings for options
int new_event; int new_event;
bool sort_set = false; bool sort_set = false;
// Short options // Short options
static const char opt_string[] = "hra:d:zve:t:"; static const char opt_string[] = "hrPa:d:zve:t:";
// Construct array // Construct array
static const struct option long_opts[] = { static const struct option long_opts[] = {
{"help", no_argument, NULL, 'h'}, {"help", no_argument, NULL, 'h'},
{"event", required_argument, NULL, 'e'}, {"event", required_argument, NULL, 'e'},
{"timeout", required_argument, NULL, 't'}, {"timeout", required_argument, NULL, 't'},
{"verbose", no_argument, NULL, 'v'}, {"verbose", no_argument, NULL, 'v'},
{"zero", no_argument, NULL, 'z'}, {"zero", no_argument, NULL, 'z'},
{"ascending", required_argument, NULL, 'a'}, {"ascending", required_argument, NULL, 'a'},
{"descending", required_argument, NULL, 'd'}, {"descending", required_argument, NULL, 'd'},
{"recursive", no_argument, NULL, 'r'}, {"recursive", no_argument, NULL, 'r'},
{"no-dereference", no_argument, NULL, 'P'},
{"fromfile", required_argument, NULL, 'o'}, {"fromfile", required_argument, NULL, 'o'},
{"exclude", required_argument, NULL, 'c'}, {"exclude", required_argument, NULL, 'c'},
{"excludei", required_argument, NULL, 'b'}, {"excludei", required_argument, NULL, 'b'},
{"include", required_argument, NULL, 'j'}, {"include", required_argument, NULL, 'j'},
{"includei", required_argument, NULL, 'k'}, {"includei", required_argument, NULL, 'k'},
{NULL, 0, 0, 0}, {NULL, 0, 0, 0},
}; };
// Get first option // Get first option
char curr_opt = getopt_long(*argc, *argv, opt_string, long_opts, NULL); char curr_opt = getopt_long(*argc, *argv, opt_string, long_opts, NULL);
skipping to change at line 430 skipping to change at line 434
// --verbose or -v // --verbose or -v
case 'v': case 'v':
++(*verbose); ++(*verbose);
break; break;
// --recursive or -r // --recursive or -r
case 'r': case 'r':
++(*recursive); ++(*recursive);
break; break;
case 'P':
++(*no_dereference);
break;
// --zero or -z // --zero or -z
case 'z': case 'z':
++(*zero); ++(*z);
break; break;
// --exclude // --exclude
case 'c': case 'c':
(*exc_regex) = optarg; (*exc_regex) = optarg;
break; break;
// --excludei // --excludei
case 'b': case 'b':
(*exc_iregex) = optarg; (*exc_iregex) = optarg;
skipping to change at line 487 skipping to change at line 494
// If optarg was invalid, abort // If optarg was invalid, abort
if (new_event == -1) { if (new_event == -1) {
fprintf(stderr, "'%s' is not a valid event! Run with the " fprintf(stderr, "'%s' is not a valid event! Run with the "
"'--help' option to see a list of " "'--help' option to see a list of "
"events.\n", "events.\n",
optarg); optarg);
return false; return false;
} }
// Add the new event to the event mask // Add the new event to the event mask
(*events) = ((*events) | new_event); (*e) = ((*e) | new_event);
break; break;
// --ascending or -a // --ascending or -a
case 'a': case 'a':
if (sort_set) { if (sort_set) {
fprintf(stderr, "Please specify -a or -d once only!\n"); fprintf(stderr, "Please specify -a or -d once only!\n");
return false; return false;
} }
if (0 == strcasecmp(optarg, "total")) { if (0 == strcasecmp(optarg, "total")) {
(*sort) = 0; (*s) = 0;
} else if (0 == strcasecmp(optarg, "move")) { } else if (0 == strcasecmp(optarg, "move")) {
fprintf(stderr, "Cannot sort by `move' event; please use " fprintf(stderr, "Cannot sort by `move' event; please use "
"`moved_from' or `moved_to'.\n"); "`moved_from' or `moved_to'.\n");
return false; return false;
} else if (0 == strcasecmp(optarg, "close")) { } else if (0 == strcasecmp(optarg, "close")) {
fprintf(stderr, "Cannot sort by `close' event; please use " fprintf(stderr, "Cannot sort by `close' event; please use "
"`close_write' or `close_nowrite'.\n"); "`close_write' or `close_nowrite'.\n");
return false; return false;
} else { } else {
int event = inotifytools_str_to_event(optarg); int event = inotifytools_str_to_event(optarg);
// If optarg was invalid, abort // If optarg was invalid, abort
if (event == -1) { if (event == -1) {
fprintf(stderr, "'%s' is not a valid key for " fprintf(stderr, "'%s' is not a valid key for "
"sorting!\n", "sorting!\n",
optarg); optarg);
return false; return false;
} }
(*sort) = event; (*s) = event;
} }
sort_set = true; sort_set = true;
break; break;
// --descending or -d // --descending or -d
case 'd': case 'd':
if (sort_set) { if (sort_set) {
fprintf(stderr, "Please specify -a or -d once only!\n"); fprintf(stderr, "Please specify -a or -d once only!\n");
return false; return false;
} }
if (0 == strcasecmp(optarg, "total")) { if (0 == strcasecmp(optarg, "total")) {
(*sort) = -1; (*s) = -1;
} else { } else {
int event = inotifytools_str_to_event(optarg); int event = inotifytools_str_to_event(optarg);
// If optarg was invalid, abort // If optarg was invalid, abort
if (event == -1) { if (event == -1) {
fprintf(stderr, "'%s' is not a valid key for " fprintf(stderr, "'%s' is not a valid key for "
"sorting!\n", "sorting!\n",
optarg); optarg);
return false; return false;
} }
(*sort) = -event; (*s) = -event;
} }
break; break;
} }
curr_opt = getopt_long(*argc, *argv, opt_string, long_opts, NULL); curr_opt = getopt_long(*argc, *argv, opt_string, long_opts, NULL);
} }
(*argc) -= optind; (*argc) -= optind;
*argv = &(*argv)[optind]; *argv = &(*argv)[optind];
if ((*sort) != 0 && (*sort) != -1 && if ((*s) != 0 && (*s) != -1 &&
!(abs(*sort) & ((*events) ? (*events) : IN_ALL_EVENTS))) { !(abs(*s) & ((*e) ? (*e) : IN_ALL_EVENTS))) {
fprintf(stderr, "Can't sort by an event which isn't being watched " fprintf(stderr, "Can't sort by an event which isn't being watched "
"for!\n"); "for!\n");
return false; return false;
} }
if (*exc_regex && *exc_iregex) { if (*exc_regex && *exc_iregex) {
fprintf(stderr, "--exclude and --excludei cannot both be specified.\n"); fprintf(stderr, "--exclude and --excludei cannot both be specified.\n");
return false; return false;
} }
if (*inc_regex && *inc_iregex) { if (*inc_regex && *inc_iregex) {
skipping to change at line 606 skipping to change at line 613
"\t\tExclude all events on files except the ones\n" "\t\tExclude all events on files except the ones\n"
"\t\tmatching the extended regular expression\n" "\t\tmatching the extended regular expression\n"
"\t\t<pattern>.\n"); "\t\t<pattern>.\n");
printf("\t--includei <pattern>\n" printf("\t--includei <pattern>\n"
"\t\tLike --include but case insensitive.\n"); "\t\tLike --include but case insensitive.\n");
printf("\t-z|--zero\n" printf("\t-z|--zero\n"
"\t\tIn the final table of results, output rows and columns even\n" "\t\tIn the final table of results, output rows and columns even\n"
"\t\tif they consist only of zeros (the default is to not output\n" "\t\tif they consist only of zeros (the default is to not output\n"
"\t\tthese rows and columns).\n"); "\t\tthese rows and columns).\n");
printf("\t-r|--recursive\tWatch directories recursively.\n"); printf("\t-r|--recursive\tWatch directories recursively.\n");
printf("\t-P|--no-dereference\n"
"\t\tDo not follow symlinks.\n");
printf("\t-t|--timeout <seconds>\n" printf("\t-t|--timeout <seconds>\n"
"\t\tListen only for specified amount of time in seconds; if\n" "\t\tListen only for specified amount of time in seconds; if\n"
"\t\tomitted or negative, inotifywatch will execute until receiving " "\t\tomitted or negative, inotifywatch will execute until receiving "
"an\n" "an\n"
"\t\tinterrupt signal.\n"); "\t\tinterrupt signal.\n");
printf("\t-e|--event <event1> [ -e|--event <event2> ... ]\n" printf("\t-e|--event <event1> [ -e|--event <event2> ... ]\n"
"\t\tListen for specific event(s). If omitted, all events are \n" "\t\tListen for specific event(s). If omitted, all events are \n"
"\t\tlistened for.\n"); "\t\tlistened for.\n");
printf("\t-a|--ascending <event>\n" printf("\t-a|--ascending <event>\n"
"\t\tSort ascending by a particular event, or `total'.\n"); "\t\tSort ascending by a particular event, or `total'.\n");
 End of changes. 20 change blocks. 
21 lines changed or deleted 30 lines changed or added

Home  |  About  |  Features  |  All  |  Newest  |  Dox  |  Diffs  |  RSS Feeds  |  Screenshots  |  Comments  |  Imprint  |  Privacy  |  HTTP(S)