| 1 |
/* WirePlumber |
| 2 |
* |
| 3 |
* Copyright © 2021 Collabora Ltd. |
| 4 |
* @author Julian Bouzas <[email protected]> |
| 5 |
* |
| 6 |
* SPDX-License-Identifier: MIT |
| 7 |
*/ |
| 8 |
|
| 9 |
#include <stdio.h> |
| 10 |
|
| 11 |
#include <wp/wp.h> |
| 12 |
|
| 13 |
WP_DEFINE_LOCAL_LOG_TOPIC ("m-file-monitor-api") |
| 14 |
|
| 15 |
struct _WpFileMonitorApi |
| 16 |
{ |
| 17 |
WpPlugin parent; |
| 18 |
|
| 19 |
GHashTable *monitors; |
| 20 |
}; |
| 21 |
|
| 22 |
enum { |
| 23 |
ACTION_ADD_WATCH, |
| 24 |
ACTION_REMOVE_WATCH, |
| 25 |
SIGNAL_CHANGED, |
| 26 |
N_SIGNALS |
| 27 |
}; |
| 28 |
|
| 29 |
static guint signals[N_SIGNALS] = {0}; |
| 30 |
|
| 31 |
G_DECLARE_FINAL_TYPE (WpFileMonitorApi, wp_file_monitor_api, WP, |
| 32 |
FILE_MONITOR_API, WpPlugin) |
| 33 |
G_DEFINE_TYPE (WpFileMonitorApi, wp_file_monitor_api, WP_TYPE_PLUGIN) |
| 34 |
|
| 35 |
static void |
| 36 |
wp_file_monitor_api_init (WpFileMonitorApi * self) |
| 37 |
{ |
| 38 |
self->monitors = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, |
| 39 |
g_object_unref); |
| 40 |
} |
| 41 |
|
| 42 |
static void |
| 43 |
wp_file_monitor_api_finalize (GObject * object) |
| 44 |
{ |
| 45 |
WpFileMonitorApi * self = WP_FILE_MONITOR_API (object); |
| 46 |
|
| 47 |
g_clear_pointer (&self->monitors, g_hash_table_unref); |
| 48 |
|
| 49 |
G_OBJECT_CLASS (wp_file_monitor_api_parent_class)->finalize (object); |
| 50 |
} |
| 51 |
|
| 52 |
static void |
| 53 |
on_file_monitor_changed (GFileMonitor *monitor, GFile *file, GFile *other, |
| 54 |
GFileMonitorEvent evtype, gpointer data) |
| 55 |
{ |
| 56 |
WpFileMonitorApi * self = WP_FILE_MONITOR_API (data); |
| 57 |
|
| 58 |
g_autofree char *fpath = g_file_get_path (file); |
| 59 |
g_autofree char *opath = NULL; |
| 60 |
const gchar *evtype_str = NULL; |
| 61 |
|
| 62 |
if (other) |
| 63 |
opath = g_file_get_path (other); |
| 64 |
|
| 65 |
switch(evtype) { |
| 66 |
case G_FILE_MONITOR_EVENT_CHANGED: |
| 67 |
evtype_str = "changed"; |
| 68 |
break; |
| 69 |
case G_FILE_MONITOR_EVENT_CHANGES_DONE_HINT: |
| 70 |
evtype_str = "changes-done-hint"; |
| 71 |
break; |
| 72 |
case G_FILE_MONITOR_EVENT_DELETED: |
| 73 |
evtype_str = "deleted"; |
| 74 |
break; |
| 75 |
case G_FILE_MONITOR_EVENT_CREATED: |
| 76 |
evtype_str = "created"; |
| 77 |
break; |
| 78 |
case G_FILE_MONITOR_EVENT_ATTRIBUTE_CHANGED: |
| 79 |
evtype_str = "attribute-changed"; |
| 80 |
break; |
| 81 |
case G_FILE_MONITOR_EVENT_PRE_UNMOUNT: |
| 82 |
evtype_str = "pre-unmount"; |
| 83 |
break; |
| 84 |
case G_FILE_MONITOR_EVENT_UNMOUNTED: |
| 85 |
evtype_str = "unmounted"; |
| 86 |
break; |
| 87 |
case G_FILE_MONITOR_EVENT_MOVED: |
| 88 |
evtype_str = "moved"; |
| 89 |
break; |
| 90 |
case G_FILE_MONITOR_EVENT_RENAMED: |
| 91 |
evtype_str = "renamed"; |
| 92 |
break; |
| 93 |
case G_FILE_MONITOR_EVENT_MOVED_IN: |
| 94 |
evtype_str = "moved-in"; |
| 95 |
break; |
| 96 |
case G_FILE_MONITOR_EVENT_MOVED_OUT: |
| 97 |
evtype_str = "moved-out"; |
| 98 |
break; |
| 99 |
default: |
| 100 |
wp_warning_object (self, "Unknown event type %d", evtype); |
| 101 |
break; |
| 102 |
} |
| 103 |
|
| 104 |
g_signal_emit (self, signals[SIGNAL_CHANGED], 0, fpath, opath, evtype_str); |
| 105 |
} |
| 106 |
|
| 107 |
static gboolean |
| 108 |
wp_file_monitor_api_add_watch (WpFileMonitorApi * self, const gchar *path, |
| 109 |
const gchar *flags_str) |
| 110 |
{ |
| 111 |
g_autoptr (GError) e = NULL; |
| 112 |
g_autoptr (GFileMonitor) fm = NULL; |
| 113 |
g_autoptr (GFile) f = NULL; |
| 114 |
GFileMonitorFlags flags = G_FILE_MONITOR_NONE; |
| 115 |
|
| 116 |
/* don't do anything if the path is already being watched */ |
| 117 |
if (g_hash_table_contains (self->monitors, path)) |
| 118 |
return TRUE; |
| 119 |
|
| 120 |
/* get path */ |
| 121 |
f = g_file_new_for_path (path); |
| 122 |
if (!f) { |
| 123 |
wp_warning_object (self, "Invalid path '%s'", path); |
| 124 |
return FALSE; |
| 125 |
} |
| 126 |
|
| 127 |
/* parse flags */ |
| 128 |
for (guint i = 0; flags_str && i < strlen (flags_str); i++) { |
| 129 |
switch (flags_str[i]) { |
| 130 |
case 'o': flags |= G_FILE_MONITOR_WATCH_MOUNTS; break; |
| 131 |
case 's': flags |= G_FILE_MONITOR_SEND_MOVED; break; |
| 132 |
case 'h': flags |= G_FILE_MONITOR_WATCH_HARD_LINKS; break; |
| 133 |
case 'm': flags |= G_FILE_MONITOR_WATCH_MOVES; break; |
| 134 |
default: |
| 135 |
break; |
| 136 |
} |
| 137 |
} |
| 138 |
|
| 139 |
/* create the file monitor for that path */ |
| 140 |
fm = g_file_monitor (f, flags, NULL, &e); |
| 141 |
if (e) { |
| 142 |
wp_warning_object (self, "Failed to add watch for path '%s': %s", path, |
| 143 |
e->message); |
| 144 |
return FALSE; |
| 145 |
} |
| 146 |
|
| 147 |
/* handle changed signal and add it to monitors table */ |
| 148 |
g_signal_connect (fm, "changed", G_CALLBACK (on_file_monitor_changed), self); |
| 149 |
g_hash_table_insert (self->monitors, g_strdup (path), g_steal_pointer (&fm)); |
| 150 |
return TRUE; |
| 151 |
} |
| 152 |
|
| 153 |
static void |
| 154 |
wp_file_monitor_api_remove_watch (WpFileMonitorApi * self, const gchar *path) |
| 155 |
{ |
| 156 |
g_hash_table_remove (self->monitors, path); |
| 157 |
} |
| 158 |
|
| 159 |
static void |
| 160 |
wp_file_monitor_api_enable (WpPlugin * plugin, WpTransition * transition) |
| 161 |
{ |
| 162 |
WpFileMonitorApi * self = WP_FILE_MONITOR_API (plugin); |
| 163 |
|
| 164 |
wp_object_update_features (WP_OBJECT (self), WP_PLUGIN_FEATURE_ENABLED, 0); |
| 165 |
} |
| 166 |
|
| 167 |
static void |
| 168 |
wp_file_monitor_api_disable (WpPlugin * plugin) |
| 169 |
{ |
| 170 |
WpFileMonitorApi * self = WP_FILE_MONITOR_API (plugin); |
| 171 |
|
| 172 |
g_hash_table_remove_all (self->monitors); |
| 173 |
} |
| 174 |
|
| 175 |
static void |
| 176 |
wp_file_monitor_api_class_init (WpFileMonitorApiClass * klass) |
| 177 |
{ |
| 178 |
GObjectClass *object_class = (GObjectClass *) klass; |
| 179 |
WpPluginClass *plugin_class = (WpPluginClass *) klass; |
| 180 |
|
| 181 |
object_class->finalize = wp_file_monitor_api_finalize; |
| 182 |
|
| 183 |
plugin_class->enable = wp_file_monitor_api_enable; |
| 184 |
plugin_class->disable = wp_file_monitor_api_disable; |
| 185 |
|
| 186 |
signals[ACTION_ADD_WATCH] = g_signal_new_class_handler ( |
| 187 |
"add-watch", G_TYPE_FROM_CLASS (klass), |
| 188 |
G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION, |
| 189 |
(GCallback) wp_file_monitor_api_add_watch, |
| 190 |
NULL, NULL, NULL, |
| 191 |
G_TYPE_BOOLEAN, 2, G_TYPE_STRING, G_TYPE_STRING); |
| 192 |
|
| 193 |
signals[ACTION_REMOVE_WATCH] = g_signal_new_class_handler ( |
| 194 |
"remove-watch", G_TYPE_FROM_CLASS (klass), |
| 195 |
G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION, |
| 196 |
(GCallback) wp_file_monitor_api_remove_watch, |
| 197 |
NULL, NULL, NULL, |
| 198 |
G_TYPE_NONE, 1, G_TYPE_STRING); |
| 199 |
|
| 200 |
signals[SIGNAL_CHANGED] = g_signal_new ( |
| 201 |
"changed", G_TYPE_FROM_CLASS (klass), |
| 202 |
G_SIGNAL_RUN_LAST, 0, NULL, NULL, NULL, |
| 203 |
G_TYPE_NONE, 3, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING); |
| 204 |
} |
| 205 |
|
| 206 |
WP_PLUGIN_EXPORT GObject * |
| 207 |
wireplumber__module_init (WpCore * core, WpSpaJson * args, GError ** error) |
| 208 |
{ |
| 209 |
return G_OBJECT (g_object_new (wp_file_monitor_api_get_type (), |
| 210 |
"name", "file-monitor-api", |
| 211 |
"core", core, |
| 212 |
NULL)); |
| 213 |
} |