GSettings: small internal refactor
[glib.git] / gio / gunixvolume.c
blob24f14d5561a478cf0b2391b2653649829619a0d2
1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
3 /* GIO - GLib Input, Output and Streaming Library
4 *
5 * Copyright (C) 2006-2007 Red Hat, Inc.
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.
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.
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.
22 * Author: Alexander Larsson <alexl@redhat.com>
23 * David Zeuthen <davidz@redhat.com>
26 #include "config.h"
28 #include <string.h>
29 #include <sys/wait.h>
30 #include <unistd.h>
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 "gtask.h"
40 #include "gioerror.h"
41 #include "glibintl.h"
42 /* for BUFSIZ */
43 #include <stdio.h>
46 struct _GUnixVolume {
47 GObject parent;
49 GVolumeMonitor *volume_monitor;
50 GUnixMount *mount; /* owned by volume monitor */
52 char *device_path;
53 char *mount_path;
54 gboolean can_eject;
56 char *identifier;
57 char *identifier_type;
59 char *name;
60 GIcon *icon;
61 GIcon *symbolic_icon;
64 static void g_unix_volume_volume_iface_init (GVolumeIface *iface);
66 #define g_unix_volume_get_type _g_unix_volume_get_type
67 G_DEFINE_TYPE_WITH_CODE (GUnixVolume, g_unix_volume, G_TYPE_OBJECT,
68 G_IMPLEMENT_INTERFACE (G_TYPE_VOLUME,
69 g_unix_volume_volume_iface_init))
71 static void
72 g_unix_volume_finalize (GObject *object)
74 GUnixVolume *volume;
76 volume = G_UNIX_VOLUME (object);
78 if (volume->volume_monitor != NULL)
79 g_object_unref (volume->volume_monitor);
81 if (volume->mount)
82 _g_unix_mount_unset_volume (volume->mount, volume);
84 g_object_unref (volume->icon);
85 g_object_unref (volume->symbolic_icon);
86 g_free (volume->name);
87 g_free (volume->mount_path);
88 g_free (volume->device_path);
89 g_free (volume->identifier);
90 g_free (volume->identifier_type);
92 G_OBJECT_CLASS (g_unix_volume_parent_class)->finalize (object);
95 static void
96 g_unix_volume_class_init (GUnixVolumeClass *klass)
98 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
100 gobject_class->finalize = g_unix_volume_finalize;
103 static void
104 g_unix_volume_init (GUnixVolume *unix_volume)
108 GUnixVolume *
109 _g_unix_volume_new (GVolumeMonitor *volume_monitor,
110 GUnixMountPoint *mountpoint)
112 GUnixVolume *volume;
114 if (!(g_unix_mount_point_is_user_mountable (mountpoint) ||
115 g_str_has_prefix (g_unix_mount_point_get_device_path (mountpoint), "/vol/")) ||
116 g_unix_mount_point_is_loopback (mountpoint))
117 return NULL;
119 volume = g_object_new (G_TYPE_UNIX_VOLUME, NULL);
120 volume->volume_monitor = volume_monitor != NULL ? g_object_ref (volume_monitor) : NULL;
121 volume->mount_path = g_strdup (g_unix_mount_point_get_mount_path (mountpoint));
122 volume->device_path = g_strdup (g_unix_mount_point_get_device_path (mountpoint));
123 volume->can_eject = g_unix_mount_point_guess_can_eject (mountpoint);
125 volume->name = g_unix_mount_point_guess_name (mountpoint);
126 volume->icon = g_unix_mount_point_guess_icon (mountpoint);
127 volume->symbolic_icon = g_unix_mount_point_guess_symbolic_icon (mountpoint);
130 if (strcmp (g_unix_mount_point_get_fs_type (mountpoint), "nfs") == 0)
132 volume->identifier_type = g_strdup (G_VOLUME_IDENTIFIER_KIND_NFS_MOUNT);
133 volume->identifier = g_strdup (volume->device_path);
135 else if (g_str_has_prefix (volume->device_path, "LABEL="))
137 volume->identifier_type = g_strdup (G_VOLUME_IDENTIFIER_KIND_LABEL);
138 volume->identifier = g_strdup (volume->device_path + 6);
140 else if (g_str_has_prefix (volume->device_path, "UUID="))
142 volume->identifier_type = g_strdup (G_VOLUME_IDENTIFIER_KIND_UUID);
143 volume->identifier = g_strdup (volume->device_path + 5);
145 else if (g_path_is_absolute (volume->device_path))
147 volume->identifier_type = g_strdup (G_VOLUME_IDENTIFIER_KIND_UNIX_DEVICE);
148 volume->identifier = g_strdup (volume->device_path);
151 return volume;
154 void
155 _g_unix_volume_disconnected (GUnixVolume *volume)
157 if (volume->mount)
159 _g_unix_mount_unset_volume (volume->mount, volume);
160 volume->mount = NULL;
164 void
165 _g_unix_volume_set_mount (GUnixVolume *volume,
166 GUnixMount *mount)
168 if (volume->mount == mount)
169 return;
171 if (volume->mount)
172 _g_unix_mount_unset_volume (volume->mount, volume);
174 volume->mount = mount;
176 /* TODO: Emit changed in idle to avoid locking issues */
177 g_signal_emit_by_name (volume, "changed");
178 if (volume->volume_monitor != NULL)
179 g_signal_emit_by_name (volume->volume_monitor, "volume-changed", volume);
182 void
183 _g_unix_volume_unset_mount (GUnixVolume *volume,
184 GUnixMount *mount)
186 if (volume->mount == mount)
188 volume->mount = NULL;
189 /* TODO: Emit changed in idle to avoid locking issues */
190 g_signal_emit_by_name (volume, "changed");
191 if (volume->volume_monitor != NULL)
192 g_signal_emit_by_name (volume->volume_monitor, "volume-changed", volume);
196 static GIcon *
197 g_unix_volume_get_icon (GVolume *volume)
199 GUnixVolume *unix_volume = G_UNIX_VOLUME (volume);
200 return g_object_ref (unix_volume->icon);
203 static GIcon *
204 g_unix_volume_get_symbolic_icon (GVolume *volume)
206 GUnixVolume *unix_volume = G_UNIX_VOLUME (volume);
207 return g_object_ref (unix_volume->symbolic_icon);
210 static char *
211 g_unix_volume_get_name (GVolume *volume)
213 GUnixVolume *unix_volume = G_UNIX_VOLUME (volume);
214 return g_strdup (unix_volume->name);
217 static char *
218 g_unix_volume_get_uuid (GVolume *volume)
220 return NULL;
223 static gboolean
224 g_unix_volume_can_mount (GVolume *volume)
226 return TRUE;
229 static gboolean
230 g_unix_volume_can_eject (GVolume *volume)
232 GUnixVolume *unix_volume = G_UNIX_VOLUME (volume);
233 return unix_volume->can_eject;
236 static gboolean
237 g_unix_volume_should_automount (GVolume *volume)
239 /* We automount all local volumes because we don't even
240 * make the internal stuff visible
242 return TRUE;
245 static GDrive *
246 g_unix_volume_get_drive (GVolume *volume)
248 return NULL;
251 static GMount *
252 g_unix_volume_get_mount (GVolume *volume)
254 GUnixVolume *unix_volume = G_UNIX_VOLUME (volume);
256 if (unix_volume->mount != NULL)
257 return g_object_ref (unix_volume->mount);
259 return NULL;
263 gboolean
264 _g_unix_volume_has_mount_path (GUnixVolume *volume,
265 const char *mount_path)
267 return strcmp (volume->mount_path, mount_path) == 0;
271 typedef struct {
272 GUnixVolume *unix_volume;
273 int error_fd;
274 GIOChannel *error_channel;
275 GSource *error_channel_source;
276 GString *error_string;
277 } EjectMountOp;
279 static void
280 eject_mount_op_free (EjectMountOp *data)
282 if (data->error_string != NULL)
283 g_string_free (data->error_string, TRUE);
285 if (data->error_channel != NULL)
286 g_io_channel_unref (data->error_channel);
288 if (data->error_channel_source)
290 g_source_destroy (data->error_channel_source);
291 g_source_unref (data->error_channel_source);
294 if (data->error_fd != -1)
295 close (data->error_fd);
297 g_free (data);
300 static void
301 eject_mount_cb (GPid pid,
302 gint status,
303 gpointer user_data)
305 GTask *task = user_data;
306 EjectMountOp *data = g_task_get_task_data (task);
308 if (WEXITSTATUS (status) != 0)
310 g_task_return_new_error (task,
311 G_IO_ERROR,
312 G_IO_ERROR_FAILED,
313 "%s", data->error_string->str);
315 else
316 g_task_return_boolean (task, TRUE);
317 g_object_unref (task);
320 static gboolean
321 eject_mount_read_error (GIOChannel *channel,
322 GIOCondition condition,
323 gpointer user_data)
325 GTask *task = user_data;
326 EjectMountOp *data = g_task_get_task_data (task);
327 char buf[BUFSIZ];
328 gsize bytes_read;
329 GError *error;
330 GIOStatus status;
332 error = NULL;
333 read:
334 status = g_io_channel_read_chars (channel, buf, sizeof (buf), &bytes_read, &error);
335 if (status == G_IO_STATUS_NORMAL)
337 g_string_append_len (data->error_string, buf, bytes_read);
338 if (bytes_read == sizeof (buf))
339 goto read;
341 else if (status == G_IO_STATUS_EOF)
342 g_string_append_len (data->error_string, buf, bytes_read);
343 else if (status == G_IO_STATUS_ERROR)
345 if (data->error_string->len > 0)
346 g_string_append (data->error_string, "\n");
348 g_string_append (data->error_string, error->message);
349 g_error_free (error);
351 if (data->error_channel_source)
353 g_source_unref (data->error_channel_source);
354 data->error_channel_source = NULL;
356 return FALSE;
359 return TRUE;
362 static void
363 eject_mount_do (GVolume *volume,
364 GCancellable *cancellable,
365 GAsyncReadyCallback callback,
366 gpointer user_data,
367 char **argv)
369 GUnixVolume *unix_volume = G_UNIX_VOLUME (volume);
370 GTask *task;
371 EjectMountOp *data;
372 GPid child_pid;
373 GSource *child_watch;
374 GError *error;
376 data = g_new0 (EjectMountOp, 1);
377 data->unix_volume = unix_volume;
378 data->error_fd = -1;
380 task = g_task_new (unix_volume, cancellable, callback, user_data);
381 g_task_set_task_data (task, data, (GDestroyNotify) eject_mount_op_free);
383 error = NULL;
384 if (!g_spawn_async_with_pipes (NULL, /* working dir */
385 argv,
386 NULL, /* envp */
387 G_SPAWN_DO_NOT_REAP_CHILD|G_SPAWN_SEARCH_PATH,
388 NULL, /* child_setup */
389 NULL, /* user_data for child_setup */
390 &child_pid,
391 NULL, /* standard_input */
392 NULL, /* standard_output */
393 &(data->error_fd),
394 &error))
396 g_assert (error != NULL);
397 goto handle_error;
400 data->error_string = g_string_new ("");
402 data->error_channel = g_io_channel_unix_new (data->error_fd);
403 g_io_channel_set_flags (data->error_channel, G_IO_FLAG_NONBLOCK, &error);
404 if (error != NULL)
405 goto handle_error;
407 data->error_channel_source = g_io_create_watch (data->error_channel, G_IO_IN);
408 g_task_attach_source (task, data->error_channel_source,
409 (GSourceFunc) eject_mount_read_error);
411 child_watch = g_child_watch_source_new (child_pid);
412 g_task_attach_source (task, child_watch, (GSourceFunc) eject_mount_cb);
413 g_source_unref (child_watch);
415 handle_error:
416 if (error != NULL)
418 g_task_return_error (task, error);
419 g_object_unref (task);
424 static void
425 g_unix_volume_mount (GVolume *volume,
426 GMountMountFlags flags,
427 GMountOperation *mount_operation,
428 GCancellable *cancellable,
429 GAsyncReadyCallback callback,
430 gpointer user_data)
432 GUnixVolume *unix_volume = G_UNIX_VOLUME (volume);
433 char *argv[] = { "mount", NULL, NULL };
435 if (unix_volume->mount_path != NULL)
436 argv[1] = unix_volume->mount_path;
437 else
438 argv[1] = unix_volume->device_path;
440 eject_mount_do (volume, cancellable, callback, user_data, argv);
443 static gboolean
444 g_unix_volume_mount_finish (GVolume *volume,
445 GAsyncResult *result,
446 GError **error)
448 return TRUE;
451 static void
452 g_unix_volume_eject (GVolume *volume,
453 GMountUnmountFlags flags,
454 GCancellable *cancellable,
455 GAsyncReadyCallback callback,
456 gpointer user_data)
458 GUnixVolume *unix_volume = G_UNIX_VOLUME (volume);
459 char *argv[] = { "eject", NULL, NULL };
461 argv[1] = unix_volume->device_path;
463 eject_mount_do (volume, cancellable, callback, user_data, argv);
466 static gboolean
467 g_unix_volume_eject_finish (GVolume *volume,
468 GAsyncResult *result,
469 GError **error)
471 return TRUE;
474 static gchar *
475 g_unix_volume_get_identifier (GVolume *volume,
476 const gchar *kind)
478 GUnixVolume *unix_volume = G_UNIX_VOLUME (volume);
480 if (unix_volume->identifier_type != NULL &&
481 strcmp (kind, unix_volume->identifier_type) == 0)
482 return g_strdup (unix_volume->identifier);
484 return NULL;
487 static gchar **
488 g_unix_volume_enumerate_identifiers (GVolume *volume)
490 GUnixVolume *unix_volume = G_UNIX_VOLUME (volume);
491 gchar **res;
493 if (unix_volume->identifier_type)
495 res = g_new (gchar *, 2);
496 res[0] = g_strdup (unix_volume->identifier_type);
497 res[1] = NULL;
499 else
501 res = g_new (gchar *, 1);
502 res[0] = NULL;
505 return res;
508 static void
509 g_unix_volume_volume_iface_init (GVolumeIface *iface)
511 iface->get_name = g_unix_volume_get_name;
512 iface->get_icon = g_unix_volume_get_icon;
513 iface->get_symbolic_icon = g_unix_volume_get_symbolic_icon;
514 iface->get_uuid = g_unix_volume_get_uuid;
515 iface->get_drive = g_unix_volume_get_drive;
516 iface->get_mount = g_unix_volume_get_mount;
517 iface->can_mount = g_unix_volume_can_mount;
518 iface->can_eject = g_unix_volume_can_eject;
519 iface->should_automount = g_unix_volume_should_automount;
520 iface->mount_fn = g_unix_volume_mount;
521 iface->mount_finish = g_unix_volume_mount_finish;
522 iface->eject = g_unix_volume_eject;
523 iface->eject_finish = g_unix_volume_eject_finish;
524 iface->get_identifier = g_unix_volume_get_identifier;
525 iface->enumerate_identifiers = g_unix_volume_enumerate_identifiers;