1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
3 /* GIO - GLib Input, Output and Streaming Library
5 * Copyright (C) 2006-2007 Red Hat, Inc.
7 * SPDX-License-Identifier: LGPL-2.1-or-later
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General
20 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
22 * Author: Alexander Larsson <alexl@redhat.com>
23 * David Zeuthen <davidz@redhat.com>
33 #include "gsubprocess.h"
35 #include "gunixvolumemonitor.h"
36 #include "gunixmount.h"
37 #include "gunixmounts.h"
38 #include "gunixvolume.h"
39 #include "gmountprivate.h"
42 #include "gvolumemonitor.h"
43 #include "gthemedicon.h"
53 GVolumeMonitor *volume_monitor;
55 GUnixVolume *volume; /* owned by volume monitor */
66 static void g_unix_mount_mount_iface_init (GMountIface *iface);
68 #define g_unix_mount_get_type _g_unix_mount_get_type
69 G_DEFINE_TYPE_WITH_CODE (GUnixMount, g_unix_mount, G_TYPE_OBJECT,
70 G_IMPLEMENT_INTERFACE (G_TYPE_MOUNT,
71 g_unix_mount_mount_iface_init))
75 g_unix_mount_finalize (GObject *object)
79 mount = G_UNIX_MOUNT (object);
81 if (mount->volume_monitor != NULL)
82 g_object_unref (mount->volume_monitor);
85 _g_unix_volume_unset_mount (mount->volume, mount);
87 /* TODO: g_warn_if_fail (volume->volume == NULL); */
88 g_object_unref (mount->icon);
89 g_object_unref (mount->symbolic_icon);
91 g_free (mount->device_path);
92 g_free (mount->mount_path);
94 G_OBJECT_CLASS (g_unix_mount_parent_class)->finalize (object);
98 g_unix_mount_class_init (GUnixMountClass *klass)
100 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
102 gobject_class->finalize = g_unix_mount_finalize;
106 g_unix_mount_init (GUnixMount *unix_mount)
111 _g_unix_mount_new (GVolumeMonitor *volume_monitor,
112 GUnixMountEntry *mount_entry,
117 /* No volume for mount: Ignore internal things */
118 if (volume == NULL && !g_unix_mount_guess_should_display (mount_entry))
121 mount = g_object_new (G_TYPE_UNIX_MOUNT, NULL);
122 mount->volume_monitor = volume_monitor != NULL ? g_object_ref (volume_monitor) : NULL;
123 mount->device_path = g_strdup (g_unix_mount_get_device_path (mount_entry));
124 mount->mount_path = g_strdup (g_unix_mount_get_mount_path (mount_entry));
125 mount->can_eject = g_unix_mount_guess_can_eject (mount_entry);
127 mount->name = g_unix_mount_guess_name (mount_entry);
128 mount->icon = g_unix_mount_guess_icon (mount_entry);
129 mount->symbolic_icon = g_unix_mount_guess_symbolic_icon (mount_entry);
131 /* need to do this last */
132 mount->volume = volume;
134 _g_unix_volume_set_mount (volume, mount);
140 _g_unix_mount_unmounted (GUnixMount *mount)
142 if (mount->volume != NULL)
144 _g_unix_volume_unset_mount (mount->volume, mount);
145 mount->volume = NULL;
146 g_signal_emit_by_name (mount, "changed");
147 /* there's really no need to emit mount_changed on the volume monitor
148 * as we're going to be deleted.. */
153 _g_unix_mount_unset_volume (GUnixMount *mount,
156 if (mount->volume == volume)
158 mount->volume = NULL;
159 /* TODO: Emit changed in idle to avoid locking issues */
160 g_signal_emit_by_name (mount, "changed");
161 if (mount->volume_monitor != NULL)
162 g_signal_emit_by_name (mount->volume_monitor, "mount-changed", mount);
167 g_unix_mount_get_root (GMount *mount)
169 GUnixMount *unix_mount = G_UNIX_MOUNT (mount);
171 return g_file_new_for_path (unix_mount->mount_path);
175 g_unix_mount_get_icon (GMount *mount)
177 GUnixMount *unix_mount = G_UNIX_MOUNT (mount);
179 return g_object_ref (unix_mount->icon);
183 g_unix_mount_get_symbolic_icon (GMount *mount)
185 GUnixMount *unix_mount = G_UNIX_MOUNT (mount);
187 return g_object_ref (unix_mount->symbolic_icon);
191 g_unix_mount_get_uuid (GMount *mount)
197 g_unix_mount_get_name (GMount *mount)
199 GUnixMount *unix_mount = G_UNIX_MOUNT (mount);
201 return g_strdup (unix_mount->name);
205 _g_unix_mount_has_mount_path (GUnixMount *mount,
206 const char *mount_path)
208 return strcmp (mount->mount_path, mount_path) == 0;
212 g_unix_mount_get_drive (GMount *mount)
214 GUnixMount *unix_mount = G_UNIX_MOUNT (mount);
216 if (unix_mount->volume != NULL)
217 return g_volume_get_drive (G_VOLUME (unix_mount->volume));
223 g_unix_mount_get_volume (GMount *mount)
225 GUnixMount *unix_mount = G_UNIX_MOUNT (mount);
227 if (unix_mount->volume)
228 return G_VOLUME (g_object_ref (unix_mount->volume));
234 g_unix_mount_can_unmount (GMount *mount)
240 g_unix_mount_can_eject (GMount *mount)
242 GUnixMount *unix_mount = G_UNIX_MOUNT (mount);
243 return unix_mount->can_eject;
247 eject_unmount_done (GObject *source,
248 GAsyncResult *result,
251 GSubprocess *subprocess = G_SUBPROCESS (source);
252 GTask *task = user_data;
253 GError *error = NULL;
256 if (!g_subprocess_communicate_utf8_finish (subprocess, result, NULL, &stderr_str, &error))
258 g_task_return_error (task, error);
259 g_error_free (error);
261 else /* successful communication */
263 if (!g_subprocess_get_successful (subprocess))
264 /* ...but bad exit code */
265 g_task_return_new_error_literal (task, G_IO_ERROR, G_IO_ERROR_FAILED, stderr_str);
267 /* ...and successful exit code */
268 g_task_return_boolean (task, TRUE);
273 g_object_unref (task);
277 eject_unmount_do_cb (gpointer user_data)
279 GTask *task = user_data;
280 GError *error = NULL;
281 GSubprocess *subprocess;
284 argv = g_task_get_task_data (task);
286 if (g_task_return_error_if_cancelled (task))
288 g_object_unref (task);
289 return G_SOURCE_REMOVE;
292 subprocess = g_subprocess_newv (argv, G_SUBPROCESS_FLAGS_STDOUT_SILENCE | G_SUBPROCESS_FLAGS_STDERR_PIPE, &error);
293 g_assert_no_error (error);
295 g_subprocess_communicate_utf8_async (subprocess, NULL,
296 g_task_get_cancellable (task),
297 eject_unmount_done, task);
299 return G_SOURCE_REMOVE;
303 eject_unmount_do (GMount *mount,
304 GCancellable *cancellable,
305 GAsyncReadyCallback callback,
308 const gchar *task_name)
310 GUnixMount *unix_mount = G_UNIX_MOUNT (mount);
314 task = g_task_new (mount, cancellable, callback, user_data);
315 g_task_set_source_tag (task, eject_unmount_do);
316 g_task_set_name (task, task_name);
317 g_task_set_task_data (task, g_strdupv (argv), (GDestroyNotify) g_strfreev);
319 if (unix_mount->volume_monitor != NULL)
320 g_signal_emit_by_name (unix_mount->volume_monitor, "mount-pre-unmount", mount);
322 g_signal_emit_by_name (mount, "pre-unmount", 0);
324 timeout = g_timeout_source_new (500);
325 g_task_attach_source (task, timeout, (GSourceFunc) eject_unmount_do_cb);
326 g_source_unref (timeout);
330 g_unix_mount_unmount (GMount *mount,
331 GMountUnmountFlags flags,
332 GCancellable *cancellable,
333 GAsyncReadyCallback callback,
336 GUnixMount *unix_mount = G_UNIX_MOUNT (mount);
337 char *argv[] = {"umount", NULL, NULL};
339 if (unix_mount->mount_path != NULL)
340 argv[1] = unix_mount->mount_path;
342 argv[1] = unix_mount->device_path;
344 eject_unmount_do (mount, cancellable, callback, user_data, argv, "[gio] unmount mount");
348 g_unix_mount_unmount_finish (GMount *mount,
349 GAsyncResult *result,
352 return g_task_propagate_boolean (G_TASK (result), error);
356 g_unix_mount_eject (GMount *mount,
357 GMountUnmountFlags flags,
358 GCancellable *cancellable,
359 GAsyncReadyCallback callback,
362 GUnixMount *unix_mount = G_UNIX_MOUNT (mount);
363 char *argv[] = {"eject", NULL, NULL};
365 if (unix_mount->mount_path != NULL)
366 argv[1] = unix_mount->mount_path;
368 argv[1] = unix_mount->device_path;
370 eject_unmount_do (mount, cancellable, callback, user_data, argv, "[gio] eject mount");
374 g_unix_mount_eject_finish (GMount *mount,
375 GAsyncResult *result,
378 return g_task_propagate_boolean (G_TASK (result), error);
382 g_unix_mount_mount_iface_init (GMountIface *iface)
384 iface->get_root = g_unix_mount_get_root;
385 iface->get_name = g_unix_mount_get_name;
386 iface->get_icon = g_unix_mount_get_icon;
387 iface->get_symbolic_icon = g_unix_mount_get_symbolic_icon;
388 iface->get_uuid = g_unix_mount_get_uuid;
389 iface->get_drive = g_unix_mount_get_drive;
390 iface->get_volume = g_unix_mount_get_volume;
391 iface->can_unmount = g_unix_mount_can_unmount;
392 iface->can_eject = g_unix_mount_can_eject;
393 iface->unmount = g_unix_mount_unmount;
394 iface->unmount_finish = g_unix_mount_unmount_finish;
395 iface->eject = g_unix_mount_eject;
396 iface->eject_finish = g_unix_mount_eject_finish;