Mercurial > hg > rlgwebd
comparison dglwatcher.c @ 167:fba1b34e7554
Rename watcher to dglwatcher.
| author | John "Elwin" Edwards |
|---|---|
| date | Wed, 07 Jan 2015 13:49:37 -0500 |
| parents | watcher.c@66fef65c34e7 |
| children |
comparison
equal
deleted
inserted
replaced
| 166:66fef65c34e7 | 167:fba1b34e7554 |
|---|---|
| 1 #include <stdio.h> | |
| 2 #include <stdlib.h> | |
| 3 #include <string.h> | |
| 4 #include <sys/inotify.h> | |
| 5 #include <sys/select.h> | |
| 6 #include <unistd.h> | |
| 7 #include <limits.h> | |
| 8 #include <dirent.h> | |
| 9 | |
| 10 struct watchdir { | |
| 11 int wd; | |
| 12 char *name; | |
| 13 }; | |
| 14 | |
| 15 char ibuf[sizeof(struct inotify_event) + NAME_MAX + 1]; | |
| 16 | |
| 17 int startwatch(int ifd, char *dir, struct watchdir *w) { | |
| 18 DIR *dstream; | |
| 19 struct dirent *ent; | |
| 20 | |
| 21 w->name = dir; | |
| 22 w->wd = inotify_add_watch(ifd, dir, IN_CREATE|IN_DELETE|IN_DELETE_SELF); | |
| 23 if (w->wd < 0) { | |
| 24 fprintf(stderr, "Could not watch %s\n", dir); | |
| 25 return 1; | |
| 26 } | |
| 27 dstream = opendir(dir); | |
| 28 if (dstream == NULL) { | |
| 29 fprintf(stderr, "%s is not a readable directory\n", dir); | |
| 30 inotify_rm_watch(ifd, w->wd); | |
| 31 w->wd = -1; | |
| 32 return 1; | |
| 33 } | |
| 34 ent = readdir(dstream); | |
| 35 while (ent != NULL) { | |
| 36 if (strcmp(ent->d_name, ".") && strcmp(ent->d_name, "..")) | |
| 37 printf("E %s/%s\n", dir, ent->d_name); | |
| 38 ent = readdir(dstream); | |
| 39 } | |
| 40 closedir(dstream); | |
| 41 fflush(stdout); | |
| 42 return 0; | |
| 43 } | |
| 44 | |
| 45 int main(int argc, char *argv[]) { | |
| 46 int ifd, rsize, off, done, nwatchers, i, result; | |
| 47 char typecode; | |
| 48 struct inotify_event *iev; | |
| 49 fd_set rfds; | |
| 50 struct watchdir *watchers; | |
| 51 | |
| 52 done = 0; | |
| 53 nwatchers = argc - 1; | |
| 54 iev = (struct inotify_event *) ibuf; | |
| 55 ifd = inotify_init(); | |
| 56 if (nwatchers == 0) { | |
| 57 watchers = malloc(sizeof(struct watchdir)); | |
| 58 nwatchers = 1; | |
| 59 startwatch(ifd, ".", watchers); | |
| 60 } | |
| 61 else { | |
| 62 watchers = malloc(nwatchers * sizeof(struct watchdir)); | |
| 63 for (i = 0; i < nwatchers; i++) { | |
| 64 startwatch(ifd, argv[i+1], watchers + i); | |
| 65 } | |
| 66 } | |
| 67 | |
| 68 while (!done) { | |
| 69 FD_ZERO(&rfds); | |
| 70 FD_SET(0, &rfds); | |
| 71 FD_SET(ifd, &rfds); | |
| 72 result = select(ifd + 1, &rfds, NULL, NULL, NULL); | |
| 73 if (result < 0) { | |
| 74 /* Something went wrong. */ | |
| 75 break; | |
| 76 } | |
| 77 if (FD_ISSET(ifd, &rfds)) { | |
| 78 off = 0; | |
| 79 rsize = read(ifd, ibuf, sizeof(struct inotify_event) + NAME_MAX + 1); | |
| 80 while (off < rsize) { | |
| 81 iev = (struct inotify_event *) (ibuf + off); | |
| 82 if (iev->mask & IN_CREATE) | |
| 83 typecode = 'C'; | |
| 84 else if (iev->mask & IN_DELETE) | |
| 85 typecode = 'D'; | |
| 86 else | |
| 87 typecode = '?'; | |
| 88 for (i = 0; i < nwatchers; i++) { | |
| 89 if (watchers[i].wd == iev->wd) | |
| 90 printf("%c %s/%s\n", typecode, watchers[i].name, iev->name); | |
| 91 } | |
| 92 off += sizeof(struct inotify_event) + iev->len; | |
| 93 } | |
| 94 fflush(stdout); | |
| 95 } | |
| 96 if (FD_ISSET(0, &rfds)) { | |
| 97 result = read(0, &typecode, 1); | |
| 98 if (result <= 0) { | |
| 99 /* EOF on stdin: controlling process probably died. */ | |
| 100 done = 1; | |
| 101 } | |
| 102 if (typecode == '\n') | |
| 103 done = 1; | |
| 104 } | |
| 105 | |
| 106 } | |
| 107 close(ifd); | |
| 108 return 0; | |
| 109 } |
