Add some more cases to the app-id unit tests
[glib.git] / gio / gunixvolume.c
blob6b58f23e0a2345b439fdda6f00da33be7b84e2f8
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, see <http://www.gnu.org/licenses/>.
20 * Author: Alexander Larsson <alexl@redhat.com>
21 * David Zeuthen <davidz@redhat.com>
24 #include "config.h"
26 #include <string.h>
27 #include <sys/wait.h>
28 #include <unistd.h>
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>
44 struct _GUnixVolume {
45 GObject parent;
47 GVolumeMonitor *volume_monitor;
48 GUnixMount *mount; /* owned by volume monitor */
50 char *device_path;
51 char *mount_path;
52 gboolean can_eject;
54 char *identifier;
55 char *identifier_type;
57 char *name;
58 GIcon *icon;
59 GIcon *symbolic_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_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);
90 G_OBJECT_CLASS (g_unix_volume_parent_class)->finalize (object);
93 static void
94 g_unix_volume_class_init (GUnixVolumeClass *klass)
96 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
98 gobject_class->finalize = g_unix_volume_finalize;
101 static void
102 g_unix_volume_init (GUnixVolume *unix_volume)
106 GUnixVolume *
107 _g_unix_volume_new (GVolumeMonitor *volume_monitor,
108 GUnixMountPoint *mountpoint)
110 GUnixVolume *volume;
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;
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);
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);
128 if (strcmp (g_unix_mount_point_get_fs_type (mountpoint), "nfs") == 0)
130 volume->identifier_type = g_strdup (G_VOLUME_IDENTIFIER_KIND_NFS_MOUNT);
131 volume->identifier = g_strdup (volume->device_path);
133 else if (g_str_has_prefix (volume->device_path, "LABEL="))
135 volume->identifier_type = g_strdup (G_VOLUME_IDENTIFIER_KIND_LABEL);
136 volume->identifier = g_strdup (volume->device_path + 6);
138 else if (g_str_has_prefix (volume->device_path, "UUID="))
140 volume->identifier_type = g_strdup (G_VOLUME_IDENTIFIER_KIND_UUID);
141 volume->identifier = g_strdup (volume->device_path + 5);
143 else if (g_path_is_absolute (volume->device_path))
145 volume->identifier_type = g_strdup (G_VOLUME_IDENTIFIER_KIND_UNIX_DEVICE);
146 volume->identifier = g_strdup (volume->device_path);
149 return volume;
152 void
153 _g_unix_volume_disconnected (GUnixVolume *volume)
155 if (volume->mount)
157 _g_unix_mount_unset_volume (volume->mount, volume);
158 volume->mount = NULL;
162 void
163 _g_unix_volume_set_mount (GUnixVolume *volume,
164 GUnixMount *mount)
166 if (volume->mount == mount)
167 return;
169 if (volume->mount)
170 _g_unix_mount_unset_volume (volume->mount, volume);
172 volume->mount = mount;
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);
180 void
181 _g_unix_volume_unset_mount (GUnixVolume *volume,
182 GUnixMount *mount)
184 if (volume->mount == mount)
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);
194 static GIcon *
195 g_unix_volume_get_icon (GVolume *volume)
197 GUnixVolume *unix_volume = G_UNIX_VOLUME (volume);
198 return g_object_ref (unix_volume->icon);
201 static GIcon *
202 g_unix_volume_get_symbolic_icon (GVolume *volume)
204 GUnixVolume *unix_volume = G_UNIX_VOLUME (volume);
205 return g_object_ref (unix_volume->symbolic_icon);
208 static char *
209 g_unix_volume_get_name (GVolume *volume)
211 GUnixVolume *unix_volume = G_UNIX_VOLUME (volume);
212 return g_strdup (unix_volume->name);
215 static char *
216 g_unix_volume_get_uuid (GVolume *volume)
218 return NULL;
221 static gboolean
222 g_unix_volume_can_mount (GVolume *volume)
224 return TRUE;
227 static gboolean
228 g_unix_volume_can_eject (GVolume *volume)
230 GUnixVolume *unix_volume = G_UNIX_VOLUME (volume);
231 return unix_volume->can_eject;
234 static gboolean
235 g_unix_volume_should_automount (GVolume *volume)
237 /* We automount all local volumes because we don't even
238 * make the internal stuff visible
240 return TRUE;
243 static GDrive *
244 g_unix_volume_get_drive (GVolume *volume)
246 return NULL;
249 static GMount *
250 g_unix_volume_get_mount (GVolume *volume)
252 GUnixVolume *unix_volume = G_UNIX_VOLUME (volume);
254 if (unix_volume->mount != NULL)
255 return g_object_ref (unix_volume->mount);
257 return NULL;
261 gboolean
262 _g_unix_volume_has_mount_path (GUnixVolume *volume,
263 const char *mount_path)
265 return strcmp (volume->mount_path, mount_path) == 0;
268 static void
269 eject_mount_done (GObject *source,
270 GAsyncResult *result,
271 gpointer user_data)
273 GSubprocess *subprocess = G_SUBPROCESS (source);
274 GTask *task = user_data;
275 GError *error = NULL;
276 gchar *stderr_str;
278 if (!g_subprocess_communicate_utf8_finish (subprocess, result, NULL, &stderr_str, &error))
280 g_task_return_error (task, error);
281 g_error_free (error);
283 else /* successful communication */
285 if (!g_subprocess_get_successful (subprocess))
286 /* ...but bad exit code */
287 g_task_return_new_error (task, G_IO_ERROR, G_IO_ERROR_FAILED, "%s", stderr_str);
288 else
289 /* ...and successful exit code */
290 g_task_return_boolean (task, TRUE);
292 g_free (stderr_str);
295 g_object_unref (task);
298 static void
299 eject_mount_do (GVolume *volume,
300 GCancellable *cancellable,
301 GAsyncReadyCallback callback,
302 gpointer user_data,
303 const gchar * const *argv)
305 GSubprocess *subprocess;
306 GError *error = NULL;
307 GTask *task;
309 task = g_task_new (volume, cancellable, callback, user_data);
310 g_task_set_source_tag (task, eject_mount_do);
312 if (g_task_return_error_if_cancelled (task))
314 g_object_unref (task);
315 return;
318 subprocess = g_subprocess_newv (argv, G_SUBPROCESS_FLAGS_STDOUT_SILENCE | G_SUBPROCESS_FLAGS_STDERR_PIPE, &error);
319 g_assert_no_error (error);
321 g_subprocess_communicate_utf8_async (subprocess, NULL,
322 g_task_get_cancellable (task),
323 eject_mount_done, task);
326 static void
327 g_unix_volume_mount (GVolume *volume,
328 GMountMountFlags flags,
329 GMountOperation *mount_operation,
330 GCancellable *cancellable,
331 GAsyncReadyCallback callback,
332 gpointer user_data)
334 GUnixVolume *unix_volume = G_UNIX_VOLUME (volume);
335 const gchar *argv[] = { "mount", NULL, NULL };
337 if (unix_volume->mount_path != NULL)
338 argv[1] = unix_volume->mount_path;
339 else
340 argv[1] = unix_volume->device_path;
342 eject_mount_do (volume, cancellable, callback, user_data, argv);
345 static gboolean
346 g_unix_volume_mount_finish (GVolume *volume,
347 GAsyncResult *result,
348 GError **error)
350 g_return_val_if_fail (g_task_is_valid (result, volume), FALSE);
352 return g_task_propagate_boolean (G_TASK (result), error);
355 static void
356 g_unix_volume_eject (GVolume *volume,
357 GMountUnmountFlags flags,
358 GCancellable *cancellable,
359 GAsyncReadyCallback callback,
360 gpointer user_data)
362 GUnixVolume *unix_volume = G_UNIX_VOLUME (volume);
363 const gchar *argv[] = { "eject", NULL, NULL };
365 argv[1] = unix_volume->device_path;
367 eject_mount_do (volume, cancellable, callback, user_data, argv);
370 static gboolean
371 g_unix_volume_eject_finish (GVolume *volume,
372 GAsyncResult *result,
373 GError **error)
375 g_return_val_if_fail (g_task_is_valid (result, volume), FALSE);
377 return g_task_propagate_boolean (G_TASK (result), error);
380 static gchar *
381 g_unix_volume_get_identifier (GVolume *volume,
382 const gchar *kind)
384 GUnixVolume *unix_volume = G_UNIX_VOLUME (volume);
386 if (unix_volume->identifier_type != NULL &&
387 strcmp (kind, unix_volume->identifier_type) == 0)
388 return g_strdup (unix_volume->identifier);
390 return NULL;
393 static gchar **
394 g_unix_volume_enumerate_identifiers (GVolume *volume)
396 GUnixVolume *unix_volume = G_UNIX_VOLUME (volume);
397 gchar **res;
399 if (unix_volume->identifier_type)
401 res = g_new (gchar *, 2);
402 res[0] = g_strdup (unix_volume->identifier_type);
403 res[1] = NULL;
405 else
407 res = g_new (gchar *, 1);
408 res[0] = NULL;
411 return res;
414 static void
415 g_unix_volume_volume_iface_init (GVolumeIface *iface)
417 iface->get_name = g_unix_volume_get_name;
418 iface->get_icon = g_unix_volume_get_icon;
419 iface->get_symbolic_icon = g_unix_volume_get_symbolic_icon;
420 iface->get_uuid = g_unix_volume_get_uuid;
421 iface->get_drive = g_unix_volume_get_drive;
422 iface->get_mount = g_unix_volume_get_mount;
423 iface->can_mount = g_unix_volume_can_mount;
424 iface->can_eject = g_unix_volume_can_eject;
425 iface->should_automount = g_unix_volume_should_automount;
426 iface->mount_fn = g_unix_volume_mount;
427 iface->mount_finish = g_unix_volume_mount_finish;
428 iface->eject = g_unix_volume_eject;
429 iface->eject_finish = g_unix_volume_eject_finish;
430 iface->get_identifier = g_unix_volume_get_identifier;
431 iface->enumerate_identifiers = g_unix_volume_enumerate_identifiers;