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