Mercurial > hg > rlgwebd
comparison watcher.c @ 155:245a2959f504
Begin support for watching dgamelaunch games.
watcher.c is a subprocess which uses inotify to watch the inprogress
directories. A C program is used because node's fs.watch() can't tell
the difference between creation and deletion.
| author | John "Elwin" Edwards |
|---|---|
| date | Tue, 01 Apr 2014 11:23:25 -0700 |
| parents | |
| children | 66fef65c34e7 |
comparison
equal
deleted
inserted
replaced
| 154:b5a1430d0f71 | 155:245a2959f504 |
|---|---|
| 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; | |
| 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 select(ifd + 1, &rfds, NULL, NULL, NULL); | |
| 73 if (FD_ISSET(ifd, &rfds)) { | |
| 74 off = 0; | |
| 75 rsize = read(ifd, ibuf, sizeof(struct inotify_event) + NAME_MAX + 1); | |
| 76 while (off < rsize) { | |
| 77 iev = (struct inotify_event *) (ibuf + off); | |
| 78 if (iev->mask & IN_CREATE) | |
| 79 typecode = 'C'; | |
| 80 else if (iev->mask & IN_DELETE) | |
| 81 typecode = 'D'; | |
| 82 else | |
| 83 typecode = '?'; | |
| 84 for (i = 0; i < nwatchers; i++) { | |
| 85 if (watchers[i].wd == iev->wd) | |
| 86 printf("%c %s/%s\n", typecode, watchers[i].name, iev->name); | |
| 87 } | |
| 88 off += sizeof(struct inotify_event) + iev->len; | |
| 89 } | |
| 90 fflush(stdout); | |
| 91 } | |
| 92 if (FD_ISSET(0, &rfds)) { | |
| 93 read(0, &typecode, 1); | |
| 94 if (typecode == '\n') | |
| 95 done = 1; | |
| 96 } | |
| 97 | |
| 98 } | |
| 99 close(ifd); | |
| 100 return 0; | |
| 101 } |
