nsxiv9ac8fc6
Fix code-style in autoreload_inotify.c
+23 −38
1 file changed
- M autoreload_inotify.c +23 −38
| @@ -27,9 +27,8 @@ | |||
| 27 | 27 | ||
| 28 | 28 | CLEANUP void arl_cleanup(arl_t *arl) | |
| 29 | 29 | { | |
| 30 | − | if (arl->fd != -1 && arl->wd != -1) | |
| 31 | − | { | |
| 32 | − | if(inotify_rm_watch(arl->fd, arl->wd)) | |
| 30 | + | if (arl->fd != -1 && arl->wd != -1) { | |
| 31 | + | if (inotify_rm_watch(arl->fd, arl->wd)) | |
| 33 | 32 | error(0, 0, "Failed to remove inotify watch."); | |
| 34 | 33 | } | |
| 35 | 34 | } | |
| @@ -38,8 +37,7 @@ static void arl_setup_dir(arl_t *arl, const char *filepath) | |||
| 38 | 37 | { | |
| 39 | 38 | char *dntmp, *dn; | |
| 40 | 39 | ||
| 41 | − | if (arl->fd == -1) | |
| 42 | − | { | |
| 40 | + | if (arl->fd == -1) { | |
| 43 | 41 | error(0, 0, "Uninitialized, could not add inotify watch on directory."); | |
| 44 | 42 | return; | |
| 45 | 43 | } | |
| @@ -49,9 +47,10 @@ static void arl_setup_dir(arl_t *arl, const char *filepath) | |||
| 49 | 47 | dn = (char*) dirname(dntmp); | |
| 50 | 48 | ||
| 51 | 49 | /* this is not one-shot as other stuff may be created too | |
| 52 | − | note: we won't handle deletion of the directory itself, | |
| 53 | − | this is a design decision */ | |
| 54 | − | arl->wd = inotify_add_watch(arl->fd, dn,IN_CREATE); | |
| 50 | + | * note: we won't handle deletion of the directory itself, | |
| 51 | + | * this is a design decision | |
| 52 | + | */ | |
| 53 | + | arl->wd = inotify_add_watch(arl->fd, dn, IN_CREATE); | |
| 55 | 54 | if (arl->wd == -1) | |
| 56 | 55 | error(0, 0, "Failed to add inotify watch on directory '%s'.", dn); | |
| 57 | 56 | else | |
| @@ -63,52 +62,40 @@ static void arl_setup_dir(arl_t *arl, const char *filepath) | |||
| 63 | 62 | bool arl_handle(arl_t *arl, const char *filepath) | |
| 64 | 63 | { | |
| 65 | 64 | bool reload = false; | |
| 66 | − | ssize_t len; | |
| 67 | 65 | char buf[4096] __attribute__ ((aligned(__alignof__(struct inotify_event)))); | |
| 68 | − | const struct inotify_event *event; | |
| 69 | 66 | char *ptr; | |
| 70 | − | char *fntmp, *fn; | |
| 67 | + | const struct inotify_event *event; | |
| 68 | + | ||
| 69 | + | ssize_t len = read(arl->fd, buf, sizeof(buf)); | |
| 71 | 70 | ||
| 72 | − | len = read(arl->fd, buf, sizeof buf); | |
| 73 | − | if (len == -1) | |
| 74 | − | { | |
| 71 | + | if (len == -1) { | |
| 75 | 72 | error(0, 0, "Failed to read inotify events."); | |
| 76 | 73 | return false; | |
| 77 | 74 | } | |
| 78 | − | ||
| 79 | − | for (ptr = buf; ptr < buf + len; | |
| 80 | − | ptr += sizeof(struct inotify_event) + event->len) | |
| 81 | − | { | |
| 82 | − | ||
| 83 | − | event = (const struct inotify_event *) ptr; | |
| 75 | + | for (ptr = buf; ptr < buf + len; ptr += sizeof(*event) + event->len) { | |
| 76 | + | event = (const struct inotify_event*) ptr; | |
| 84 | 77 | ||
| 85 | 78 | /* events from watching the file itself */ | |
| 86 | − | if (event->mask & IN_CLOSE_WRITE) | |
| 87 | − | { | |
| 79 | + | if (event->mask & IN_CLOSE_WRITE) { | |
| 88 | 80 | reload = true; | |
| 89 | 81 | } | |
| 90 | − | ||
| 91 | 82 | if (event->mask & IN_DELETE_SELF) | |
| 92 | 83 | arl_setup_dir(arl, filepath); | |
| 93 | 84 | ||
| 94 | 85 | /* events from watching the file's directory */ | |
| 95 | − | if (event->mask & IN_CREATE) | |
| 96 | − | { | |
| 97 | − | fntmp = strdup(filepath); | |
| 98 | − | fn = basename(fntmp); | |
| 86 | + | if (event->mask & IN_CREATE) { | |
| 87 | + | char *fntmp = strdup(filepath); | |
| 88 | + | char *fn = basename(fntmp); | |
| 99 | 89 | ||
| 100 | − | if (0 == strcmp(event->name, fn)) | |
| 101 | − | { | |
| 90 | + | if (STREQ(event->name, fn)) { | |
| 102 | 91 | /* this is the file we're looking for */ | |
| 103 | 92 | ||
| 104 | 93 | /* cleanup, this has not been one-shot */ | |
| 105 | − | if (arl->watching_dir) | |
| 106 | − | { | |
| 107 | − | if(inotify_rm_watch(arl->fd, arl->wd)) | |
| 94 | + | if (arl->watching_dir) { | |
| 95 | + | if (inotify_rm_watch(arl->fd, arl->wd)) | |
| 108 | 96 | error(0, 0, "Failed to remove inotify watch."); | |
| 109 | 97 | arl->watching_dir = false; | |
| 110 | 98 | } | |
| 111 | − | ||
| 112 | 99 | reload = true; | |
| 113 | 100 | } | |
| 114 | 101 | free(fntmp); | |
| @@ -128,22 +115,20 @@ void arl_init(arl_t *arl) | |||
| 128 | 115 | ||
| 129 | 116 | void arl_setup(arl_t *arl, const char *filepath) | |
| 130 | 117 | { | |
| 131 | − | if (arl->fd == -1) | |
| 132 | − | { | |
| 118 | + | if (arl->fd == -1) { | |
| 133 | 119 | error(0, 0, "Uninitialized, could not add inotify watch."); | |
| 134 | 120 | return; | |
| 135 | 121 | } | |
| 136 | 122 | ||
| 137 | 123 | /* may have switched from a deleted to another image */ | |
| 138 | − | if (arl->watching_dir) | |
| 139 | − | { | |
| 124 | + | if (arl->watching_dir) { | |
| 140 | 125 | if (inotify_rm_watch(arl->fd, arl->wd)) | |
| 141 | 126 | error(0, 0, "Failed to remove inotify watch."); | |
| 142 | 127 | arl->watching_dir = false; | |
| 143 | 128 | } | |
| 144 | 129 | ||
| 145 | 130 | arl->wd = inotify_add_watch(arl->fd, filepath, | |
| 146 | − | IN_ONESHOT | IN_CLOSE_WRITE | IN_DELETE_SELF); | |
| 131 | + | IN_ONESHOT | IN_CLOSE_WRITE | IN_DELETE_SELF); | |
| 147 | 132 | if (arl->wd == -1) | |
| 148 | 133 | error(0, 0, "Failed to add inotify watch on file '%s'.", filepath); | |
| 149 | 134 | } | |