Revert "GIOScheduler: Avoid constant iteration over pending job list"
[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 /* for BUFSIZ */
43 #include <stdio.h>
44
45
46 struct _GUnixVolume {
47   GObject parent;
48
49   GVolumeMonitor *volume_monitor;
50   GUnixMount     *mount; /* owned by volume monitor */
51   
52   char *device_path;
53   char *mount_path;
54   gboolean can_eject;
55
56   char *identifier;
57   char *identifier_type;
58   
59   char *name;
60   GIcon *icon;
61 };
62
63 static void g_unix_volume_volume_iface_init (GVolumeIface *iface);
64
65 #define g_unix_volume_get_type _g_unix_volume_get_type
66 G_DEFINE_TYPE_WITH_CODE (GUnixVolume, g_unix_volume, G_TYPE_OBJECT,
67                          G_IMPLEMENT_INTERFACE (G_TYPE_VOLUME,
68                                                 g_unix_volume_volume_iface_init))
69
70 static void
71 g_unix_volume_finalize (GObject *object)
72 {
73   GUnixVolume *volume;
74   
75   volume = G_UNIX_VOLUME (object);
76
77   if (volume->volume_monitor != NULL)
78     g_object_unref (volume->volume_monitor);
79
80   if (volume->mount)
81     _g_unix_mount_unset_volume (volume->mount, volume);
82   
83   g_object_unref (volume->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
126
127   if (strcmp (g_unix_mount_point_get_fs_type (mountpoint), "nfs") == 0)
128     {
129       volume->identifier_type = g_strdup (G_VOLUME_IDENTIFIER_KIND_NFS_MOUNT);
130       volume->identifier = g_strdup (volume->device_path);
131     }
132   else if (g_str_has_prefix (volume->device_path, "LABEL="))
133     {
134       volume->identifier_type = g_strdup (G_VOLUME_IDENTIFIER_KIND_LABEL);
135       volume->identifier = g_strdup (volume->device_path + 6);
136     }
137   else if (g_str_has_prefix (volume->device_path, "UUID="))
138     {
139       volume->identifier_type = g_strdup (G_VOLUME_IDENTIFIER_KIND_UUID);
140       volume->identifier = g_strdup (volume->device_path + 5);
141     }
142   else if (g_path_is_absolute (volume->device_path))
143     {
144       volume->identifier_type = g_strdup (G_VOLUME_IDENTIFIER_KIND_UNIX_DEVICE);
145       volume->identifier = g_strdup (volume->device_path);
146     }
147   
148   return volume;
149 }
150
151 void
152 _g_unix_volume_disconnected (GUnixVolume *volume)
153 {
154   if (volume->mount)
155     {
156       _g_unix_mount_unset_volume (volume->mount, volume);
157       volume->mount = NULL;
158     }
159 }
160
161 void
162 _g_unix_volume_set_mount (GUnixVolume *volume,
163                           GUnixMount  *mount)
164 {
165   if (volume->mount == mount)
166     return;
167   
168   if (volume->mount)
169     _g_unix_mount_unset_volume (volume->mount, volume);
170   
171   volume->mount = mount;
172   
173   /* TODO: Emit changed in idle to avoid locking issues */
174   g_signal_emit_by_name (volume, "changed");
175   if (volume->volume_monitor != NULL)
176     g_signal_emit_by_name (volume->volume_monitor, "volume-changed", volume);
177 }
178
179 void
180 _g_unix_volume_unset_mount (GUnixVolume  *volume,
181                             GUnixMount *mount)
182 {
183   if (volume->mount == mount)
184     {
185       volume->mount = NULL;
186       /* TODO: Emit changed in idle to avoid locking issues */
187       g_signal_emit_by_name (volume, "changed");
188       if (volume->volume_monitor != NULL)
189         g_signal_emit_by_name (volume->volume_monitor, "volume-changed", volume);
190     }
191 }
192
193 static GIcon *
194 g_unix_volume_get_icon (GVolume *volume)
195 {
196   GUnixVolume *unix_volume = G_UNIX_VOLUME (volume);
197   return g_object_ref (unix_volume->icon);
198 }
199
200 static char *
201 g_unix_volume_get_name (GVolume *volume)
202 {
203   GUnixVolume *unix_volume = G_UNIX_VOLUME (volume);
204   return g_strdup (unix_volume->name);
205 }
206
207 static char *
208 g_unix_volume_get_uuid (GVolume *volume)
209 {
210   return NULL;
211 }
212
213 static gboolean
214 g_unix_volume_can_mount (GVolume *volume)
215 {
216   return TRUE;
217 }
218
219 static gboolean
220 g_unix_volume_can_eject (GVolume *volume)
221 {
222   GUnixVolume *unix_volume = G_UNIX_VOLUME (volume);
223   return unix_volume->can_eject;
224 }
225
226 static gboolean
227 g_unix_volume_should_automount (GVolume *volume)
228 {
229   /* We automount all local volumes because we don't even
230    * make the internal stuff visible
231    */
232   return TRUE;
233 }
234
235 static GDrive *
236 g_unix_volume_get_drive (GVolume *volume)
237 {
238   return NULL;
239 }
240
241 static GMount *
242 g_unix_volume_get_mount (GVolume *volume)
243 {
244   GUnixVolume *unix_volume = G_UNIX_VOLUME (volume);
245
246   if (unix_volume->mount != NULL)
247     return g_object_ref (unix_volume->mount);
248
249   return NULL;
250 }
251
252
253 gboolean
254 _g_unix_volume_has_mount_path (GUnixVolume *volume,
255                                const char  *mount_path)
256 {
257   return strcmp (volume->mount_path, mount_path) == 0;
258 }
259
260
261 typedef struct {
262   GUnixVolume *unix_volume;
263   GAsyncReadyCallback callback;
264   gpointer user_data;
265   GCancellable *cancellable;
266   int error_fd;
267   GIOChannel *error_channel;
268   GSource *error_channel_source;
269   GString *error_string;
270 } EjectMountOp;
271
272 static void
273 eject_mount_cb (GPid     pid,
274                 gint     status,
275                 gpointer user_data)
276 {
277   EjectMountOp *data = user_data;
278   GSimpleAsyncResult *simple;
279   
280   if (WEXITSTATUS (status) != 0)
281     {
282       GError *error;
283       error = g_error_new_literal (G_IO_ERROR, 
284                                    G_IO_ERROR_FAILED,
285                                    data->error_string->str);
286       simple = g_simple_async_result_new_from_error (G_OBJECT (data->unix_volume),
287                                                      data->callback,
288                                                      data->user_data,
289                                                      error);
290       g_error_free (error);
291     }
292   else
293     {
294       simple = g_simple_async_result_new (G_OBJECT (data->unix_volume),
295                                           data->callback,
296                                           data->user_data,
297                                           NULL);
298     }
299
300   g_simple_async_result_complete (simple);
301   g_object_unref (simple);
302
303   if (data->error_channel_source)
304     {
305       g_source_destroy (data->error_channel_source);
306       g_source_unref (data->error_channel_source);
307     }
308   g_io_channel_unref (data->error_channel);
309   g_string_free (data->error_string, TRUE);
310   close (data->error_fd);
311   g_spawn_close_pid (pid);
312   g_free (data);
313 }
314
315 static gboolean
316 eject_mount_read_error (GIOChannel   *channel,
317                         GIOCondition  condition,
318                         gpointer      user_data)
319 {
320   EjectMountOp *data = user_data;
321   char buf[BUFSIZ];
322   gsize bytes_read;
323   GError *error;
324   GIOStatus status;
325
326   error = NULL;
327 read:
328   status = g_io_channel_read_chars (channel, buf, sizeof (buf), &bytes_read, &error);
329   if (status == G_IO_STATUS_NORMAL)
330    {
331      g_string_append_len (data->error_string, buf, bytes_read);
332      if (bytes_read == sizeof (buf))
333         goto read;
334    }
335   else if (status == G_IO_STATUS_EOF)
336     g_string_append_len (data->error_string, buf, bytes_read);
337   else if (status == G_IO_STATUS_ERROR)
338     {
339       if (data->error_string->len > 0)
340         g_string_append (data->error_string, "\n");
341
342       g_string_append (data->error_string, error->message);
343       g_error_free (error);
344
345       if (data->error_channel_source)
346         {
347           g_source_unref (data->error_channel_source);
348           data->error_channel_source = NULL;
349         }
350       return FALSE;
351     }
352
353   return TRUE;
354 }
355
356 static void
357 eject_mount_do (GVolume             *volume,
358                 GCancellable        *cancellable,
359                 GAsyncReadyCallback  callback,
360                 gpointer             user_data,
361                 char               **argv)
362 {
363   GUnixVolume *unix_volume = G_UNIX_VOLUME (volume);
364   EjectMountOp *data;
365   GPid child_pid;
366   GSource *child_watch;
367   GError *error;
368   
369   data = g_new0 (EjectMountOp, 1);
370   data->unix_volume = unix_volume;
371   data->callback = callback;
372   data->user_data = user_data;
373   data->cancellable = cancellable;
374   
375   error = NULL;
376   if (!g_spawn_async_with_pipes (NULL,         /* working dir */
377                                  argv,
378                                  NULL,         /* envp */
379                                  G_SPAWN_DO_NOT_REAP_CHILD|G_SPAWN_SEARCH_PATH,
380                                  NULL,         /* child_setup */
381                                  NULL,         /* user_data for child_setup */
382                                  &child_pid,
383                                  NULL,           /* standard_input */
384                                  NULL,           /* standard_output */
385                                  &(data->error_fd),
386                                  &error))
387     {
388       g_assert (error != NULL);
389       goto handle_error;
390     }
391
392   data->error_string = g_string_new ("");
393
394   data->error_channel = g_io_channel_unix_new (data->error_fd);
395   g_io_channel_set_flags (data->error_channel, G_IO_FLAG_NONBLOCK, &error);
396   if (error != NULL)
397     goto handle_error;
398
399   data->error_channel_source = g_io_create_watch (data->error_channel, G_IO_IN);
400   g_source_set_callback (data->error_channel_source,
401                          (GSourceFunc) eject_mount_read_error, data, NULL);
402   g_source_attach (data->error_channel_source, g_main_context_get_thread_default ());
403
404   child_watch = g_child_watch_source_new (child_pid);
405   g_source_set_callback (child_watch, (GSourceFunc) eject_mount_cb, data, NULL);
406   g_source_attach (child_watch, g_main_context_get_thread_default ());
407   g_source_unref (child_watch);
408
409 handle_error:
410   if (error != NULL)
411     {
412       GSimpleAsyncResult *simple;
413       simple = g_simple_async_result_new_take_error (G_OBJECT (data->unix_volume),
414                                                      data->callback,
415                                                      data->user_data,
416                                                      error);
417       g_simple_async_result_complete (simple);
418       g_object_unref (simple);
419
420       if (data->error_string != NULL)
421         g_string_free (data->error_string, TRUE);
422
423       if (data->error_channel != NULL)
424         g_io_channel_unref (data->error_channel);
425
426       g_free (data);
427     }
428 }
429
430
431 static void
432 g_unix_volume_mount (GVolume            *volume,
433                      GMountMountFlags    flags,
434                      GMountOperation     *mount_operation,
435                      GCancellable        *cancellable,
436                      GAsyncReadyCallback  callback,
437                      gpointer             user_data)
438 {
439   GUnixVolume *unix_volume = G_UNIX_VOLUME (volume);
440   char *argv[] = { "mount", NULL, NULL };
441
442   if (unix_volume->mount_path != NULL)
443     argv[1] = unix_volume->mount_path;
444   else
445     argv[1] = unix_volume->device_path;
446
447   eject_mount_do (volume, cancellable, callback, user_data, argv);
448 }
449
450 static gboolean
451 g_unix_volume_mount_finish (GVolume        *volume,
452                             GAsyncResult  *result,
453                             GError       **error)
454 {
455   return TRUE;
456 }
457
458 static void
459 g_unix_volume_eject (GVolume             *volume,
460                      GMountUnmountFlags   flags,
461                      GCancellable        *cancellable,
462                      GAsyncReadyCallback  callback,
463                      gpointer             user_data)
464 {
465   GUnixVolume *unix_volume = G_UNIX_VOLUME (volume);
466   char *argv[] = { "eject", NULL, NULL };
467
468   argv[1] = unix_volume->device_path;
469
470   eject_mount_do (volume, cancellable, callback, user_data, argv);
471 }
472
473 static gboolean
474 g_unix_volume_eject_finish (GVolume       *volume,
475                             GAsyncResult  *result,
476                             GError       **error)
477 {
478   return TRUE;
479 }
480
481 static gchar *
482 g_unix_volume_get_identifier (GVolume     *volume,
483                               const gchar *kind)
484 {
485   GUnixVolume *unix_volume = G_UNIX_VOLUME (volume);
486
487   if (unix_volume->identifier_type != NULL &&
488       strcmp (kind, unix_volume->identifier_type) == 0)
489     return g_strdup (unix_volume->identifier);
490
491   return NULL;
492 }
493
494 static gchar **
495 g_unix_volume_enumerate_identifiers (GVolume *volume)
496 {
497   GUnixVolume *unix_volume = G_UNIX_VOLUME (volume);
498   gchar **res;
499
500   if (unix_volume->identifier_type)
501     {
502       res = g_new (gchar *, 2);
503       res[0] = g_strdup (unix_volume->identifier_type);
504       res[1] = NULL;
505     }
506   else
507     {
508       res = g_new (gchar *, 1);
509       res[0] = NULL;
510     }
511
512   return res;
513 }
514
515 static void
516 g_unix_volume_volume_iface_init (GVolumeIface *iface)
517 {
518   iface->get_name = g_unix_volume_get_name;
519   iface->get_icon = g_unix_volume_get_icon;
520   iface->get_uuid = g_unix_volume_get_uuid;
521   iface->get_drive = g_unix_volume_get_drive;
522   iface->get_mount = g_unix_volume_get_mount;
523   iface->can_mount = g_unix_volume_can_mount;
524   iface->can_eject = g_unix_volume_can_eject;
525   iface->should_automount = g_unix_volume_should_automount;
526   iface->mount_fn = g_unix_volume_mount;
527   iface->mount_finish = g_unix_volume_mount_finish;
528   iface->eject = g_unix_volume_eject;
529   iface->eject_finish = g_unix_volume_eject_finish;
530   iface->get_identifier = g_unix_volume_get_identifier;
531   iface->enumerate_identifiers = g_unix_volume_enumerate_identifiers;
532 }