gunixvolumemonitor.c 11 KB
Newer Older
1 2
/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */

3 4 5 6 7 8 9
/* GIO - GLib Input, Output and Streaming Library
 * 
 * Copyright (C) 2006-2007 Red Hat, Inc.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * 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 13 14 15 16 17
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * 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 27 28 29 30

#include <string.h>

#include <glib.h>
#include "gunixvolumemonitor.h"
#include "gunixmounts.h"
31
#include "gunixmount.h"
32
#include "gunixvolume.h"
33
#include "gmount.h"
34
#include "gmountprivate.h"
35
#include "giomodule.h"
36 37
#include "glibintl.h"

38

39 40 41 42 43 44 45 46 47
struct _GUnixVolumeMonitor {
  GNativeVolumeMonitor parent;

  GUnixMountMonitor *mount_monitor;

  GList *last_mountpoints;
  GList *last_mounts;

  GList *volumes;
48
  GList *mounts;
49 50
};

51 52 53 54 55 56
static void mountpoints_changed      (GUnixMountMonitor  *mount_monitor,
                                      gpointer            user_data);
static void mounts_changed           (GUnixMountMonitor  *mount_monitor,
                                      gpointer            user_data);
static void update_volumes           (GUnixVolumeMonitor *monitor);
static void update_mounts            (GUnixVolumeMonitor *monitor);
57

58
#define g_unix_volume_monitor_get_type _g_unix_volume_monitor_get_type
59 60 61 62 63
G_DEFINE_TYPE_WITH_CODE (GUnixVolumeMonitor, g_unix_volume_monitor, G_TYPE_NATIVE_VOLUME_MONITOR,
                         g_io_extension_point_implement (G_NATIVE_VOLUME_MONITOR_EXTENSION_POINT_NAME,
							 g_define_type_id,
							 "unix",
							 0));
64 65 66 67 68 69 70 71 72 73 74 75 76

static void
g_unix_volume_monitor_finalize (GObject *object)
{
  GUnixVolumeMonitor *monitor;
  
  monitor = G_UNIX_VOLUME_MONITOR (object);

  g_signal_handlers_disconnect_by_func (monitor->mount_monitor, mountpoints_changed, monitor);
  g_signal_handlers_disconnect_by_func (monitor->mount_monitor, mounts_changed, monitor);
					
  g_object_unref (monitor->mount_monitor);

77 78
  g_list_free_full (monitor->last_mountpoints, (GDestroyNotify) g_unix_mount_point_free);
  g_list_free_full (monitor->last_mounts, (GDestroyNotify) g_unix_mount_free);
79

80 81
  g_list_free_full (monitor->volumes, g_object_unref);
  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 87 88 89 90 91
static void
g_unix_volume_monitor_dispose (GObject *object)
{
  GUnixVolumeMonitor *monitor;

  monitor = G_UNIX_VOLUME_MONITOR (object);
92 93

  g_list_free_full (monitor->volumes, g_object_unref);
94 95
  monitor->volumes = NULL;
  
96
  g_list_free_full (monitor->mounts, g_object_unref);
97 98 99 100 101
  monitor->mounts = NULL;
  
  G_OBJECT_CLASS (g_unix_volume_monitor_parent_class)->dispose (object);
}

102
static GList *
103
get_mounts (GVolumeMonitor *volume_monitor)
104 105 106 107 108
{
  GUnixVolumeMonitor *monitor;
  
  monitor = G_UNIX_VOLUME_MONITOR (volume_monitor);

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 116 117 118
{
  GUnixVolumeMonitor *monitor;
  
  monitor = G_UNIX_VOLUME_MONITOR (volume_monitor);

119
  return g_list_copy_deep (monitor->volumes, (GCopyFunc) g_object_ref, NULL);
120 121
}

122 123
static GList *
get_connected_drives (GVolumeMonitor *volume_monitor)
124
{
125 126 127
  return NULL;
}

128 129 130 131 132 133 134 135 136 137 138 139
static GVolume *
get_volume_for_uuid (GVolumeMonitor *volume_monitor, const char *uuid)
{
  return NULL;
}

static GMount *
get_mount_for_uuid (GVolumeMonitor *volume_monitor, const char *uuid)
{
  return NULL;
}

140 141 142 143 144 145
static gboolean
is_supported (void)
{
  return TRUE;
}

146
static GMount *
147 148
get_mount_for_mount_path (const char *mount_path,
                          GCancellable *cancellable)
149 150
{
  GUnixMountEntry *mount_entry;
151 152
  GUnixMount *mount;

153
  mount_entry = g_unix_mount_at (mount_path, NULL);
154 155 156 157

  if (!mount_entry)
    return NULL;

158 159
  /* TODO: Set mountable volume? */
  mount = _g_unix_mount_new (NULL, mount_entry, NULL);
160

Matthias Clasen's avatar
Matthias Clasen committed
161 162
  g_unix_mount_free (mount_entry);

163
  return G_MOUNT (mount);
164 165 166 167 168 169 170 171 172 173
}

static void
g_unix_volume_monitor_class_init (GUnixVolumeMonitorClass *klass)
{
  GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
  GVolumeMonitorClass *monitor_class = G_VOLUME_MONITOR_CLASS (klass);
  GNativeVolumeMonitorClass *native_class = G_NATIVE_VOLUME_MONITOR_CLASS (klass);
  
  gobject_class->finalize = g_unix_volume_monitor_finalize;
174
  gobject_class->dispose = g_unix_volume_monitor_dispose;
175

176 177
  monitor_class->get_mounts = get_mounts;
  monitor_class->get_volumes = get_volumes;
178
  monitor_class->get_connected_drives = get_connected_drives;
179 180
  monitor_class->get_volume_for_uuid = get_volume_for_uuid;
  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 187 188 189 190 191 192 193
void
_g_unix_volume_monitor_update (GUnixVolumeMonitor *unix_monitor)
{
  /* Update both to make sure volumes are created before mounts */
  update_volumes (unix_monitor);
  update_mounts (unix_monitor);
}

194 195
static void
mountpoints_changed (GUnixMountMonitor *mount_monitor,
196
		     gpointer           user_data)
197 198 199
{
  GUnixVolumeMonitor *unix_monitor = user_data;

200
  _g_unix_volume_monitor_update (unix_monitor);
201 202 203 204
}

static void
mounts_changed (GUnixMountMonitor *mount_monitor,
205
		gpointer           user_data)
206 207 208
{
  GUnixVolumeMonitor *unix_monitor = user_data;

209
  _g_unix_volume_monitor_update (unix_monitor);
210 211 212 213 214 215
}

static void
g_unix_volume_monitor_init (GUnixVolumeMonitor *unix_monitor)
{

216
  unix_monitor->mount_monitor = g_unix_mount_monitor_get ();
217 218

  g_signal_connect (unix_monitor->mount_monitor,
219
		    "mounts-changed", G_CALLBACK (mounts_changed),
220 221 222
		    unix_monitor);
  
  g_signal_connect (unix_monitor->mount_monitor,
223
		    "mountpoints-changed", G_CALLBACK (mountpoints_changed),
224 225
		    unix_monitor);
		    
226
  _g_unix_volume_monitor_update (unix_monitor);
227 228 229
}

GVolumeMonitor *
230
_g_unix_volume_monitor_new (void)
231 232 233 234 235 236 237 238 239
{
  GUnixVolumeMonitor *monitor;

  monitor = g_object_new (G_TYPE_UNIX_VOLUME_MONITOR, NULL);
  
  return G_VOLUME_MONITOR (monitor);
}

static void
240 241 242 243 244
diff_sorted_lists (GList         *list1, 
                   GList         *list2, 
                   GCompareFunc   compare,
		   GList        **added, 
                   GList        **removed)
245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282
{
  int order;
  
  *added = *removed = NULL;
  
  while (list1 != NULL &&
	 list2 != NULL)
    {
      order = (*compare) (list1->data, list2->data);
      if (order < 0)
	{
	  *removed = g_list_prepend (*removed, list1->data);
	  list1 = list1->next;
	}
      else if (order > 0)
	{
	  *added = g_list_prepend (*added, list2->data);
	  list2 = list2->next;
	}
      else
	{ /* same item */
	  list1 = list1->next;
	  list2 = list2->next;
	}
    }

  while (list1 != NULL)
    {
      *removed = g_list_prepend (*removed, list1->data);
      list1 = list1->next;
    }
  while (list2 != NULL)
    {
      *added = g_list_prepend (*added, list2->data);
      list2 = list2->next;
    }
}

283 284 285
GUnixVolume *
_g_unix_volume_monitor_lookup_volume_for_mount_path (GUnixVolumeMonitor *monitor,
                                                     const char         *mount_path)
286 287 288
{
  GList *l;

289
  for (l = monitor->volumes; l != NULL; l = l->next)
290
    {
291
      GUnixVolume *volume = l->data;
292

293 294
      if (_g_unix_volume_has_mount_path (volume, mount_path))
	return volume;
295 296 297 298 299
    }
  
  return NULL;
}

300 301 302
static GUnixMount *
find_mount_by_mountpath (GUnixVolumeMonitor *monitor,
                         const char *mount_path)
303 304 305
{
  GList *l;

306
  for (l = monitor->mounts; l != NULL; l = l->next)
307
    {
308
      GUnixMount *mount = l->data;
309

310 311
      if (_g_unix_mount_has_mount_path (mount, mount_path))
	return mount;
312 313 314 315 316 317
    }
  
  return NULL;
}

static void
318
update_volumes (GUnixVolumeMonitor *monitor)
319 320 321 322
{
  GList *new_mountpoints;
  GList *removed, *added;
  GList *l;
323
  GUnixVolume *volume;
324
  
325
  new_mountpoints = g_unix_mount_points_get (NULL);
326 327 328 329 330 331 332 333 334 335 336
  
  new_mountpoints = g_list_sort (new_mountpoints, (GCompareFunc) g_unix_mount_point_compare);
  
  diff_sorted_lists (monitor->last_mountpoints,
		     new_mountpoints, (GCompareFunc) g_unix_mount_point_compare,
		     &added, &removed);
  
  for (l = removed; l != NULL; l = l->next)
    {
      GUnixMountPoint *mountpoint = l->data;
      
337 338 339
      volume = _g_unix_volume_monitor_lookup_volume_for_mount_path (monitor,
                                                                    g_unix_mount_point_get_mount_path (mountpoint));
      if (volume)
340
	{
341 342
	  _g_unix_volume_disconnected (volume);
	  monitor->volumes = g_list_remove (monitor->volumes, volume);
343
	  g_signal_emit_by_name (monitor, "volume-removed", volume);
344
	  g_signal_emit_by_name (volume, "removed");
345
	  g_object_unref (volume);
346 347 348 349 350 351 352
	}
    }
  
  for (l = added; l != NULL; l = l->next)
    {
      GUnixMountPoint *mountpoint = l->data;
      
353 354
      volume = _g_unix_volume_new (G_VOLUME_MONITOR (monitor), mountpoint);
      if (volume)
355
	{
356
	  monitor->volumes = g_list_prepend (monitor->volumes, volume);
357
	  g_signal_emit_by_name (monitor, "volume-added", volume);
358 359 360 361 362
	}
    }
  
  g_list_free (added);
  g_list_free (removed);
363
  g_list_free_full (monitor->last_mountpoints, (GDestroyNotify) g_unix_mount_point_free);
364 365 366 367
  monitor->last_mountpoints = new_mountpoints;
}

static void
368
update_mounts (GUnixVolumeMonitor *monitor)
369 370 371 372
{
  GList *new_mounts;
  GList *removed, *added;
  GList *l;
373
  GUnixMount *mount;
374 375 376
  GUnixVolume *volume;
  const char *mount_path;
  
377
  new_mounts = g_unix_mounts_get (NULL);
378 379 380 381 382 383 384 385 386
  
  new_mounts = g_list_sort (new_mounts, (GCompareFunc) g_unix_mount_compare);
  
  diff_sorted_lists (monitor->last_mounts,
		     new_mounts, (GCompareFunc) g_unix_mount_compare,
		     &added, &removed);
  
  for (l = removed; l != NULL; l = l->next)
    {
387
      GUnixMountEntry *mount_entry = l->data;
388
      
389 390
      mount = find_mount_by_mountpath (monitor, g_unix_mount_get_mount_path (mount_entry));
      if (mount)
391
	{
392 393
	  _g_unix_mount_unmounted (mount);
	  monitor->mounts = g_list_remove (monitor->mounts, mount);
394
	  g_signal_emit_by_name (monitor, "mount-removed", mount);
395
	  g_signal_emit_by_name (mount, "unmounted");
396
	  g_object_unref (mount);
397 398 399 400 401
	}
    }
  
  for (l = added; l != NULL; l = l->next)
    {
402
      GUnixMountEntry *mount_entry = l->data;
403

404
      mount_path = g_unix_mount_get_mount_path (mount_entry);
405
      
406 407 408
      volume = _g_unix_volume_monitor_lookup_volume_for_mount_path (monitor, mount_path);
      mount = _g_unix_mount_new (G_VOLUME_MONITOR (monitor), mount_entry, volume);
      if (mount)
409
	{
410
	  monitor->mounts = g_list_prepend (monitor->mounts, mount);
411
	  g_signal_emit_by_name (monitor, "mount-added", mount);
412 413 414 415 416
	}
    }
  
  g_list_free (added);
  g_list_free (removed);
417
  g_list_free_full (monitor->last_mounts, (GDestroyNotify) g_unix_mount_free);
418 419
  monitor->last_mounts = new_mounts;
}