obt/watch.c (raw)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 |
/* -*- indent-tabs-mode: nil; tab-width: 4; c-basic-offset: 4; -*- obt/watch.c for the Openbox window manager Copyright (c) 2010 Dana Jansens This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. See the COPYING file for a copy of the GNU General Public License. */ #include "obt/watch.h" #ifdef HAVE_SYS_INOTIFY_H # include <sys/inotify.h> #endif #ifdef HAVE_UNISTD_H # include <unistd.h> #endif #include <errno.h> struct _ObtWatch { guint ref; gint ino_fd; guint ino_watch; GHashTable *targets; #ifdef HAVE_SYS_INOTIFY_H GHashTable *targets_by_wd; #endif }; typedef struct _ObtWatchTarget { ObtWatch *w; #ifdef HAVE_SYS_INOTIFY_H gint wd; #endif gchar *path; ObtWatchFunc func; gpointer data; } ObtWatchTarget; static void init_inot(ObtWatch *w); static gboolean read_inot(GIOChannel *s, GIOCondition cond, gpointer data); static gboolean add_inot(ObtWatch *w, ObtWatchTarget *t, const char *path, gboolean dir); static void rm_inot(ObtWatchTarget *t); static ObtWatchTarget* target_new(ObtWatch *w, const gchar *path, ObtWatchFunc func, gpointer data); static void target_free(ObtWatchTarget *t); ObtWatch* obt_watch_new() { ObtWatch *w; w = g_slice_new(ObtWatch); w->ref = 1; w->ino_fd = -1; w->targets = g_hash_table_new_full(g_str_hash, g_str_equal, NULL, (GDestroyNotify)target_free); #ifdef HAVE_SYS_INOTIFY_H w->targets_by_wd = g_hash_table_new(g_int_hash, g_int_equal); #endif init_inot(w); return w; } void obt_watch_ref(ObtWatch *w) { ++w->ref; } void obt_watch_unref(ObtWatch *w) { if (--w->ref < 1) { if (w->ino_fd >= 0 && w->ino_watch) g_source_remove(w->ino_watch); g_hash_table_destroy(w->targets); g_hash_table_destroy(w->targets_by_wd); g_slice_free(ObtWatch, w); } } static void init_inot(ObtWatch *w) { #ifdef HAVE_SYS_INOTIFY_H if (w->ino_fd >= 0) return; w->ino_fd = inotify_init(); if (w->ino_fd >= 0) { GIOChannel *ch; ch = g_io_channel_unix_new(w->ino_fd); w->ino_watch = g_io_add_watch(ch, G_IO_IN | G_IO_HUP | G_IO_ERR, read_inot, w); g_io_channel_unref(ch); } #endif } static gboolean read_inot(GIOChannel *src, GIOCondition cond, gpointer data) { #ifdef HAVE_SYS_INOTIFY_H ObtWatch *w = data; ObtWatchTarget *t; struct inotify_event s; gint len; guint ilen; char *name; /* read the event */ for (ilen = 0; ilen < sizeof(s); ilen += len) { len = read(w->ino_fd, ((char*)&s)+ilen, sizeof(s)-ilen); if (len < 0 && errno != EINTR) return FALSE; /* error, don't repeat */ if (!len) return TRUE; /* nothing there */ } name = g_new(char, s.len); /* read the filename */ for (ilen = 0; ilen < s.len; ilen += len) { len = read(w->ino_fd, name+ilen, s.len-ilen); if (len < 0 && errno != EINTR) return FALSE; /* error, don't repeat */ if (!len) return TRUE; /* nothing there */ } t = g_hash_table_lookup(w->targets, &s.wd); if (t) t->func(w, name, t->data); g_free(name); #endif return TRUE; /* repeat */ } static gboolean add_inot(ObtWatch *w, ObtWatchTarget *t, const char *path, gboolean dir) { #ifndef HAVE_SYS_INOTIFY_H return FALSE; #else gint mask; if (w->ino_fd < 0) return FALSE; if (dir) mask = IN_MODIFY | IN_CREATE | IN_DELETE | IN_MOVE; else g_assert_not_reached(); t->wd = inotify_add_watch(w->ino_fd, path, mask); return TRUE; #endif } static void rm_inot(ObtWatchTarget *t) { #ifdef HAVE_SYS_INOTIFY_H if (t->w->ino_fd < 0) return; if (t->wd < 0) return; inotify_rm_watch(t->w->ino_fd, t->wd); #endif } static ObtWatchTarget* target_new(ObtWatch *w, const gchar *path, ObtWatchFunc func, gpointer data) { ObtWatchTarget *t; t = g_slice_new0(ObtWatchTarget); t->w = w; t->wd = -1; t->path = g_strdup(path); t->func = func; t->data = data; if (!add_inot(w, t, path, TRUE)) { g_assert_not_reached(); /* XXX do something */ } #ifndef HAVE_SYS_INOTIFY_H #error need inotify for now #endif return t; } static void target_free(ObtWatchTarget *t) { rm_inot(t); g_free(t->path); g_slice_free(ObtWatchTarget, t); } void obt_paths_watch_dir(ObtWatch *w, const gchar *path, ObtWatchFunc func, gpointer data) { ObtWatchTarget *t; g_return_if_fail(w != NULL); g_return_if_fail(path != NULL); g_return_if_fail(data != NULL); t = target_new(w, path, func, data); g_hash_table_insert(w->targets, t->path, t); #ifdef HAVE_SYS_INOTIFY_H g_hash_table_insert(w->targets_by_wd, &t->wd, t); #endif } void obt_paths_unwatch_dir(ObtWatch *w, const gchar *path) { ObtWatchTarget *t; g_return_if_fail(w != NULL); g_return_if_fail(path != NULL); t = g_hash_table_lookup(w->targets, path); if (t) { #ifdef HAVE_SYS_INOTIFY_H g_hash_table_remove(w->targets_by_wd, &t->wd); #endif g_hash_table_remove(w->targets, path); } } |