Blame gio/gunixvolume.c

Packit ae235b
/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
Packit ae235b
Packit ae235b
/* GIO - GLib Input, Output and Streaming Library
Packit ae235b
 * 
Packit ae235b
 * Copyright (C) 2006-2007 Red Hat, Inc.
Packit ae235b
 *
Packit ae235b
 * This library is free software; you can redistribute it and/or
Packit ae235b
 * modify it under the terms of the GNU Lesser General Public
Packit ae235b
 * License as published by the Free Software Foundation; either
Packit ae235b
 * version 2.1 of the License, or (at your option) any later version.
Packit ae235b
 *
Packit ae235b
 * This library is distributed in the hope that it will be useful,
Packit ae235b
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit ae235b
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit ae235b
 * Lesser General Public License for more details.
Packit ae235b
 *
Packit ae235b
 * You should have received a copy of the GNU Lesser General
Packit ae235b
 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
Packit ae235b
 *
Packit ae235b
 * Author: Alexander Larsson <alexl@redhat.com>
Packit ae235b
 *         David Zeuthen <davidz@redhat.com>
Packit ae235b
 */
Packit ae235b
Packit ae235b
#include "config.h"
Packit ae235b
Packit ae235b
#include <string.h>
Packit ae235b
#include <sys/wait.h>
Packit ae235b
#include <unistd.h>
Packit ae235b
Packit ae235b
#include <glib.h>
Packit ae235b
#include "gunixvolume.h"
Packit ae235b
#include "gunixmount.h"
Packit ae235b
#include "gunixmounts.h"
Packit ae235b
#include "gthemedicon.h"
Packit ae235b
#include "gvolume.h"
Packit ae235b
#include "gvolumemonitor.h"
Packit ae235b
#include "gtask.h"
Packit ae235b
#include "gioerror.h"
Packit ae235b
#include "glibintl.h"
Packit ae235b
/* for BUFSIZ */
Packit ae235b
#include <stdio.h>
Packit ae235b
Packit ae235b
Packit ae235b
struct _GUnixVolume {
Packit ae235b
  GObject parent;
Packit ae235b
Packit ae235b
  GVolumeMonitor *volume_monitor;
Packit ae235b
  GUnixMount     *mount; /* owned by volume monitor */
Packit ae235b
  
Packit ae235b
  char *device_path;
Packit ae235b
  char *mount_path;
Packit ae235b
  gboolean can_eject;
Packit ae235b
Packit ae235b
  char *identifier;
Packit ae235b
  char *identifier_type;
Packit ae235b
  
Packit ae235b
  char *name;
Packit ae235b
  GIcon *icon;
Packit ae235b
  GIcon *symbolic_icon;
Packit ae235b
};
Packit ae235b
Packit ae235b
static void g_unix_volume_volume_iface_init (GVolumeIface *iface);
Packit ae235b
Packit ae235b
#define g_unix_volume_get_type _g_unix_volume_get_type
Packit ae235b
G_DEFINE_TYPE_WITH_CODE (GUnixVolume, g_unix_volume, G_TYPE_OBJECT,
Packit ae235b
			 G_IMPLEMENT_INTERFACE (G_TYPE_VOLUME,
Packit ae235b
						g_unix_volume_volume_iface_init))
Packit ae235b
Packit ae235b
static void
Packit ae235b
g_unix_volume_finalize (GObject *object)
Packit ae235b
{
Packit ae235b
  GUnixVolume *volume;
Packit ae235b
  
Packit ae235b
  volume = G_UNIX_VOLUME (object);
Packit ae235b
Packit ae235b
  if (volume->volume_monitor != NULL)
Packit ae235b
    g_object_unref (volume->volume_monitor);
Packit ae235b
Packit ae235b
  if (volume->mount)
Packit ae235b
    _g_unix_mount_unset_volume (volume->mount, volume);
Packit ae235b
  
Packit ae235b
  g_object_unref (volume->icon);
Packit ae235b
  g_object_unref (volume->symbolic_icon);
Packit ae235b
  g_free (volume->name);
Packit ae235b
  g_free (volume->mount_path);
Packit ae235b
  g_free (volume->device_path);
Packit ae235b
  g_free (volume->identifier);
Packit ae235b
  g_free (volume->identifier_type);
Packit ae235b
Packit ae235b
  G_OBJECT_CLASS (g_unix_volume_parent_class)->finalize (object);
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
g_unix_volume_class_init (GUnixVolumeClass *klass)
Packit ae235b
{
Packit ae235b
  GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
Packit ae235b
Packit ae235b
  gobject_class->finalize = g_unix_volume_finalize;
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
g_unix_volume_init (GUnixVolume *unix_volume)
Packit ae235b
{
Packit ae235b
}
Packit ae235b
Packit ae235b
GUnixVolume *
Packit ae235b
_g_unix_volume_new (GVolumeMonitor  *volume_monitor,
Packit ae235b
                    GUnixMountPoint *mountpoint)
Packit ae235b
{
Packit ae235b
  GUnixVolume *volume;
Packit ae235b
  
Packit ae235b
  if (!(g_unix_mount_point_is_user_mountable (mountpoint) ||
Packit ae235b
	g_str_has_prefix (g_unix_mount_point_get_device_path (mountpoint), "/vol/")) ||
Packit ae235b
      g_unix_mount_point_is_loopback (mountpoint))
Packit ae235b
    return NULL;
Packit ae235b
  
Packit ae235b
  volume = g_object_new (G_TYPE_UNIX_VOLUME, NULL);
Packit ae235b
  volume->volume_monitor = volume_monitor != NULL ? g_object_ref (volume_monitor) : NULL;
Packit ae235b
  volume->mount_path = g_strdup (g_unix_mount_point_get_mount_path (mountpoint));
Packit ae235b
  volume->device_path = g_strdup (g_unix_mount_point_get_device_path (mountpoint));
Packit ae235b
  volume->can_eject = g_unix_mount_point_guess_can_eject (mountpoint);
Packit ae235b
Packit ae235b
  volume->name = g_unix_mount_point_guess_name (mountpoint);
Packit ae235b
  volume->icon = g_unix_mount_point_guess_icon (mountpoint);
Packit ae235b
  volume->symbolic_icon = g_unix_mount_point_guess_symbolic_icon (mountpoint);
Packit ae235b
Packit ae235b
Packit ae235b
  if (strcmp (g_unix_mount_point_get_fs_type (mountpoint), "nfs") == 0)
Packit ae235b
    {
Packit ae235b
      volume->identifier_type = g_strdup (G_VOLUME_IDENTIFIER_KIND_NFS_MOUNT);
Packit ae235b
      volume->identifier = g_strdup (volume->device_path);
Packit ae235b
    }
Packit ae235b
  else if (g_str_has_prefix (volume->device_path, "LABEL="))
Packit ae235b
    {
Packit ae235b
      volume->identifier_type = g_strdup (G_VOLUME_IDENTIFIER_KIND_LABEL);
Packit ae235b
      volume->identifier = g_strdup (volume->device_path + 6);
Packit ae235b
    }
Packit ae235b
  else if (g_str_has_prefix (volume->device_path, "UUID="))
Packit ae235b
    {
Packit ae235b
      volume->identifier_type = g_strdup (G_VOLUME_IDENTIFIER_KIND_UUID);
Packit ae235b
      volume->identifier = g_strdup (volume->device_path + 5);
Packit ae235b
    }
Packit ae235b
  else if (g_path_is_absolute (volume->device_path))
Packit ae235b
    {
Packit ae235b
      volume->identifier_type = g_strdup (G_VOLUME_IDENTIFIER_KIND_UNIX_DEVICE);
Packit ae235b
      volume->identifier = g_strdup (volume->device_path);
Packit ae235b
    }
Packit ae235b
  
Packit ae235b
  return volume;
Packit ae235b
}
Packit ae235b
Packit ae235b
void
Packit ae235b
_g_unix_volume_disconnected (GUnixVolume *volume)
Packit ae235b
{
Packit ae235b
  if (volume->mount)
Packit ae235b
    {
Packit ae235b
      _g_unix_mount_unset_volume (volume->mount, volume);
Packit ae235b
      volume->mount = NULL;
Packit ae235b
    }
Packit ae235b
}
Packit ae235b
Packit ae235b
void
Packit ae235b
_g_unix_volume_set_mount (GUnixVolume *volume,
Packit ae235b
                          GUnixMount  *mount)
Packit ae235b
{
Packit ae235b
  if (volume->mount == mount)
Packit ae235b
    return;
Packit ae235b
  
Packit ae235b
  if (volume->mount)
Packit ae235b
    _g_unix_mount_unset_volume (volume->mount, volume);
Packit ae235b
  
Packit ae235b
  volume->mount = mount;
Packit ae235b
  
Packit ae235b
  /* TODO: Emit changed in idle to avoid locking issues */
Packit ae235b
  g_signal_emit_by_name (volume, "changed");
Packit ae235b
  if (volume->volume_monitor != NULL)
Packit ae235b
    g_signal_emit_by_name (volume->volume_monitor, "volume-changed", volume);
Packit ae235b
}
Packit ae235b
Packit ae235b
void
Packit ae235b
_g_unix_volume_unset_mount (GUnixVolume  *volume,
Packit ae235b
                            GUnixMount *mount)
Packit ae235b
{
Packit ae235b
  if (volume->mount == mount)
Packit ae235b
    {
Packit ae235b
      volume->mount = NULL;
Packit ae235b
      /* TODO: Emit changed in idle to avoid locking issues */
Packit ae235b
      g_signal_emit_by_name (volume, "changed");
Packit ae235b
      if (volume->volume_monitor != NULL)
Packit ae235b
        g_signal_emit_by_name (volume->volume_monitor, "volume-changed", volume);
Packit ae235b
    }
Packit ae235b
}
Packit ae235b
Packit ae235b
static GIcon *
Packit ae235b
g_unix_volume_get_icon (GVolume *volume)
Packit ae235b
{
Packit ae235b
  GUnixVolume *unix_volume = G_UNIX_VOLUME (volume);
Packit ae235b
  return g_object_ref (unix_volume->icon);
Packit ae235b
}
Packit ae235b
Packit ae235b
static GIcon *
Packit ae235b
g_unix_volume_get_symbolic_icon (GVolume *volume)
Packit ae235b
{
Packit ae235b
  GUnixVolume *unix_volume = G_UNIX_VOLUME (volume);
Packit ae235b
  return g_object_ref (unix_volume->symbolic_icon);
Packit ae235b
}
Packit ae235b
Packit ae235b
static char *
Packit ae235b
g_unix_volume_get_name (GVolume *volume)
Packit ae235b
{
Packit ae235b
  GUnixVolume *unix_volume = G_UNIX_VOLUME (volume);
Packit ae235b
  return g_strdup (unix_volume->name);
Packit ae235b
}
Packit ae235b
Packit ae235b
static char *
Packit ae235b
g_unix_volume_get_uuid (GVolume *volume)
Packit ae235b
{
Packit ae235b
  return NULL;
Packit ae235b
}
Packit ae235b
Packit ae235b
static gboolean
Packit ae235b
g_unix_volume_can_mount (GVolume *volume)
Packit ae235b
{
Packit ae235b
  return TRUE;
Packit ae235b
}
Packit ae235b
Packit ae235b
static gboolean
Packit ae235b
g_unix_volume_can_eject (GVolume *volume)
Packit ae235b
{
Packit ae235b
  GUnixVolume *unix_volume = G_UNIX_VOLUME (volume);
Packit ae235b
  return unix_volume->can_eject;
Packit ae235b
}
Packit ae235b
Packit ae235b
static gboolean
Packit ae235b
g_unix_volume_should_automount (GVolume *volume)
Packit ae235b
{
Packit ae235b
  /* We automount all local volumes because we don't even
Packit ae235b
   * make the internal stuff visible
Packit ae235b
   */
Packit ae235b
  return TRUE;
Packit ae235b
}
Packit ae235b
Packit ae235b
static GDrive *
Packit ae235b
g_unix_volume_get_drive (GVolume *volume)
Packit ae235b
{
Packit ae235b
  return NULL;
Packit ae235b
}
Packit ae235b
Packit ae235b
static GMount *
Packit ae235b
g_unix_volume_get_mount (GVolume *volume)
Packit ae235b
{
Packit ae235b
  GUnixVolume *unix_volume = G_UNIX_VOLUME (volume);
Packit ae235b
Packit ae235b
  if (unix_volume->mount != NULL)
Packit ae235b
    return g_object_ref (G_MOUNT (unix_volume->mount));
Packit ae235b
Packit ae235b
  return NULL;
Packit ae235b
}
Packit ae235b
Packit ae235b
Packit ae235b
gboolean
Packit ae235b
_g_unix_volume_has_mount_path (GUnixVolume *volume,
Packit ae235b
                               const char  *mount_path)
Packit ae235b
{
Packit ae235b
  return strcmp (volume->mount_path, mount_path) == 0;
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
eject_mount_done (GObject      *source,
Packit ae235b
                  GAsyncResult *result,
Packit ae235b
                  gpointer      user_data)
Packit ae235b
{
Packit ae235b
  GSubprocess *subprocess = G_SUBPROCESS (source);
Packit ae235b
  GTask *task = user_data;
Packit ae235b
  GError *error = NULL;
Packit ae235b
  gchar *stderr_str;
Packit ae235b
Packit ae235b
  if (!g_subprocess_communicate_utf8_finish (subprocess, result, NULL, &stderr_str, &error))
Packit ae235b
    {
Packit ae235b
      g_task_return_error (task, error);
Packit ae235b
      g_error_free (error);
Packit ae235b
    }
Packit ae235b
  else /* successful communication */
Packit ae235b
    {
Packit ae235b
      if (!g_subprocess_get_successful (subprocess))
Packit ae235b
        /* ...but bad exit code */
Packit ae235b
        g_task_return_new_error (task, G_IO_ERROR, G_IO_ERROR_FAILED, "%s", stderr_str);
Packit ae235b
      else
Packit ae235b
        /* ...and successful exit code */
Packit ae235b
        g_task_return_boolean (task, TRUE);
Packit ae235b
Packit ae235b
      g_free (stderr_str);
Packit ae235b
    }
Packit ae235b
Packit ae235b
  g_object_unref (task);
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
eject_mount_do (GVolume              *volume,
Packit ae235b
                GCancellable         *cancellable,
Packit ae235b
                GAsyncReadyCallback   callback,
Packit ae235b
                gpointer              user_data,
Packit ae235b
                const gchar * const  *argv)
Packit ae235b
{
Packit ae235b
  GSubprocess *subprocess;
Packit ae235b
  GError *error = NULL;
Packit ae235b
  GTask *task;
Packit ae235b
Packit ae235b
  task = g_task_new (volume, cancellable, callback, user_data);
Packit ae235b
  g_task_set_source_tag (task, eject_mount_do);
Packit ae235b
Packit ae235b
  if (g_task_return_error_if_cancelled (task))
Packit ae235b
    {
Packit ae235b
      g_object_unref (task);
Packit ae235b
      return;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  subprocess = g_subprocess_newv (argv, G_SUBPROCESS_FLAGS_STDOUT_SILENCE | G_SUBPROCESS_FLAGS_STDERR_PIPE, &error);
Packit ae235b
  g_assert_no_error (error);
Packit ae235b
Packit ae235b
  g_subprocess_communicate_utf8_async (subprocess, NULL,
Packit ae235b
                                       g_task_get_cancellable (task),
Packit ae235b
                                       eject_mount_done, task);
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
g_unix_volume_mount (GVolume            *volume,
Packit ae235b
                     GMountMountFlags    flags,
Packit ae235b
                     GMountOperation     *mount_operation,
Packit ae235b
                     GCancellable        *cancellable,
Packit ae235b
                     GAsyncReadyCallback  callback,
Packit ae235b
                     gpointer             user_data)
Packit ae235b
{
Packit ae235b
  GUnixVolume *unix_volume = G_UNIX_VOLUME (volume);
Packit ae235b
  const gchar *argv[] = { "mount", NULL, NULL };
Packit ae235b
Packit ae235b
  if (unix_volume->mount_path != NULL)
Packit ae235b
    argv[1] = unix_volume->mount_path;
Packit ae235b
  else
Packit ae235b
    argv[1] = unix_volume->device_path;
Packit ae235b
Packit ae235b
  eject_mount_do (volume, cancellable, callback, user_data, argv);
Packit ae235b
}
Packit ae235b
Packit ae235b
static gboolean
Packit ae235b
g_unix_volume_mount_finish (GVolume        *volume,
Packit ae235b
                            GAsyncResult  *result,
Packit ae235b
                            GError       **error)
Packit ae235b
{
Packit ae235b
  g_return_val_if_fail (g_task_is_valid (result, volume), FALSE);
Packit ae235b
Packit ae235b
  return g_task_propagate_boolean (G_TASK (result), error);
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
g_unix_volume_eject (GVolume             *volume,
Packit ae235b
                     GMountUnmountFlags   flags,
Packit ae235b
                     GCancellable        *cancellable,
Packit ae235b
                     GAsyncReadyCallback  callback,
Packit ae235b
                     gpointer             user_data)
Packit ae235b
{
Packit ae235b
  GUnixVolume *unix_volume = G_UNIX_VOLUME (volume);
Packit ae235b
  const gchar *argv[] = { "eject", NULL, NULL };
Packit ae235b
Packit ae235b
  argv[1] = unix_volume->device_path;
Packit ae235b
Packit ae235b
  eject_mount_do (volume, cancellable, callback, user_data, argv);
Packit ae235b
}
Packit ae235b
Packit ae235b
static gboolean
Packit ae235b
g_unix_volume_eject_finish (GVolume       *volume,
Packit ae235b
                            GAsyncResult  *result,
Packit ae235b
                            GError       **error)
Packit ae235b
{
Packit ae235b
  g_return_val_if_fail (g_task_is_valid (result, volume), FALSE);
Packit ae235b
Packit ae235b
  return g_task_propagate_boolean (G_TASK (result), error);
Packit ae235b
}
Packit ae235b
Packit ae235b
static gchar *
Packit ae235b
g_unix_volume_get_identifier (GVolume     *volume,
Packit ae235b
                              const gchar *kind)
Packit ae235b
{
Packit ae235b
  GUnixVolume *unix_volume = G_UNIX_VOLUME (volume);
Packit ae235b
Packit ae235b
  if (unix_volume->identifier_type != NULL &&
Packit ae235b
      strcmp (kind, unix_volume->identifier_type) == 0)
Packit ae235b
    return g_strdup (unix_volume->identifier);
Packit ae235b
Packit ae235b
  return NULL;
Packit ae235b
}
Packit ae235b
Packit ae235b
static gchar **
Packit ae235b
g_unix_volume_enumerate_identifiers (GVolume *volume)
Packit ae235b
{
Packit ae235b
  GUnixVolume *unix_volume = G_UNIX_VOLUME (volume);
Packit ae235b
  gchar **res;
Packit ae235b
Packit ae235b
  if (unix_volume->identifier_type)
Packit ae235b
    {
Packit ae235b
      res = g_new (gchar *, 2);
Packit ae235b
      res[0] = g_strdup (unix_volume->identifier_type);
Packit ae235b
      res[1] = NULL;
Packit ae235b
    }
Packit ae235b
  else
Packit ae235b
    {
Packit ae235b
      res = g_new (gchar *, 1);
Packit ae235b
      res[0] = NULL;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  return res;
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
g_unix_volume_volume_iface_init (GVolumeIface *iface)
Packit ae235b
{
Packit ae235b
  iface->get_name = g_unix_volume_get_name;
Packit ae235b
  iface->get_icon = g_unix_volume_get_icon;
Packit ae235b
  iface->get_symbolic_icon = g_unix_volume_get_symbolic_icon;
Packit ae235b
  iface->get_uuid = g_unix_volume_get_uuid;
Packit ae235b
  iface->get_drive = g_unix_volume_get_drive;
Packit ae235b
  iface->get_mount = g_unix_volume_get_mount;
Packit ae235b
  iface->can_mount = g_unix_volume_can_mount;
Packit ae235b
  iface->can_eject = g_unix_volume_can_eject;
Packit ae235b
  iface->should_automount = g_unix_volume_should_automount;
Packit ae235b
  iface->mount_fn = g_unix_volume_mount;
Packit ae235b
  iface->mount_finish = g_unix_volume_mount_finish;
Packit ae235b
  iface->eject = g_unix_volume_eject;
Packit ae235b
  iface->eject_finish = g_unix_volume_eject_finish;
Packit ae235b
  iface->get_identifier = g_unix_volume_get_identifier;
Packit ae235b
  iface->enumerate_identifiers = g_unix_volume_enumerate_identifiers;
Packit ae235b
}