Include "config.h" instead of <config.h> Command used: find -name
[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   gboolean can_eject;
57 };
58
59 static void g_unix_mount_mount_iface_init (GMountIface *iface);
60
61 #define g_unix_mount_get_type _g_unix_mount_get_type
62 G_DEFINE_TYPE_WITH_CODE (GUnixMount, g_unix_mount, G_TYPE_OBJECT,
63                          G_IMPLEMENT_INTERFACE (G_TYPE_MOUNT,
64                                                 g_unix_mount_mount_iface_init))
65
66
67 static void
68 g_unix_mount_finalize (GObject *object)
69 {
70   GUnixMount *mount;
71   
72   mount = G_UNIX_MOUNT (object);
73
74   if (mount->volume_monitor != NULL)
75     g_object_unref (mount->volume_monitor);
76
77   if (mount->volume)
78     _g_unix_volume_unset_mount (mount->volume, mount);
79     
80   /* TODO: g_warn_if_fail (volume->volume == NULL); */
81   g_object_unref (mount->icon);
82   g_free (mount->name);
83   g_free (mount->device_path);
84   g_free (mount->mount_path);
85
86   G_OBJECT_CLASS (g_unix_mount_parent_class)->finalize (object);
87 }
88
89 static void
90 g_unix_mount_class_init (GUnixMountClass *klass)
91 {
92   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
93
94   gobject_class->finalize = g_unix_mount_finalize;
95 }
96
97 static void
98 g_unix_mount_init (GUnixMount *unix_mount)
99 {
100 }
101
102 GUnixMount *
103 _g_unix_mount_new (GVolumeMonitor  *volume_monitor,
104                    GUnixMountEntry *mount_entry,
105                    GUnixVolume     *volume)
106 {
107   GUnixMount *mount;
108   
109   /* No volume for mount: Ignore internal things */
110   if (volume == NULL && !g_unix_mount_guess_should_display (mount_entry))
111     return NULL;
112
113   mount = g_object_new (G_TYPE_UNIX_MOUNT, NULL);
114   mount->volume_monitor = volume_monitor != NULL ? g_object_ref (volume_monitor) : NULL;
115   mount->device_path = g_strdup (g_unix_mount_get_device_path (mount_entry));
116   mount->mount_path = g_strdup (g_unix_mount_get_mount_path (mount_entry));
117   mount->can_eject = g_unix_mount_guess_can_eject (mount_entry);
118
119   mount->name = g_unix_mount_guess_name (mount_entry);
120   mount->icon = g_unix_mount_guess_icon (mount_entry);
121
122   /* need to do this last */
123   mount->volume = volume;
124   if (volume != NULL)
125     _g_unix_volume_set_mount (volume, mount);
126
127   return mount;
128 }
129
130 void
131 _g_unix_mount_unmounted (GUnixMount *mount)
132 {
133   if (mount->volume != NULL)
134     {
135       _g_unix_volume_unset_mount (mount->volume, mount);
136       mount->volume = NULL;
137       g_signal_emit_by_name (mount, "changed");
138       /* there's really no need to emit mount_changed on the volume monitor 
139        * as we're going to be deleted.. */
140     }
141 }
142
143 void
144 _g_unix_mount_unset_volume (GUnixMount *mount,
145                             GUnixVolume  *volume)
146 {
147   if (mount->volume == volume)
148     {
149       mount->volume = NULL;
150       /* TODO: Emit changed in idle to avoid locking issues */
151       g_signal_emit_by_name (mount, "changed");
152       if (mount->volume_monitor != NULL)
153         g_signal_emit_by_name (mount->volume_monitor, "mount_changed", mount);
154     }
155 }
156
157 static GFile *
158 g_unix_mount_get_root (GMount *mount)
159 {
160   GUnixMount *unix_mount = G_UNIX_MOUNT (mount);
161
162   return g_file_new_for_path (unix_mount->mount_path);
163 }
164
165 static GIcon *
166 g_unix_mount_get_icon (GMount *mount)
167 {
168   GUnixMount *unix_mount = G_UNIX_MOUNT (mount);
169
170   return g_object_ref (unix_mount->icon);
171 }
172
173 static char *
174 g_unix_mount_get_uuid (GMount *mount)
175 {
176   return NULL;
177 }
178
179 static char *
180 g_unix_mount_get_name (GMount *mount)
181 {
182   GUnixMount *unix_mount = G_UNIX_MOUNT (mount);
183   
184   return g_strdup (unix_mount->name);
185 }
186
187 gboolean
188 _g_unix_mount_has_mount_path (GUnixMount *mount,
189                               const char  *mount_path)
190 {
191   return strcmp (mount->mount_path, mount_path) == 0;
192 }
193
194 static GDrive *
195 g_unix_mount_get_drive (GMount *mount)
196 {
197   GUnixMount *unix_mount = G_UNIX_MOUNT (mount);
198
199   if (unix_mount->volume != NULL)
200     return g_volume_get_drive (G_VOLUME (unix_mount->volume));
201
202   return NULL;
203 }
204
205 static GVolume *
206 g_unix_mount_get_volume (GMount *mount)
207 {
208   GUnixMount *unix_mount = G_UNIX_MOUNT (mount);
209
210   if (unix_mount->volume)
211     return G_VOLUME (g_object_ref (unix_mount->volume));
212   
213   return NULL;
214 }
215
216 static gboolean
217 g_unix_mount_can_unmount (GMount *mount)
218 {
219   return TRUE;
220 }
221
222 static gboolean
223 g_unix_mount_can_eject (GMount *mount)
224 {
225   GUnixMount *unix_mount = G_UNIX_MOUNT (mount);
226   return unix_mount->can_eject;
227 }
228
229
230 typedef struct {
231   GUnixMount *unix_mount;
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 } UnmountEjectOp;
240
241 static void 
242 eject_unmount_cb (GPid pid, gint status, gpointer user_data)
243 {
244   UnmountEjectOp *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_mount),
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_mount),
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 eject_unmount_read_error (GIOChannel *channel,
280                     GIOCondition condition,
281                     gpointer user_data)
282 {
283   char *str;
284   gsize str_len;
285   UnmountEjectOp *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 eject_unmount_do (GMount              *mount,
295                   GCancellable        *cancellable,
296                   GAsyncReadyCallback  callback,
297                   gpointer             user_data,
298                   char               **argv)
299 {
300   GUnixMount *unix_mount = G_UNIX_MOUNT (mount);
301   UnmountEjectOp *data;
302   GPid child_pid;
303   GError *error;
304   
305   data = g_new0 (UnmountEjectOp, 1);
306   data->unix_mount = unix_mount;
307   data->callback = callback;
308   data->user_data = user_data;
309   data->cancellable = cancellable;
310   
311   error = NULL;
312   if (!g_spawn_async_with_pipes (NULL,         /* working dir */
313                                  argv,
314                                  NULL,         /* envp */
315                                  G_SPAWN_DO_NOT_REAP_CHILD|G_SPAWN_SEARCH_PATH,
316                                  NULL,         /* child_setup */
317                                  NULL,         /* user_data for child_setup */
318                                  &child_pid,
319                                  NULL,           /* standard_input */
320                                  NULL,           /* standard_output */
321                                  &(data->error_fd),
322                                  &error)) {
323     GSimpleAsyncResult *simple;
324     simple = g_simple_async_result_new_from_error (G_OBJECT (data->unix_mount),
325                                                    data->callback,
326                                                    data->user_data,
327                                                    error);
328     g_simple_async_result_complete (simple);
329     g_object_unref (simple);
330     g_error_free (error);
331     g_free (data);
332     return;
333   }
334   data->error_string = g_string_new ("");
335   data->error_channel = g_io_channel_unix_new (data->error_fd);
336   data->error_channel_source_id = g_io_add_watch (data->error_channel, G_IO_IN, eject_unmount_read_error, data);
337   g_child_watch_add (child_pid, eject_unmount_cb, data);
338 }
339
340 static void
341 g_unix_mount_unmount (GMount             *mount,
342                       GMountUnmountFlags flags,
343                       GCancellable        *cancellable,
344                       GAsyncReadyCallback  callback,
345                       gpointer             user_data)
346 {
347   GUnixMount *unix_mount = G_UNIX_MOUNT (mount);
348   char *argv[] = {"umount", NULL, NULL};
349
350   if (unix_mount->mount_path != NULL)
351     argv[1] = unix_mount->mount_path;
352   else
353     argv[1] = unix_mount->device_path;
354
355   eject_unmount_do (mount, cancellable, callback, user_data, argv);
356 }
357
358 static gboolean
359 g_unix_mount_unmount_finish (GMount       *mount,
360                              GAsyncResult  *result,
361                              GError       **error)
362 {
363   return TRUE;
364 }
365
366 static void
367 g_unix_mount_eject (GMount             *mount,
368                     GMountUnmountFlags flags,
369                     GCancellable        *cancellable,
370                     GAsyncReadyCallback  callback,
371                     gpointer             user_data)
372 {
373   GUnixMount *unix_mount = G_UNIX_MOUNT (mount);
374   char *argv[] = {"eject", NULL, NULL};
375
376   if (unix_mount->mount_path != NULL)
377     argv[1] = unix_mount->mount_path;
378   else
379     argv[1] = unix_mount->device_path;
380
381   eject_unmount_do (mount, cancellable, callback, user_data, argv);
382 }
383
384 static gboolean
385 g_unix_mount_eject_finish (GMount       *mount,
386                            GAsyncResult  *result,
387                            GError       **error)
388 {
389   return TRUE;
390 }
391
392 static void
393 g_unix_mount_mount_iface_init (GMountIface *iface)
394 {
395   iface->get_root = g_unix_mount_get_root;
396   iface->get_name = g_unix_mount_get_name;
397   iface->get_icon = g_unix_mount_get_icon;
398   iface->get_uuid = g_unix_mount_get_uuid;
399   iface->get_drive = g_unix_mount_get_drive;
400   iface->get_volume = g_unix_mount_get_volume;
401   iface->can_unmount = g_unix_mount_can_unmount;
402   iface->can_eject = g_unix_mount_can_eject;
403   iface->unmount = g_unix_mount_unmount;
404   iface->unmount_finish = g_unix_mount_unmount_finish;
405   iface->eject = g_unix_mount_eject;
406   iface->eject_finish = g_unix_mount_eject_finish;
407 }