gunixvolume.c 14.8 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

#include <string.h>
29 30
#include <sys/wait.h>
#include <unistd.h>
31 32 33

#include <glib.h>
#include "gunixvolume.h"
34
#include "gunixmount.h"
35
#include "gunixmounts.h"
36
#include "gthemedicon.h"
37
#include "gvolume.h"
38 39
#include "gvolumemonitor.h"
#include "gsimpleasyncresult.h"
40
#include "gioerror.h"
41
#include "glibintl.h"
Christian Neumair's avatar
Christian Neumair committed
42 43
/* for BUFSIZ */
#include <stdio.h>
44

45

46 47 48
struct _GUnixVolume {
  GObject parent;

49 50 51 52 53
  GVolumeMonitor *volume_monitor;
  GUnixMount     *mount; /* owned by volume monitor */
  
  char *device_path;
  char *mount_path;
54 55
  gboolean can_eject;

56 57 58
  char *identifier;
  char *identifier_type;
  
59
  char *name;
60
  GIcon *icon;
61 62 63 64
};

static void g_unix_volume_volume_iface_init (GVolumeIface *iface);

65
#define g_unix_volume_get_type _g_unix_volume_get_type
66 67 68 69 70 71 72 73 74 75 76
G_DEFINE_TYPE_WITH_CODE (GUnixVolume, g_unix_volume, G_TYPE_OBJECT,
			 G_IMPLEMENT_INTERFACE (G_TYPE_VOLUME,
						g_unix_volume_volume_iface_init))

static void
g_unix_volume_finalize (GObject *object)
{
  GUnixVolume *volume;
  
  volume = G_UNIX_VOLUME (object);

77 78 79 80 81
  if (volume->volume_monitor != NULL)
    g_object_unref (volume->volume_monitor);

  if (volume->mount)
    _g_unix_mount_unset_volume (volume->mount, volume);
82
  
83 84 85 86
  g_object_unref (volume->icon);
  g_free (volume->name);
  g_free (volume->mount_path);
  g_free (volume->device_path);
87 88
  g_free (volume->identifier);
  g_free (volume->identifier_type);
89

90
  G_OBJECT_CLASS (g_unix_volume_parent_class)->finalize (object);
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
}

static void
g_unix_volume_class_init (GUnixVolumeClass *klass)
{
  GObjectClass *gobject_class = G_OBJECT_CLASS (klass);

  gobject_class->finalize = g_unix_volume_finalize;
}

static void
g_unix_volume_init (GUnixVolume *unix_volume)
{
}

GUnixVolume *
107 108
_g_unix_volume_new (GVolumeMonitor  *volume_monitor,
                    GUnixMountPoint *mountpoint)
109 110 111
{
  GUnixVolume *volume;
  
112 113 114
  if (!(g_unix_mount_point_is_user_mountable (mountpoint) ||
	g_str_has_prefix (g_unix_mount_point_get_device_path (mountpoint), "/vol/")) ||
      g_unix_mount_point_is_loopback (mountpoint))
115 116 117
    return NULL;
  
  volume = g_object_new (G_TYPE_UNIX_VOLUME, NULL);
118 119 120
  volume->volume_monitor = volume_monitor != NULL ? g_object_ref (volume_monitor) : NULL;
  volume->mount_path = g_strdup (g_unix_mount_point_get_mount_path (mountpoint));
  volume->device_path = g_strdup (g_unix_mount_point_get_device_path (mountpoint));
121
  volume->can_eject = g_unix_mount_point_guess_can_eject (mountpoint);
122

123 124
  volume->name = g_unix_mount_point_guess_name (mountpoint);
  volume->icon = g_unix_mount_point_guess_icon (mountpoint);
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147


  if (strcmp (g_unix_mount_point_get_fs_type (mountpoint), "nfs") == 0)
    {
      volume->identifier_type = g_strdup (G_VOLUME_IDENTIFIER_KIND_NFS_MOUNT);
      volume->identifier = g_strdup (volume->device_path);
    }
  else if (g_str_has_prefix (volume->device_path, "LABEL="))
    {
      volume->identifier_type = g_strdup (G_VOLUME_IDENTIFIER_KIND_LABEL);
      volume->identifier = g_strdup (volume->device_path + 6);
    }
  else if (g_str_has_prefix (volume->device_path, "UUID="))
    {
      volume->identifier_type = g_strdup (G_VOLUME_IDENTIFIER_KIND_UUID);
      volume->identifier = g_strdup (volume->device_path + 5);
    }
  else if (g_path_is_absolute (volume->device_path))
    {
      volume->identifier_type = g_strdup (G_VOLUME_IDENTIFIER_KIND_UNIX_DEVICE);
      volume->identifier = g_strdup (volume->device_path);
    }
  
148 149 150 151
  return volume;
}

void
152
_g_unix_volume_disconnected (GUnixVolume *volume)
153
{
154
  if (volume->mount)
155
    {
156 157
      _g_unix_mount_unset_volume (volume->mount, volume);
      volume->mount = NULL;
158 159 160
    }
}

161
void
Matthias Clasen's avatar
Matthias Clasen committed
162 163
_g_unix_volume_set_mount (GUnixVolume *volume,
                          GUnixMount  *mount)
164 165 166 167 168 169 170 171 172 173 174 175
{
  if (volume->mount == mount)
    return;
  
  if (volume->mount)
    _g_unix_mount_unset_volume (volume->mount, volume);
  
  volume->mount = mount;
  
  /* TODO: Emit changed in idle to avoid locking issues */
  g_signal_emit_by_name (volume, "changed");
  if (volume->volume_monitor != NULL)
176
    g_signal_emit_by_name (volume->volume_monitor, "volume-changed", volume);
177 178
}

179
void
180 181
_g_unix_volume_unset_mount (GUnixVolume  *volume,
                            GUnixMount *mount)
182
{
183
  if (volume->mount == mount)
184
    {
185
      volume->mount = NULL;
186 187
      /* TODO: Emit changed in idle to avoid locking issues */
      g_signal_emit_by_name (volume, "changed");
188
      if (volume->volume_monitor != NULL)
189
        g_signal_emit_by_name (volume->volume_monitor, "volume-changed", volume);
190 191 192 193 194 195 196
    }
}

static GIcon *
g_unix_volume_get_icon (GVolume *volume)
{
  GUnixVolume *unix_volume = G_UNIX_VOLUME (volume);
197
  return g_object_ref (unix_volume->icon);
198 199 200 201 202 203 204 205 206
}

static char *
g_unix_volume_get_name (GVolume *volume)
{
  GUnixVolume *unix_volume = G_UNIX_VOLUME (volume);
  return g_strdup (unix_volume->name);
}

207 208 209 210 211 212
static char *
g_unix_volume_get_uuid (GVolume *volume)
{
  return NULL;
}

213 214
static gboolean
g_unix_volume_can_mount (GVolume *volume)
215
{
216
  return TRUE;
217 218
}

219 220 221 222 223 224 225
static gboolean
g_unix_volume_can_eject (GVolume *volume)
{
  GUnixVolume *unix_volume = G_UNIX_VOLUME (volume);
  return unix_volume->can_eject;
}

226 227 228 229
static gboolean
g_unix_volume_should_automount (GVolume *volume)
{
  /* We automount all local volumes because we don't even
Matthias Clasen's avatar
Matthias Clasen committed
230 231
   * make the internal stuff visible
   */
232 233 234
  return TRUE;
}

235 236 237 238 239 240
static GDrive *
g_unix_volume_get_drive (GVolume *volume)
{
  return NULL;
}

241 242
static GMount *
g_unix_volume_get_mount (GVolume *volume)
243
{
244 245 246 247 248 249
  GUnixVolume *unix_volume = G_UNIX_VOLUME (volume);

  if (unix_volume->mount != NULL)
    return g_object_ref (unix_volume->mount);

  return NULL;
250 251
}

252 253 254

gboolean
_g_unix_volume_has_mount_path (GUnixVolume *volume,
Matthias Clasen's avatar
Matthias Clasen committed
255
                               const char  *mount_path)
256
{
257
  return strcmp (volume->mount_path, mount_path) == 0;
258 259
}

260 261 262 263 264 265 266 267

typedef struct {
  GUnixVolume *unix_volume;
  GAsyncReadyCallback callback;
  gpointer user_data;
  GCancellable *cancellable;
  int error_fd;
  GIOChannel *error_channel;
268
  GSource *error_channel_source;
269
  GString *error_string;
270
} EjectMountOp;
271

Matthias Clasen's avatar
Matthias Clasen committed
272 273 274 275
static void
eject_mount_cb (GPid     pid,
                gint     status,
                gpointer user_data)
276
{
277
  EjectMountOp *data = user_data;
278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302
  GSimpleAsyncResult *simple;
  
  if (WEXITSTATUS (status) != 0)
    {
      GError *error;
      error = g_error_new_literal (G_IO_ERROR, 
                                   G_IO_ERROR_FAILED,
                                   data->error_string->str);
      simple = g_simple_async_result_new_from_error (G_OBJECT (data->unix_volume),
                                                     data->callback,
                                                     data->user_data,
                                                     error);
      g_error_free (error);
    }
  else
    {
      simple = g_simple_async_result_new (G_OBJECT (data->unix_volume),
                                          data->callback,
                                          data->user_data,
                                          NULL);
    }

  g_simple_async_result_complete (simple);
  g_object_unref (simple);

303 304 305 306 307
  if (data->error_channel_source)
    {
      g_source_destroy (data->error_channel_source);
      g_source_unref (data->error_channel_source);
    }
308 309 310 311 312
  g_io_channel_unref (data->error_channel);
  g_string_free (data->error_string, TRUE);
  close (data->error_fd);
  g_spawn_close_pid (pid);
  g_free (data);
313 314 315
}

static gboolean
Matthias Clasen's avatar
Matthias Clasen committed
316 317 318
eject_mount_read_error (GIOChannel   *channel,
                        GIOCondition  condition,
                        gpointer      user_data)
319
{
320
  EjectMountOp *data = user_data;
Christian Neumair's avatar
Christian Neumair committed
321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343
  char buf[BUFSIZ];
  gsize bytes_read;
  GError *error;
  GIOStatus status;

  error = NULL;
read:
  status = g_io_channel_read_chars (channel, buf, sizeof (buf), &bytes_read, &error);
  if (status == G_IO_STATUS_NORMAL)
   {
     g_string_append_len (data->error_string, buf, bytes_read);
     if (bytes_read == sizeof (buf))
        goto read;
   }
  else if (status == G_IO_STATUS_EOF)
    g_string_append_len (data->error_string, buf, bytes_read);
  else if (status == G_IO_STATUS_ERROR)
    {
      if (data->error_string->len > 0)
        g_string_append (data->error_string, "\n");

      g_string_append (data->error_string, error->message);
      g_error_free (error);
344 345 346 347 348 349

      if (data->error_channel_source)
        {
          g_source_unref (data->error_channel_source);
          data->error_channel_source = NULL;
        }
Christian Neumair's avatar
Christian Neumair committed
350 351
      return FALSE;
    }
352

353 354 355 356
  return TRUE;
}

static void
357 358 359 360 361
eject_mount_do (GVolume             *volume,
                GCancellable        *cancellable,
                GAsyncReadyCallback  callback,
                gpointer             user_data,
                char               **argv)
362
{
363
  GUnixVolume *unix_volume = G_UNIX_VOLUME (volume);
364
  EjectMountOp *data;
365
  GPid child_pid;
366
  GSource *child_watch;
367 368
  GError *error;
  
369
  data = g_new0 (EjectMountOp, 1);
370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385
  data->unix_volume = unix_volume;
  data->callback = callback;
  data->user_data = user_data;
  data->cancellable = cancellable;
  
  error = NULL;
  if (!g_spawn_async_with_pipes (NULL,         /* working dir */
                                 argv,
                                 NULL,         /* envp */
                                 G_SPAWN_DO_NOT_REAP_CHILD|G_SPAWN_SEARCH_PATH,
                                 NULL,         /* child_setup */
                                 NULL,         /* user_data for child_setup */
                                 &child_pid,
                                 NULL,           /* standard_input */
                                 NULL,           /* standard_output */
                                 &(data->error_fd),
Matthias Clasen's avatar
Matthias Clasen committed
386 387 388 389 390
                                 &error))
    {
      g_assert (error != NULL);
      goto handle_error;
    }
Christian Neumair's avatar
Christian Neumair committed
391 392 393 394 395 396 397 398

  data->error_string = g_string_new ("");

  data->error_channel = g_io_channel_unix_new (data->error_fd);
  g_io_channel_set_flags (data->error_channel, G_IO_FLAG_NONBLOCK, &error);
  if (error != NULL)
    goto handle_error;

399 400 401
  data->error_channel_source = g_io_create_watch (data->error_channel, G_IO_IN);
  g_source_set_callback (data->error_channel_source,
                         (GSourceFunc) eject_mount_read_error, data, NULL);
402
  g_source_attach (data->error_channel_source, g_main_context_get_thread_default ());
403 404 405

  child_watch = g_child_watch_source_new (child_pid);
  g_source_set_callback (child_watch, (GSourceFunc) eject_mount_cb, data, NULL);
406
  g_source_attach (child_watch, g_main_context_get_thread_default ());
407
  g_source_unref (child_watch);
Christian Neumair's avatar
Christian Neumair committed
408 409

handle_error:
Matthias Clasen's avatar
Matthias Clasen committed
410 411 412
  if (error != NULL)
    {
      GSimpleAsyncResult *simple;
413
      simple = g_simple_async_result_new_take_error (G_OBJECT (data->unix_volume),
Matthias Clasen's avatar
Matthias Clasen committed
414 415 416 417 418 419 420 421 422 423 424 425 426 427
                                                     data->callback,
                                                     data->user_data,
                                                     error);
      g_simple_async_result_complete (simple);
      g_object_unref (simple);

      if (data->error_string != NULL)
        g_string_free (data->error_string, TRUE);

      if (data->error_channel != NULL)
        g_io_channel_unref (data->error_channel);

      g_free (data);
    }
428 429
}

430

431
static void
Matthias Clasen's avatar
Matthias Clasen committed
432
g_unix_volume_mount (GVolume            *volume,
Alexander Larsson's avatar
Alexander Larsson committed
433
                     GMountMountFlags    flags,
434 435 436 437 438 439
                     GMountOperation     *mount_operation,
                     GCancellable        *cancellable,
                     GAsyncReadyCallback  callback,
                     gpointer             user_data)
{
  GUnixVolume *unix_volume = G_UNIX_VOLUME (volume);
Matthias Clasen's avatar
Matthias Clasen committed
440
  char *argv[] = { "mount", NULL, NULL };
441 442 443 444 445 446 447 448 449

  if (unix_volume->mount_path != NULL)
    argv[1] = unix_volume->mount_path;
  else
    argv[1] = unix_volume->device_path;

  eject_mount_do (volume, cancellable, callback, user_data, argv);
}

450
static gboolean
451
g_unix_volume_mount_finish (GVolume        *volume,
Matthias Clasen's avatar
Matthias Clasen committed
452 453
                            GAsyncResult  *result,
                            GError       **error)
454 455 456 457
{
  return TRUE;
}

458
static void
Matthias Clasen's avatar
Matthias Clasen committed
459
g_unix_volume_eject (GVolume             *volume,
460
                     GMountUnmountFlags   flags,
461 462 463 464 465
                     GCancellable        *cancellable,
                     GAsyncReadyCallback  callback,
                     gpointer             user_data)
{
  GUnixVolume *unix_volume = G_UNIX_VOLUME (volume);
Matthias Clasen's avatar
Matthias Clasen committed
466
  char *argv[] = { "eject", NULL, NULL };
467 468 469 470 471 472 473

  argv[1] = unix_volume->device_path;

  eject_mount_do (volume, cancellable, callback, user_data, argv);
}

static gboolean
Matthias Clasen's avatar
Matthias Clasen committed
474
g_unix_volume_eject_finish (GVolume       *volume,
475 476 477 478 479 480
                            GAsyncResult  *result,
                            GError       **error)
{
  return TRUE;
}

Matthias Clasen's avatar
Matthias Clasen committed
481 482 483
static gchar *
g_unix_volume_get_identifier (GVolume     *volume,
                              const gchar *kind)
484 485 486
{
  GUnixVolume *unix_volume = G_UNIX_VOLUME (volume);

487 488
  if (unix_volume->identifier_type != NULL &&
      strcmp (kind, unix_volume->identifier_type) == 0)
489
    return g_strdup (unix_volume->identifier);
Matthias Clasen's avatar
Matthias Clasen committed
490

491 492 493
  return NULL;
}

Matthias Clasen's avatar
Matthias Clasen committed
494
static gchar **
495 496 497
g_unix_volume_enumerate_identifiers (GVolume *volume)
{
  GUnixVolume *unix_volume = G_UNIX_VOLUME (volume);
Matthias Clasen's avatar
Matthias Clasen committed
498
  gchar **res;
499 500 501

  if (unix_volume->identifier_type)
    {
Matthias Clasen's avatar
Matthias Clasen committed
502
      res = g_new (gchar *, 2);
503 504 505 506 507
      res[0] = g_strdup (unix_volume->identifier_type);
      res[1] = NULL;
    }
  else
    {
Matthias Clasen's avatar
Matthias Clasen committed
508
      res = g_new (gchar *, 1);
509 510 511 512 513 514
      res[0] = NULL;
    }

  return res;
}

515 516 517 518 519
static void
g_unix_volume_volume_iface_init (GVolumeIface *iface)
{
  iface->get_name = g_unix_volume_get_name;
  iface->get_icon = g_unix_volume_get_icon;
520
  iface->get_uuid = g_unix_volume_get_uuid;
521
  iface->get_drive = g_unix_volume_get_drive;
522 523
  iface->get_mount = g_unix_volume_get_mount;
  iface->can_mount = g_unix_volume_can_mount;
524
  iface->can_eject = g_unix_volume_can_eject;
525
  iface->should_automount = g_unix_volume_should_automount;
526 527
  iface->mount_fn = g_unix_volume_mount;
  iface->mount_finish = g_unix_volume_mount_finish;
528 529
  iface->eject = g_unix_volume_eject;
  iface->eject_finish = g_unix_volume_eject_finish;
530 531
  iface->get_identifier = g_unix_volume_get_identifier;
  iface->enumerate_identifiers = g_unix_volume_enumerate_identifiers;
532
}