Fix the build
[glib.git] / gio / gunixvolume.c
blobf03d4a6c55c1924332245976a25f71861aff0f00
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 "gsimpleasyncresult.h"
40 #include "gioerror.h"
41 #include "glibintl.h"
43 #include "gioalias.h"
45 struct _GUnixVolume {
46 GObject parent;
48 GVolumeMonitor *volume_monitor;
49 GUnixMount *mount; /* owned by volume monitor */
51 char *device_path;
52 char *mount_path;
53 gboolean can_eject;
55 char *identifier;
56 char *identifier_type;
58 char *name;
59 GIcon *icon;
62 static void g_unix_volume_volume_iface_init (GVolumeIface *iface);
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))
69 static void
70 g_unix_volume_finalize (GObject *object)
72 GUnixVolume *volume;
74 volume = G_UNIX_VOLUME (object);
76 if (volume->volume_monitor != NULL)
77 g_object_unref (volume->volume_monitor);
79 if (volume->mount)
80 _g_unix_mount_unset_volume (volume->mount, volume);
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);
89 G_OBJECT_CLASS (g_unix_volume_parent_class)->finalize (object);
92 static void
93 g_unix_volume_class_init (GUnixVolumeClass *klass)
95 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
97 gobject_class->finalize = g_unix_volume_finalize;
100 static void
101 g_unix_volume_init (GUnixVolume *unix_volume)
106 * g_unix_volume_new:
107 * @volume_monitor: a #GVolumeMonitor.
108 * @mountpoint: a #GUnixMountPoint.
110 * Returns: a #GUnixVolume for the given #GUnixMountPoint.
112 GUnixVolume *
113 _g_unix_volume_new (GVolumeMonitor *volume_monitor,
114 GUnixMountPoint *mountpoint)
116 GUnixVolume *volume;
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;
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);
129 volume->name = g_unix_mount_point_guess_name (mountpoint);
130 volume->icon = g_unix_mount_point_guess_icon (mountpoint);
133 if (strcmp (g_unix_mount_point_get_fs_type (mountpoint), "nfs") == 0)
135 volume->identifier_type = g_strdup (G_VOLUME_IDENTIFIER_KIND_NFS_MOUNT);
136 volume->identifier = g_strdup (volume->device_path);
138 else if (g_str_has_prefix (volume->device_path, "LABEL="))
140 volume->identifier_type = g_strdup (G_VOLUME_IDENTIFIER_KIND_LABEL);
141 volume->identifier = g_strdup (volume->device_path + 6);
143 else if (g_str_has_prefix (volume->device_path, "UUID="))
145 volume->identifier_type = g_strdup (G_VOLUME_IDENTIFIER_KIND_UUID);
146 volume->identifier = g_strdup (volume->device_path + 5);
148 else if (g_path_is_absolute (volume->device_path))
150 volume->identifier_type = g_strdup (G_VOLUME_IDENTIFIER_KIND_UNIX_DEVICE);
151 volume->identifier = g_strdup (volume->device_path);
154 return volume;
158 * g_unix_volume_disconnected:
159 * @volume:
162 void
163 _g_unix_volume_disconnected (GUnixVolume *volume)
165 if (volume->mount)
167 _g_unix_mount_unset_volume (volume->mount, volume);
168 volume->mount = NULL;
173 * g_unix_volume_set_mount:
174 * @volume:
175 * @mount:
178 void
179 _g_unix_volume_set_mount (GUnixVolume *volume,
180 GUnixMount *mount)
182 if (volume->mount == mount)
183 return;
185 if (volume->mount)
186 _g_unix_mount_unset_volume (volume->mount, volume);
188 volume->mount = mount;
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);
197 * g_unix_volume_unset_mount:
198 * @volume:
199 * @mount:
202 void
203 _g_unix_volume_unset_mount (GUnixVolume *volume,
204 GUnixMount *mount)
206 if (volume->mount == mount)
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);
216 static GIcon *
217 g_unix_volume_get_icon (GVolume *volume)
219 GUnixVolume *unix_volume = G_UNIX_VOLUME (volume);
220 return g_object_ref (unix_volume->icon);
223 static char *
224 g_unix_volume_get_name (GVolume *volume)
226 GUnixVolume *unix_volume = G_UNIX_VOLUME (volume);
227 return g_strdup (unix_volume->name);
230 static char *
231 g_unix_volume_get_uuid (GVolume *volume)
233 return NULL;
236 static gboolean
237 g_unix_volume_can_mount (GVolume *volume)
239 return TRUE;
242 static gboolean
243 g_unix_volume_can_eject (GVolume *volume)
245 GUnixVolume *unix_volume = G_UNIX_VOLUME (volume);
246 return unix_volume->can_eject;
249 static gboolean
250 g_unix_volume_should_automount (GVolume *volume)
252 /* We automount all local volumes because we don't even
253 make the internal stuff visible */
254 return TRUE;
257 static GDrive *
258 g_unix_volume_get_drive (GVolume *volume)
260 return NULL;
263 static GMount *
264 g_unix_volume_get_mount (GVolume *volume)
266 GUnixVolume *unix_volume = G_UNIX_VOLUME (volume);
268 if (unix_volume->mount != NULL)
269 return g_object_ref (unix_volume->mount);
271 return NULL;
275 gboolean
276 _g_unix_volume_has_mount_path (GUnixVolume *volume,
277 const char *mount_path)
279 return strcmp (volume->mount_path, mount_path) == 0;
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;
294 static void
295 eject_mount_cb (GPid pid, gint status, gpointer user_data)
297 EjectMountOp *data = user_data;
298 GSimpleAsyncResult *simple;
300 if (WEXITSTATUS (status) != 0)
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);
312 else
314 simple = g_simple_async_result_new (G_OBJECT (data->unix_volume),
315 data->callback,
316 data->user_data,
317 NULL);
320 g_simple_async_result_complete (simple);
321 g_object_unref (simple);
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);
331 static gboolean
332 eject_mount_read_error (GIOChannel *channel,
333 GIOCondition condition,
334 gpointer user_data)
336 char *str;
337 gsize str_len;
338 EjectMountOp *data = user_data;
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;
346 static void
347 eject_mount_do (GVolume *volume,
348 GCancellable *cancellable,
349 GAsyncReadyCallback callback,
350 gpointer user_data,
351 char **argv)
353 GUnixVolume *unix_volume = G_UNIX_VOLUME (volume);
354 EjectMountOp *data;
355 GPid child_pid;
356 GError *error;
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;
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;
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);
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)
402 GUnixVolume *unix_volume = G_UNIX_VOLUME (volume);
403 char *argv[] = {"mount", NULL, NULL};
405 if (unix_volume->mount_path != NULL)
406 argv[1] = unix_volume->mount_path;
407 else
408 argv[1] = unix_volume->device_path;
410 eject_mount_do (volume, cancellable, callback, user_data, argv);
413 static gboolean
414 g_unix_volume_mount_finish (GVolume *volume,
415 GAsyncResult *result,
416 GError **error)
418 return TRUE;
421 static void
422 g_unix_volume_eject (GVolume *volume,
423 GMountUnmountFlags flags,
424 GCancellable *cancellable,
425 GAsyncReadyCallback callback,
426 gpointer user_data)
428 GUnixVolume *unix_volume = G_UNIX_VOLUME (volume);
429 char *argv[] = {"eject", NULL, NULL};
431 argv[1] = unix_volume->device_path;
433 eject_mount_do (volume, cancellable, callback, user_data, argv);
436 static gboolean
437 g_unix_volume_eject_finish (GVolume *volume,
438 GAsyncResult *result,
439 GError **error)
441 return TRUE;
444 static char *
445 g_unix_volume_get_identifier (GVolume *volume,
446 const char *kind)
448 GUnixVolume *unix_volume = G_UNIX_VOLUME (volume);
450 if (strcmp (kind, unix_volume->identifier_type) == 0)
451 return g_strdup (unix_volume->identifier);
452 return NULL;
455 static char **
456 g_unix_volume_enumerate_identifiers (GVolume *volume)
458 GUnixVolume *unix_volume = G_UNIX_VOLUME (volume);
459 char **res;
461 if (unix_volume->identifier_type)
463 res = g_new (char *, 2);
464 res[0] = g_strdup (unix_volume->identifier_type);
465 res[1] = NULL;
467 else
469 res = g_new (char *, 1);
470 res[0] = NULL;
473 return res;
476 static void
477 g_unix_volume_volume_iface_init (GVolumeIface *iface)
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;