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