Drop use of the command tag
[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, see <http://www.gnu.org/licenses/>.
19  *
20  * Author: Alexander Larsson <alexl@redhat.com>
21  *         David Zeuthen <davidz@redhat.com>
22  */
23
24 #include "config.h"
25
26 #include <string.h>
27 #include <sys/wait.h>
28 #include <unistd.h>
29
30 #include <glib.h>
31 #include "gunixvolume.h"
32 #include "gunixmount.h"
33 #include "gunixmounts.h"
34 #include "gthemedicon.h"
35 #include "gvolume.h"
36 #include "gvolumemonitor.h"
37 #include "gtask.h"
38 #include "gioerror.h"
39 #include "glibintl.h"
40 /* for BUFSIZ */
41 #include <stdio.h>
42
43
44 struct _GUnixVolume {
45   GObject parent;
46
47   GVolumeMonitor *volume_monitor;
48   GUnixMount     *mount; /* owned by volume monitor */
49   
50   char *device_path;
51   char *mount_path;
52   gboolean can_eject;
53
54   char *identifier;
55   char *identifier_type;
56   
57   char *name;
58   GIcon *icon;
59   GIcon *symbolic_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_object_unref (volume->symbolic_icon);
84   g_free (volume->name);
85   g_free (volume->mount_path);
86   g_free (volume->device_path);
87   g_free (volume->identifier);
88   g_free (volume->identifier_type);
89
90   G_OBJECT_CLASS (g_unix_volume_parent_class)->finalize (object);
91 }
92
93 static void
94 g_unix_volume_class_init (GUnixVolumeClass *klass)
95 {
96   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
97
98   gobject_class->finalize = g_unix_volume_finalize;
99 }
100
101 static void
102 g_unix_volume_init (GUnixVolume *unix_volume)
103 {
104 }
105
106 GUnixVolume *
107 _g_unix_volume_new (GVolumeMonitor  *volume_monitor,
108                     GUnixMountPoint *mountpoint)
109 {
110   GUnixVolume *volume;
111   
112   if (!(g_unix_mount_point_is_user_mountable (mountpoint) ||
113         g_str_has_prefix (g_unix_mount_point_get_device_path (mountpoint), "/vol/")) ||
114       g_unix_mount_point_is_loopback (mountpoint))
115     return NULL;
116   
117   volume = g_object_new (G_TYPE_UNIX_VOLUME, NULL);
118   volume->volume_monitor = volume_monitor != NULL ? g_object_ref (volume_monitor) : NULL;
119   volume->mount_path = g_strdup (g_unix_mount_point_get_mount_path (mountpoint));
120   volume->device_path = g_strdup (g_unix_mount_point_get_device_path (mountpoint));
121   volume->can_eject = g_unix_mount_point_guess_can_eject (mountpoint);
122
123   volume->name = g_unix_mount_point_guess_name (mountpoint);
124   volume->icon = g_unix_mount_point_guess_icon (mountpoint);
125   volume->symbolic_icon = g_unix_mount_point_guess_symbolic_icon (mountpoint);
126
127
128   if (strcmp (g_unix_mount_point_get_fs_type (mountpoint), "nfs") == 0)
129     {
130       volume->identifier_type = g_strdup (G_VOLUME_IDENTIFIER_KIND_NFS_MOUNT);
131       volume->identifier = g_strdup (volume->device_path);
132     }
133   else if (g_str_has_prefix (volume->device_path, "LABEL="))
134     {
135       volume->identifier_type = g_strdup (G_VOLUME_IDENTIFIER_KIND_LABEL);
136       volume->identifier = g_strdup (volume->device_path + 6);
137     }
138   else if (g_str_has_prefix (volume->device_path, "UUID="))
139     {
140       volume->identifier_type = g_strdup (G_VOLUME_IDENTIFIER_KIND_UUID);
141       volume->identifier = g_strdup (volume->device_path + 5);
142     }
143   else if (g_path_is_absolute (volume->device_path))
144     {
145       volume->identifier_type = g_strdup (G_VOLUME_IDENTIFIER_KIND_UNIX_DEVICE);
146       volume->identifier = g_strdup (volume->device_path);
147     }
148   
149   return volume;
150 }
151
152 void
153 _g_unix_volume_disconnected (GUnixVolume *volume)
154 {
155   if (volume->mount)
156     {
157       _g_unix_mount_unset_volume (volume->mount, volume);
158       volume->mount = NULL;
159     }
160 }
161
162 void
163 _g_unix_volume_set_mount (GUnixVolume *volume,
164                           GUnixMount  *mount)
165 {
166   if (volume->mount == mount)
167     return;
168   
169   if (volume->mount)
170     _g_unix_mount_unset_volume (volume->mount, volume);
171   
172   volume->mount = mount;
173   
174   /* TODO: Emit changed in idle to avoid locking issues */
175   g_signal_emit_by_name (volume, "changed");
176   if (volume->volume_monitor != NULL)
177     g_signal_emit_by_name (volume->volume_monitor, "volume-changed", volume);
178 }
179
180 void
181 _g_unix_volume_unset_mount (GUnixVolume  *volume,
182                             GUnixMount *mount)
183 {
184   if (volume->mount == mount)
185     {
186       volume->mount = NULL;
187       /* TODO: Emit changed in idle to avoid locking issues */
188       g_signal_emit_by_name (volume, "changed");
189       if (volume->volume_monitor != NULL)
190         g_signal_emit_by_name (volume->volume_monitor, "volume-changed", volume);
191     }
192 }
193
194 static GIcon *
195 g_unix_volume_get_icon (GVolume *volume)
196 {
197   GUnixVolume *unix_volume = G_UNIX_VOLUME (volume);
198   return g_object_ref (unix_volume->icon);
199 }
200
201 static GIcon *
202 g_unix_volume_get_symbolic_icon (GVolume *volume)
203 {
204   GUnixVolume *unix_volume = G_UNIX_VOLUME (volume);
205   return g_object_ref (unix_volume->symbolic_icon);
206 }
207
208 static char *
209 g_unix_volume_get_name (GVolume *volume)
210 {
211   GUnixVolume *unix_volume = G_UNIX_VOLUME (volume);
212   return g_strdup (unix_volume->name);
213 }
214
215 static char *
216 g_unix_volume_get_uuid (GVolume *volume)
217 {
218   return NULL;
219 }
220
221 static gboolean
222 g_unix_volume_can_mount (GVolume *volume)
223 {
224   return TRUE;
225 }
226
227 static gboolean
228 g_unix_volume_can_eject (GVolume *volume)
229 {
230   GUnixVolume *unix_volume = G_UNIX_VOLUME (volume);
231   return unix_volume->can_eject;
232 }
233
234 static gboolean
235 g_unix_volume_should_automount (GVolume *volume)
236 {
237   /* We automount all local volumes because we don't even
238    * make the internal stuff visible
239    */
240   return TRUE;
241 }
242
243 static GDrive *
244 g_unix_volume_get_drive (GVolume *volume)
245 {
246   return NULL;
247 }
248
249 static GMount *
250 g_unix_volume_get_mount (GVolume *volume)
251 {
252   GUnixVolume *unix_volume = G_UNIX_VOLUME (volume);
253
254   if (unix_volume->mount != NULL)
255     return g_object_ref (unix_volume->mount);
256
257   return NULL;
258 }
259
260
261 gboolean
262 _g_unix_volume_has_mount_path (GUnixVolume *volume,
263                                const char  *mount_path)
264 {
265   return strcmp (volume->mount_path, mount_path) == 0;
266 }
267
268
269 typedef struct {
270   GUnixVolume *unix_volume;
271   int error_fd;
272   GIOChannel *error_channel;
273   GSource *error_channel_source;
274   GString *error_string;
275 } EjectMountOp;
276
277 static void
278 eject_mount_op_free (EjectMountOp *data)
279 {
280   if (data->error_string != NULL)
281     g_string_free (data->error_string, TRUE);
282
283   if (data->error_channel != NULL)
284     g_io_channel_unref (data->error_channel);
285
286   if (data->error_channel_source)
287     {
288       g_source_destroy (data->error_channel_source);
289       g_source_unref (data->error_channel_source);
290     }
291
292   if (data->error_fd != -1)
293     close (data->error_fd);
294
295   g_free (data);
296 }
297
298 static void
299 eject_mount_cb (GPid     pid,
300                 gint     status,
301                 gpointer user_data)
302 {
303   GTask *task = user_data;
304   EjectMountOp *data = g_task_get_task_data (task);
305   
306   if (WEXITSTATUS (status) != 0)
307     {
308       g_task_return_new_error (task,
309                                G_IO_ERROR, 
310                                G_IO_ERROR_FAILED,
311                                "%s", data->error_string->str);
312     }
313   else
314     g_task_return_boolean (task, TRUE);
315   g_object_unref (task);
316 }
317
318 static gboolean
319 eject_mount_read_error (GIOChannel   *channel,
320                         GIOCondition  condition,
321                         gpointer      user_data)
322 {
323   GTask *task = user_data;
324   EjectMountOp *data = g_task_get_task_data (task);
325   char buf[BUFSIZ];
326   gsize bytes_read;
327   GError *error;
328   GIOStatus status;
329
330   error = NULL;
331 read:
332   status = g_io_channel_read_chars (channel, buf, sizeof (buf), &bytes_read, &error);
333   if (status == G_IO_STATUS_NORMAL)
334    {
335      g_string_append_len (data->error_string, buf, bytes_read);
336      if (bytes_read == sizeof (buf))
337         goto read;
338    }
339   else if (status == G_IO_STATUS_EOF)
340     g_string_append_len (data->error_string, buf, bytes_read);
341   else if (status == G_IO_STATUS_ERROR)
342     {
343       if (data->error_string->len > 0)
344         g_string_append (data->error_string, "\n");
345
346       g_string_append (data->error_string, error->message);
347       g_error_free (error);
348
349       if (data->error_channel_source)
350         {
351           g_source_unref (data->error_channel_source);
352           data->error_channel_source = NULL;
353         }
354       return FALSE;
355     }
356
357   return TRUE;
358 }
359
360 static void
361 eject_mount_do (GVolume             *volume,
362                 GCancellable        *cancellable,
363                 GAsyncReadyCallback  callback,
364                 gpointer             user_data,
365                 char               **argv)
366 {
367   GUnixVolume *unix_volume = G_UNIX_VOLUME (volume);
368   GTask *task;
369   EjectMountOp *data;
370   GPid child_pid;
371   GSource *child_watch;
372   GError *error;
373   
374   data = g_new0 (EjectMountOp, 1);
375   data->unix_volume = unix_volume;
376   data->error_fd = -1;
377   
378   task = g_task_new (unix_volume, cancellable, callback, user_data);
379   g_task_set_task_data (task, data, (GDestroyNotify) eject_mount_op_free);
380
381   error = NULL;
382   if (!g_spawn_async_with_pipes (NULL,         /* working dir */
383                                  argv,
384                                  NULL,         /* envp */
385                                  G_SPAWN_DO_NOT_REAP_CHILD|G_SPAWN_SEARCH_PATH,
386                                  NULL,         /* child_setup */
387                                  NULL,         /* user_data for child_setup */
388                                  &child_pid,
389                                  NULL,           /* standard_input */
390                                  NULL,           /* standard_output */
391                                  &(data->error_fd),
392                                  &error))
393     {
394       g_assert (error != NULL);
395       goto handle_error;
396     }
397
398   data->error_string = g_string_new ("");
399
400   data->error_channel = g_io_channel_unix_new (data->error_fd);
401   g_io_channel_set_flags (data->error_channel, G_IO_FLAG_NONBLOCK, &error);
402   if (error != NULL)
403     goto handle_error;
404
405   data->error_channel_source = g_io_create_watch (data->error_channel, G_IO_IN);
406   g_task_attach_source (task, data->error_channel_source,
407                         (GSourceFunc) eject_mount_read_error);
408
409   child_watch = g_child_watch_source_new (child_pid);
410   g_task_attach_source (task, child_watch, (GSourceFunc) eject_mount_cb);
411   g_source_unref (child_watch);
412
413 handle_error:
414   if (error != NULL)
415     {
416       g_task_return_error (task, error);
417       g_object_unref (task);
418     }
419 }
420
421
422 static void
423 g_unix_volume_mount (GVolume            *volume,
424                      GMountMountFlags    flags,
425                      GMountOperation     *mount_operation,
426                      GCancellable        *cancellable,
427                      GAsyncReadyCallback  callback,
428                      gpointer             user_data)
429 {
430   GUnixVolume *unix_volume = G_UNIX_VOLUME (volume);
431   char *argv[] = { "mount", NULL, NULL };
432
433   if (unix_volume->mount_path != NULL)
434     argv[1] = unix_volume->mount_path;
435   else
436     argv[1] = unix_volume->device_path;
437
438   eject_mount_do (volume, cancellable, callback, user_data, argv);
439 }
440
441 static gboolean
442 g_unix_volume_mount_finish (GVolume        *volume,
443                             GAsyncResult  *result,
444                             GError       **error)
445 {
446   return TRUE;
447 }
448
449 static void
450 g_unix_volume_eject (GVolume             *volume,
451                      GMountUnmountFlags   flags,
452                      GCancellable        *cancellable,
453                      GAsyncReadyCallback  callback,
454                      gpointer             user_data)
455 {
456   GUnixVolume *unix_volume = G_UNIX_VOLUME (volume);
457   char *argv[] = { "eject", NULL, NULL };
458
459   argv[1] = unix_volume->device_path;
460
461   eject_mount_do (volume, cancellable, callback, user_data, argv);
462 }
463
464 static gboolean
465 g_unix_volume_eject_finish (GVolume       *volume,
466                             GAsyncResult  *result,
467                             GError       **error)
468 {
469   return TRUE;
470 }
471
472 static gchar *
473 g_unix_volume_get_identifier (GVolume     *volume,
474                               const gchar *kind)
475 {
476   GUnixVolume *unix_volume = G_UNIX_VOLUME (volume);
477
478   if (unix_volume->identifier_type != NULL &&
479       strcmp (kind, unix_volume->identifier_type) == 0)
480     return g_strdup (unix_volume->identifier);
481
482   return NULL;
483 }
484
485 static gchar **
486 g_unix_volume_enumerate_identifiers (GVolume *volume)
487 {
488   GUnixVolume *unix_volume = G_UNIX_VOLUME (volume);
489   gchar **res;
490
491   if (unix_volume->identifier_type)
492     {
493       res = g_new (gchar *, 2);
494       res[0] = g_strdup (unix_volume->identifier_type);
495       res[1] = NULL;
496     }
497   else
498     {
499       res = g_new (gchar *, 1);
500       res[0] = NULL;
501     }
502
503   return res;
504 }
505
506 static void
507 g_unix_volume_volume_iface_init (GVolumeIface *iface)
508 {
509   iface->get_name = g_unix_volume_get_name;
510   iface->get_icon = g_unix_volume_get_icon;
511   iface->get_symbolic_icon = g_unix_volume_get_symbolic_icon;
512   iface->get_uuid = g_unix_volume_get_uuid;
513   iface->get_drive = g_unix_volume_get_drive;
514   iface->get_mount = g_unix_volume_get_mount;
515   iface->can_mount = g_unix_volume_can_mount;
516   iface->can_eject = g_unix_volume_can_eject;
517   iface->should_automount = g_unix_volume_should_automount;
518   iface->mount_fn = g_unix_volume_mount;
519   iface->mount_finish = g_unix_volume_mount_finish;
520   iface->eject = g_unix_volume_eject;
521   iface->eject_finish = g_unix_volume_eject_finish;
522   iface->get_identifier = g_unix_volume_get_identifier;
523   iface->enumerate_identifiers = g_unix_volume_enumerate_identifiers;
524 }