/src/glib/gio/gunixmount.c
Line | Count | Source (jump to first uncovered line) |
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 |
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 | 0 | { |
75 | 0 | GUnixMount *mount; |
76 | | |
77 | 0 | mount = G_UNIX_MOUNT (object); |
78 | |
|
79 | 0 | if (mount->volume_monitor != NULL) |
80 | 0 | g_object_unref (mount->volume_monitor); |
81 | |
|
82 | 0 | if (mount->volume) |
83 | 0 | _g_unix_volume_unset_mount (mount->volume, mount); |
84 | | |
85 | | /* TODO: g_warn_if_fail (volume->volume == NULL); */ |
86 | 0 | g_object_unref (mount->icon); |
87 | 0 | g_object_unref (mount->symbolic_icon); |
88 | 0 | g_free (mount->name); |
89 | 0 | g_free (mount->device_path); |
90 | 0 | g_free (mount->mount_path); |
91 | |
|
92 | 0 | G_OBJECT_CLASS (g_unix_mount_parent_class)->finalize (object); |
93 | 0 | } |
94 | | |
95 | | static void |
96 | | g_unix_mount_class_init (GUnixMountClass *klass) |
97 | 0 | { |
98 | 0 | GObjectClass *gobject_class = G_OBJECT_CLASS (klass); |
99 | |
|
100 | 0 | gobject_class->finalize = g_unix_mount_finalize; |
101 | 0 | } |
102 | | |
103 | | static void |
104 | | g_unix_mount_init (GUnixMount *unix_mount) |
105 | 0 | { |
106 | 0 | } |
107 | | |
108 | | GUnixMount * |
109 | | _g_unix_mount_new (GVolumeMonitor *volume_monitor, |
110 | | GUnixMountEntry *mount_entry, |
111 | | GUnixVolume *volume) |
112 | 0 | { |
113 | 0 | GUnixMount *mount; |
114 | | |
115 | | /* No volume for mount: Ignore internal things */ |
116 | 0 | if (volume == NULL && !g_unix_mount_guess_should_display (mount_entry)) |
117 | 0 | return NULL; |
118 | | |
119 | 0 | mount = g_object_new (G_TYPE_UNIX_MOUNT, NULL); |
120 | 0 | mount->volume_monitor = volume_monitor != NULL ? g_object_ref (volume_monitor) : NULL; |
121 | 0 | mount->device_path = g_strdup (g_unix_mount_get_device_path (mount_entry)); |
122 | 0 | mount->mount_path = g_strdup (g_unix_mount_get_mount_path (mount_entry)); |
123 | 0 | mount->can_eject = g_unix_mount_guess_can_eject (mount_entry); |
124 | |
|
125 | 0 | mount->name = g_unix_mount_guess_name (mount_entry); |
126 | 0 | mount->icon = g_unix_mount_guess_icon (mount_entry); |
127 | 0 | mount->symbolic_icon = g_unix_mount_guess_symbolic_icon (mount_entry); |
128 | | |
129 | | /* need to do this last */ |
130 | 0 | mount->volume = volume; |
131 | 0 | if (volume != NULL) |
132 | 0 | _g_unix_volume_set_mount (volume, mount); |
133 | |
|
134 | 0 | return mount; |
135 | 0 | } |
136 | | |
137 | | void |
138 | | _g_unix_mount_unmounted (GUnixMount *mount) |
139 | 0 | { |
140 | 0 | if (mount->volume != NULL) |
141 | 0 | { |
142 | 0 | _g_unix_volume_unset_mount (mount->volume, mount); |
143 | 0 | mount->volume = NULL; |
144 | 0 | 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 | 0 | } |
148 | 0 | } |
149 | | |
150 | | void |
151 | | _g_unix_mount_unset_volume (GUnixMount *mount, |
152 | | GUnixVolume *volume) |
153 | 0 | { |
154 | 0 | if (mount->volume == volume) |
155 | 0 | { |
156 | 0 | mount->volume = NULL; |
157 | | /* TODO: Emit changed in idle to avoid locking issues */ |
158 | 0 | g_signal_emit_by_name (mount, "changed"); |
159 | 0 | if (mount->volume_monitor != NULL) |
160 | 0 | g_signal_emit_by_name (mount->volume_monitor, "mount-changed", mount); |
161 | 0 | } |
162 | 0 | } |
163 | | |
164 | | static GFile * |
165 | | g_unix_mount_get_root (GMount *mount) |
166 | 0 | { |
167 | 0 | GUnixMount *unix_mount = G_UNIX_MOUNT (mount); |
168 | |
|
169 | 0 | return g_file_new_for_path (unix_mount->mount_path); |
170 | 0 | } |
171 | | |
172 | | static GIcon * |
173 | | g_unix_mount_get_icon (GMount *mount) |
174 | 0 | { |
175 | 0 | GUnixMount *unix_mount = G_UNIX_MOUNT (mount); |
176 | |
|
177 | 0 | return g_object_ref (unix_mount->icon); |
178 | 0 | } |
179 | | |
180 | | static GIcon * |
181 | | g_unix_mount_get_symbolic_icon (GMount *mount) |
182 | 0 | { |
183 | 0 | GUnixMount *unix_mount = G_UNIX_MOUNT (mount); |
184 | |
|
185 | 0 | return g_object_ref (unix_mount->symbolic_icon); |
186 | 0 | } |
187 | | |
188 | | static char * |
189 | | g_unix_mount_get_uuid (GMount *mount) |
190 | 0 | { |
191 | 0 | return NULL; |
192 | 0 | } |
193 | | |
194 | | static char * |
195 | | g_unix_mount_get_name (GMount *mount) |
196 | 0 | { |
197 | 0 | GUnixMount *unix_mount = G_UNIX_MOUNT (mount); |
198 | | |
199 | 0 | return g_strdup (unix_mount->name); |
200 | 0 | } |
201 | | |
202 | | gboolean |
203 | | _g_unix_mount_has_mount_path (GUnixMount *mount, |
204 | | const char *mount_path) |
205 | 0 | { |
206 | 0 | return strcmp (mount->mount_path, mount_path) == 0; |
207 | 0 | } |
208 | | |
209 | | static GDrive * |
210 | | g_unix_mount_get_drive (GMount *mount) |
211 | 0 | { |
212 | 0 | GUnixMount *unix_mount = G_UNIX_MOUNT (mount); |
213 | |
|
214 | 0 | if (unix_mount->volume != NULL) |
215 | 0 | return g_volume_get_drive (G_VOLUME (unix_mount->volume)); |
216 | | |
217 | 0 | return NULL; |
218 | 0 | } |
219 | | |
220 | | static GVolume * |
221 | | g_unix_mount_get_volume (GMount *mount) |
222 | 0 | { |
223 | 0 | GUnixMount *unix_mount = G_UNIX_MOUNT (mount); |
224 | |
|
225 | 0 | if (unix_mount->volume) |
226 | 0 | return G_VOLUME (g_object_ref (unix_mount->volume)); |
227 | | |
228 | 0 | return NULL; |
229 | 0 | } |
230 | | |
231 | | static gboolean |
232 | | g_unix_mount_can_unmount (GMount *mount) |
233 | 0 | { |
234 | 0 | return TRUE; |
235 | 0 | } |
236 | | |
237 | | static gboolean |
238 | | g_unix_mount_can_eject (GMount *mount) |
239 | 0 | { |
240 | 0 | GUnixMount *unix_mount = G_UNIX_MOUNT (mount); |
241 | 0 | return unix_mount->can_eject; |
242 | 0 | } |
243 | | |
244 | | static void |
245 | | eject_unmount_done (GObject *source, |
246 | | GAsyncResult *result, |
247 | | gpointer user_data) |
248 | 0 | { |
249 | 0 | GSubprocess *subprocess = G_SUBPROCESS (source); |
250 | 0 | GTask *task = user_data; |
251 | 0 | GError *error = NULL; |
252 | 0 | gchar *stderr_str; |
253 | |
|
254 | 0 | if (!g_subprocess_communicate_utf8_finish (subprocess, result, NULL, &stderr_str, &error)) |
255 | 0 | { |
256 | 0 | g_task_return_error (task, error); |
257 | 0 | g_error_free (error); |
258 | 0 | } |
259 | 0 | else /* successful communication */ |
260 | 0 | { |
261 | 0 | if (!g_subprocess_get_successful (subprocess)) |
262 | | /* ...but bad exit code */ |
263 | 0 | g_task_return_new_error (task, G_IO_ERROR, G_IO_ERROR_FAILED, "%s", stderr_str); |
264 | 0 | else |
265 | | /* ...and successful exit code */ |
266 | 0 | g_task_return_boolean (task, TRUE); |
267 | |
|
268 | 0 | g_free (stderr_str); |
269 | 0 | } |
270 | |
|
271 | 0 | g_object_unref (task); |
272 | 0 | } |
273 | | |
274 | | static gboolean |
275 | | eject_unmount_do_cb (gpointer user_data) |
276 | 0 | { |
277 | 0 | GTask *task = user_data; |
278 | 0 | GError *error = NULL; |
279 | 0 | GSubprocess *subprocess; |
280 | 0 | const gchar **argv; |
281 | |
|
282 | 0 | argv = g_task_get_task_data (task); |
283 | |
|
284 | 0 | if (g_task_return_error_if_cancelled (task)) |
285 | 0 | { |
286 | 0 | g_object_unref (task); |
287 | 0 | return G_SOURCE_REMOVE; |
288 | 0 | } |
289 | | |
290 | 0 | subprocess = g_subprocess_newv (argv, G_SUBPROCESS_FLAGS_STDOUT_SILENCE | G_SUBPROCESS_FLAGS_STDERR_PIPE, &error); |
291 | 0 | g_assert_no_error (error); |
292 | |
|
293 | 0 | g_subprocess_communicate_utf8_async (subprocess, NULL, |
294 | 0 | g_task_get_cancellable (task), |
295 | 0 | eject_unmount_done, task); |
296 | |
|
297 | 0 | return G_SOURCE_REMOVE; |
298 | 0 | } |
299 | | |
300 | | static void |
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 | 0 | { |
308 | 0 | GUnixMount *unix_mount = G_UNIX_MOUNT (mount); |
309 | 0 | GTask *task; |
310 | 0 | GSource *timeout; |
311 | |
|
312 | 0 | task = g_task_new (mount, cancellable, callback, user_data); |
313 | 0 | g_task_set_source_tag (task, eject_unmount_do); |
314 | 0 | g_task_set_name (task, task_name); |
315 | 0 | g_task_set_task_data (task, g_strdupv (argv), (GDestroyNotify) g_strfreev); |
316 | |
|
317 | 0 | if (unix_mount->volume_monitor != NULL) |
318 | 0 | g_signal_emit_by_name (unix_mount->volume_monitor, "mount-pre-unmount", mount); |
319 | |
|
320 | 0 | g_signal_emit_by_name (mount, "pre-unmount", 0); |
321 | |
|
322 | 0 | timeout = g_timeout_source_new (500); |
323 | 0 | g_task_attach_source (task, timeout, (GSourceFunc) eject_unmount_do_cb); |
324 | 0 | g_source_unref (timeout); |
325 | 0 | } |
326 | | |
327 | | static void |
328 | | g_unix_mount_unmount (GMount *mount, |
329 | | GMountUnmountFlags flags, |
330 | | GCancellable *cancellable, |
331 | | GAsyncReadyCallback callback, |
332 | | gpointer user_data) |
333 | 0 | { |
334 | 0 | GUnixMount *unix_mount = G_UNIX_MOUNT (mount); |
335 | 0 | char *argv[] = {"umount", NULL, NULL}; |
336 | |
|
337 | 0 | if (unix_mount->mount_path != NULL) |
338 | 0 | argv[1] = unix_mount->mount_path; |
339 | 0 | else |
340 | 0 | argv[1] = unix_mount->device_path; |
341 | |
|
342 | 0 | eject_unmount_do (mount, cancellable, callback, user_data, argv, "[gio] unmount mount"); |
343 | 0 | } |
344 | | |
345 | | static gboolean |
346 | | g_unix_mount_unmount_finish (GMount *mount, |
347 | | GAsyncResult *result, |
348 | | GError **error) |
349 | 0 | { |
350 | 0 | return g_task_propagate_boolean (G_TASK (result), error); |
351 | 0 | } |
352 | | |
353 | | static void |
354 | | g_unix_mount_eject (GMount *mount, |
355 | | GMountUnmountFlags flags, |
356 | | GCancellable *cancellable, |
357 | | GAsyncReadyCallback callback, |
358 | | gpointer user_data) |
359 | 0 | { |
360 | 0 | GUnixMount *unix_mount = G_UNIX_MOUNT (mount); |
361 | 0 | char *argv[] = {"eject", NULL, NULL}; |
362 | |
|
363 | 0 | if (unix_mount->mount_path != NULL) |
364 | 0 | argv[1] = unix_mount->mount_path; |
365 | 0 | else |
366 | 0 | argv[1] = unix_mount->device_path; |
367 | |
|
368 | 0 | eject_unmount_do (mount, cancellable, callback, user_data, argv, "[gio] eject mount"); |
369 | 0 | } |
370 | | |
371 | | static gboolean |
372 | | g_unix_mount_eject_finish (GMount *mount, |
373 | | GAsyncResult *result, |
374 | | GError **error) |
375 | 0 | { |
376 | 0 | return g_task_propagate_boolean (G_TASK (result), error); |
377 | 0 | } |
378 | | |
379 | | static void |
380 | | g_unix_mount_mount_iface_init (GMountIface *iface) |
381 | 0 | { |
382 | 0 | iface->get_root = g_unix_mount_get_root; |
383 | 0 | iface->get_name = g_unix_mount_get_name; |
384 | 0 | iface->get_icon = g_unix_mount_get_icon; |
385 | 0 | iface->get_symbolic_icon = g_unix_mount_get_symbolic_icon; |
386 | 0 | iface->get_uuid = g_unix_mount_get_uuid; |
387 | 0 | iface->get_drive = g_unix_mount_get_drive; |
388 | 0 | iface->get_volume = g_unix_mount_get_volume; |
389 | 0 | iface->can_unmount = g_unix_mount_can_unmount; |
390 | 0 | iface->can_eject = g_unix_mount_can_eject; |
391 | 0 | iface->unmount = g_unix_mount_unmount; |
392 | 0 | iface->unmount_finish = g_unix_mount_unmount_finish; |
393 | 0 | iface->eject = g_unix_mount_eject; |
394 | 0 | iface->eject_finish = g_unix_mount_eject_finish; |
395 | 0 | } |