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