1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
3 /* GIO - GLib Input, Output and Streaming Library
5 * Copyright (C) 2006-2007 Red Hat, Inc.
7 * SPDX-License-Identifier: LGPL-2.1-or-later
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General
20 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
22 * Author: Alexander Larsson <alexl@redhat.com>
23 * David Zeuthen <davidz@redhat.com>
31 #include "gunixvolumemonitor.h"
32 #include "gunixmounts.h"
33 #include "gunixmount.h"
34 #include "gunixvolume.h"
36 #include "gmountprivate.h"
37 #include "giomodule.h"
41 struct _GUnixVolumeMonitor {
42 GNativeVolumeMonitor parent;
44 GUnixMountMonitor *mount_monitor;
46 GList *last_mountpoints;
53 static void mountpoints_changed (GUnixMountMonitor *mount_monitor,
55 static void mounts_changed (GUnixMountMonitor *mount_monitor,
57 static void update_volumes (GUnixVolumeMonitor *monitor);
58 static void update_mounts (GUnixVolumeMonitor *monitor);
60 #define g_unix_volume_monitor_get_type _g_unix_volume_monitor_get_type
61 G_DEFINE_TYPE_WITH_CODE (GUnixVolumeMonitor, g_unix_volume_monitor, G_TYPE_NATIVE_VOLUME_MONITOR,
62 g_io_extension_point_implement (G_NATIVE_VOLUME_MONITOR_EXTENSION_POINT_NAME,
68 g_unix_volume_monitor_finalize (GObject *object)
70 GUnixVolumeMonitor *monitor;
72 monitor = G_UNIX_VOLUME_MONITOR (object);
74 g_signal_handlers_disconnect_by_func (monitor->mount_monitor, mountpoints_changed, monitor);
75 g_signal_handlers_disconnect_by_func (monitor->mount_monitor, mounts_changed, monitor);
77 g_object_unref (monitor->mount_monitor);
79 g_list_free_full (monitor->last_mountpoints, (GDestroyNotify) g_unix_mount_point_free);
80 g_list_free_full (monitor->last_mounts, (GDestroyNotify) g_unix_mount_free);
82 g_list_free_full (monitor->volumes, g_object_unref);
83 g_list_free_full (monitor->mounts, g_object_unref);
85 G_OBJECT_CLASS (g_unix_volume_monitor_parent_class)->finalize (object);
89 g_unix_volume_monitor_dispose (GObject *object)
91 GUnixVolumeMonitor *monitor;
93 monitor = G_UNIX_VOLUME_MONITOR (object);
95 g_list_free_full (monitor->volumes, g_object_unref);
96 monitor->volumes = NULL;
98 g_list_free_full (monitor->mounts, g_object_unref);
99 monitor->mounts = NULL;
101 G_OBJECT_CLASS (g_unix_volume_monitor_parent_class)->dispose (object);
105 get_mounts (GVolumeMonitor *volume_monitor)
107 GUnixVolumeMonitor *monitor;
109 monitor = G_UNIX_VOLUME_MONITOR (volume_monitor);
111 return g_list_copy_deep (monitor->mounts, (GCopyFunc) g_object_ref, NULL);
115 get_volumes (GVolumeMonitor *volume_monitor)
117 GUnixVolumeMonitor *monitor;
119 monitor = G_UNIX_VOLUME_MONITOR (volume_monitor);
121 return g_list_copy_deep (monitor->volumes, (GCopyFunc) g_object_ref, NULL);
125 get_connected_drives (GVolumeMonitor *volume_monitor)
131 get_volume_for_uuid (GVolumeMonitor *volume_monitor, const char *uuid)
137 get_mount_for_uuid (GVolumeMonitor *volume_monitor, const char *uuid)
149 get_mount_for_mount_path (const char *mount_path,
150 GCancellable *cancellable)
152 GUnixMountEntry *mount_entry;
155 mount_entry = g_unix_mount_at (mount_path, NULL);
160 /* TODO: Set mountable volume? */
161 mount = _g_unix_mount_new (NULL, mount_entry, NULL);
163 g_unix_mount_free (mount_entry);
165 return G_MOUNT (mount);
169 g_unix_volume_monitor_class_init (GUnixVolumeMonitorClass *klass)
171 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
172 GVolumeMonitorClass *monitor_class = G_VOLUME_MONITOR_CLASS (klass);
173 GNativeVolumeMonitorClass *native_class = G_NATIVE_VOLUME_MONITOR_CLASS (klass);
175 gobject_class->finalize = g_unix_volume_monitor_finalize;
176 gobject_class->dispose = g_unix_volume_monitor_dispose;
178 monitor_class->get_mounts = get_mounts;
179 monitor_class->get_volumes = get_volumes;
180 monitor_class->get_connected_drives = get_connected_drives;
181 monitor_class->get_volume_for_uuid = get_volume_for_uuid;
182 monitor_class->get_mount_for_uuid = get_mount_for_uuid;
183 monitor_class->is_supported = is_supported;
185 native_class->get_mount_for_mount_path = get_mount_for_mount_path;
189 _g_unix_volume_monitor_update (GUnixVolumeMonitor *unix_monitor)
191 /* Update both to make sure volumes are created before mounts */
192 update_volumes (unix_monitor);
193 update_mounts (unix_monitor);
197 mountpoints_changed (GUnixMountMonitor *mount_monitor,
200 GUnixVolumeMonitor *unix_monitor = user_data;
202 _g_unix_volume_monitor_update (unix_monitor);
206 mounts_changed (GUnixMountMonitor *mount_monitor,
209 GUnixVolumeMonitor *unix_monitor = user_data;
211 _g_unix_volume_monitor_update (unix_monitor);
215 g_unix_volume_monitor_init (GUnixVolumeMonitor *unix_monitor)
218 unix_monitor->mount_monitor = g_unix_mount_monitor_get ();
220 g_signal_connect (unix_monitor->mount_monitor,
221 "mounts-changed", G_CALLBACK (mounts_changed),
224 g_signal_connect (unix_monitor->mount_monitor,
225 "mountpoints-changed", G_CALLBACK (mountpoints_changed),
228 _g_unix_volume_monitor_update (unix_monitor);
232 _g_unix_volume_monitor_new (void)
234 GUnixVolumeMonitor *monitor;
236 monitor = g_object_new (G_TYPE_UNIX_VOLUME_MONITOR, NULL);
238 return G_VOLUME_MONITOR (monitor);
242 diff_sorted_lists (GList *list1,
244 GCompareFunc compare,
250 *added = *removed = NULL;
252 while (list1 != NULL &&
255 order = (*compare) (list1->data, list2->data);
258 *removed = g_list_prepend (*removed, list1->data);
263 *added = g_list_prepend (*added, list2->data);
273 while (list1 != NULL)
275 *removed = g_list_prepend (*removed, list1->data);
278 while (list2 != NULL)
280 *added = g_list_prepend (*added, list2->data);
286 _g_unix_volume_monitor_lookup_volume_for_mount_path (GUnixVolumeMonitor *monitor,
287 const char *mount_path)
291 for (l = monitor->volumes; l != NULL; l = l->next)
293 GUnixVolume *volume = l->data;
295 if (_g_unix_volume_has_mount_path (volume, mount_path))
303 find_mount_by_mountpath (GUnixVolumeMonitor *monitor,
304 const char *mount_path)
308 for (l = monitor->mounts; l != NULL; l = l->next)
310 GUnixMount *mount = l->data;
312 if (_g_unix_mount_has_mount_path (mount, mount_path))
320 update_volumes (GUnixVolumeMonitor *monitor)
322 GList *new_mountpoints;
323 GList *removed, *added;
327 new_mountpoints = g_unix_mount_points_get (NULL);
329 new_mountpoints = g_list_sort (new_mountpoints, (GCompareFunc) g_unix_mount_point_compare);
331 diff_sorted_lists (monitor->last_mountpoints,
332 new_mountpoints, (GCompareFunc) g_unix_mount_point_compare,
335 for (l = removed; l != NULL; l = l->next)
337 GUnixMountPoint *mountpoint = l->data;
339 volume = _g_unix_volume_monitor_lookup_volume_for_mount_path (monitor,
340 g_unix_mount_point_get_mount_path (mountpoint));
343 _g_unix_volume_disconnected (volume);
344 monitor->volumes = g_list_remove (monitor->volumes, volume);
345 g_signal_emit_by_name (monitor, "volume-removed", volume);
346 g_signal_emit_by_name (volume, "removed");
347 g_object_unref (volume);
351 for (l = added; l != NULL; l = l->next)
353 GUnixMountPoint *mountpoint = l->data;
355 volume = _g_unix_volume_new (G_VOLUME_MONITOR (monitor), mountpoint);
358 monitor->volumes = g_list_prepend (monitor->volumes, volume);
359 g_signal_emit_by_name (monitor, "volume-added", volume);
364 g_list_free (removed);
365 g_list_free_full (monitor->last_mountpoints, (GDestroyNotify) g_unix_mount_point_free);
366 monitor->last_mountpoints = new_mountpoints;
370 update_mounts (GUnixVolumeMonitor *monitor)
373 GList *removed, *added;
377 const char *mount_path;
379 new_mounts = g_unix_mounts_get (NULL);
381 new_mounts = g_list_sort (new_mounts, (GCompareFunc) g_unix_mount_compare);
383 diff_sorted_lists (monitor->last_mounts,
384 new_mounts, (GCompareFunc) g_unix_mount_compare,
387 for (l = removed; l != NULL; l = l->next)
389 GUnixMountEntry *mount_entry = l->data;
391 mount = find_mount_by_mountpath (monitor, g_unix_mount_get_mount_path (mount_entry));
394 _g_unix_mount_unmounted (mount);
395 monitor->mounts = g_list_remove (monitor->mounts, mount);
396 g_signal_emit_by_name (monitor, "mount-removed", mount);
397 g_signal_emit_by_name (mount, "unmounted");
398 g_object_unref (mount);
402 for (l = added; l != NULL; l = l->next)
404 GUnixMountEntry *mount_entry = l->data;
406 mount_path = g_unix_mount_get_mount_path (mount_entry);
408 volume = _g_unix_volume_monitor_lookup_volume_for_mount_path (monitor, mount_path);
409 mount = _g_unix_mount_new (G_VOLUME_MONITOR (monitor), mount_entry, volume);
412 monitor->mounts = g_list_prepend (monitor->mounts, mount);
413 g_signal_emit_by_name (monitor, "mount-added", mount);
418 g_list_free (removed);
419 g_list_free_full (monitor->last_mounts, (GDestroyNotify) g_unix_mount_free);
420 monitor->last_mounts = new_mounts;