1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
2
3 /* GIO - GLib Input, Output and Streaming Library
4 *
5 * Copyright (C) 2006-2007 Red Hat, Inc.
6 *
7 * SPDX-License-Identifier: LGPL-2.1-or-later
8 *
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.
13 *
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.
18 *
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/>.
21 *
22 * Author: Alexander Larsson <alexl@redhat.com>
23 * David Zeuthen <davidz@redhat.com>
24 */
25
26 #include "config.h"
27
28 #include <string.h>
29 #include <sys/wait.h>
30 #include <unistd.h>
31
32 #include <glib.h>
33 #include "gsubprocess.h"
34 #include "gioenums.h"
35 #include "gunixvolumemonitor.h"
36 #include "gunixmount.h"
37 #include "gunixmounts.h"
38 #include "gunixvolume.h"
39 #include "gmountprivate.h"
40 #include "gmount.h"
41 #include "gfile.h"
42 #include "gvolumemonitor.h"
43 #include "gthemedicon.h"
44 #include "gioerror.h"
45 #include "glibintl.h"
46 /* for BUFSIZ */
47 #include <stdio.h>
48
49
50 struct _GUnixMount {
51 GObject parent;
52
53 GVolumeMonitor *volume_monitor;
54
55 GUnixVolume *volume; /* owned by volume monitor */
56
57 char *name;
58 GIcon *icon;
59 GIcon *symbolic_icon;
60 char *device_path;
61 char *mount_path;
62
63 gboolean can_eject;
64 };
65
66 static void g_unix_mount_mount_iface_init (GMountIface *iface);
67
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))
72
73
74 static void
75 g_unix_mount_finalize (GObject *object)
76 {
77 GUnixMount *mount;
78
79 mount = G_UNIX_MOUNT (object);
80
81 if (mount->volume_monitor != NULL)
82 g_object_unref (mount->volume_monitor);
83
84 if (mount->volume)
85 _g_unix_volume_unset_mount (mount->volume, mount);
86
87 /* TODO: g_warn_if_fail (volume->volume == NULL); */
88 g_object_unref (mount->icon);
89 g_object_unref (mount->symbolic_icon);
90 g_free (mount->name);
91 g_free (mount->device_path);
92 g_free (mount->mount_path);
93
94 G_OBJECT_CLASS (g_unix_mount_parent_class)->finalize (object);
95 }
96
97 static void
98 g_unix_mount_class_init (GUnixMountClass *klass)
99 {
100 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
101
102 gobject_class->finalize = g_unix_mount_finalize;
103 }
104
105 static void
106 g_unix_mount_init (GUnixMount *unix_mount)
107 {
108 }
109
110 GUnixMount *
111 _g_unix_mount_new (GVolumeMonitor *volume_monitor,
112 GUnixMountEntry *mount_entry,
113 GUnixVolume *volume)
114 {
115 GUnixMount *mount;
116
117 /* No volume for mount: Ignore internal things */
118 if (volume == NULL && !g_unix_mount_guess_should_display (mount_entry))
119 return NULL;
120
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);
126
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);
130
131 /* need to do this last */
132 mount->volume = volume;
133 if (volume != NULL)
134 _g_unix_volume_set_mount (volume, mount);
135
136 return mount;
137 }
138
139 void
140 _g_unix_mount_unmounted (GUnixMount *mount)
141 {
142 if (mount->volume != NULL)
143 {
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.. */
149 }
150 }
151
152 void
153 _g_unix_mount_unset_volume (GUnixMount *mount,
154 GUnixVolume *volume)
155 {
156 if (mount->volume == volume)
157 {
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);
163 }
164 }
165
166 static GFile *
167 g_unix_mount_get_root (GMount *mount)
168 {
169 GUnixMount *unix_mount = G_UNIX_MOUNT (mount);
170
171 return g_file_new_for_path (unix_mount->mount_path);
172 }
173
174 static GIcon *
175 g_unix_mount_get_icon (GMount *mount)
176 {
177 GUnixMount *unix_mount = G_UNIX_MOUNT (mount);
178
179 return g_object_ref (unix_mount->icon);
180 }
181
182 static GIcon *
183 g_unix_mount_get_symbolic_icon (GMount *mount)
184 {
185 GUnixMount *unix_mount = G_UNIX_MOUNT (mount);
186
187 return g_object_ref (unix_mount->symbolic_icon);
188 }
189
190 static char *
191 g_unix_mount_get_uuid (GMount *mount)
192 {
193 return NULL;
194 }
195
196 static char *
197 g_unix_mount_get_name (GMount *mount)
198 {
199 GUnixMount *unix_mount = G_UNIX_MOUNT (mount);
200
201 return g_strdup (unix_mount->name);
202 }
203
204 gboolean
205 _g_unix_mount_has_mount_path (GUnixMount *mount,
206 const char *mount_path)
207 {
208 return strcmp (mount->mount_path, mount_path) == 0;
209 }
210
211 static GDrive *
212 g_unix_mount_get_drive (GMount *mount)
213 {
214 GUnixMount *unix_mount = G_UNIX_MOUNT (mount);
215
216 if (unix_mount->volume != NULL)
217 return g_volume_get_drive (G_VOLUME (unix_mount->volume));
218
219 return NULL;
220 }
221
222 static GVolume *
223 g_unix_mount_get_volume (GMount *mount)
224 {
225 GUnixMount *unix_mount = G_UNIX_MOUNT (mount);
226
227 if (unix_mount->volume)
228 return G_VOLUME (g_object_ref (unix_mount->volume));
229
230 return NULL;
231 }
232
233 static gboolean
234 g_unix_mount_can_unmount (GMount *mount)
235 {
236 return TRUE;
237 }
238
239 static gboolean
240 g_unix_mount_can_eject (GMount *mount)
241 {
242 GUnixMount *unix_mount = G_UNIX_MOUNT (mount);
243 return unix_mount->can_eject;
244 }
245
246 static void
247 eject_unmount_done (GObject *source,
248 GAsyncResult *result,
249 gpointer user_data)
250 {
251 GSubprocess *subprocess = G_SUBPROCESS (source);
252 GTask *task = user_data;
253 GError *error = NULL;
254 gchar *stderr_str;
255
256 if (!g_subprocess_communicate_utf8_finish (subprocess, result, NULL, &stderr_str, &error))
257 {
258 g_task_return_error (task, error);
259 g_error_free (error);
260 }
261 else /* successful communication */
262 {
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);
266 else
267 /* ...and successful exit code */
268 g_task_return_boolean (task, TRUE);
269
270 g_free (stderr_str);
271 }
272
273 g_object_unref (task);
274 }
275
276 static gboolean
277 eject_unmount_do_cb (gpointer user_data)
278 {
279 GTask *task = user_data;
280 GError *error = NULL;
281 GSubprocess *subprocess;
282 const gchar **argv;
283
284 argv = g_task_get_task_data (task);
285
286 if (g_task_return_error_if_cancelled (task))
287 {
288 g_object_unref (task);
289 return G_SOURCE_REMOVE;
290 }
291
292 subprocess = g_subprocess_newv (argv, G_SUBPROCESS_FLAGS_STDOUT_SILENCE | G_SUBPROCESS_FLAGS_STDERR_PIPE, &error);
293 g_assert_no_error (error);
294
295 g_subprocess_communicate_utf8_async (subprocess, NULL,
296 g_task_get_cancellable (task),
297 eject_unmount_done, task);
298
299 return G_SOURCE_REMOVE;
300 }
301
302 static void
303 eject_unmount_do (GMount *mount,
304 GCancellable *cancellable,
305 GAsyncReadyCallback callback,
306 gpointer user_data,
307 char **argv,
308 const gchar *task_name)
309 {
310 GUnixMount *unix_mount = G_UNIX_MOUNT (mount);
311 GTask *task;
312 GSource *timeout;
313
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);
318
319 if (unix_mount->volume_monitor != NULL)
320 g_signal_emit_by_name (unix_mount->volume_monitor, "mount-pre-unmount", mount);
321
322 g_signal_emit_by_name (mount, "pre-unmount", 0);
323
324 timeout = g_timeout_source_new (500);
325 g_task_attach_source (task, timeout, (GSourceFunc) eject_unmount_do_cb);
326 g_source_unref (timeout);
327 }
328
329 static void
330 g_unix_mount_unmount (GMount *mount,
331 GMountUnmountFlags flags,
332 GCancellable *cancellable,
333 GAsyncReadyCallback callback,
334 gpointer user_data)
335 {
336 GUnixMount *unix_mount = G_UNIX_MOUNT (mount);
337 char *argv[] = {"umount", NULL, NULL};
338
339 if (unix_mount->mount_path != NULL)
340 argv[1] = unix_mount->mount_path;
341 else
342 argv[1] = unix_mount->device_path;
343
344 eject_unmount_do (mount, cancellable, callback, user_data, argv, "[gio] unmount mount");
345 }
346
347 static gboolean
348 g_unix_mount_unmount_finish (GMount *mount,
349 GAsyncResult *result,
350 GError **error)
351 {
352 return g_task_propagate_boolean (G_TASK (result), error);
353 }
354
355 static void
356 g_unix_mount_eject (GMount *mount,
357 GMountUnmountFlags flags,
358 GCancellable *cancellable,
359 GAsyncReadyCallback callback,
360 gpointer user_data)
361 {
362 GUnixMount *unix_mount = G_UNIX_MOUNT (mount);
363 char *argv[] = {"eject", NULL, NULL};
364
365 if (unix_mount->mount_path != NULL)
366 argv[1] = unix_mount->mount_path;
367 else
368 argv[1] = unix_mount->device_path;
369
370 eject_unmount_do (mount, cancellable, callback, user_data, argv, "[gio] eject mount");
371 }
372
373 static gboolean
374 g_unix_mount_eject_finish (GMount *mount,
375 GAsyncResult *result,
376 GError **error)
377 {
378 return g_task_propagate_boolean (G_TASK (result), error);
379 }
380
381 static void
382 g_unix_mount_mount_iface_init (GMountIface *iface)
383 {
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;
397 }