Blame gio/gunixmount.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 "gsubprocess.h"
Packit ae235b
#include "gioenums.h"
Packit ae235b
#include "gunixvolumemonitor.h"
Packit ae235b
#include "gunixmount.h"
Packit ae235b
#include "gunixmounts.h"
Packit ae235b
#include "gunixvolume.h"
Packit ae235b
#include "gmountprivate.h"
Packit ae235b
#include "gmount.h"
Packit ae235b
#include "gfile.h"
Packit ae235b
#include "gvolumemonitor.h"
Packit ae235b
#include "gthemedicon.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 _GUnixMount {
Packit ae235b
  GObject parent;
Packit ae235b
Packit ae235b
  GVolumeMonitor   *volume_monitor;
Packit ae235b
Packit ae235b
  GUnixVolume      *volume; /* owned by volume monitor */
Packit ae235b
Packit ae235b
  char *name;
Packit ae235b
  GIcon *icon;
Packit ae235b
  GIcon *symbolic_icon;
Packit ae235b
  char *device_path;
Packit ae235b
  char *mount_path;
Packit ae235b
Packit ae235b
  gboolean can_eject;
Packit ae235b
};
Packit ae235b
Packit ae235b
static void g_unix_mount_mount_iface_init (GMountIface *iface);
Packit ae235b
Packit ae235b
#define g_unix_mount_get_type _g_unix_mount_get_type
Packit ae235b
G_DEFINE_TYPE_WITH_CODE (GUnixMount, g_unix_mount, G_TYPE_OBJECT,
Packit ae235b
			 G_IMPLEMENT_INTERFACE (G_TYPE_MOUNT,
Packit ae235b
						g_unix_mount_mount_iface_init))
Packit ae235b
Packit ae235b
Packit ae235b
static void
Packit ae235b
g_unix_mount_finalize (GObject *object)
Packit ae235b
{
Packit ae235b
  GUnixMount *mount;
Packit ae235b
  
Packit ae235b
  mount = G_UNIX_MOUNT (object);
Packit ae235b
Packit ae235b
  if (mount->volume_monitor != NULL)
Packit ae235b
    g_object_unref (mount->volume_monitor);
Packit ae235b
Packit ae235b
  if (mount->volume)
Packit ae235b
    _g_unix_volume_unset_mount (mount->volume, mount);
Packit ae235b
    
Packit ae235b
  /* TODO: g_warn_if_fail (volume->volume == NULL); */
Packit ae235b
  g_object_unref (mount->icon);
Packit ae235b
  g_object_unref (mount->symbolic_icon);
Packit ae235b
  g_free (mount->name);
Packit ae235b
  g_free (mount->device_path);
Packit ae235b
  g_free (mount->mount_path);
Packit ae235b
Packit ae235b
  G_OBJECT_CLASS (g_unix_mount_parent_class)->finalize (object);
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
g_unix_mount_class_init (GUnixMountClass *klass)
Packit ae235b
{
Packit ae235b
  GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
Packit ae235b
Packit ae235b
  gobject_class->finalize = g_unix_mount_finalize;
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
g_unix_mount_init (GUnixMount *unix_mount)
Packit ae235b
{
Packit ae235b
}
Packit ae235b
Packit ae235b
GUnixMount *
Packit ae235b
_g_unix_mount_new (GVolumeMonitor  *volume_monitor,
Packit ae235b
                   GUnixMountEntry *mount_entry,
Packit ae235b
                   GUnixVolume     *volume)
Packit ae235b
{
Packit ae235b
  GUnixMount *mount;
Packit ae235b
  
Packit ae235b
  /* No volume for mount: Ignore internal things */
Packit ae235b
  if (volume == NULL && !g_unix_mount_guess_should_display (mount_entry))
Packit ae235b
    return NULL;
Packit ae235b
Packit ae235b
  mount = g_object_new (G_TYPE_UNIX_MOUNT, NULL);
Packit ae235b
  mount->volume_monitor = volume_monitor != NULL ? g_object_ref (volume_monitor) : NULL;
Packit ae235b
  mount->device_path = g_strdup (g_unix_mount_get_device_path (mount_entry));
Packit ae235b
  mount->mount_path = g_strdup (g_unix_mount_get_mount_path (mount_entry));
Packit ae235b
  mount->can_eject = g_unix_mount_guess_can_eject (mount_entry);
Packit ae235b
Packit ae235b
  mount->name = g_unix_mount_guess_name (mount_entry);
Packit ae235b
  mount->icon = g_unix_mount_guess_icon (mount_entry);
Packit ae235b
  mount->symbolic_icon = g_unix_mount_guess_symbolic_icon (mount_entry);
Packit ae235b
Packit ae235b
  /* need to do this last */
Packit ae235b
  mount->volume = volume;
Packit ae235b
  if (volume != NULL)
Packit ae235b
    _g_unix_volume_set_mount (volume, mount);
Packit ae235b
Packit ae235b
  return mount;
Packit ae235b
}
Packit ae235b
Packit ae235b
void
Packit ae235b
_g_unix_mount_unmounted (GUnixMount *mount)
Packit ae235b
{
Packit ae235b
  if (mount->volume != NULL)
Packit ae235b
    {
Packit ae235b
      _g_unix_volume_unset_mount (mount->volume, mount);
Packit ae235b
      mount->volume = NULL;
Packit ae235b
      g_signal_emit_by_name (mount, "changed");
Packit ae235b
      /* there's really no need to emit mount_changed on the volume monitor 
Packit ae235b
       * as we're going to be deleted.. */
Packit ae235b
    }
Packit ae235b
}
Packit ae235b
Packit ae235b
void
Packit ae235b
_g_unix_mount_unset_volume (GUnixMount *mount,
Packit ae235b
                            GUnixVolume  *volume)
Packit ae235b
{
Packit ae235b
  if (mount->volume == volume)
Packit ae235b
    {
Packit ae235b
      mount->volume = NULL;
Packit ae235b
      /* TODO: Emit changed in idle to avoid locking issues */
Packit ae235b
      g_signal_emit_by_name (mount, "changed");
Packit ae235b
      if (mount->volume_monitor != NULL)
Packit ae235b
        g_signal_emit_by_name (mount->volume_monitor, "mount-changed", mount);
Packit ae235b
    }
Packit ae235b
}
Packit ae235b
Packit ae235b
static GFile *
Packit ae235b
g_unix_mount_get_root (GMount *mount)
Packit ae235b
{
Packit ae235b
  GUnixMount *unix_mount = G_UNIX_MOUNT (mount);
Packit ae235b
Packit ae235b
  return g_file_new_for_path (unix_mount->mount_path);
Packit ae235b
}
Packit ae235b
Packit ae235b
static GIcon *
Packit ae235b
g_unix_mount_get_icon (GMount *mount)
Packit ae235b
{
Packit ae235b
  GUnixMount *unix_mount = G_UNIX_MOUNT (mount);
Packit ae235b
Packit ae235b
  return g_object_ref (unix_mount->icon);
Packit ae235b
}
Packit ae235b
Packit ae235b
static GIcon *
Packit ae235b
g_unix_mount_get_symbolic_icon (GMount *mount)
Packit ae235b
{
Packit ae235b
  GUnixMount *unix_mount = G_UNIX_MOUNT (mount);
Packit ae235b
Packit ae235b
  return g_object_ref (unix_mount->symbolic_icon);
Packit ae235b
}
Packit ae235b
Packit ae235b
static char *
Packit ae235b
g_unix_mount_get_uuid (GMount *mount)
Packit ae235b
{
Packit ae235b
  return NULL;
Packit ae235b
}
Packit ae235b
Packit ae235b
static char *
Packit ae235b
g_unix_mount_get_name (GMount *mount)
Packit ae235b
{
Packit ae235b
  GUnixMount *unix_mount = G_UNIX_MOUNT (mount);
Packit ae235b
  
Packit ae235b
  return g_strdup (unix_mount->name);
Packit ae235b
}
Packit ae235b
Packit ae235b
gboolean
Packit ae235b
_g_unix_mount_has_mount_path (GUnixMount *mount,
Packit ae235b
                              const char  *mount_path)
Packit ae235b
{
Packit ae235b
  return strcmp (mount->mount_path, mount_path) == 0;
Packit ae235b
}
Packit ae235b
Packit ae235b
static GDrive *
Packit ae235b
g_unix_mount_get_drive (GMount *mount)
Packit ae235b
{
Packit ae235b
  GUnixMount *unix_mount = G_UNIX_MOUNT (mount);
Packit ae235b
Packit ae235b
  if (unix_mount->volume != NULL)
Packit ae235b
    return g_volume_get_drive (G_VOLUME (unix_mount->volume));
Packit ae235b
Packit ae235b
  return NULL;
Packit ae235b
}
Packit ae235b
Packit ae235b
static GVolume *
Packit ae235b
g_unix_mount_get_volume (GMount *mount)
Packit ae235b
{
Packit ae235b
  GUnixMount *unix_mount = G_UNIX_MOUNT (mount);
Packit ae235b
Packit ae235b
  if (unix_mount->volume)
Packit ae235b
    return G_VOLUME (g_object_ref (unix_mount->volume));
Packit ae235b
  
Packit ae235b
  return NULL;
Packit ae235b
}
Packit ae235b
Packit ae235b
static gboolean
Packit ae235b
g_unix_mount_can_unmount (GMount *mount)
Packit ae235b
{
Packit ae235b
  return TRUE;
Packit ae235b
}
Packit ae235b
Packit ae235b
static gboolean
Packit ae235b
g_unix_mount_can_eject (GMount *mount)
Packit ae235b
{
Packit ae235b
  GUnixMount *unix_mount = G_UNIX_MOUNT (mount);
Packit ae235b
  return unix_mount->can_eject;
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
eject_unmount_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 gboolean
Packit ae235b
eject_unmount_do_cb (gpointer user_data)
Packit ae235b
{
Packit ae235b
  GTask *task = user_data;
Packit ae235b
  GError *error = NULL;
Packit ae235b
  GSubprocess *subprocess;
Packit ae235b
  const gchar **argv;
Packit ae235b
Packit ae235b
  argv = g_task_get_task_data (task);
Packit ae235b
Packit ae235b
  if (g_task_return_error_if_cancelled (task))
Packit ae235b
    {
Packit ae235b
      g_object_unref (task);
Packit ae235b
      return G_SOURCE_REMOVE;
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_unmount_done, task);
Packit ae235b
Packit ae235b
  return G_SOURCE_REMOVE;
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
eject_unmount_do (GMount              *mount,
Packit ae235b
                  GCancellable        *cancellable,
Packit ae235b
                  GAsyncReadyCallback  callback,
Packit ae235b
                  gpointer             user_data,
Packit ae235b
                  char               **argv)
Packit ae235b
{
Packit ae235b
  GUnixMount *unix_mount = G_UNIX_MOUNT (mount);
Packit ae235b
  GTask *task;
Packit ae235b
  GSource *timeout;
Packit ae235b
Packit ae235b
  task = g_task_new (mount, cancellable, callback, user_data);
Packit ae235b
  g_task_set_source_tag (task, eject_unmount_do);
Packit ae235b
  g_task_set_task_data (task, g_strdupv (argv), (GDestroyNotify) g_strfreev);
Packit ae235b
Packit ae235b
  if (unix_mount->volume_monitor != NULL)
Packit ae235b
    g_signal_emit_by_name (unix_mount->volume_monitor, "mount-pre-unmount", mount);
Packit ae235b
Packit ae235b
  g_signal_emit_by_name (mount, "pre-unmount", 0);
Packit ae235b
Packit ae235b
  timeout = g_timeout_source_new (500);
Packit ae235b
  g_task_attach_source (task, timeout, (GSourceFunc) eject_unmount_do_cb);
Packit ae235b
  g_source_unref (timeout);
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
g_unix_mount_unmount (GMount             *mount,
Packit ae235b
                      GMountUnmountFlags flags,
Packit ae235b
                      GCancellable        *cancellable,
Packit ae235b
                      GAsyncReadyCallback  callback,
Packit ae235b
                      gpointer             user_data)
Packit ae235b
{
Packit ae235b
  GUnixMount *unix_mount = G_UNIX_MOUNT (mount);
Packit ae235b
  char *argv[] = {"umount", NULL, NULL};
Packit ae235b
Packit ae235b
  if (unix_mount->mount_path != NULL)
Packit ae235b
    argv[1] = unix_mount->mount_path;
Packit ae235b
  else
Packit ae235b
    argv[1] = unix_mount->device_path;
Packit ae235b
Packit ae235b
  eject_unmount_do (mount, cancellable, callback, user_data, argv);
Packit ae235b
}
Packit ae235b
Packit ae235b
static gboolean
Packit ae235b
g_unix_mount_unmount_finish (GMount       *mount,
Packit ae235b
                             GAsyncResult  *result,
Packit ae235b
                             GError       **error)
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_mount_eject (GMount             *mount,
Packit ae235b
                    GMountUnmountFlags flags,
Packit ae235b
                    GCancellable        *cancellable,
Packit ae235b
                    GAsyncReadyCallback  callback,
Packit ae235b
                    gpointer             user_data)
Packit ae235b
{
Packit ae235b
  GUnixMount *unix_mount = G_UNIX_MOUNT (mount);
Packit ae235b
  char *argv[] = {"eject", NULL, NULL};
Packit ae235b
Packit ae235b
  if (unix_mount->mount_path != NULL)
Packit ae235b
    argv[1] = unix_mount->mount_path;
Packit ae235b
  else
Packit ae235b
    argv[1] = unix_mount->device_path;
Packit ae235b
Packit ae235b
  eject_unmount_do (mount, cancellable, callback, user_data, argv);
Packit ae235b
}
Packit ae235b
Packit ae235b
static gboolean
Packit ae235b
g_unix_mount_eject_finish (GMount       *mount,
Packit ae235b
                           GAsyncResult  *result,
Packit ae235b
                           GError       **error)
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_mount_mount_iface_init (GMountIface *iface)
Packit ae235b
{
Packit ae235b
  iface->get_root = g_unix_mount_get_root;
Packit ae235b
  iface->get_name = g_unix_mount_get_name;
Packit ae235b
  iface->get_icon = g_unix_mount_get_icon;
Packit ae235b
  iface->get_symbolic_icon = g_unix_mount_get_symbolic_icon;
Packit ae235b
  iface->get_uuid = g_unix_mount_get_uuid;
Packit ae235b
  iface->get_drive = g_unix_mount_get_drive;
Packit ae235b
  iface->get_volume = g_unix_mount_get_volume;
Packit ae235b
  iface->can_unmount = g_unix_mount_can_unmount;
Packit ae235b
  iface->can_eject = g_unix_mount_can_eject;
Packit ae235b
  iface->unmount = g_unix_mount_unmount;
Packit ae235b
  iface->unmount_finish = g_unix_mount_unmount_finish;
Packit ae235b
  iface->eject = g_unix_mount_eject;
Packit ae235b
  iface->eject_finish = g_unix_mount_eject_finish;
Packit ae235b
}