| @@ -21,39 +21,60 @@ |
| 21 | 21 | | #include <string.h> |
| 22 | 22 | | #include <unistd.h> |
| 23 | 23 | | #include <sys/inotify.h> |
| 24 | | − | #include <libgen.h> |
| 25 | 24 | | |
| 26 | 25 | | #include "util.h" |
| 27 | 26 | | #include "autoreload.h" |
| 28 | 27 | | |
| 28 | + | void arl_init(arl_t *arl) |
| 29 | + | { |
| 30 | + | arl->fd = inotify_init1(IN_CLOEXEC | IN_NONBLOCK); |
| 31 | + | arl->wd_dir = arl->wd_file = -1; |
| 32 | + | if (arl->fd == -1) |
| 33 | + | error(0, 0, "Could not initialize inotify, no automatic image reloading"); |
| 34 | + | } |
| 35 | + | |
| 29 | 36 | | CLEANUP void arl_cleanup(arl_t *arl) |
| 30 | 37 | | { |
| 31 | 38 | | if (arl->fd != -1) |
| 32 | 39 | | close(arl->fd); |
| 40 | + | free(arl->filename); |
| 33 | 41 | | } |
| 34 | 42 | | |
| 35 | | − | static void arl_setup_dir(arl_t *arl, const char *filepath) |
| 43 | + | static void rm_watch(int fd, int *wd) |
| 36 | 44 | | { |
| 37 | | − | char *dntmp, *dn; |
| 45 | + | if (*wd != -1) { |
| 46 | + | inotify_rm_watch(fd, *wd); |
| 47 | + | *wd = -1; |
| 48 | + | } |
| 49 | + | } |
| 50 | + | |
| 51 | + | static void add_watch(int fd, int *wd, const char *path, uint32_t mask) |
| 52 | + | { |
| 53 | + | *wd = inotify_add_watch(fd, path, mask); |
| 54 | + | if (*wd == -1) |
| 55 | + | error(0, errno, "inotify: %s", path); |
| 56 | + | } |
| 57 | + | |
| 58 | + | void arl_setup(arl_t *arl, const char *filepath) |
| 59 | + | { |
| 60 | + | char *base = strrchr(filepath, '/'); |
| 38 | 61 | | |
| 39 | 62 | | if (arl->fd == -1) |
| 40 | 63 | | return; |
| 41 | 64 | | |
| 42 | | − | /* get dirname */ |
| 43 | | − | dntmp = (char*) strdup(filepath); |
| 44 | | − | dn = (char*) dirname(dntmp); |
| 45 | | − | |
| 46 | | − | /* this is not one-shot as other stuff may be created too |
| 47 | | − | * note: we won't handle deletion of the directory itself, |
| 48 | | − | * this is a design decision |
| 49 | | − | */ |
| 50 | | − | arl->wd = inotify_add_watch(arl->fd, dn, IN_CREATE); |
| 51 | | − | if (arl->wd == -1) |
| 52 | | − | error(0, 0, "%s: Error watching directory", dn); |
| 53 | | − | else |
| 54 | | − | arl->watching_dir = true; |
| 55 | | − | |
| 56 | | − | free(dntmp); |
| 65 | + | rm_watch(arl->fd, &arl->wd_dir); |
| 66 | + | rm_watch(arl->fd, &arl->wd_file); |
| 67 | + | |
| 68 | + | add_watch(arl->fd, &arl->wd_file, filepath, IN_CLOSE_WRITE | IN_DELETE_SELF); |
| 69 | + | |
| 70 | + | free(arl->filename); |
| 71 | + | arl->filename = estrdup(filepath); |
| 72 | + | |
| 73 | + | if (base != NULL) { |
| 74 | + | arl->filename[++base - filepath] = '\0'; |
| 75 | + | add_watch(arl->fd, &arl->wd_dir, arl->filename, IN_CREATE | IN_MOVED_TO); |
| 76 | + | strcpy(arl->filename, base); |
| 77 | + | } |
| 57 | 78 | | } |
| 58 | 79 | | |
| 59 | 80 | | union { |
| @@ -61,7 +82,7 @@ union { |
| 61 | 82 | | struct inotify_event e; |
| 62 | 83 | | } buf; |
| 63 | 84 | | |
| 64 | | − | bool arl_handle(arl_t *arl, const char *filepath) |
| 85 | + | bool arl_handle(arl_t *arl) |
| 65 | 86 | | { |
| 66 | 87 | | bool reload = false; |
| 67 | 88 | | char *ptr; |
| @@ -77,59 +98,16 @@ bool arl_handle(arl_t *arl, const char *filepath) |
| 77 | 98 | | } |
| 78 | 99 | | for (ptr = buf.d; ptr < buf.d + len; ptr += sizeof(*event) + event->len) { |
| 79 | 100 | | event = (const struct inotify_event*) ptr; |
| 80 | | − | |
| 81 | | − | /* events from watching the file itself */ |
| 82 | 101 | | if (event->mask & IN_CLOSE_WRITE) { |
| 83 | 102 | | reload = true; |
| 84 | | − | } |
| 85 | | − | if (event->mask & IN_DELETE_SELF) |
| 86 | | − | arl_setup_dir(arl, filepath); |
| 87 | | − | |
| 88 | | − | /* events from watching the file's directory */ |
| 89 | | − | if (event->mask & IN_CREATE) { |
| 90 | | − | char *fntmp = strdup(filepath); |
| 91 | | − | char *fn = basename(fntmp); |
| 92 | | − | |
| 93 | | − | if (STREQ(event->name, fn)) { |
| 94 | | − | /* this is the file we're looking for */ |
| 95 | | − | |
| 96 | | − | /* cleanup, this has not been one-shot */ |
| 97 | | − | if (arl->watching_dir) { |
| 98 | | − | inotify_rm_watch(arl->fd, arl->wd); |
| 99 | | − | arl->watching_dir = false; |
| 100 | | − | } |
| 103 | + | } else if (event->mask & IN_DELETE_SELF) { |
| 104 | + | rm_watch(arl->fd, &arl->wd_file); |
| 105 | + | } else if (event->mask & (IN_CREATE | IN_MOVED_TO)) { |
| 106 | + | if (STREQ(event->name, arl->filename)) |
| 101 | 107 | | reload = true; |
| 102 | | − | } |
| 103 | | − | free(fntmp); |
| 104 | 108 | | } |
| 105 | 109 | | } |
| 106 | 110 | | } |
| 107 | 111 | | return reload; |
| 108 | 112 | | } |
| 109 | 113 | | |
| 110 | | − | void arl_init(arl_t *arl) |
| 111 | | − | { |
| 112 | | − | /* this needs to be done only once */ |
| 113 | | − | arl->fd = inotify_init1(IN_CLOEXEC | IN_NONBLOCK); |
| 114 | | − | arl->watching_dir = false; |
| 115 | | − | if (arl->fd == -1) |
| 116 | | − | error(0, 0, "Could not initialize inotify, no automatic image reloading"); |
| 117 | | − | } |
| 118 | | − | |
| 119 | | − | void arl_setup(arl_t *arl, const char *filepath) |
| 120 | | − | { |
| 121 | | − | if (arl->fd == -1) |
| 122 | | − | return; |
| 123 | | − | |
| 124 | | − | /* may have switched from a deleted to another image */ |
| 125 | | − | if (arl->watching_dir) { |
| 126 | | − | inotify_rm_watch(arl->fd, arl->wd); |
| 127 | | − | arl->watching_dir = false; |
| 128 | | − | } |
| 129 | | − | |
| 130 | | − | arl->wd = inotify_add_watch(arl->fd, filepath, |
| 131 | | − | IN_ONESHOT | IN_CLOSE_WRITE | IN_DELETE_SELF); |
| 132 | | − | if (arl->wd == -1) |
| 133 | | − | error(0, 0, "%s: Error watching file", filepath); |
| 134 | | − | } |
| 135 | | − | |