wendy

inotify based node watcher
git clone git://git.2f30.org/wendy
Log | Files | Refs | README | LICENSE

commit 4347b16b35ccf473b2bc53e11fb96fe79fe8f1d5
parent 87734b64361a83547623eea8e53d111992b65a9e
Author: z3bra <willy@mailoo.org>
Date:   Tue, 11 Feb 2014 18:27:26 +0100

Added more option to view and specify every mask

Diffstat:
MREADME | 38+++++++++++++++++++++++++++++++++++---
Mwendy.c | 61+++++++++++++++++++++++++++++++++++++++++++++++++++----------
2 files changed, 86 insertions(+), 13 deletions(-)

diff --git a/README b/README @@ -12,13 +12,45 @@ specific command when an event occurs. The program is made the moire simple possible, to leave the room to unlimited uses. Be creative ! +Every event raised by inotify is handled. Just sum them up to watch multiple +event at the same time. Here is the full table: +(see inotify(1) for a better explanation of those events) + + IN_ACCESS ........ 1 + IN_MODIFY ........ 2 + IN_ATTRIB ........ 4 + IN_CLOSE_WRITE ... 8 + IN_CLOSE_NOWRITE . 16 + IN_OPEN .......... 32 + IN_MOVED_FROM .... 64 + IN_MOVED_TO ...... 128 + IN_CREATE ........ 256 + IN_DELETE ........ 512 + IN_DELETE_SELF ... 1024 + IN_MOVE_SELF ..... 2048 + +To watch for both creation AND deletion in a directory, do some math: + + 256 + 512 = 768 + +then, pass that value to wendy so that she can watch after both of them (did I +just say 'she'?). + +For more convenience, the IN_CREATE, IN_DELETE and IN_MODIFY events are bound to +(respectively) -C, -D and -M. + Here are some examples: # Tell me whenever I have a new mail - wendy -C ~/mails/INBOX/new -t 60 -e espeak "You got a new mail" + wendy -C -d ~/mails/INBOX/new -t 60 -e espeak "You got a new mail" # On-the-fly recompilation - wendy -M -f ~/src/dev/program/source.c -t 1 -e make + wendy -M -q -d ~/src/dev/program/ -f source.c -t 1 -e make + # or eventually + wendy -l | grep -i close_write + IN_CLOSE_WRITE ... 8 + wendy -m 8 -q -d ~/src/dev/program/ -f source.c -t 1 -e make + # Get up to date with community based projects - wendy -D -M -C -f /mnt/nfs/project/ -t 30 -e notify-send 'project updated' + wendy -D -M -C -d /mnt/nfs/project/ -t 30 -e notify-send 'project updated' diff --git a/wendy.c b/wendy.c @@ -34,19 +34,53 @@ extern char **environ; void usage() { - fputs("usage: wendy [-C] [-D] [-M] [-d directory] [-f file] [-t timeout] " - "-e command [arguments]\n" - "\t-C : raise creation events\n" + fputs("usage: wendy [-C] [-D] [-M] [-m mask] [-l] [-f file] [-t timeout] [-q] " + "[-e command [args] ..]\n" + "\t-C : raise creation events (default)\n" "\t-D : raise deletion events\n" "\t-M : raise modification events\n" - "\t-d directory : directory to watch\n" - "\t-f file : file to watch in the directory\n" + "\t-m mask : set mask manually (see -l))\n" + "\t-l : list events mask values\n" + "\t-f file : file to watch (everything is a file)\n" "\t-t timeout : time between event check (in seconds)\n" + "\t-q : don't talk to me, program\n" "\t-e command : command to launch (must be the last argument!)\n", stdout); exit(1); } +void +list_events() +{ + fprintf(stdout, + "IN_ACCESS ........ %u\n" + "IN_MODIFY ........ %u\n" + "IN_ATTRIB ........ %u\n" + "IN_CLOSE_WRITE ... %u\n" + "IN_CLOSE_NOWRITE . %u\n" + "IN_OPEN .......... %u\n" + "IN_MOVED_FROM .... %u\n" + "IN_MOVED_TO ...... %u\n" + "IN_CREATE ........ %u\n" + "IN_DELETE ........ %u\n" + "IN_DELETE_SELF ... %u\n" + "IN_MOVE_SELF ..... %u\n", + IN_ACCESS, + IN_MODIFY, + IN_ATTRIB, + IN_CLOSE_WRITE, + IN_CLOSE_NOWRITE, + IN_OPEN, + IN_MOVED_FROM, + IN_MOVED_TO, + IN_CREATE, + IN_DELETE, + IN_DELETE_SELF, + IN_MOVE_SELF + ); + exit(0); +} + int execvpe(const char *program, char **argv, char **envp) { @@ -61,7 +95,8 @@ execvpe(const char *program, char **argv, char **envp) int main (int argc, char **argv) { - int fd, wd, len, mask = 0, i = 0, timeout = 0, ignore = 0; + int fd, wd, len, i = 0, timeout = 0, ignore = 0, quiet = 0; + uint32_t mask = 0; char buf[BUF_LEN]; char *dir = NULL, *file = NULL, **cmd = NULL; struct inotify_event *ev; @@ -69,22 +104,24 @@ main (int argc, char **argv) if ((argc == 2 && argv[1][0] == '-' && argv[1][1] == 'h')) usage(); /* parse option given. see usage() above */ - for(i = 1; (i + 1 < argc) && (argv[i][0] == '-') && !ignore; i++) { + for(i = 1; (i < argc) && (argv[i][0] == '-') && !ignore; i++) { switch (argv[i][1]) { case 'C': mask |= IN_CREATE; break; case 'D': mask |= IN_DELETE; break; case 'M': mask |= IN_MODIFY; break; + case 'm': mask |= atoi(argv[++i]); break; + case 'l': list_events(); break; + case 'q': quiet = 1; break; case 'f': file = argv[++i]; break; - case 'd': dir = argv[++i]; break; case 't': timeout = atoi(argv[++i]); break; case 'e': cmd = &argv[++i]; ignore=1; break; + default: usage(); } } /* test given arguments */ if (!dir) { dir = DEFAULT_FILE; } if (!timeout) { timeout = DEFAULT_CHECK; } - if (!mask) { mask |= IN_CREATE; } /* get file descriptor */ fd = inotify_init(); @@ -97,6 +134,10 @@ main (int argc, char **argv) if (wd < 0) perror("inotify_add_watch"); + if (!quiet) { + printf( "watching directory %s with event mask %u\n", dir, mask); + } + /* start looping */ for (;;) { /* get every event raised, and queue them */ @@ -115,7 +156,7 @@ main (int argc, char **argv) /* get events one by one */ ev = (struct inotify_event *) &buf[i]; - if (ev->len > 0) { + if (!quiet && ev->len > 0) { printf("event on file %s: %u\n", ev->name, ev->mask); }