gfilenamecompleter.c glocaldirectorymonitor.c gmountoperation.c
[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 "gunixmounts.h"
36 #include "gthemedicon.h"
37 #include "gvolume.h"
38 #include "gvolumemonitor.h"
39 #include "gsimpleasyncresult.h"
40 #include "gioerror.h"
41 #include "glibintl.h"
42
43 #include "gioalias.h"
44
45 struct _GUnixVolume {
46   GObject parent;
47
48   GVolumeMonitor *volume_monitor;
49   GUnixMount     *mount; /* owned by volume monitor */
50   
51   char *device_path;
52   char *mount_path;
53   gboolean can_eject;
54
55   char *identifier;
56   char *identifier_type;
57   
58   char *name;
59   GIcon *icon;
60 };
61
62 static void g_unix_volume_volume_iface_init (GVolumeIface *iface);
63
64 #define g_unix_volume_get_type _g_unix_volume_get_type
65 G_DEFINE_TYPE_WITH_CODE (GUnixVolume, g_unix_volume, G_TYPE_OBJECT,
66                          G_IMPLEMENT_INTERFACE (G_TYPE_VOLUME,
67                                                 g_unix_volume_volume_iface_init))
68
69 static void
70 g_unix_volume_finalize (GObject *object)
71 {
72   GUnixVolume *volume;
73   
74   volume = G_UNIX_VOLUME (object);
75
76   if (volume->volume_monitor != NULL)
77     g_object_unref (volume->volume_monitor);
78
79   if (volume->mount)
80     _g_unix_mount_unset_volume (volume->mount, volume);
81   
82   g_object_unref (volume->icon);
83   g_free (volume->name);
84   g_free (volume->mount_path);
85   g_free (volume->device_path);
86   g_free (volume->identifier);
87   g_free (volume->identifier_type);
88
89   G_OBJECT_CLASS (g_unix_volume_parent_class)->finalize (object);
90 }
91
92 static void
93 g_unix_volume_class_init (GUnixVolumeClass *klass)
94 {
95   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
96
97   gobject_class->finalize = g_unix_volume_finalize;
98 }
99
100 static void
101 g_unix_volume_init (GUnixVolume *unix_volume)
102 {
103 }
104
105 /**
106  * g_unix_volume_new:
107  * @volume_monitor: a #GVolumeMonitor.
108  * @mountpoint: a #GUnixMountPoint.
109  * 
110  * Returns: a #GUnixVolume for the given #GUnixMountPoint.
111  **/
112 GUnixVolume *
113 _g_unix_volume_new (GVolumeMonitor  *volume_monitor,
114                     GUnixMountPoint *mountpoint)
115 {
116   GUnixVolume *volume;
117   
118   if (!(g_unix_mount_point_is_user_mountable (mountpoint) ||
119         g_str_has_prefix (g_unix_mount_point_get_device_path (mountpoint), "/vol/")) ||
120       g_unix_mount_point_is_loopback (mountpoint))
121     return NULL;
122   
123   volume = g_object_new (G_TYPE_UNIX_VOLUME, NULL);
124   volume->volume_monitor = volume_monitor != NULL ? g_object_ref (volume_monitor) : NULL;
125   volume->mount_path = g_strdup (g_unix_mount_point_get_mount_path (mountpoint));
126   volume->device_path = g_strdup (g_unix_mount_point_get_device_path (mountpoint));
127   volume->can_eject = g_unix_mount_point_guess_can_eject (mountpoint);
128
129   volume->name = g_unix_mount_point_guess_name (mountpoint);
130   volume->icon = g_unix_mount_point_guess_icon (mountpoint);
131
132
133   if (strcmp (g_unix_mount_point_get_fs_type (mountpoint), "nfs") == 0)
134     {
135       volume->identifier_type = g_strdup (G_VOLUME_IDENTIFIER_KIND_NFS_MOUNT);
136       volume->identifier = g_strdup (volume->device_path);
137     }
138   else if (g_str_has_prefix (volume->device_path, "LABEL="))
139     {
140       volume->identifier_type = g_strdup (G_VOLUME_IDENTIFIER_KIND_LABEL);
141       volume->identifier = g_strdup (volume->device_path + 6);
142     }
143   else if (g_str_has_prefix (volume->device_path, "UUID="))
144     {
145       volume->identifier_type = g_strdup (G_VOLUME_IDENTIFIER_KIND_UUID);
146       volume->identifier = g_strdup (volume->device_path + 5);
147     }
148   else if (g_path_is_absolute (volume->device_path))
149     {
150       volume->identifier_type = g_strdup (G_VOLUME_IDENTIFIER_KIND_UNIX_DEVICE);
151       volume->identifier = g_strdup (volume->device_path);
152     }
153   
154   return volume;
155 }
156
157 /**
158  * g_unix_volume_disconnected:
159  * @volume:
160  * 
161  **/
162 void
163 _g_unix_volume_disconnected (GUnixVolume *volume)
164 {
165   if (volume->mount)
166     {
167       _g_unix_mount_unset_volume (volume->mount, volume);
168       volume->mount = NULL;
169     }
170 }
171
172 /**
173  * g_unix_volume_set_mount:
174  * @volume:
175  * @mount:
176  *  
177  **/
178 void
179 _g_unix_volume_set_mount (GUnixVolume  *volume,
180                           GUnixMount *mount)
181 {
182   if (volume->mount == mount)
183     return;
184   
185   if (volume->mount)
186     _g_unix_mount_unset_volume (volume->mount, volume);
187   
188   volume->mount = mount;
189   
190   /* TODO: Emit changed in idle to avoid locking issues */
191   g_signal_emit_by_name (volume, "changed");
192   if (volume->volume_monitor != NULL)
193     g_signal_emit_by_name (volume->volume_monitor, "volume-changed", volume);
194 }
195
196 /**
197  * g_unix_volume_unset_mount:
198  * @volume:
199  * @mount:
200  *
201  **/
202 void
203 _g_unix_volume_unset_mount (GUnixVolume  *volume,
204                             GUnixMount *mount)
205 {
206   if (volume->mount == mount)
207     {
208       volume->mount = NULL;
209       /* TODO: Emit changed in idle to avoid locking issues */
210       g_signal_emit_by_name (volume, "changed");
211       if (volume->volume_monitor != NULL)
212         g_signal_emit_by_name (volume->volume_monitor, "volume-changed", volume);
213     }
214 }
215
216 static GIcon *
217 g_unix_volume_get_icon (GVolume *volume)
218 {
219   GUnixVolume *unix_volume = G_UNIX_VOLUME (volume);
220   return g_object_ref (unix_volume->icon);
221 }
222
223 static char *
224 g_unix_volume_get_name (GVolume *volume)
225 {
226   GUnixVolume *unix_volume = G_UNIX_VOLUME (volume);
227   return g_strdup (unix_volume->name);
228 }
229
230 static char *
231 g_unix_volume_get_uuid (GVolume *volume)
232 {
233   return NULL;
234 }
235
236 static gboolean
237 g_unix_volume_can_mount (GVolume *volume)
238 {
239   return TRUE;
240 }
241
242 static gboolean
243 g_unix_volume_can_eject (GVolume *volume)
244 {
245   GUnixVolume *unix_volume = G_UNIX_VOLUME (volume);
246   return unix_volume->can_eject;
247 }
248
249 static gboolean
250 g_unix_volume_should_automount (GVolume *volume)
251 {
252   /* We automount all local volumes because we don't even
253      make the internal stuff visible */
254   return TRUE;
255 }
256
257 static GDrive *
258 g_unix_volume_get_drive (GVolume *volume)
259 {
260   return NULL;
261 }
262
263 static GMount *
264 g_unix_volume_get_mount (GVolume *volume)
265 {
266   GUnixVolume *unix_volume = G_UNIX_VOLUME (volume);
267
268   if (unix_volume->mount != NULL)
269     return g_object_ref (unix_volume->mount);
270
271   return NULL;
272 }
273
274
275 gboolean
276 _g_unix_volume_has_mount_path (GUnixVolume *volume,
277                                          const char  *mount_path)
278 {
279   return strcmp (volume->mount_path, mount_path) == 0;
280 }
281
282
283 typedef struct {
284   GUnixVolume *unix_volume;
285   GAsyncReadyCallback callback;
286   gpointer user_data;
287   GCancellable *cancellable;
288   int error_fd;
289   GIOChannel *error_channel;
290   guint error_channel_source_id;
291   GString *error_string;
292 } EjectMountOp;
293
294 static void 
295 eject_mount_cb (GPid pid, gint status, gpointer user_data)
296 {
297   EjectMountOp *data = user_data;
298   GSimpleAsyncResult *simple;
299   
300   if (WEXITSTATUS (status) != 0)
301     {
302       GError *error;
303       error = g_error_new_literal (G_IO_ERROR, 
304                                    G_IO_ERROR_FAILED,
305                                    data->error_string->str);
306       simple = g_simple_async_result_new_from_error (G_OBJECT (data->unix_volume),
307                                                      data->callback,
308                                                      data->user_data,
309                                                      error);
310       g_error_free (error);
311     }
312   else
313     {
314       simple = g_simple_async_result_new (G_OBJECT (data->unix_volume),
315                                           data->callback,
316                                           data->user_data,
317                                           NULL);
318     }
319
320   g_simple_async_result_complete (simple);
321   g_object_unref (simple);
322
323   g_source_remove (data->error_channel_source_id);
324   g_io_channel_unref (data->error_channel);
325   g_string_free (data->error_string, TRUE);
326   close (data->error_fd);
327   g_spawn_close_pid (pid);
328   g_free (data);
329 }
330
331 static gboolean
332 eject_mount_read_error (GIOChannel *channel,
333                   GIOCondition condition,
334                   gpointer user_data)
335 {
336   char *str;
337   gsize str_len;
338   EjectMountOp *data = user_data;
339
340   g_io_channel_read_to_end (channel, &str, &str_len, NULL);
341   g_string_append (data->error_string, str);
342   g_free (str);
343   return TRUE;
344 }
345
346 static void
347 eject_mount_do (GVolume             *volume,
348                 GCancellable        *cancellable,
349                 GAsyncReadyCallback  callback,
350                 gpointer             user_data,
351                 char               **argv)
352 {
353   GUnixVolume *unix_volume = G_UNIX_VOLUME (volume);
354   EjectMountOp *data;
355   GPid child_pid;
356   GError *error;
357   
358   data = g_new0 (EjectMountOp, 1);
359   data->unix_volume = unix_volume;
360   data->callback = callback;
361   data->user_data = user_data;
362   data->cancellable = cancellable;
363   
364   error = NULL;
365   if (!g_spawn_async_with_pipes (NULL,         /* working dir */
366                                  argv,
367                                  NULL,         /* envp */
368                                  G_SPAWN_DO_NOT_REAP_CHILD|G_SPAWN_SEARCH_PATH,
369                                  NULL,         /* child_setup */
370                                  NULL,         /* user_data for child_setup */
371                                  &child_pid,
372                                  NULL,           /* standard_input */
373                                  NULL,           /* standard_output */
374                                  &(data->error_fd),
375                                  &error)) {
376     GSimpleAsyncResult *simple;
377     simple = g_simple_async_result_new_from_error (G_OBJECT (data->unix_volume),
378                                                    data->callback,
379                                                    data->user_data,
380                                                    error);
381     g_simple_async_result_complete (simple);
382     g_object_unref (simple);
383     g_error_free (error);
384     g_free (data);
385     return;
386   }
387   data->error_string = g_string_new ("");
388   data->error_channel = g_io_channel_unix_new (data->error_fd);
389   data->error_channel_source_id = g_io_add_watch (data->error_channel, G_IO_IN, eject_mount_read_error, data);
390   g_child_watch_add (child_pid, eject_mount_cb, data);
391 }
392
393
394 static void
395 g_unix_volume_mount (GVolume    *volume,
396                      GMountMountFlags    flags,
397                      GMountOperation     *mount_operation,
398                      GCancellable        *cancellable,
399                      GAsyncReadyCallback  callback,
400                      gpointer             user_data)
401 {
402   GUnixVolume *unix_volume = G_UNIX_VOLUME (volume);
403   char *argv[] = {"mount", NULL, NULL};
404
405   if (unix_volume->mount_path != NULL)
406     argv[1] = unix_volume->mount_path;
407   else
408     argv[1] = unix_volume->device_path;
409
410   eject_mount_do (volume, cancellable, callback, user_data, argv);
411 }
412
413 static gboolean
414 g_unix_volume_mount_finish (GVolume        *volume,
415                            GAsyncResult  *result,
416                            GError       **error)
417 {
418   return TRUE;
419 }
420
421 static void
422 g_unix_volume_eject (GVolume    *volume,
423                      GMountUnmountFlags   flags,
424                      GCancellable        *cancellable,
425                      GAsyncReadyCallback  callback,
426                      gpointer             user_data)
427 {
428   GUnixVolume *unix_volume = G_UNIX_VOLUME (volume);
429   char *argv[] = {"eject", NULL, NULL};
430
431   argv[1] = unix_volume->device_path;
432
433   eject_mount_do (volume, cancellable, callback, user_data, argv);
434 }
435
436 static gboolean
437 g_unix_volume_eject_finish (GVolume        *volume,
438                             GAsyncResult  *result,
439                             GError       **error)
440 {
441   return TRUE;
442 }
443
444 static char *
445 g_unix_volume_get_identifier (GVolume              *volume,
446                               const char          *kind)
447 {
448   GUnixVolume *unix_volume = G_UNIX_VOLUME (volume);
449
450   if (strcmp (kind, unix_volume->identifier_type) == 0)
451     return g_strdup (unix_volume->identifier);
452   return NULL;
453 }
454
455 static char **
456 g_unix_volume_enumerate_identifiers (GVolume *volume)
457 {
458   GUnixVolume *unix_volume = G_UNIX_VOLUME (volume);
459   char **res;
460
461   if (unix_volume->identifier_type)
462     {
463       res = g_new (char *, 2);
464       res[0] = g_strdup (unix_volume->identifier_type);
465       res[1] = NULL;
466     }
467   else
468     {
469       res = g_new (char *, 1);
470       res[0] = NULL;
471     }
472
473   return res;
474 }
475
476 static void
477 g_unix_volume_volume_iface_init (GVolumeIface *iface)
478 {
479   iface->get_name = g_unix_volume_get_name;
480   iface->get_icon = g_unix_volume_get_icon;
481   iface->get_uuid = g_unix_volume_get_uuid;
482   iface->get_drive = g_unix_volume_get_drive;
483   iface->get_mount = g_unix_volume_get_mount;
484   iface->can_mount = g_unix_volume_can_mount;
485   iface->can_eject = g_unix_volume_can_eject;
486   iface->should_automount = g_unix_volume_should_automount;
487   iface->mount_fn = g_unix_volume_mount;
488   iface->mount_finish = g_unix_volume_mount_finish;
489   iface->eject = g_unix_volume_eject;
490   iface->eject_finish = g_unix_volume_eject_finish;
491   iface->get_identifier = g_unix_volume_get_identifier;
492   iface->enumerate_identifiers = g_unix_volume_enumerate_identifiers;
493 }