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 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General
18 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
19 *
20 * Author: Alexander Larsson <alexl@redhat.com>
21 * David Zeuthen <davidz@redhat.com>
22 */
23
24 #include "config.h"
25
26 #include <string.h>
27 #include <sys/wait.h>
28 #include <unistd.h>
29
30 #include <glib.h>
31 #include "gsubprocess.h"
32 #include "gioenums.h"
33 #include "gunixvolumemonitor.h"
34 #include "gunixmount.h"
35 #include "gunixmounts.h"
36 #include "gunixvolume.h"
37 #include "gmountprivate.h"
38 #include "gmount.h"
39 #include "gfile.h"
40 #include "gvolumemonitor.h"
41 #include "gthemedicon.h"
42 #include "gioerror.h"
43 #include "glibintl.h"
44 /* for BUFSIZ */
45 #include <stdio.h>
46
47
48 struct _GUnixMount {
49 GObject parent;
50
51 GVolumeMonitor *volume_monitor;
52
53 GUnixVolume *volume; /* owned by volume monitor */
54
55 char *name;
56 GIcon *icon;
57 GIcon *symbolic_icon;
58 char *device_path;
59 char *mount_path;
60
61 gboolean can_eject;
62 };
63
64 static void g_unix_mount_mount_iface_init (GMountIface *iface);
65
66 #define g_unix_mount_get_type _g_unix_mount_get_type
G_DEFINE_TYPE_WITH_CODE(GUnixMount,g_unix_mount,G_TYPE_OBJECT,G_IMPLEMENT_INTERFACE (G_TYPE_MOUNT,g_unix_mount_mount_iface_init))67 G_DEFINE_TYPE_WITH_CODE (GUnixMount, g_unix_mount, G_TYPE_OBJECT,
68 G_IMPLEMENT_INTERFACE (G_TYPE_MOUNT,
69 g_unix_mount_mount_iface_init))
70
71
72 static void
73 g_unix_mount_finalize (GObject *object)
74 {
75 GUnixMount *mount;
76
77 mount = G_UNIX_MOUNT (object);
78
79 if (mount->volume_monitor != NULL)
80 g_object_unref (mount->volume_monitor);
81
82 if (mount->volume)
83 _g_unix_volume_unset_mount (mount->volume, mount);
84
85 /* TODO: g_warn_if_fail (volume->volume == NULL); */
86 g_object_unref (mount->icon);
87 g_object_unref (mount->symbolic_icon);
88 g_free (mount->name);
89 g_free (mount->device_path);
90 g_free (mount->mount_path);
91
92 G_OBJECT_CLASS (g_unix_mount_parent_class)->finalize (object);
93 }
94
95 static void
g_unix_mount_class_init(GUnixMountClass * klass)96 g_unix_mount_class_init (GUnixMountClass *klass)
97 {
98 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
99
100 gobject_class->finalize = g_unix_mount_finalize;
101 }
102
103 static void
g_unix_mount_init(GUnixMount * unix_mount)104 g_unix_mount_init (GUnixMount *unix_mount)
105 {
106 }
107
108 GUnixMount *
_g_unix_mount_new(GVolumeMonitor * volume_monitor,GUnixMountEntry * mount_entry,GUnixVolume * volume)109 _g_unix_mount_new (GVolumeMonitor *volume_monitor,
110 GUnixMountEntry *mount_entry,
111 GUnixVolume *volume)
112 {
113 GUnixMount *mount;
114
115 /* No volume for mount: Ignore internal things */
116 if (volume == NULL && !g_unix_mount_guess_should_display (mount_entry))
117 return NULL;
118
119 mount = g_object_new (G_TYPE_UNIX_MOUNT, NULL);
120 mount->volume_monitor = volume_monitor != NULL ? g_object_ref (volume_monitor) : NULL;
121 mount->device_path = g_strdup (g_unix_mount_get_device_path (mount_entry));
122 mount->mount_path = g_strdup (g_unix_mount_get_mount_path (mount_entry));
123 mount->can_eject = g_unix_mount_guess_can_eject (mount_entry);
124
125 mount->name = g_unix_mount_guess_name (mount_entry);
126 mount->icon = g_unix_mount_guess_icon (mount_entry);
127 mount->symbolic_icon = g_unix_mount_guess_symbolic_icon (mount_entry);
128
129 /* need to do this last */
130 mount->volume = volume;
131 if (volume != NULL)
132 _g_unix_volume_set_mount (volume, mount);
133
134 return mount;
135 }
136
137 void
_g_unix_mount_unmounted(GUnixMount * mount)138 _g_unix_mount_unmounted (GUnixMount *mount)
139 {
140 if (mount->volume != NULL)
141 {
142 _g_unix_volume_unset_mount (mount->volume, mount);
143 mount->volume = NULL;
144 g_signal_emit_by_name (mount, "changed");
145 /* there's really no need to emit mount_changed on the volume monitor
146 * as we're going to be deleted.. */
147 }
148 }
149
150 void
_g_unix_mount_unset_volume(GUnixMount * mount,GUnixVolume * volume)151 _g_unix_mount_unset_volume (GUnixMount *mount,
152 GUnixVolume *volume)
153 {
154 if (mount->volume == volume)
155 {
156 mount->volume = NULL;
157 /* TODO: Emit changed in idle to avoid locking issues */
158 g_signal_emit_by_name (mount, "changed");
159 if (mount->volume_monitor != NULL)
160 g_signal_emit_by_name (mount->volume_monitor, "mount-changed", mount);
161 }
162 }
163
164 static GFile *
g_unix_mount_get_root(GMount * mount)165 g_unix_mount_get_root (GMount *mount)
166 {
167 GUnixMount *unix_mount = G_UNIX_MOUNT (mount);
168
169 return g_file_new_for_path (unix_mount->mount_path);
170 }
171
172 static GIcon *
g_unix_mount_get_icon(GMount * mount)173 g_unix_mount_get_icon (GMount *mount)
174 {
175 GUnixMount *unix_mount = G_UNIX_MOUNT (mount);
176
177 return g_object_ref (unix_mount->icon);
178 }
179
180 static GIcon *
g_unix_mount_get_symbolic_icon(GMount * mount)181 g_unix_mount_get_symbolic_icon (GMount *mount)
182 {
183 GUnixMount *unix_mount = G_UNIX_MOUNT (mount);
184
185 return g_object_ref (unix_mount->symbolic_icon);
186 }
187
188 static char *
g_unix_mount_get_uuid(GMount * mount)189 g_unix_mount_get_uuid (GMount *mount)
190 {
191 return NULL;
192 }
193
194 static char *
g_unix_mount_get_name(GMount * mount)195 g_unix_mount_get_name (GMount *mount)
196 {
197 GUnixMount *unix_mount = G_UNIX_MOUNT (mount);
198
199 return g_strdup (unix_mount->name);
200 }
201
202 gboolean
_g_unix_mount_has_mount_path(GUnixMount * mount,const char * mount_path)203 _g_unix_mount_has_mount_path (GUnixMount *mount,
204 const char *mount_path)
205 {
206 return strcmp (mount->mount_path, mount_path) == 0;
207 }
208
209 static GDrive *
g_unix_mount_get_drive(GMount * mount)210 g_unix_mount_get_drive (GMount *mount)
211 {
212 GUnixMount *unix_mount = G_UNIX_MOUNT (mount);
213
214 if (unix_mount->volume != NULL)
215 return g_volume_get_drive (G_VOLUME (unix_mount->volume));
216
217 return NULL;
218 }
219
220 static GVolume *
g_unix_mount_get_volume(GMount * mount)221 g_unix_mount_get_volume (GMount *mount)
222 {
223 GUnixMount *unix_mount = G_UNIX_MOUNT (mount);
224
225 if (unix_mount->volume)
226 return G_VOLUME (g_object_ref (unix_mount->volume));
227
228 return NULL;
229 }
230
231 static gboolean
g_unix_mount_can_unmount(GMount * mount)232 g_unix_mount_can_unmount (GMount *mount)
233 {
234 return TRUE;
235 }
236
237 static gboolean
g_unix_mount_can_eject(GMount * mount)238 g_unix_mount_can_eject (GMount *mount)
239 {
240 GUnixMount *unix_mount = G_UNIX_MOUNT (mount);
241 return unix_mount->can_eject;
242 }
243
244 static void
eject_unmount_done(GObject * source,GAsyncResult * result,gpointer user_data)245 eject_unmount_done (GObject *source,
246 GAsyncResult *result,
247 gpointer user_data)
248 {
249 GSubprocess *subprocess = G_SUBPROCESS (source);
250 GTask *task = user_data;
251 GError *error = NULL;
252 gchar *stderr_str;
253
254 if (!g_subprocess_communicate_utf8_finish (subprocess, result, NULL, &stderr_str, &error))
255 {
256 g_task_return_error (task, error);
257 g_error_free (error);
258 }
259 else /* successful communication */
260 {
261 if (!g_subprocess_get_successful (subprocess))
262 /* ...but bad exit code */
263 g_task_return_new_error (task, G_IO_ERROR, G_IO_ERROR_FAILED, "%s", stderr_str);
264 else
265 /* ...and successful exit code */
266 g_task_return_boolean (task, TRUE);
267
268 g_free (stderr_str);
269 }
270
271 g_object_unref (task);
272 }
273
274 static gboolean
eject_unmount_do_cb(gpointer user_data)275 eject_unmount_do_cb (gpointer user_data)
276 {
277 GTask *task = user_data;
278 GError *error = NULL;
279 GSubprocess *subprocess;
280 const gchar **argv;
281
282 argv = g_task_get_task_data (task);
283
284 if (g_task_return_error_if_cancelled (task))
285 {
286 g_object_unref (task);
287 return G_SOURCE_REMOVE;
288 }
289
290 subprocess = g_subprocess_newv (argv, G_SUBPROCESS_FLAGS_STDOUT_SILENCE | G_SUBPROCESS_FLAGS_STDERR_PIPE, &error);
291 g_assert_no_error (error);
292
293 g_subprocess_communicate_utf8_async (subprocess, NULL,
294 g_task_get_cancellable (task),
295 eject_unmount_done, task);
296
297 return G_SOURCE_REMOVE;
298 }
299
300 static void
eject_unmount_do(GMount * mount,GCancellable * cancellable,GAsyncReadyCallback callback,gpointer user_data,char ** argv,const gchar * task_name)301 eject_unmount_do (GMount *mount,
302 GCancellable *cancellable,
303 GAsyncReadyCallback callback,
304 gpointer user_data,
305 char **argv,
306 const gchar *task_name)
307 {
308 GUnixMount *unix_mount = G_UNIX_MOUNT (mount);
309 GTask *task;
310 GSource *timeout;
311
312 task = g_task_new (mount, cancellable, callback, user_data);
313 g_task_set_source_tag (task, eject_unmount_do);
314 g_task_set_name (task, task_name);
315 g_task_set_task_data (task, g_strdupv (argv), (GDestroyNotify) g_strfreev);
316
317 if (unix_mount->volume_monitor != NULL)
318 g_signal_emit_by_name (unix_mount->volume_monitor, "mount-pre-unmount", mount);
319
320 g_signal_emit_by_name (mount, "pre-unmount", 0);
321
322 timeout = g_timeout_source_new (500);
323 g_task_attach_source (task, timeout, (GSourceFunc) eject_unmount_do_cb);
324 g_source_unref (timeout);
325 }
326
327 static void
g_unix_mount_unmount(GMount * mount,GMountUnmountFlags flags,GCancellable * cancellable,GAsyncReadyCallback callback,gpointer user_data)328 g_unix_mount_unmount (GMount *mount,
329 GMountUnmountFlags flags,
330 GCancellable *cancellable,
331 GAsyncReadyCallback callback,
332 gpointer user_data)
333 {
334 GUnixMount *unix_mount = G_UNIX_MOUNT (mount);
335 char *argv[] = {"umount", NULL, NULL};
336
337 if (unix_mount->mount_path != NULL)
338 argv[1] = unix_mount->mount_path;
339 else
340 argv[1] = unix_mount->device_path;
341
342 eject_unmount_do (mount, cancellable, callback, user_data, argv, "[gio] unmount mount");
343 }
344
345 static gboolean
g_unix_mount_unmount_finish(GMount * mount,GAsyncResult * result,GError ** error)346 g_unix_mount_unmount_finish (GMount *mount,
347 GAsyncResult *result,
348 GError **error)
349 {
350 return g_task_propagate_boolean (G_TASK (result), error);
351 }
352
353 static void
g_unix_mount_eject(GMount * mount,GMountUnmountFlags flags,GCancellable * cancellable,GAsyncReadyCallback callback,gpointer user_data)354 g_unix_mount_eject (GMount *mount,
355 GMountUnmountFlags flags,
356 GCancellable *cancellable,
357 GAsyncReadyCallback callback,
358 gpointer user_data)
359 {
360 GUnixMount *unix_mount = G_UNIX_MOUNT (mount);
361 char *argv[] = {"eject", NULL, NULL};
362
363 if (unix_mount->mount_path != NULL)
364 argv[1] = unix_mount->mount_path;
365 else
366 argv[1] = unix_mount->device_path;
367
368 eject_unmount_do (mount, cancellable, callback, user_data, argv, "[gio] eject mount");
369 }
370
371 static gboolean
g_unix_mount_eject_finish(GMount * mount,GAsyncResult * result,GError ** error)372 g_unix_mount_eject_finish (GMount *mount,
373 GAsyncResult *result,
374 GError **error)
375 {
376 return g_task_propagate_boolean (G_TASK (result), error);
377 }
378
379 static void
g_unix_mount_mount_iface_init(GMountIface * iface)380 g_unix_mount_mount_iface_init (GMountIface *iface)
381 {
382 iface->get_root = g_unix_mount_get_root;
383 iface->get_name = g_unix_mount_get_name;
384 iface->get_icon = g_unix_mount_get_icon;
385 iface->get_symbolic_icon = g_unix_mount_get_symbolic_icon;
386 iface->get_uuid = g_unix_mount_get_uuid;
387 iface->get_drive = g_unix_mount_get_drive;
388 iface->get_volume = g_unix_mount_get_volume;
389 iface->can_unmount = g_unix_mount_can_unmount;
390 iface->can_eject = g_unix_mount_can_eject;
391 iface->unmount = g_unix_mount_unmount;
392 iface->unmount_finish = g_unix_mount_unmount_finish;
393 iface->eject = g_unix_mount_eject;
394 iface->eject_finish = g_unix_mount_eject_finish;
395 }
396