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

3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
/* 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
 * version 2 of the License, or (at your option) any later version.
 *
 * 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
 * Public License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
 * Boston, MA 02111-1307, USA.
 *
 * Author: Alexander Larsson <alexl@redhat.com>
23
 *         David Zeuthen <davidz@redhat.com>
24 25
 */

26
#include "config.h"
27 28 29 30 31 32

#include <string.h>

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

40

41 42 43 44 45 46 47 48 49
struct _GUnixVolumeMonitor {
  GNativeVolumeMonitor parent;

  GUnixMountMonitor *mount_monitor;

  GList *last_mountpoints;
  GList *last_mounts;

  GList *volumes;
50
  GList *mounts;
51 52
};

53 54 55 56 57 58
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);
59

60
#define g_unix_volume_monitor_get_type _g_unix_volume_monitor_get_type
61 62 63 64 65
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));
66 67 68 69 70 71 72 73 74 75 76 77 78

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);

79 80
  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);
81

82 83
  g_list_free_full (monitor->volumes, g_object_unref);
  g_list_free_full (monitor->mounts, g_object_unref);
84 85

  G_OBJECT_CLASS (g_unix_volume_monitor_parent_class)->finalize (object);
86 87
}

88 89 90 91 92 93
static void
g_unix_volume_monitor_dispose (GObject *object)
{
  GUnixVolumeMonitor *monitor;

  monitor = G_UNIX_VOLUME_MONITOR (object);
94 95

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

104
static GList *
105
get_mounts (GVolumeMonitor *volume_monitor)
106 107 108 109 110 111
{
  GUnixVolumeMonitor *monitor;
  GList *l;
  
  monitor = G_UNIX_VOLUME_MONITOR (volume_monitor);

112
  l = g_list_copy (monitor->mounts);
113 114 115 116 117 118
  g_list_foreach (l, (GFunc)g_object_ref, NULL);

  return l;
}

static GList *
119
get_volumes (GVolumeMonitor *volume_monitor)
120 121 122 123 124 125
{
  GUnixVolumeMonitor *monitor;
  GList *l;
  
  monitor = G_UNIX_VOLUME_MONITOR (volume_monitor);

126
  l = g_list_copy (monitor->volumes);
127 128 129 130 131
  g_list_foreach (l, (GFunc)g_object_ref, NULL);

  return l;
}

132 133
static GList *
get_connected_drives (GVolumeMonitor *volume_monitor)
134
{
135 136 137
  return NULL;
}

138 139 140 141 142 143 144 145 146 147 148 149
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;
}

150 151 152 153 154 155
static gboolean
is_supported (void)
{
  return TRUE;
}

156
static GMount *
157 158
get_mount_for_mount_path (const char *mount_path,
                          GCancellable *cancellable)
159 160
{
  GUnixMountEntry *mount_entry;
161 162
  GUnixMount *mount;

163
  mount_entry = g_unix_mount_at (mount_path, NULL);
164 165 166 167

  if (!mount_entry)
    return NULL;

168 169
  /* TODO: Set mountable volume? */
  mount = _g_unix_mount_new (NULL, mount_entry, NULL);
170

Matthias Clasen's avatar
Matthias Clasen committed
171 172
  g_unix_mount_free (mount_entry);

173
  return G_MOUNT (mount);
174 175 176 177 178 179 180 181 182 183
}

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;
184
  gobject_class->dispose = g_unix_volume_monitor_dispose;
185

186 187
  monitor_class->get_mounts = get_mounts;
  monitor_class->get_volumes = get_volumes;
188
  monitor_class->get_connected_drives = get_connected_drives;
189 190
  monitor_class->get_volume_for_uuid = get_volume_for_uuid;
  monitor_class->get_mount_for_uuid = get_mount_for_uuid;
191
  monitor_class->is_supported = is_supported;
192

193
  native_class->get_mount_for_mount_path = get_mount_for_mount_path;
194 195 196 197
}

static void
mountpoints_changed (GUnixMountMonitor *mount_monitor,
198
		     gpointer           user_data)
199 200 201
{
  GUnixVolumeMonitor *unix_monitor = user_data;

202
  /* Update both to make sure volumes are created before mounts */
203
  update_volumes (unix_monitor);
204
  update_mounts (unix_monitor);
205 206 207 208
}

static void
mounts_changed (GUnixMountMonitor *mount_monitor,
209
		gpointer           user_data)
210 211 212
{
  GUnixVolumeMonitor *unix_monitor = user_data;

213
  /* Update both to make sure volumes are created before mounts */
214
  update_volumes (unix_monitor);
215
  update_mounts (unix_monitor);
216 217 218 219 220 221 222 223 224
}

static void
g_unix_volume_monitor_init (GUnixVolumeMonitor *unix_monitor)
{

  unix_monitor->mount_monitor = g_unix_mount_monitor_new ();

  g_signal_connect (unix_monitor->mount_monitor,
225
		    "mounts-changed", G_CALLBACK (mounts_changed),
226 227 228
		    unix_monitor);
  
  g_signal_connect (unix_monitor->mount_monitor,
229
		    "mountpoints-changed", G_CALLBACK (mountpoints_changed),
230 231 232
		    unix_monitor);
		    
  update_volumes (unix_monitor);
233
  update_mounts (unix_monitor);
234 235 236
}

GVolumeMonitor *
237
_g_unix_volume_monitor_new (void)
238 239 240 241 242 243 244 245 246
{
  GUnixVolumeMonitor *monitor;

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

static void
247 248 249 250 251
diff_sorted_lists (GList         *list1, 
                   GList         *list2, 
                   GCompareFunc   compare,
		   GList        **added, 
                   GList        **removed)
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 283 284 285 286 287 288 289
{
  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;
    }
}

290 291 292
GUnixVolume *
_g_unix_volume_monitor_lookup_volume_for_mount_path (GUnixVolumeMonitor *monitor,
                                                     const char         *mount_path)
293 294 295
{
  GList *l;

296
  for (l = monitor->volumes; l != NULL; l = l->next)
297
    {
298
      GUnixVolume *volume = l->data;
299

300 301
      if (_g_unix_volume_has_mount_path (volume, mount_path))
	return volume;
302 303 304 305 306
    }
  
  return NULL;
}

307 308 309
static GUnixMount *
find_mount_by_mountpath (GUnixVolumeMonitor *monitor,
                         const char *mount_path)
310 311 312
{
  GList *l;

313
  for (l = monitor->mounts; l != NULL; l = l->next)
314
    {
315
      GUnixMount *mount = l->data;
316

317 318
      if (_g_unix_mount_has_mount_path (mount, mount_path))
	return mount;
319 320 321 322 323 324
    }
  
  return NULL;
}

static void
325
update_volumes (GUnixVolumeMonitor *monitor)
326 327 328 329
{
  GList *new_mountpoints;
  GList *removed, *added;
  GList *l;
330
  GUnixVolume *volume;
331
  
332
  new_mountpoints = g_unix_mount_points_get (NULL);
333 334 335 336 337 338 339 340 341 342 343
  
  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;
      
344 345 346
      volume = _g_unix_volume_monitor_lookup_volume_for_mount_path (monitor,
                                                                    g_unix_mount_point_get_mount_path (mountpoint));
      if (volume)
347
	{
348 349
	  _g_unix_volume_disconnected (volume);
	  monitor->volumes = g_list_remove (monitor->volumes, volume);
350
	  g_signal_emit_by_name (monitor, "volume-removed", volume);
351
	  g_signal_emit_by_name (volume, "removed");
352
	  g_object_unref (volume);
353 354 355 356 357 358 359
	}
    }
  
  for (l = added; l != NULL; l = l->next)
    {
      GUnixMountPoint *mountpoint = l->data;
      
360 361
      volume = _g_unix_volume_new (G_VOLUME_MONITOR (monitor), mountpoint);
      if (volume)
362
	{
363
	  monitor->volumes = g_list_prepend (monitor->volumes, volume);
364
	  g_signal_emit_by_name (monitor, "volume-added", volume);
365 366 367 368 369
	}
    }
  
  g_list_free (added);
  g_list_free (removed);
370
  g_list_free_full (monitor->last_mountpoints, (GDestroyNotify) g_unix_mount_point_free);
371 372 373 374
  monitor->last_mountpoints = new_mountpoints;
}

static void
375
update_mounts (GUnixVolumeMonitor *monitor)
376 377 378 379
{
  GList *new_mounts;
  GList *removed, *added;
  GList *l;
380
  GUnixMount *mount;
381 382 383
  GUnixVolume *volume;
  const char *mount_path;
  
384
  new_mounts = g_unix_mounts_get (NULL);
385 386 387 388 389 390 391 392 393
  
  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)
    {
394
      GUnixMountEntry *mount_entry = l->data;
395
      
396 397
      mount = find_mount_by_mountpath (monitor, g_unix_mount_get_mount_path (mount_entry));
      if (mount)
398
	{
399 400
	  _g_unix_mount_unmounted (mount);
	  monitor->mounts = g_list_remove (monitor->mounts, mount);
401
	  g_signal_emit_by_name (monitor, "mount-removed", mount);
402
	  g_signal_emit_by_name (mount, "unmounted");
403
	  g_object_unref (mount);
404 405 406 407 408
	}
    }
  
  for (l = added; l != NULL; l = l->next)
    {
409
      GUnixMountEntry *mount_entry = l->data;
410

411
      mount_path = g_unix_mount_get_mount_path (mount_entry);
412
      
413 414 415
      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)
416
	{
417
	  monitor->mounts = g_list_prepend (monitor->mounts, mount);
418
	  g_signal_emit_by_name (monitor, "mount-added", mount);
419 420 421 422 423
	}
    }
  
  g_list_free (added);
  g_list_free (removed);
424
  g_list_free_full (monitor->last_mounts, (GDestroyNotify) g_unix_mount_free);
425 426
  monitor->last_mounts = new_mounts;
}