nexmon – Blame information for rev 1
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
1 | office | 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 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 | { |
||
68 | GUnixVolumeMonitor *monitor; |
||
69 | |||
70 | monitor = G_UNIX_VOLUME_MONITOR (object); |
||
71 | |||
72 | g_signal_handlers_disconnect_by_func (monitor->mount_monitor, mountpoints_changed, monitor); |
||
73 | g_signal_handlers_disconnect_by_func (monitor->mount_monitor, mounts_changed, monitor); |
||
74 | |||
75 | g_object_unref (monitor->mount_monitor); |
||
76 | |||
77 | g_list_free_full (monitor->last_mountpoints, (GDestroyNotify) g_unix_mount_point_free); |
||
78 | g_list_free_full (monitor->last_mounts, (GDestroyNotify) g_unix_mount_free); |
||
79 | |||
80 | g_list_free_full (monitor->volumes, g_object_unref); |
||
81 | g_list_free_full (monitor->mounts, g_object_unref); |
||
82 | |||
83 | G_OBJECT_CLASS (g_unix_volume_monitor_parent_class)->finalize (object); |
||
84 | } |
||
85 | |||
86 | static void |
||
87 | g_unix_volume_monitor_dispose (GObject *object) |
||
88 | { |
||
89 | GUnixVolumeMonitor *monitor; |
||
90 | |||
91 | monitor = G_UNIX_VOLUME_MONITOR (object); |
||
92 | |||
93 | g_list_free_full (monitor->volumes, g_object_unref); |
||
94 | monitor->volumes = NULL; |
||
95 | |||
96 | g_list_free_full (monitor->mounts, g_object_unref); |
||
97 | monitor->mounts = NULL; |
||
98 | |||
99 | G_OBJECT_CLASS (g_unix_volume_monitor_parent_class)->dispose (object); |
||
100 | } |
||
101 | |||
102 | static GList * |
||
103 | get_mounts (GVolumeMonitor *volume_monitor) |
||
104 | { |
||
105 | GUnixVolumeMonitor *monitor; |
||
106 | |||
107 | monitor = G_UNIX_VOLUME_MONITOR (volume_monitor); |
||
108 | |||
109 | return g_list_copy_deep (monitor->mounts, (GCopyFunc) g_object_ref, NULL); |
||
110 | } |
||
111 | |||
112 | static GList * |
||
113 | get_volumes (GVolumeMonitor *volume_monitor) |
||
114 | { |
||
115 | GUnixVolumeMonitor *monitor; |
||
116 | |||
117 | monitor = G_UNIX_VOLUME_MONITOR (volume_monitor); |
||
118 | |||
119 | return g_list_copy_deep (monitor->volumes, (GCopyFunc) g_object_ref, NULL); |
||
120 | } |
||
121 | |||
122 | static GList * |
||
123 | get_connected_drives (GVolumeMonitor *volume_monitor) |
||
124 | { |
||
125 | return NULL; |
||
126 | } |
||
127 | |||
128 | static GVolume * |
||
129 | get_volume_for_uuid (GVolumeMonitor *volume_monitor, const char *uuid) |
||
130 | { |
||
131 | return NULL; |
||
132 | } |
||
133 | |||
134 | static GMount * |
||
135 | get_mount_for_uuid (GVolumeMonitor *volume_monitor, const char *uuid) |
||
136 | { |
||
137 | return NULL; |
||
138 | } |
||
139 | |||
140 | static gboolean |
||
141 | is_supported (void) |
||
142 | { |
||
143 | return TRUE; |
||
144 | } |
||
145 | |||
146 | static GMount * |
||
147 | get_mount_for_mount_path (const char *mount_path, |
||
148 | GCancellable *cancellable) |
||
149 | { |
||
150 | GUnixMountEntry *mount_entry; |
||
151 | GUnixMount *mount; |
||
152 | |||
153 | mount_entry = g_unix_mount_at (mount_path, NULL); |
||
154 | |||
155 | if (!mount_entry) |
||
156 | return NULL; |
||
157 | |||
158 | /* TODO: Set mountable volume? */ |
||
159 | mount = _g_unix_mount_new (NULL, mount_entry, NULL); |
||
160 | |||
161 | g_unix_mount_free (mount_entry); |
||
162 | |||
163 | return G_MOUNT (mount); |
||
164 | } |
||
165 | |||
166 | static void |
||
167 | g_unix_volume_monitor_class_init (GUnixVolumeMonitorClass *klass) |
||
168 | { |
||
169 | GObjectClass *gobject_class = G_OBJECT_CLASS (klass); |
||
170 | GVolumeMonitorClass *monitor_class = G_VOLUME_MONITOR_CLASS (klass); |
||
171 | GNativeVolumeMonitorClass *native_class = G_NATIVE_VOLUME_MONITOR_CLASS (klass); |
||
172 | |||
173 | gobject_class->finalize = g_unix_volume_monitor_finalize; |
||
174 | gobject_class->dispose = g_unix_volume_monitor_dispose; |
||
175 | |||
176 | monitor_class->get_mounts = get_mounts; |
||
177 | monitor_class->get_volumes = get_volumes; |
||
178 | monitor_class->get_connected_drives = get_connected_drives; |
||
179 | monitor_class->get_volume_for_uuid = get_volume_for_uuid; |
||
180 | monitor_class->get_mount_for_uuid = get_mount_for_uuid; |
||
181 | monitor_class->is_supported = is_supported; |
||
182 | |||
183 | native_class->get_mount_for_mount_path = get_mount_for_mount_path; |
||
184 | } |
||
185 | |||
186 | static void |
||
187 | mountpoints_changed (GUnixMountMonitor *mount_monitor, |
||
188 | gpointer user_data) |
||
189 | { |
||
190 | GUnixVolumeMonitor *unix_monitor = user_data; |
||
191 | |||
192 | /* Update both to make sure volumes are created before mounts */ |
||
193 | update_volumes (unix_monitor); |
||
194 | update_mounts (unix_monitor); |
||
195 | } |
||
196 | |||
197 | static void |
||
198 | mounts_changed (GUnixMountMonitor *mount_monitor, |
||
199 | gpointer user_data) |
||
200 | { |
||
201 | GUnixVolumeMonitor *unix_monitor = user_data; |
||
202 | |||
203 | /* Update both to make sure volumes are created before mounts */ |
||
204 | update_volumes (unix_monitor); |
||
205 | update_mounts (unix_monitor); |
||
206 | } |
||
207 | |||
208 | static void |
||
209 | g_unix_volume_monitor_init (GUnixVolumeMonitor *unix_monitor) |
||
210 | { |
||
211 | |||
212 | unix_monitor->mount_monitor = g_unix_mount_monitor_get (); |
||
213 | |||
214 | g_signal_connect (unix_monitor->mount_monitor, |
||
215 | "mounts-changed", G_CALLBACK (mounts_changed), |
||
216 | unix_monitor); |
||
217 | |||
218 | g_signal_connect (unix_monitor->mount_monitor, |
||
219 | "mountpoints-changed", G_CALLBACK (mountpoints_changed), |
||
220 | unix_monitor); |
||
221 | |||
222 | update_volumes (unix_monitor); |
||
223 | update_mounts (unix_monitor); |
||
224 | } |
||
225 | |||
226 | GVolumeMonitor * |
||
227 | _g_unix_volume_monitor_new (void) |
||
228 | { |
||
229 | GUnixVolumeMonitor *monitor; |
||
230 | |||
231 | monitor = g_object_new (G_TYPE_UNIX_VOLUME_MONITOR, NULL); |
||
232 | |||
233 | return G_VOLUME_MONITOR (monitor); |
||
234 | } |
||
235 | |||
236 | static void |
||
237 | diff_sorted_lists (GList *list1, |
||
238 | GList *list2, |
||
239 | GCompareFunc compare, |
||
240 | GList **added, |
||
241 | GList **removed) |
||
242 | { |
||
243 | int order; |
||
244 | |||
245 | *added = *removed = NULL; |
||
246 | |||
247 | while (list1 != NULL && |
||
248 | list2 != NULL) |
||
249 | { |
||
250 | order = (*compare) (list1->data, list2->data); |
||
251 | if (order < 0) |
||
252 | { |
||
253 | *removed = g_list_prepend (*removed, list1->data); |
||
254 | list1 = list1->next; |
||
255 | } |
||
256 | else if (order > 0) |
||
257 | { |
||
258 | *added = g_list_prepend (*added, list2->data); |
||
259 | list2 = list2->next; |
||
260 | } |
||
261 | else |
||
262 | { /* same item */ |
||
263 | list1 = list1->next; |
||
264 | list2 = list2->next; |
||
265 | } |
||
266 | } |
||
267 | |||
268 | while (list1 != NULL) |
||
269 | { |
||
270 | *removed = g_list_prepend (*removed, list1->data); |
||
271 | list1 = list1->next; |
||
272 | } |
||
273 | while (list2 != NULL) |
||
274 | { |
||
275 | *added = g_list_prepend (*added, list2->data); |
||
276 | list2 = list2->next; |
||
277 | } |
||
278 | } |
||
279 | |||
280 | GUnixVolume * |
||
281 | _g_unix_volume_monitor_lookup_volume_for_mount_path (GUnixVolumeMonitor *monitor, |
||
282 | const char *mount_path) |
||
283 | { |
||
284 | GList *l; |
||
285 | |||
286 | for (l = monitor->volumes; l != NULL; l = l->next) |
||
287 | { |
||
288 | GUnixVolume *volume = l->data; |
||
289 | |||
290 | if (_g_unix_volume_has_mount_path (volume, mount_path)) |
||
291 | return volume; |
||
292 | } |
||
293 | |||
294 | return NULL; |
||
295 | } |
||
296 | |||
297 | static GUnixMount * |
||
298 | find_mount_by_mountpath (GUnixVolumeMonitor *monitor, |
||
299 | const char *mount_path) |
||
300 | { |
||
301 | GList *l; |
||
302 | |||
303 | for (l = monitor->mounts; l != NULL; l = l->next) |
||
304 | { |
||
305 | GUnixMount *mount = l->data; |
||
306 | |||
307 | if (_g_unix_mount_has_mount_path (mount, mount_path)) |
||
308 | return mount; |
||
309 | } |
||
310 | |||
311 | return NULL; |
||
312 | } |
||
313 | |||
314 | static void |
||
315 | update_volumes (GUnixVolumeMonitor *monitor) |
||
316 | { |
||
317 | GList *new_mountpoints; |
||
318 | GList *removed, *added; |
||
319 | GList *l; |
||
320 | GUnixVolume *volume; |
||
321 | |||
322 | new_mountpoints = g_unix_mount_points_get (NULL); |
||
323 | |||
324 | new_mountpoints = g_list_sort (new_mountpoints, (GCompareFunc) g_unix_mount_point_compare); |
||
325 | |||
326 | diff_sorted_lists (monitor->last_mountpoints, |
||
327 | new_mountpoints, (GCompareFunc) g_unix_mount_point_compare, |
||
328 | &added, &removed); |
||
329 | |||
330 | for (l = removed; l != NULL; l = l->next) |
||
331 | { |
||
332 | GUnixMountPoint *mountpoint = l->data; |
||
333 | |||
334 | volume = _g_unix_volume_monitor_lookup_volume_for_mount_path (monitor, |
||
335 | g_unix_mount_point_get_mount_path (mountpoint)); |
||
336 | if (volume) |
||
337 | { |
||
338 | _g_unix_volume_disconnected (volume); |
||
339 | monitor->volumes = g_list_remove (monitor->volumes, volume); |
||
340 | g_signal_emit_by_name (monitor, "volume-removed", volume); |
||
341 | g_signal_emit_by_name (volume, "removed"); |
||
342 | g_object_unref (volume); |
||
343 | } |
||
344 | } |
||
345 | |||
346 | for (l = added; l != NULL; l = l->next) |
||
347 | { |
||
348 | GUnixMountPoint *mountpoint = l->data; |
||
349 | |||
350 | volume = _g_unix_volume_new (G_VOLUME_MONITOR (monitor), mountpoint); |
||
351 | if (volume) |
||
352 | { |
||
353 | monitor->volumes = g_list_prepend (monitor->volumes, volume); |
||
354 | g_signal_emit_by_name (monitor, "volume-added", volume); |
||
355 | } |
||
356 | } |
||
357 | |||
358 | g_list_free (added); |
||
359 | g_list_free (removed); |
||
360 | g_list_free_full (monitor->last_mountpoints, (GDestroyNotify) g_unix_mount_point_free); |
||
361 | monitor->last_mountpoints = new_mountpoints; |
||
362 | } |
||
363 | |||
364 | static void |
||
365 | update_mounts (GUnixVolumeMonitor *monitor) |
||
366 | { |
||
367 | GList *new_mounts; |
||
368 | GList *removed, *added; |
||
369 | GList *l; |
||
370 | GUnixMount *mount; |
||
371 | GUnixVolume *volume; |
||
372 | const char *mount_path; |
||
373 | |||
374 | new_mounts = g_unix_mounts_get (NULL); |
||
375 | |||
376 | new_mounts = g_list_sort (new_mounts, (GCompareFunc) g_unix_mount_compare); |
||
377 | |||
378 | diff_sorted_lists (monitor->last_mounts, |
||
379 | new_mounts, (GCompareFunc) g_unix_mount_compare, |
||
380 | &added, &removed); |
||
381 | |||
382 | for (l = removed; l != NULL; l = l->next) |
||
383 | { |
||
384 | GUnixMountEntry *mount_entry = l->data; |
||
385 | |||
386 | mount = find_mount_by_mountpath (monitor, g_unix_mount_get_mount_path (mount_entry)); |
||
387 | if (mount) |
||
388 | { |
||
389 | _g_unix_mount_unmounted (mount); |
||
390 | monitor->mounts = g_list_remove (monitor->mounts, mount); |
||
391 | g_signal_emit_by_name (monitor, "mount-removed", mount); |
||
392 | g_signal_emit_by_name (mount, "unmounted"); |
||
393 | g_object_unref (mount); |
||
394 | } |
||
395 | } |
||
396 | |||
397 | for (l = added; l != NULL; l = l->next) |
||
398 | { |
||
399 | GUnixMountEntry *mount_entry = l->data; |
||
400 | |||
401 | mount_path = g_unix_mount_get_mount_path (mount_entry); |
||
402 | |||
403 | volume = _g_unix_volume_monitor_lookup_volume_for_mount_path (monitor, mount_path); |
||
404 | mount = _g_unix_mount_new (G_VOLUME_MONITOR (monitor), mount_entry, volume); |
||
405 | if (mount) |
||
406 | { |
||
407 | monitor->mounts = g_list_prepend (monitor->mounts, mount); |
||
408 | g_signal_emit_by_name (monitor, "mount-added", mount); |
||
409 | } |
||
410 | } |
||
411 | |||
412 | g_list_free (added); |
||
413 | g_list_free (removed); |
||
414 | g_list_free_full (monitor->last_mounts, (GDestroyNotify) g_unix_mount_free); |
||
415 | monitor->last_mounts = new_mounts; |
||
416 | } |