/src/glib/gio/gunixvolumemonitor.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */ |
2 | | |
3 | | /* GIO - GLib Input, Output and Streaming Library |
4 | | * |
5 | | * Copyright (C) 2006-2007 Red Hat, Inc. |
6 | | * |
7 | | * This library is free software; you can redistribute it and/or |
8 | | * modify it under the terms of the GNU Lesser General Public |
9 | | * License as published by the Free Software Foundation; either |
10 | | * version 2.1 of the License, or (at your option) any later version. |
11 | | * |
12 | | * This library is distributed in the hope that it will be useful, |
13 | | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
15 | | * Lesser General Public License for more details. |
16 | | * |
17 | | * You should have received a copy of the GNU Lesser General |
18 | | * Public License along with this library; if not, see <http://www.gnu.org/licenses/>. |
19 | | * |
20 | | * Author: Alexander Larsson <alexl@redhat.com> |
21 | | * David Zeuthen <davidz@redhat.com> |
22 | | */ |
23 | | |
24 | | #include "config.h" |
25 | | |
26 | | #include <string.h> |
27 | | |
28 | | #include <glib.h> |
29 | | #include "gunixvolumemonitor.h" |
30 | | #include "gunixmounts.h" |
31 | | #include "gunixmount.h" |
32 | | #include "gunixvolume.h" |
33 | | #include "gmount.h" |
34 | | #include "gmountprivate.h" |
35 | | #include "giomodule.h" |
36 | | #include "glibintl.h" |
37 | | |
38 | | |
39 | | struct _GUnixVolumeMonitor { |
40 | | GNativeVolumeMonitor parent; |
41 | | |
42 | | GUnixMountMonitor *mount_monitor; |
43 | | |
44 | | GList *last_mountpoints; |
45 | | GList *last_mounts; |
46 | | |
47 | | GList *volumes; |
48 | | GList *mounts; |
49 | | }; |
50 | | |
51 | | static void mountpoints_changed (GUnixMountMonitor *mount_monitor, |
52 | | gpointer user_data); |
53 | | static void mounts_changed (GUnixMountMonitor *mount_monitor, |
54 | | gpointer user_data); |
55 | | static void update_volumes (GUnixVolumeMonitor *monitor); |
56 | | static void update_mounts (GUnixVolumeMonitor *monitor); |
57 | | |
58 | | #define g_unix_volume_monitor_get_type _g_unix_volume_monitor_get_type |
59 | | G_DEFINE_TYPE_WITH_CODE (GUnixVolumeMonitor, g_unix_volume_monitor, G_TYPE_NATIVE_VOLUME_MONITOR, |
60 | | g_io_extension_point_implement (G_NATIVE_VOLUME_MONITOR_EXTENSION_POINT_NAME, |
61 | | g_define_type_id, |
62 | | "unix", |
63 | | 0)); |
64 | | |
65 | | static void |
66 | | g_unix_volume_monitor_finalize (GObject *object) |
67 | 0 | { |
68 | 0 | GUnixVolumeMonitor *monitor; |
69 | | |
70 | 0 | monitor = G_UNIX_VOLUME_MONITOR (object); |
71 | |
|
72 | 0 | g_signal_handlers_disconnect_by_func (monitor->mount_monitor, mountpoints_changed, monitor); |
73 | 0 | g_signal_handlers_disconnect_by_func (monitor->mount_monitor, mounts_changed, monitor); |
74 | | |
75 | 0 | g_object_unref (monitor->mount_monitor); |
76 | |
|
77 | 0 | g_list_free_full (monitor->last_mountpoints, (GDestroyNotify) g_unix_mount_point_free); |
78 | 0 | g_list_free_full (monitor->last_mounts, (GDestroyNotify) g_unix_mount_free); |
79 | |
|
80 | 0 | g_list_free_full (monitor->volumes, g_object_unref); |
81 | 0 | g_list_free_full (monitor->mounts, g_object_unref); |
82 | |
|
83 | 0 | G_OBJECT_CLASS (g_unix_volume_monitor_parent_class)->finalize (object); |
84 | 0 | } |
85 | | |
86 | | static void |
87 | | g_unix_volume_monitor_dispose (GObject *object) |
88 | 0 | { |
89 | 0 | GUnixVolumeMonitor *monitor; |
90 | |
|
91 | 0 | monitor = G_UNIX_VOLUME_MONITOR (object); |
92 | |
|
93 | 0 | g_list_free_full (monitor->volumes, g_object_unref); |
94 | 0 | monitor->volumes = NULL; |
95 | | |
96 | 0 | g_list_free_full (monitor->mounts, g_object_unref); |
97 | 0 | monitor->mounts = NULL; |
98 | | |
99 | 0 | G_OBJECT_CLASS (g_unix_volume_monitor_parent_class)->dispose (object); |
100 | 0 | } |
101 | | |
102 | | static GList * |
103 | | get_mounts (GVolumeMonitor *volume_monitor) |
104 | 0 | { |
105 | 0 | GUnixVolumeMonitor *monitor; |
106 | | |
107 | 0 | monitor = G_UNIX_VOLUME_MONITOR (volume_monitor); |
108 | |
|
109 | 0 | return g_list_copy_deep (monitor->mounts, (GCopyFunc) g_object_ref, NULL); |
110 | 0 | } |
111 | | |
112 | | static GList * |
113 | | get_volumes (GVolumeMonitor *volume_monitor) |
114 | 0 | { |
115 | 0 | GUnixVolumeMonitor *monitor; |
116 | | |
117 | 0 | monitor = G_UNIX_VOLUME_MONITOR (volume_monitor); |
118 | |
|
119 | 0 | return g_list_copy_deep (monitor->volumes, (GCopyFunc) g_object_ref, NULL); |
120 | 0 | } |
121 | | |
122 | | static GList * |
123 | | get_connected_drives (GVolumeMonitor *volume_monitor) |
124 | 0 | { |
125 | 0 | return NULL; |
126 | 0 | } |
127 | | |
128 | | static GVolume * |
129 | | get_volume_for_uuid (GVolumeMonitor *volume_monitor, const char *uuid) |
130 | 0 | { |
131 | 0 | return NULL; |
132 | 0 | } |
133 | | |
134 | | static GMount * |
135 | | get_mount_for_uuid (GVolumeMonitor *volume_monitor, const char *uuid) |
136 | 0 | { |
137 | 0 | return NULL; |
138 | 0 | } |
139 | | |
140 | | static gboolean |
141 | | is_supported (void) |
142 | 0 | { |
143 | 0 | return TRUE; |
144 | 0 | } |
145 | | |
146 | | static GMount * |
147 | | get_mount_for_mount_path (const char *mount_path, |
148 | | GCancellable *cancellable) |
149 | 0 | { |
150 | 0 | GUnixMountEntry *mount_entry; |
151 | 0 | GUnixMount *mount; |
152 | |
|
153 | 0 | mount_entry = g_unix_mount_at (mount_path, NULL); |
154 | |
|
155 | 0 | if (!mount_entry) |
156 | 0 | return NULL; |
157 | | |
158 | | /* TODO: Set mountable volume? */ |
159 | 0 | mount = _g_unix_mount_new (NULL, mount_entry, NULL); |
160 | |
|
161 | 0 | g_unix_mount_free (mount_entry); |
162 | |
|
163 | 0 | return G_MOUNT (mount); |
164 | 0 | } |
165 | | |
166 | | static void |
167 | | g_unix_volume_monitor_class_init (GUnixVolumeMonitorClass *klass) |
168 | 0 | { |
169 | 0 | GObjectClass *gobject_class = G_OBJECT_CLASS (klass); |
170 | 0 | GVolumeMonitorClass *monitor_class = G_VOLUME_MONITOR_CLASS (klass); |
171 | 0 | GNativeVolumeMonitorClass *native_class = G_NATIVE_VOLUME_MONITOR_CLASS (klass); |
172 | | |
173 | 0 | gobject_class->finalize = g_unix_volume_monitor_finalize; |
174 | 0 | gobject_class->dispose = g_unix_volume_monitor_dispose; |
175 | |
|
176 | 0 | monitor_class->get_mounts = get_mounts; |
177 | 0 | monitor_class->get_volumes = get_volumes; |
178 | 0 | monitor_class->get_connected_drives = get_connected_drives; |
179 | 0 | monitor_class->get_volume_for_uuid = get_volume_for_uuid; |
180 | 0 | monitor_class->get_mount_for_uuid = get_mount_for_uuid; |
181 | 0 | monitor_class->is_supported = is_supported; |
182 | |
|
183 | 0 | native_class->get_mount_for_mount_path = get_mount_for_mount_path; |
184 | 0 | } |
185 | | |
186 | | void |
187 | | _g_unix_volume_monitor_update (GUnixVolumeMonitor *unix_monitor) |
188 | 0 | { |
189 | | /* Update both to make sure volumes are created before mounts */ |
190 | 0 | update_volumes (unix_monitor); |
191 | 0 | update_mounts (unix_monitor); |
192 | 0 | } |
193 | | |
194 | | static void |
195 | | mountpoints_changed (GUnixMountMonitor *mount_monitor, |
196 | | gpointer user_data) |
197 | 0 | { |
198 | 0 | GUnixVolumeMonitor *unix_monitor = user_data; |
199 | |
|
200 | 0 | _g_unix_volume_monitor_update (unix_monitor); |
201 | 0 | } |
202 | | |
203 | | static void |
204 | | mounts_changed (GUnixMountMonitor *mount_monitor, |
205 | | gpointer user_data) |
206 | 0 | { |
207 | 0 | GUnixVolumeMonitor *unix_monitor = user_data; |
208 | |
|
209 | 0 | _g_unix_volume_monitor_update (unix_monitor); |
210 | 0 | } |
211 | | |
212 | | static void |
213 | | g_unix_volume_monitor_init (GUnixVolumeMonitor *unix_monitor) |
214 | 0 | { |
215 | |
|
216 | 0 | unix_monitor->mount_monitor = g_unix_mount_monitor_get (); |
217 | |
|
218 | 0 | g_signal_connect (unix_monitor->mount_monitor, |
219 | 0 | "mounts-changed", G_CALLBACK (mounts_changed), |
220 | 0 | unix_monitor); |
221 | | |
222 | 0 | g_signal_connect (unix_monitor->mount_monitor, |
223 | 0 | "mountpoints-changed", G_CALLBACK (mountpoints_changed), |
224 | 0 | unix_monitor); |
225 | | |
226 | 0 | _g_unix_volume_monitor_update (unix_monitor); |
227 | 0 | } |
228 | | |
229 | | GVolumeMonitor * |
230 | | _g_unix_volume_monitor_new (void) |
231 | 0 | { |
232 | 0 | GUnixVolumeMonitor *monitor; |
233 | |
|
234 | 0 | monitor = g_object_new (G_TYPE_UNIX_VOLUME_MONITOR, NULL); |
235 | | |
236 | 0 | return G_VOLUME_MONITOR (monitor); |
237 | 0 | } |
238 | | |
239 | | static void |
240 | | diff_sorted_lists (GList *list1, |
241 | | GList *list2, |
242 | | GCompareFunc compare, |
243 | | GList **added, |
244 | | GList **removed) |
245 | 0 | { |
246 | 0 | int order; |
247 | | |
248 | 0 | *added = *removed = NULL; |
249 | | |
250 | 0 | while (list1 != NULL && |
251 | 0 | list2 != NULL) |
252 | 0 | { |
253 | 0 | order = (*compare) (list1->data, list2->data); |
254 | 0 | if (order < 0) |
255 | 0 | { |
256 | 0 | *removed = g_list_prepend (*removed, list1->data); |
257 | 0 | list1 = list1->next; |
258 | 0 | } |
259 | 0 | else if (order > 0) |
260 | 0 | { |
261 | 0 | *added = g_list_prepend (*added, list2->data); |
262 | 0 | list2 = list2->next; |
263 | 0 | } |
264 | 0 | else |
265 | 0 | { /* same item */ |
266 | 0 | list1 = list1->next; |
267 | 0 | list2 = list2->next; |
268 | 0 | } |
269 | 0 | } |
270 | |
|
271 | 0 | while (list1 != NULL) |
272 | 0 | { |
273 | 0 | *removed = g_list_prepend (*removed, list1->data); |
274 | 0 | list1 = list1->next; |
275 | 0 | } |
276 | 0 | while (list2 != NULL) |
277 | 0 | { |
278 | 0 | *added = g_list_prepend (*added, list2->data); |
279 | 0 | list2 = list2->next; |
280 | 0 | } |
281 | 0 | } |
282 | | |
283 | | GUnixVolume * |
284 | | _g_unix_volume_monitor_lookup_volume_for_mount_path (GUnixVolumeMonitor *monitor, |
285 | | const char *mount_path) |
286 | 0 | { |
287 | 0 | GList *l; |
288 | |
|
289 | 0 | for (l = monitor->volumes; l != NULL; l = l->next) |
290 | 0 | { |
291 | 0 | GUnixVolume *volume = l->data; |
292 | |
|
293 | 0 | if (_g_unix_volume_has_mount_path (volume, mount_path)) |
294 | 0 | return volume; |
295 | 0 | } |
296 | | |
297 | 0 | return NULL; |
298 | 0 | } |
299 | | |
300 | | static GUnixMount * |
301 | | find_mount_by_mountpath (GUnixVolumeMonitor *monitor, |
302 | | const char *mount_path) |
303 | 0 | { |
304 | 0 | GList *l; |
305 | |
|
306 | 0 | for (l = monitor->mounts; l != NULL; l = l->next) |
307 | 0 | { |
308 | 0 | GUnixMount *mount = l->data; |
309 | |
|
310 | 0 | if (_g_unix_mount_has_mount_path (mount, mount_path)) |
311 | 0 | return mount; |
312 | 0 | } |
313 | | |
314 | 0 | return NULL; |
315 | 0 | } |
316 | | |
317 | | static void |
318 | | update_volumes (GUnixVolumeMonitor *monitor) |
319 | 0 | { |
320 | 0 | GList *new_mountpoints; |
321 | 0 | GList *removed, *added; |
322 | 0 | GList *l; |
323 | 0 | GUnixVolume *volume; |
324 | | |
325 | 0 | new_mountpoints = g_unix_mount_points_get (NULL); |
326 | | |
327 | 0 | new_mountpoints = g_list_sort (new_mountpoints, (GCompareFunc) g_unix_mount_point_compare); |
328 | | |
329 | 0 | diff_sorted_lists (monitor->last_mountpoints, |
330 | 0 | new_mountpoints, (GCompareFunc) g_unix_mount_point_compare, |
331 | 0 | &added, &removed); |
332 | | |
333 | 0 | for (l = removed; l != NULL; l = l->next) |
334 | 0 | { |
335 | 0 | GUnixMountPoint *mountpoint = l->data; |
336 | | |
337 | 0 | volume = _g_unix_volume_monitor_lookup_volume_for_mount_path (monitor, |
338 | 0 | g_unix_mount_point_get_mount_path (mountpoint)); |
339 | 0 | if (volume) |
340 | 0 | { |
341 | 0 | _g_unix_volume_disconnected (volume); |
342 | 0 | monitor->volumes = g_list_remove (monitor->volumes, volume); |
343 | 0 | g_signal_emit_by_name (monitor, "volume-removed", volume); |
344 | 0 | g_signal_emit_by_name (volume, "removed"); |
345 | 0 | g_object_unref (volume); |
346 | 0 | } |
347 | 0 | } |
348 | | |
349 | 0 | for (l = added; l != NULL; l = l->next) |
350 | 0 | { |
351 | 0 | GUnixMountPoint *mountpoint = l->data; |
352 | | |
353 | 0 | volume = _g_unix_volume_new (G_VOLUME_MONITOR (monitor), mountpoint); |
354 | 0 | if (volume) |
355 | 0 | { |
356 | 0 | monitor->volumes = g_list_prepend (monitor->volumes, volume); |
357 | 0 | g_signal_emit_by_name (monitor, "volume-added", volume); |
358 | 0 | } |
359 | 0 | } |
360 | | |
361 | 0 | g_list_free (added); |
362 | 0 | g_list_free (removed); |
363 | 0 | g_list_free_full (monitor->last_mountpoints, (GDestroyNotify) g_unix_mount_point_free); |
364 | 0 | monitor->last_mountpoints = new_mountpoints; |
365 | 0 | } |
366 | | |
367 | | static void |
368 | | update_mounts (GUnixVolumeMonitor *monitor) |
369 | 0 | { |
370 | 0 | GList *new_mounts; |
371 | 0 | GList *removed, *added; |
372 | 0 | GList *l; |
373 | 0 | GUnixMount *mount; |
374 | 0 | GUnixVolume *volume; |
375 | 0 | const char *mount_path; |
376 | | |
377 | 0 | new_mounts = g_unix_mounts_get (NULL); |
378 | | |
379 | 0 | new_mounts = g_list_sort (new_mounts, (GCompareFunc) g_unix_mount_compare); |
380 | | |
381 | 0 | diff_sorted_lists (monitor->last_mounts, |
382 | 0 | new_mounts, (GCompareFunc) g_unix_mount_compare, |
383 | 0 | &added, &removed); |
384 | | |
385 | 0 | for (l = removed; l != NULL; l = l->next) |
386 | 0 | { |
387 | 0 | GUnixMountEntry *mount_entry = l->data; |
388 | | |
389 | 0 | mount = find_mount_by_mountpath (monitor, g_unix_mount_get_mount_path (mount_entry)); |
390 | 0 | if (mount) |
391 | 0 | { |
392 | 0 | _g_unix_mount_unmounted (mount); |
393 | 0 | monitor->mounts = g_list_remove (monitor->mounts, mount); |
394 | 0 | g_signal_emit_by_name (monitor, "mount-removed", mount); |
395 | 0 | g_signal_emit_by_name (mount, "unmounted"); |
396 | 0 | g_object_unref (mount); |
397 | 0 | } |
398 | 0 | } |
399 | | |
400 | 0 | for (l = added; l != NULL; l = l->next) |
401 | 0 | { |
402 | 0 | GUnixMountEntry *mount_entry = l->data; |
403 | |
|
404 | 0 | mount_path = g_unix_mount_get_mount_path (mount_entry); |
405 | | |
406 | 0 | volume = _g_unix_volume_monitor_lookup_volume_for_mount_path (monitor, mount_path); |
407 | 0 | mount = _g_unix_mount_new (G_VOLUME_MONITOR (monitor), mount_entry, volume); |
408 | 0 | if (mount) |
409 | 0 | { |
410 | 0 | monitor->mounts = g_list_prepend (monitor->mounts, mount); |
411 | 0 | g_signal_emit_by_name (monitor, "mount-added", mount); |
412 | 0 | } |
413 | 0 | } |
414 | | |
415 | 0 | g_list_free (added); |
416 | 0 | g_list_free (removed); |
417 | 0 | g_list_free_full (monitor->last_mounts, (GDestroyNotify) g_unix_mount_free); |
418 | 0 | monitor->last_mounts = new_mounts; |
419 | 0 | } |