Rework how volumes, drives and volume monitoring is done. Previosly the
[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, write to the
19  * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
20  * Boston, MA 02111-1307, USA.
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 "gunixvolumemonitor.h"
34 #include "gunixmount.h"
35 #include "gunixvolume.h"
36 #include "gmountprivate.h"
37 #include "gvolumemonitor.h"
38 #include "gthemedicon.h"
39 #include "gsimpleasyncresult.h"
40 #include "glibintl.h"
41
42 #include "gioalias.h"
43
44 struct _GUnixMount {
45   GObject parent;
46
47   GVolumeMonitor   *volume_monitor;
48
49   GUnixVolume      *volume; /* owned by volume monitor */
50
51   char *name;
52   GIcon *icon;
53   char *device_path;
54   char *mount_path;
55 };
56
57 static void g_unix_mount_mount_iface_init (GMountIface *iface);
58
59 #define g_unix_mount_get_type _g_unix_mount_get_type
60 G_DEFINE_TYPE_WITH_CODE (GUnixMount, g_unix_mount, G_TYPE_OBJECT,
61                          G_IMPLEMENT_INTERFACE (G_TYPE_MOUNT,
62                                                 g_unix_mount_mount_iface_init))
63
64
65 static void
66 g_unix_mount_finalize (GObject *object)
67 {
68   GUnixMount *mount;
69   
70   mount = G_UNIX_MOUNT (object);
71
72   if (mount->volume_monitor != NULL)
73     g_object_unref (mount->volume_monitor);
74
75   if (mount->volume)
76     _g_unix_volume_unset_mount (mount->volume, mount);
77     
78   //TODO: g_warn_if_fail (volume->volume == NULL);
79   g_object_unref (mount->icon);
80   g_free (mount->name);
81   g_free (mount->device_path);
82   g_free (mount->mount_path);
83   
84   if (G_OBJECT_CLASS (g_unix_mount_parent_class)->finalize)
85     (*G_OBJECT_CLASS (g_unix_mount_parent_class)->finalize) (object);
86 }
87
88 static void
89 g_unix_mount_class_init (GUnixMountClass *klass)
90 {
91   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
92
93   gobject_class->finalize = g_unix_mount_finalize;
94 }
95
96 static void
97 g_unix_mount_init (GUnixMount *unix_mount)
98 {
99 }
100
101 GUnixMount *
102 _g_unix_mount_new (GVolumeMonitor        *volume_monitor,
103                    GUnixMountEntry       *mount_entry,
104                    GUnixVolume  *volume)
105 {
106   GUnixMount *mount;
107   
108   /* No volume for mount: Ignore internal things */
109   if (volume == NULL && g_unix_mount_is_system_internal (mount_entry))
110     return NULL;
111
112   mount = g_object_new (G_TYPE_UNIX_MOUNT, NULL);
113   mount->volume_monitor = volume_monitor != NULL ? g_object_ref (volume_monitor) : NULL;
114   mount->device_path = g_strdup (g_unix_mount_get_device_path (mount_entry));
115   mount->mount_path = g_strdup (g_unix_mount_get_mount_path (mount_entry));
116   mount->name = g_unix_mount_guess_name (mount_entry);
117   mount->icon = g_unix_mount_guess_icon (mount_entry);
118
119   /* need to do this last */
120   mount->volume = volume;
121   if (volume != NULL)
122     _g_unix_volume_set_mount (volume, mount);
123
124   return mount;
125 }
126
127 void
128 _g_unix_mount_unmounted (GUnixMount *mount)
129 {
130   if (mount->volume != NULL)
131     {
132       _g_unix_volume_unset_mount (mount->volume, mount);
133       mount->volume = NULL;
134       g_signal_emit_by_name (mount, "changed");
135       /* there's really no need to emit mount_changed on the volume monitor 
136        * as we're going to be deleted.. */
137     }
138 }
139
140 void
141 _g_unix_mount_unset_volume (GUnixMount *mount,
142                             GUnixVolume  *volume)
143 {
144   if (mount->volume == volume)
145     {
146       mount->volume = NULL;
147       /* TODO: Emit changed in idle to avoid locking issues */
148       g_signal_emit_by_name (mount, "changed");
149       if (mount->volume_monitor != NULL)
150         g_signal_emit_by_name (mount->volume_monitor, "mount_changed", mount);
151     }
152 }
153
154 static GFile *
155 g_unix_mount_get_root (GMount *mount)
156 {
157   GUnixMount *unix_mount = G_UNIX_MOUNT (mount);
158
159   return g_file_new_for_path (unix_mount->mount_path);
160 }
161
162 static GIcon *
163 g_unix_mount_get_icon (GMount *mount)
164 {
165   GUnixMount *unix_mount = G_UNIX_MOUNT (mount);
166
167   return g_object_ref (unix_mount->icon);
168 }
169
170 static char *
171 g_unix_mount_get_name (GMount *mount)
172 {
173   GUnixMount *unix_mount = G_UNIX_MOUNT (mount);
174   
175   return g_strdup (unix_mount->name);
176 }
177
178 gboolean
179 _g_unix_mount_has_mount_path (GUnixMount *mount,
180                               const char  *mount_path)
181 {
182   return strcmp (mount->mount_path, mount_path) == 0;
183 }
184
185 static GDrive *
186 g_unix_mount_get_drive (GMount *mount)
187 {
188   GUnixMount *unix_mount = G_UNIX_MOUNT (mount);
189
190   if (unix_mount->volume != NULL)
191     return g_volume_get_drive (G_VOLUME (unix_mount->volume));
192
193   return NULL;
194 }
195
196 static GVolume *
197 g_unix_mount_get_volume (GMount *mount)
198 {
199   GUnixMount *unix_mount = G_UNIX_MOUNT (mount);
200
201   if (unix_mount->volume)
202     return G_VOLUME (g_object_ref (unix_mount->volume));
203   
204   return NULL;
205 }
206
207 static gboolean
208 g_unix_mount_can_unmount (GMount *mount)
209 {
210   return TRUE;
211 }
212
213
214 typedef struct {
215   GUnixMount *unix_mount;
216   GAsyncReadyCallback callback;
217   gpointer user_data;
218   GCancellable *cancellable;
219   int error_fd;
220   GIOChannel *error_channel;
221   guint error_channel_source_id;
222   GString *error_string;
223 } UnmountOp;
224
225 static void 
226 unmount_cb (GPid pid, gint status, gpointer user_data)
227 {
228   UnmountOp *data = user_data;
229   GSimpleAsyncResult *simple;
230   
231   if (WEXITSTATUS (status) != 0)
232     {
233       GError *error;
234       error = g_error_new_literal (G_IO_ERROR, 
235                                    G_IO_ERROR_FAILED,
236                                    data->error_string->str);
237       simple = g_simple_async_result_new_from_error (G_OBJECT (data->unix_mount),
238                                                      data->callback,
239                                                      data->user_data,
240                                                      error);
241       g_error_free (error);
242     }
243   else
244     {
245       simple = g_simple_async_result_new (G_OBJECT (data->unix_mount),
246                                           data->callback,
247                                           data->user_data,
248                                           NULL);
249     }
250
251   g_simple_async_result_complete (simple);
252   g_object_unref (simple);
253
254   g_source_remove (data->error_channel_source_id);
255   g_io_channel_unref (data->error_channel);
256   g_string_free (data->error_string, TRUE);
257   close (data->error_fd);
258   g_spawn_close_pid (pid);
259   g_free (data);
260 }
261
262 static gboolean
263 unmount_read_error (GIOChannel *channel,
264                     GIOCondition condition,
265                     gpointer user_data)
266 {
267   char *str;
268   gsize str_len;
269   UnmountOp *data = user_data;
270
271   g_io_channel_read_to_end (channel, &str, &str_len, NULL);
272   g_string_append (data->error_string, str);
273   g_free (str);
274   return TRUE;
275 }
276
277 static void
278 g_unix_mount_unmount (GMount             *mount,
279                       GCancellable        *cancellable,
280                       GAsyncReadyCallback  callback,
281                       gpointer             user_data)
282 {
283   GUnixMount *unix_mount = G_UNIX_MOUNT (mount);
284   UnmountOp *data;
285   GPid child_pid;
286   GError *error;
287   char *argv[] = {"umount", NULL, NULL};
288
289   if (unix_mount->mount_path != NULL)
290     argv[1] = unix_mount->mount_path;
291   else
292     argv[1] = unix_mount->device_path;
293   
294   data = g_new0 (UnmountOp, 1);
295   data->unix_mount = unix_mount;
296   data->callback = callback;
297   data->user_data = user_data;
298   data->cancellable = cancellable;
299   
300   error = NULL;
301   if (!g_spawn_async_with_pipes (NULL,         /* working dir */
302                                  argv,
303                                  NULL,         /* envp */
304                                  G_SPAWN_DO_NOT_REAP_CHILD|G_SPAWN_SEARCH_PATH,
305                                  NULL,         /* child_setup */
306                                  NULL,         /* user_data for child_setup */
307                                  &child_pid,
308                                  NULL,           /* standard_input */
309                                  NULL,           /* standard_output */
310                                  &(data->error_fd),
311                                  &error)) {
312     GSimpleAsyncResult *simple;
313     simple = g_simple_async_result_new_from_error (G_OBJECT (data->unix_mount),
314                                                    data->callback,
315                                                    data->user_data,
316                                                    error);
317     g_simple_async_result_complete (simple);
318     g_object_unref (simple);
319     g_error_free (error);
320     g_free (data);
321     return;
322   }
323   data->error_string = g_string_new ("");
324   data->error_channel = g_io_channel_unix_new (data->error_fd);
325   data->error_channel_source_id = g_io_add_watch (data->error_channel, G_IO_IN, unmount_read_error, data);
326   g_child_watch_add (child_pid, unmount_cb, data);
327 }
328
329 static gboolean
330 g_unix_mount_unmount_finish (GMount       *mount,
331                              GAsyncResult  *result,
332                              GError       **error)
333 {
334   return TRUE;
335 }
336
337 static void
338 g_unix_mount_mount_iface_init (GMountIface *iface)
339 {
340   iface->get_root = g_unix_mount_get_root;
341   iface->get_name = g_unix_mount_get_name;
342   iface->get_icon = g_unix_mount_get_icon;
343   iface->get_drive = g_unix_mount_get_drive;
344   iface->get_volume = g_unix_mount_get_volume;
345   iface->can_unmount = g_unix_mount_can_unmount;
346   iface->unmount = g_unix_mount_unmount;
347   iface->unmount_finish = g_unix_mount_unmount_finish;
348 }