Fix AKS -> ASK typo
[platform/upstream/glib.git] / gio / gunixvolume.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 "gunixvolume.h"
34 #include "gunixmount.h"
35 #include "gthemedicon.h"
36 #include "gvolumemonitor.h"
37 #include "gsimpleasyncresult.h"
38 #include "glibintl.h"
39
40 #include "gioalias.h"
41
42 struct _GUnixVolume {
43   GObject parent;
44
45   GVolumeMonitor *volume_monitor;
46   GUnixMount     *mount; /* owned by volume monitor */
47   
48   char *device_path;
49   char *mount_path;
50   char *name;
51   GIcon *icon;
52 };
53
54 static void g_unix_volume_volume_iface_init (GVolumeIface *iface);
55
56 #define g_unix_volume_get_type _g_unix_volume_get_type
57 G_DEFINE_TYPE_WITH_CODE (GUnixVolume, g_unix_volume, G_TYPE_OBJECT,
58                          G_IMPLEMENT_INTERFACE (G_TYPE_VOLUME,
59                                                 g_unix_volume_volume_iface_init))
60
61 static void
62 g_unix_volume_finalize (GObject *object)
63 {
64   GUnixVolume *volume;
65   
66   volume = G_UNIX_VOLUME (object);
67
68   if (volume->volume_monitor != NULL)
69     g_object_unref (volume->volume_monitor);
70
71   if (volume->mount)
72     _g_unix_mount_unset_volume (volume->mount, volume);
73   
74   g_object_unref (volume->icon);
75   g_free (volume->name);
76   g_free (volume->mount_path);
77   g_free (volume->device_path);
78
79   if (G_OBJECT_CLASS (g_unix_volume_parent_class)->finalize)
80     (*G_OBJECT_CLASS (g_unix_volume_parent_class)->finalize) (object);
81 }
82
83 static void
84 g_unix_volume_class_init (GUnixVolumeClass *klass)
85 {
86   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
87
88   gobject_class->finalize = g_unix_volume_finalize;
89 }
90
91 static void
92 g_unix_volume_init (GUnixVolume *unix_volume)
93 {
94 }
95
96 /**
97  * g_unix_volume_new:
98  * @volume_monitor: a #GVolumeMonitor.
99  * @mountpoint: a #GUnixMountPoint.
100  * 
101  * Returns: a #GUnixVolume for the given #GUnixMountPoint.
102  **/
103 GUnixVolume *
104 _g_unix_volume_new (GVolumeMonitor  *volume_monitor,
105                     GUnixMountPoint *mountpoint)
106 {
107   GUnixVolume *volume;
108   
109   if (!(g_unix_mount_point_is_user_mountable (mountpoint) ||
110         g_str_has_prefix (g_unix_mount_point_get_device_path (mountpoint), "/vol/")) ||
111       g_unix_mount_point_is_loopback (mountpoint))
112     return NULL;
113   
114   volume = g_object_new (G_TYPE_UNIX_VOLUME, NULL);
115   volume->volume_monitor = volume_monitor != NULL ? g_object_ref (volume_monitor) : NULL;
116   volume->mount_path = g_strdup (g_unix_mount_point_get_mount_path (mountpoint));
117   volume->device_path = g_strdup (g_unix_mount_point_get_device_path (mountpoint));
118
119   volume->name = g_unix_mount_point_guess_name (mountpoint);
120   volume->icon = g_unix_mount_point_guess_icon (mountpoint);
121   return volume;
122 }
123
124 /**
125  * g_unix_volume_disconnected:
126  * @volume:
127  * 
128  **/
129 void
130 _g_unix_volume_disconnected (GUnixVolume *volume)
131 {
132   if (volume->mount)
133     {
134       _g_unix_mount_unset_volume (volume->mount, volume);
135       volume->mount = NULL;
136     }
137 }
138
139 /**
140  * g_unix_volume_set_mount:
141  * @volume:
142  * @mount:
143  *  
144  **/
145 void
146 _g_unix_volume_set_mount (GUnixVolume  *volume,
147                           GUnixMount *mount)
148 {
149   if (volume->mount == mount)
150     return;
151   
152   if (volume->mount)
153     _g_unix_mount_unset_volume (volume->mount, volume);
154   
155   volume->mount = mount;
156   
157   /* TODO: Emit changed in idle to avoid locking issues */
158   g_signal_emit_by_name (volume, "changed");
159   if (volume->volume_monitor != NULL)
160     g_signal_emit_by_name (volume->volume_monitor, "volume_changed", volume);
161 }
162
163 /**
164  * g_unix_volume_unset_mount:
165  * @volume:
166  * @mount:
167  *
168  **/
169 void
170 _g_unix_volume_unset_mount (GUnixVolume  *volume,
171                             GUnixMount *mount)
172 {
173   if (volume->mount == mount)
174     {
175       volume->mount = NULL;
176       /* TODO: Emit changed in idle to avoid locking issues */
177       g_signal_emit_by_name (volume, "changed");
178       if (volume->volume_monitor != NULL)
179         g_signal_emit_by_name (volume->volume_monitor, "volume_changed", volume);
180     }
181 }
182
183 static GIcon *
184 g_unix_volume_get_icon (GVolume *volume)
185 {
186   GUnixVolume *unix_volume = G_UNIX_VOLUME (volume);
187   return g_object_ref (unix_volume->icon);
188 }
189
190 static char *
191 g_unix_volume_get_name (GVolume *volume)
192 {
193   GUnixVolume *unix_volume = G_UNIX_VOLUME (volume);
194   return g_strdup (unix_volume->name);
195 }
196
197 static gboolean
198 g_unix_volume_can_mount (GVolume *volume)
199 {
200   return TRUE;
201 }
202
203 static GDrive *
204 g_unix_volume_get_drive (GVolume *volume)
205 {
206   /* TODO */
207   return NULL;
208 }
209
210 static GMount *
211 g_unix_volume_get_mount (GVolume *volume)
212 {
213   GUnixVolume *unix_volume = G_UNIX_VOLUME (volume);
214
215   if (unix_volume->mount != NULL)
216     return g_object_ref (unix_volume->mount);
217
218   return NULL;
219 }
220
221
222 gboolean
223 _g_unix_volume_has_mount_path (GUnixVolume *volume,
224                                          const char  *mount_path)
225 {
226   return strcmp (volume->mount_path, mount_path) == 0;
227 }
228
229
230 typedef struct {
231   GUnixVolume *unix_volume;
232   GAsyncReadyCallback callback;
233   gpointer user_data;
234   GCancellable *cancellable;
235   int error_fd;
236   GIOChannel *error_channel;
237   guint error_channel_source_id;
238   GString *error_string;
239 } MountOp;
240
241 static void 
242 mount_cb (GPid pid, gint status, gpointer user_data)
243 {
244   MountOp *data = user_data;
245   GSimpleAsyncResult *simple;
246   
247   if (WEXITSTATUS (status) != 0)
248     {
249       GError *error;
250       error = g_error_new_literal (G_IO_ERROR, 
251                                    G_IO_ERROR_FAILED,
252                                    data->error_string->str);
253       simple = g_simple_async_result_new_from_error (G_OBJECT (data->unix_volume),
254                                                      data->callback,
255                                                      data->user_data,
256                                                      error);
257       g_error_free (error);
258     }
259   else
260     {
261       simple = g_simple_async_result_new (G_OBJECT (data->unix_volume),
262                                           data->callback,
263                                           data->user_data,
264                                           NULL);
265     }
266
267   g_simple_async_result_complete (simple);
268   g_object_unref (simple);
269
270   g_source_remove (data->error_channel_source_id);
271   g_io_channel_unref (data->error_channel);
272   g_string_free (data->error_string, TRUE);
273   close (data->error_fd);
274   g_spawn_close_pid (pid);
275   g_free (data);
276 }
277
278 static gboolean
279 mount_read_error (GIOChannel *channel,
280                   GIOCondition condition,
281                   gpointer user_data)
282 {
283   char *str;
284   gsize str_len;
285   MountOp *data = user_data;
286
287   g_io_channel_read_to_end (channel, &str, &str_len, NULL);
288   g_string_append (data->error_string, str);
289   g_free (str);
290   return TRUE;
291 }
292
293 static void
294 g_unix_volume_mount (GVolume    *volume,
295                      GMountOperation     *mount_operation,
296                      GCancellable        *cancellable,
297                      GAsyncReadyCallback  callback,
298                      gpointer             user_data)
299 {
300   GUnixVolume *unix_volume = G_UNIX_VOLUME (volume);
301   MountOp *data;
302   GPid child_pid;
303   GError *error;
304   char *argv[] = {"mount", NULL, NULL};
305
306   if (unix_volume->mount_path != NULL)
307     argv[1] = unix_volume->mount_path;
308   else
309     argv[1] = unix_volume->device_path;
310   
311   data = g_new0 (MountOp, 1);
312   data->unix_volume = unix_volume;
313   data->callback = callback;
314   data->user_data = user_data;
315   data->cancellable = cancellable;
316   
317   error = NULL;
318   if (!g_spawn_async_with_pipes (NULL,         /* working dir */
319                                  argv,
320                                  NULL,         /* envp */
321                                  G_SPAWN_DO_NOT_REAP_CHILD|G_SPAWN_SEARCH_PATH,
322                                  NULL,         /* child_setup */
323                                  NULL,         /* user_data for child_setup */
324                                  &child_pid,
325                                  NULL,           /* standard_input */
326                                  NULL,           /* standard_output */
327                                  &(data->error_fd),
328                                  &error)) {
329     GSimpleAsyncResult *simple;
330     simple = g_simple_async_result_new_from_error (G_OBJECT (data->unix_volume),
331                                                    data->callback,
332                                                    data->user_data,
333                                                    error);
334     g_simple_async_result_complete (simple);
335     g_object_unref (simple);
336     g_error_free (error);
337     g_free (data);
338     return;
339   }
340   data->error_string = g_string_new ("");
341   data->error_channel = g_io_channel_unix_new (data->error_fd);
342   data->error_channel_source_id = g_io_add_watch (data->error_channel, G_IO_IN, mount_read_error, data);
343   g_child_watch_add (child_pid, mount_cb, data);
344 }
345
346
347 static gboolean
348 g_unix_volume_mount_finish (GVolume        *volume,
349                            GAsyncResult  *result,
350                            GError       **error)
351 {
352   return TRUE;
353 }
354
355 static void
356 g_unix_volume_volume_iface_init (GVolumeIface *iface)
357 {
358   iface->get_name = g_unix_volume_get_name;
359   iface->get_icon = g_unix_volume_get_icon;
360   iface->get_drive = g_unix_volume_get_drive;
361   iface->get_mount = g_unix_volume_get_mount;
362   iface->can_mount = g_unix_volume_can_mount;
363   iface->mount_fn = g_unix_volume_mount;
364   iface->mount_finish = g_unix_volume_mount_finish;
365 }