Merge branch 'test-ip_mreq_source-android-only' into 'master'
[glib.git] / gio / gio-tool-mount.c
blob05647d91ef5843cee698e9559d4c1cd093ba5d8a
1 /*
2 * Copyright 2015 Red Hat, Inc.
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
17 * Author: Matthias Clasen <mclasen@redhat.com>
20 #include "config.h"
22 #include <gio/gio.h>
23 #include <gi18n.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #ifdef HAVE_TERMIOS_H
27 #include <termios.h>
28 #endif
30 #include "gio-tool.h"
32 #define STDIN_FILENO 0
34 typedef enum {
35 MOUNT_OP_NONE,
36 MOUNT_OP_ASKED,
37 MOUNT_OP_ABORTED
38 } MountOpState;
40 static int outstanding_mounts = 0;
41 static GMainLoop *main_loop;
42 static GVolumeMonitor *volume_monitor;
44 static gboolean mount_mountable = FALSE;
45 static gboolean mount_unmount = FALSE;
46 static gboolean mount_eject = FALSE;
47 static gboolean force = FALSE;
48 static gboolean anonymous = FALSE;
49 static gboolean mount_list = FALSE;
50 static gboolean extra_detail = FALSE;
51 static gboolean mount_monitor = FALSE;
52 static gboolean tcrypt_hidden = FALSE;
53 static gboolean tcrypt_system = FALSE;
54 static guint tcrypt_pim = 0;
55 static const char *unmount_scheme = NULL;
56 static const char *mount_device_file = NULL;
57 static const char *stop_device_file = NULL;
58 static gboolean success = TRUE;
61 static const GOptionEntry entries[] =
63 { "mountable", 'm', 0, G_OPTION_ARG_NONE, &mount_mountable, N_("Mount as mountable"), NULL },
64 { "device", 'd', 0, G_OPTION_ARG_STRING, &mount_device_file, N_("Mount volume with device file"), N_("DEVICE") },
65 { "unmount", 'u', 0, G_OPTION_ARG_NONE, &mount_unmount, N_("Unmount"), NULL},
66 { "eject", 'e', 0, G_OPTION_ARG_NONE, &mount_eject, N_("Eject"), NULL},
67 { "stop", 't', 0, G_OPTION_ARG_STRING, &stop_device_file, N_("Stop drive with device file"), N_("DEVICE") },
68 { "unmount-scheme", 's', 0, G_OPTION_ARG_STRING, &unmount_scheme, N_("Unmount all mounts with the given scheme"), N_("SCHEME") },
69 { "force", 'f', 0, G_OPTION_ARG_NONE, &force, N_("Ignore outstanding file operations when unmounting or ejecting"), NULL },
70 { "anonymous", 'a', 0, G_OPTION_ARG_NONE, &anonymous, N_("Use an anonymous user when authenticating"), NULL },
71 /* Translator: List here is a verb as in 'List all mounts' */
72 { "list", 'l', 0, G_OPTION_ARG_NONE, &mount_list, N_("List"), NULL},
73 { "monitor", 'o', 0, G_OPTION_ARG_NONE, &mount_monitor, N_("Monitor events"), NULL},
74 { "detail", 'i', 0, G_OPTION_ARG_NONE, &extra_detail, N_("Show extra information"), NULL},
75 { "tcrypt-pim", 0, 0, G_OPTION_ARG_INT, &tcrypt_pim, N_("The numeric PIM when unlocking a VeraCrypt volume"), N_("PIM")},
76 { "tcrypt-hidden", 0, 0, G_OPTION_ARG_NONE, &tcrypt_hidden, N_("Mount a TCRYPT hidden volume"), NULL},
77 { "tcrypt-system", 0, 0, G_OPTION_ARG_NONE, &tcrypt_system, N_("Mount a TCRYPT system volume"), NULL},
78 { NULL }
81 static char *
82 prompt_for (const char *prompt, const char *default_value, gboolean echo)
84 #ifdef HAVE_TERMIOS_H
85 struct termios term_attr;
86 int old_flags;
87 gboolean restore_flags;
88 #endif
89 char data[256];
90 int len;
92 if (default_value && *default_value != 0)
93 g_print ("%s [%s]: ", prompt, default_value);
94 else
95 g_print ("%s: ", prompt);
97 data[0] = 0;
99 #ifdef HAVE_TERMIOS_H
100 restore_flags = FALSE;
101 if (!echo && tcgetattr (STDIN_FILENO, &term_attr) == 0)
103 old_flags = term_attr.c_lflag;
104 term_attr.c_lflag &= ~ECHO;
105 restore_flags = TRUE;
107 if (tcsetattr (STDIN_FILENO, TCSAFLUSH, &term_attr) != 0)
108 g_print ("Warning! Password will be echoed");
111 #endif
113 fgets(data, sizeof (data), stdin);
115 #ifdef HAVE_TERMIOS_H
116 if (restore_flags)
118 term_attr.c_lflag = old_flags;
119 tcsetattr (STDIN_FILENO, TCSAFLUSH, &term_attr);
121 #endif
123 len = strlen (data);
124 if (len == 0)
126 g_print ("\n");
127 return NULL;
129 if (data[len-1] == '\n')
130 data[len-1] = 0;
132 if (!echo)
133 g_print ("\n");
135 if (*data == 0 && default_value)
136 return g_strdup (default_value);
137 return g_strdup (data);
140 static void
141 ask_password_cb (GMountOperation *op,
142 const char *message,
143 const char *default_user,
144 const char *default_domain,
145 GAskPasswordFlags flags)
147 if ((flags & G_ASK_PASSWORD_ANONYMOUS_SUPPORTED) && anonymous)
149 g_mount_operation_set_anonymous (op, TRUE);
151 else
153 char *s;
154 g_print ("%s\n", message);
156 if (flags & G_ASK_PASSWORD_NEED_USERNAME)
158 s = prompt_for ("User", default_user, TRUE);
159 if (!s)
160 goto error;
161 g_mount_operation_set_username (op, s);
162 g_free (s);
165 if (flags & G_ASK_PASSWORD_NEED_DOMAIN)
167 s = prompt_for ("Domain", default_domain, TRUE);
168 if (!s)
169 goto error;
170 g_mount_operation_set_domain (op, s);
171 g_free (s);
174 if (flags & G_ASK_PASSWORD_NEED_PASSWORD)
176 s = prompt_for ("Password", NULL, FALSE);
177 if (!s)
178 goto error;
179 g_mount_operation_set_password (op, s);
180 g_free (s);
184 if (flags & G_ASK_PASSWORD_TCRYPT)
186 if (tcrypt_pim)
187 g_mount_operation_set_pim (op, tcrypt_pim);
188 if (tcrypt_hidden)
189 g_mount_operation_set_is_tcrypt_hidden_volume (op, TRUE);
190 if (tcrypt_system)
191 g_mount_operation_set_is_tcrypt_system_volume (op, TRUE);
194 /* Only try anonymous access once. */
195 if (anonymous &&
196 GPOINTER_TO_INT (g_object_get_data (G_OBJECT (op), "state")) == MOUNT_OP_ASKED)
198 g_object_set_data (G_OBJECT (op), "state", GINT_TO_POINTER (MOUNT_OP_ABORTED));
199 g_mount_operation_reply (op, G_MOUNT_OPERATION_ABORTED);
201 else
203 g_object_set_data (G_OBJECT (op), "state", GINT_TO_POINTER (MOUNT_OP_ASKED));
204 g_mount_operation_reply (op, G_MOUNT_OPERATION_HANDLED);
207 return;
209 error:
210 g_mount_operation_reply (op, G_MOUNT_OPERATION_ABORTED);
213 static void
214 ask_question_cb (GMountOperation *op,
215 char *message,
216 char **choices,
217 gpointer user_data)
219 char **ptr = choices;
220 char *s;
221 int i, choice;
223 g_print ("%s\n", message);
225 i = 1;
226 while (*ptr)
228 g_print ("[%d] %s\n", i, *ptr++);
229 i++;
232 s = prompt_for ("Choice", NULL, TRUE);
233 if (!s)
234 goto error;
236 choice = atoi (s);
237 if (choice > 0 && choice < i)
239 g_mount_operation_set_choice (op, choice - 1);
240 g_mount_operation_reply (op, G_MOUNT_OPERATION_HANDLED);
242 g_free (s);
244 return;
246 error:
247 g_mount_operation_reply (op, G_MOUNT_OPERATION_ABORTED);
250 static void
251 mount_mountable_done_cb (GObject *object,
252 GAsyncResult *res,
253 gpointer user_data)
255 GFile *target;
256 GError *error = NULL;
257 GMountOperation *op = user_data;
259 target = g_file_mount_mountable_finish (G_FILE (object), res, &error);
261 if (target == NULL)
263 success = FALSE;
264 if (GPOINTER_TO_INT (g_object_get_data (G_OBJECT (op), "state")) == MOUNT_OP_ABORTED)
265 print_file_error (G_FILE (object), _("Anonymous access denied"));
266 else if (!g_error_matches (error, G_IO_ERROR, G_IO_ERROR_FAILED_HANDLED))
267 print_file_error (G_FILE (object), error->message);
269 g_error_free (error);
271 else
272 g_object_unref (target);
274 g_object_unref (op);
276 outstanding_mounts--;
278 if (outstanding_mounts == 0)
279 g_main_loop_quit (main_loop);
282 static void
283 mount_done_cb (GObject *object,
284 GAsyncResult *res,
285 gpointer user_data)
287 gboolean succeeded;
288 GError *error = NULL;
289 GMountOperation *op = user_data;
291 succeeded = g_file_mount_enclosing_volume_finish (G_FILE (object), res, &error);
293 if (!succeeded)
295 success = FALSE;
296 if (GPOINTER_TO_INT (g_object_get_data (G_OBJECT (op), "state")) == MOUNT_OP_ABORTED)
297 print_file_error (G_FILE (object), _("Anonymous access denied"));
298 else if (!g_error_matches (error, G_IO_ERROR, G_IO_ERROR_FAILED_HANDLED))
299 print_file_error (G_FILE (object), error->message);
301 g_error_free (error);
304 g_object_unref (op);
306 outstanding_mounts--;
308 if (outstanding_mounts == 0)
309 g_main_loop_quit (main_loop);
312 static GMountOperation *
313 new_mount_op (void)
315 GMountOperation *op;
317 op = g_mount_operation_new ();
319 g_object_set_data (G_OBJECT (op), "state", GINT_TO_POINTER (MOUNT_OP_NONE));
321 g_signal_connect (op, "ask_password", G_CALLBACK (ask_password_cb), NULL);
322 g_signal_connect (op, "ask_question", G_CALLBACK (ask_question_cb), NULL);
324 /* TODO: we *should* also connect to the "aborted" signal but since the
325 * main thread is blocked handling input we won't get that signal anyway...
328 return op;
332 static void
333 mount (GFile *file)
335 GMountOperation *op;
337 if (file == NULL)
338 return;
340 op = new_mount_op ();
342 if (mount_mountable)
343 g_file_mount_mountable (file, 0, op, NULL, mount_mountable_done_cb, op);
344 else
345 g_file_mount_enclosing_volume (file, 0, op, NULL, mount_done_cb, op);
347 outstanding_mounts++;
350 static void
351 unmount_done_cb (GObject *object,
352 GAsyncResult *res,
353 gpointer user_data)
355 gboolean succeeded;
356 GError *error = NULL;
357 GFile *file = G_FILE (user_data);
359 succeeded = g_mount_unmount_with_operation_finish (G_MOUNT (object), res, &error);
361 g_object_unref (G_MOUNT (object));
363 if (!succeeded)
365 print_file_error (file, error->message);
366 success = FALSE;
367 g_error_free (error);
370 g_object_unref (file);
372 outstanding_mounts--;
374 if (outstanding_mounts == 0)
375 g_main_loop_quit (main_loop);
378 static void
379 unmount (GFile *file)
381 GMount *mount;
382 GError *error = NULL;
383 GMountOperation *mount_op;
384 GMountUnmountFlags flags;
386 if (file == NULL)
387 return;
389 mount = g_file_find_enclosing_mount (file, NULL, &error);
390 if (mount == NULL)
392 print_file_error (file, error->message);
393 success = FALSE;
394 g_error_free (error);
395 return;
398 mount_op = new_mount_op ();
399 flags = force ? G_MOUNT_UNMOUNT_FORCE : G_MOUNT_UNMOUNT_NONE;
400 g_mount_unmount_with_operation (mount, flags, mount_op, NULL, unmount_done_cb, g_object_ref (file));
401 g_object_unref (mount_op);
403 outstanding_mounts++;
406 static void
407 eject_done_cb (GObject *object,
408 GAsyncResult *res,
409 gpointer user_data)
411 gboolean succeeded;
412 GError *error = NULL;
413 GFile *file = G_FILE (user_data);
415 succeeded = g_mount_eject_with_operation_finish (G_MOUNT (object), res, &error);
417 g_object_unref (G_MOUNT (object));
419 if (!succeeded)
421 print_file_error (file, error->message);
422 success = FALSE;
423 g_error_free (error);
426 g_object_unref (file);
428 outstanding_mounts--;
430 if (outstanding_mounts == 0)
431 g_main_loop_quit (main_loop);
434 static void
435 eject (GFile *file)
437 GMount *mount;
438 GError *error = NULL;
439 GMountOperation *mount_op;
440 GMountUnmountFlags flags;
442 if (file == NULL)
443 return;
445 mount = g_file_find_enclosing_mount (file, NULL, &error);
446 if (mount == NULL)
448 print_file_error (file, error->message);
449 success = FALSE;
450 g_error_free (error);
451 return;
454 mount_op = new_mount_op ();
455 flags = force ? G_MOUNT_UNMOUNT_FORCE : G_MOUNT_UNMOUNT_NONE;
456 g_mount_eject_with_operation (mount, flags, mount_op, NULL, eject_done_cb, g_object_ref (file));
457 g_object_unref (mount_op);
459 outstanding_mounts++;
462 static void
463 stop_with_device_file_cb (GObject *object,
464 GAsyncResult *res,
465 gpointer user_data)
467 GError *error = NULL;
468 gchar *device_path = user_data;
470 if (!g_drive_stop_finish (G_DRIVE (object), res, &error))
472 print_error ("%s: %s", device_path, error->message);
473 g_error_free (error);
474 success = FALSE;
477 g_free (device_path);
479 outstanding_mounts--;
481 if (outstanding_mounts == 0)
482 g_main_loop_quit (main_loop);
485 static void
486 stop_with_device_file (const char *device_file)
488 GList *drives;
489 GList *l;
491 drives = g_volume_monitor_get_connected_drives (volume_monitor);
492 for (l = drives; l != NULL; l = l->next)
494 GDrive *drive = G_DRIVE (l->data);
495 gchar *id;
497 id = g_drive_get_identifier (drive, G_VOLUME_IDENTIFIER_KIND_UNIX_DEVICE);
498 if (g_strcmp0 (id, device_file) == 0)
500 GMountOperation *op;
501 GMountUnmountFlags flags;
503 op = new_mount_op ();
504 flags = force ? G_MOUNT_UNMOUNT_FORCE : G_MOUNT_UNMOUNT_NONE;
505 g_drive_stop (drive,
506 flags,
508 NULL,
509 stop_with_device_file_cb,
510 g_steal_pointer (&id));
511 g_object_unref (op);
513 outstanding_mounts++;
516 g_free (id);
518 g_list_free_full (drives, g_object_unref);
520 if (outstanding_mounts == 0)
522 print_error ("%s: %s", device_file, _("No drive for device file"));
523 success = FALSE;
527 static gboolean
528 iterate_gmain_timeout_function (gpointer data)
530 g_main_loop_quit (main_loop);
531 return FALSE;
534 static void
535 iterate_gmain(void)
537 g_timeout_add (500, iterate_gmain_timeout_function, NULL);
538 g_main_loop_run (main_loop);
541 static void
542 show_themed_icon_names (GThemedIcon *icon, gboolean symbolic, int indent)
544 char **names;
545 char **iter;
547 g_print ("%*s%sthemed icons:", indent, " ", symbolic ? "symbolic " : "");
549 names = NULL;
551 g_object_get (icon, "names", &names, NULL);
553 for (iter = names; *iter; iter++)
554 g_print (" [%s]", *iter);
556 g_print ("\n");
557 g_strfreev (names);
560 /* don't copy-paste this code */
561 static char *
562 get_type_name (gpointer object)
564 const char *type_name;
565 char *ret;
567 type_name = g_type_name (G_TYPE_FROM_INSTANCE (object));
568 if (strcmp ("GProxyDrive", type_name) == 0)
570 ret = g_strdup_printf ("%s (%s)",
571 type_name,
572 (const char *) g_object_get_data (G_OBJECT (object),
573 "g-proxy-drive-volume-monitor-name"));
575 else if (strcmp ("GProxyVolume", type_name) == 0)
577 ret = g_strdup_printf ("%s (%s)",
578 type_name,
579 (const char *) g_object_get_data (G_OBJECT (object),
580 "g-proxy-volume-volume-monitor-name"));
582 else if (strcmp ("GProxyMount", type_name) == 0)
584 ret = g_strdup_printf ("%s (%s)",
585 type_name,
586 (const char *) g_object_get_data (G_OBJECT (object),
587 "g-proxy-mount-volume-monitor-name"));
589 else if (strcmp ("GProxyShadowMount", type_name) == 0)
591 ret = g_strdup_printf ("%s (%s)",
592 type_name,
593 (const char *) g_object_get_data (G_OBJECT (object),
594 "g-proxy-shadow-mount-volume-monitor-name"));
596 else
598 ret = g_strdup (type_name);
601 return ret;
604 static void
605 list_mounts (GList *mounts,
606 int indent,
607 gboolean only_with_no_volume)
609 GList *l;
610 int c;
611 GMount *mount;
612 GVolume *volume;
613 char *name, *uuid, *uri;
614 GFile *root, *default_location;
615 GIcon *icon;
616 char **x_content_types;
617 char *type_name;
618 const gchar *sort_key;
620 for (c = 0, l = mounts; l != NULL; l = l->next, c++)
622 mount = (GMount *) l->data;
624 if (only_with_no_volume)
626 volume = g_mount_get_volume (mount);
627 if (volume != NULL)
629 g_object_unref (volume);
630 continue;
634 name = g_mount_get_name (mount);
635 root = g_mount_get_root (mount);
636 uri = g_file_get_uri (root);
638 g_print ("%*sMount(%d): %s -> %s\n", indent, "", c, name, uri);
640 type_name = get_type_name (mount);
641 g_print ("%*sType: %s\n", indent+2, "", type_name);
642 g_free (type_name);
644 if (extra_detail)
646 uuid = g_mount_get_uuid (mount);
647 if (uuid)
648 g_print ("%*suuid=%s\n", indent + 2, "", uuid);
650 default_location = g_mount_get_default_location (mount);
651 if (default_location)
653 char *loc_uri = g_file_get_uri (default_location);
654 g_print ("%*sdefault_location=%s\n", indent + 2, "", loc_uri);
655 g_free (loc_uri);
656 g_object_unref (default_location);
659 icon = g_mount_get_icon (mount);
660 if (icon)
662 if (G_IS_THEMED_ICON (icon))
663 show_themed_icon_names (G_THEMED_ICON (icon), FALSE, indent + 2);
665 g_object_unref (icon);
668 icon = g_mount_get_symbolic_icon (mount);
669 if (icon)
671 if (G_IS_THEMED_ICON (icon))
672 show_themed_icon_names (G_THEMED_ICON (icon), TRUE, indent + 2);
674 g_object_unref (icon);
677 x_content_types = g_mount_guess_content_type_sync (mount, FALSE, NULL, NULL);
678 if (x_content_types != NULL && g_strv_length (x_content_types) > 0)
680 int n;
681 g_print ("%*sx_content_types:", indent + 2, "");
682 for (n = 0; x_content_types[n] != NULL; n++)
683 g_print (" %s", x_content_types[n]);
684 g_print ("\n");
686 g_strfreev (x_content_types);
688 g_print ("%*scan_unmount=%d\n", indent + 2, "", g_mount_can_unmount (mount));
689 g_print ("%*scan_eject=%d\n", indent + 2, "", g_mount_can_eject (mount));
690 g_print ("%*sis_shadowed=%d\n", indent + 2, "", g_mount_is_shadowed (mount));
691 sort_key = g_mount_get_sort_key (mount);
692 if (sort_key != NULL)
693 g_print ("%*ssort_key=%s\n", indent + 2, "", sort_key);
694 g_free (uuid);
697 g_object_unref (root);
698 g_free (name);
699 g_free (uri);
703 static void
704 list_volumes (GList *volumes,
705 int indent,
706 gboolean only_with_no_drive)
708 GList *l, *mounts;
709 int c, i;
710 GMount *mount;
711 GVolume *volume;
712 GDrive *drive;
713 char *name;
714 char *uuid;
715 GFile *activation_root;
716 char **ids;
717 GIcon *icon;
718 char *type_name;
719 const gchar *sort_key;
721 for (c = 0, l = volumes; l != NULL; l = l->next, c++)
723 volume = (GVolume *) l->data;
725 if (only_with_no_drive)
727 drive = g_volume_get_drive (volume);
728 if (drive != NULL)
730 g_object_unref (drive);
731 continue;
735 name = g_volume_get_name (volume);
737 g_print ("%*sVolume(%d): %s\n", indent, "", c, name);
738 g_free (name);
740 type_name = get_type_name (volume);
741 g_print ("%*sType: %s\n", indent+2, "", type_name);
742 g_free (type_name);
744 if (extra_detail)
746 ids = g_volume_enumerate_identifiers (volume);
747 if (ids && ids[0] != NULL)
749 g_print ("%*sids:\n", indent+2, "");
750 for (i = 0; ids[i] != NULL; i++)
752 char *id = g_volume_get_identifier (volume,
753 ids[i]);
754 g_print ("%*s %s: '%s'\n", indent+2, "", ids[i], id);
755 g_free (id);
758 g_strfreev (ids);
760 uuid = g_volume_get_uuid (volume);
761 if (uuid)
762 g_print ("%*suuid=%s\n", indent + 2, "", uuid);
763 activation_root = g_volume_get_activation_root (volume);
764 if (activation_root)
766 char *uri;
767 uri = g_file_get_uri (activation_root);
768 g_print ("%*sactivation_root=%s\n", indent + 2, "", uri);
769 g_free (uri);
770 g_object_unref (activation_root);
772 icon = g_volume_get_icon (volume);
773 if (icon)
775 if (G_IS_THEMED_ICON (icon))
776 show_themed_icon_names (G_THEMED_ICON (icon), FALSE, indent + 2);
778 g_object_unref (icon);
781 icon = g_volume_get_symbolic_icon (volume);
782 if (icon)
784 if (G_IS_THEMED_ICON (icon))
785 show_themed_icon_names (G_THEMED_ICON (icon), TRUE, indent + 2);
787 g_object_unref (icon);
790 g_print ("%*scan_mount=%d\n", indent + 2, "", g_volume_can_mount (volume));
791 g_print ("%*scan_eject=%d\n", indent + 2, "", g_volume_can_eject (volume));
792 g_print ("%*sshould_automount=%d\n", indent + 2, "", g_volume_should_automount (volume));
793 sort_key = g_volume_get_sort_key (volume);
794 if (sort_key != NULL)
795 g_print ("%*ssort_key=%s\n", indent + 2, "", sort_key);
796 g_free (uuid);
799 mount = g_volume_get_mount (volume);
800 if (mount)
802 mounts = g_list_prepend (NULL, mount);
803 list_mounts (mounts, indent + 2, FALSE);
804 g_list_free (mounts);
805 g_object_unref (mount);
810 static void
811 list_drives (GList *drives,
812 int indent)
814 GList *volumes, *l;
815 int c, i;
816 GDrive *drive;
817 char *name;
818 char **ids;
819 GIcon *icon;
820 char *type_name;
821 const gchar *sort_key;
823 for (c = 0, l = drives; l != NULL; l = l->next, c++)
825 drive = (GDrive *) l->data;
826 name = g_drive_get_name (drive);
828 g_print ("%*sDrive(%d): %s\n", indent, "", c, name);
829 g_free (name);
831 type_name = get_type_name (drive);
832 g_print ("%*sType: %s\n", indent+2, "", type_name);
833 g_free (type_name);
835 if (extra_detail)
837 GEnumValue *enum_value;
838 gpointer klass;
840 ids = g_drive_enumerate_identifiers (drive);
841 if (ids && ids[0] != NULL)
843 g_print ("%*sids:\n", indent+2, "");
844 for (i = 0; ids[i] != NULL; i++)
846 char *id = g_drive_get_identifier (drive,
847 ids[i]);
848 g_print ("%*s %s: '%s'\n", indent+2, "", ids[i], id);
849 g_free (id);
852 g_strfreev (ids);
854 icon = g_drive_get_icon (drive);
855 if (icon)
857 if (G_IS_THEMED_ICON (icon))
858 show_themed_icon_names (G_THEMED_ICON (icon), FALSE, indent + 2);
859 g_object_unref (icon);
862 icon = g_drive_get_symbolic_icon (drive);
863 if (icon)
865 if (G_IS_THEMED_ICON (icon))
866 show_themed_icon_names (G_THEMED_ICON (icon), TRUE, indent + 2);
868 g_object_unref (icon);
871 g_print ("%*sis_removable=%d\n", indent + 2, "", g_drive_is_removable (drive));
872 g_print ("%*sis_media_removable=%d\n", indent + 2, "", g_drive_is_media_removable (drive));
873 g_print ("%*shas_media=%d\n", indent + 2, "", g_drive_has_media (drive));
874 g_print ("%*sis_media_check_automatic=%d\n", indent + 2, "", g_drive_is_media_check_automatic (drive));
875 g_print ("%*scan_poll_for_media=%d\n", indent + 2, "", g_drive_can_poll_for_media (drive));
876 g_print ("%*scan_eject=%d\n", indent + 2, "", g_drive_can_eject (drive));
877 g_print ("%*scan_start=%d\n", indent + 2, "", g_drive_can_start (drive));
878 g_print ("%*scan_stop=%d\n", indent + 2, "", g_drive_can_stop (drive));
880 enum_value = NULL;
881 klass = g_type_class_ref (G_TYPE_DRIVE_START_STOP_TYPE);
882 if (klass != NULL)
884 enum_value = g_enum_get_value (klass, g_drive_get_start_stop_type (drive));
885 g_print ("%*sstart_stop_type=%s\n", indent + 2, "",
886 enum_value != NULL ? enum_value->value_nick : "UNKNOWN");
887 g_type_class_unref (klass);
890 sort_key = g_drive_get_sort_key (drive);
891 if (sort_key != NULL)
892 g_print ("%*ssort_key=%s\n", indent + 2, "", sort_key);
894 volumes = g_drive_get_volumes (drive);
895 list_volumes (volumes, indent + 2, FALSE);
896 g_list_free_full (volumes, g_object_unref);
901 static void
902 list_monitor_items (void)
904 GList *drives, *volumes, *mounts;
906 /* populate gvfs network mounts */
907 iterate_gmain();
909 drives = g_volume_monitor_get_connected_drives (volume_monitor);
910 list_drives (drives, 0);
911 g_list_free_full (drives, g_object_unref);
913 volumes = g_volume_monitor_get_volumes (volume_monitor);
914 list_volumes (volumes, 0, TRUE);
915 g_list_free_full (volumes, g_object_unref);
917 mounts = g_volume_monitor_get_mounts (volume_monitor);
918 list_mounts (mounts, 0, TRUE);
919 g_list_free_full (mounts, g_object_unref);
922 static void
923 unmount_all_with_scheme (const char *scheme)
925 GList *mounts;
926 GList *l;
928 /* populate gvfs network mounts */
929 iterate_gmain();
931 mounts = g_volume_monitor_get_mounts (volume_monitor);
932 for (l = mounts; l != NULL; l = l->next) {
933 GMount *mount = G_MOUNT (l->data);
934 GFile *root;
936 root = g_mount_get_root (mount);
937 if (g_file_has_uri_scheme (root, scheme)) {
938 unmount (root);
940 g_object_unref (root);
942 g_list_free_full (mounts, g_object_unref);
945 static void
946 mount_with_device_file_cb (GObject *object,
947 GAsyncResult *res,
948 gpointer user_data)
950 GVolume *volume;
951 gboolean succeeded;
952 GError *error = NULL;
953 gchar *device_path = (gchar *)user_data;
955 volume = G_VOLUME (object);
957 succeeded = g_volume_mount_finish (volume, res, &error);
959 if (!succeeded)
961 print_error ("%s: %s", device_path, error->message);
962 g_error_free (error);
963 success = FALSE;
965 else
967 GMount *mount;
968 GFile *root;
969 char *mount_path;
971 mount = g_volume_get_mount (volume);
972 root = g_mount_get_root (mount);
973 mount_path = g_file_get_path (root);
975 g_print (_("Mounted %s at %s\n"), device_path, mount_path);
977 g_object_unref (mount);
978 g_object_unref (root);
979 g_free (mount_path);
982 g_free (device_path);
984 outstanding_mounts--;
986 if (outstanding_mounts == 0)
987 g_main_loop_quit (main_loop);
990 static void
991 mount_with_device_file (const char *device_file)
993 GList *volumes;
994 GList *l;
996 volumes = g_volume_monitor_get_volumes (volume_monitor);
997 for (l = volumes; l != NULL; l = l->next)
999 GVolume *volume = G_VOLUME (l->data);
1000 gchar *id;
1002 id = g_volume_get_identifier (volume, G_VOLUME_IDENTIFIER_KIND_UNIX_DEVICE);
1003 if (g_strcmp0 (id, device_file) == 0)
1005 GMountOperation *op;
1007 op = new_mount_op ();
1009 g_volume_mount (volume,
1010 G_MOUNT_MOUNT_NONE,
1012 NULL,
1013 mount_with_device_file_cb,
1014 id);
1016 g_object_unref (op);
1018 outstanding_mounts++;
1020 else
1021 g_free (id);
1023 g_list_free_full (volumes, g_object_unref);
1025 if (outstanding_mounts == 0)
1027 print_error ("%s: %s", device_file, _("No volume for device file"));
1028 success = FALSE;
1032 static void
1033 monitor_print_mount (GMount *mount)
1035 if (extra_detail)
1037 GList *l;
1038 l = g_list_prepend (NULL, mount);
1039 list_mounts (l, 2, FALSE);
1040 g_list_free (l);
1041 g_print ("\n");
1045 static void
1046 monitor_print_volume (GVolume *volume)
1048 if (extra_detail)
1050 GList *l;
1051 l = g_list_prepend (NULL, volume);
1052 list_volumes (l, 2, FALSE);
1053 g_list_free (l);
1054 g_print ("\n");
1058 static void
1059 monitor_print_drive (GDrive *drive)
1061 if (extra_detail)
1063 GList *l;
1064 l = g_list_prepend (NULL, drive);
1065 list_drives (l, 2);
1066 g_list_free (l);
1067 g_print ("\n");
1071 static void
1072 monitor_mount_added (GVolumeMonitor *volume_monitor, GMount *mount)
1074 char *name;
1075 name = g_mount_get_name (mount);
1076 g_print ("Mount added: '%s'\n", name);
1077 g_free (name);
1078 monitor_print_mount (mount);
1081 static void
1082 monitor_mount_removed (GVolumeMonitor *volume_monitor, GMount *mount)
1084 char *name;
1085 name = g_mount_get_name (mount);
1086 g_print ("Mount removed: '%s'\n", name);
1087 g_free (name);
1088 monitor_print_mount (mount);
1091 static void
1092 monitor_mount_changed (GVolumeMonitor *volume_monitor, GMount *mount)
1094 char *name;
1095 name = g_mount_get_name (mount);
1096 g_print ("Mount changed: '%s'\n", name);
1097 g_free (name);
1098 monitor_print_mount (mount);
1101 static void
1102 monitor_mount_pre_unmount (GVolumeMonitor *volume_monitor, GMount *mount)
1104 char *name;
1105 name = g_mount_get_name (mount);
1106 g_print ("Mount pre-unmount: '%s'\n", name);
1107 g_free (name);
1108 monitor_print_mount (mount);
1111 static void
1112 monitor_volume_added (GVolumeMonitor *volume_monitor, GVolume *volume)
1114 char *name;
1115 name = g_volume_get_name (volume);
1116 g_print ("Volume added: '%s'\n", name);
1117 g_free (name);
1118 monitor_print_volume (volume);
1121 static void
1122 monitor_volume_removed (GVolumeMonitor *volume_monitor, GVolume *volume)
1124 char *name;
1125 name = g_volume_get_name (volume);
1126 g_print ("Volume removed: '%s'\n", name);
1127 g_free (name);
1128 monitor_print_volume (volume);
1131 static void
1132 monitor_volume_changed (GVolumeMonitor *volume_monitor, GVolume *volume)
1134 char *name;
1135 name = g_volume_get_name (volume);
1136 g_print ("Volume changed: '%s'\n", name);
1137 g_free (name);
1138 monitor_print_volume (volume);
1141 static void
1142 monitor_drive_connected (GVolumeMonitor *volume_monitor, GDrive *drive)
1144 char *name;
1145 name = g_drive_get_name (drive);
1146 g_print ("Drive connected: '%s'\n", name);
1147 g_free (name);
1148 monitor_print_drive (drive);
1151 static void
1152 monitor_drive_disconnected (GVolumeMonitor *volume_monitor, GDrive *drive)
1154 char *name;
1155 name = g_drive_get_name (drive);
1156 g_print ("Drive disconnected: '%s'\n", name);
1157 g_free (name);
1158 monitor_print_drive (drive);
1161 static void
1162 monitor_drive_changed (GVolumeMonitor *volume_monitor, GDrive *drive)
1164 char *name;
1165 name = g_drive_get_name (drive);
1166 g_print ("Drive changed: '%s'\n", name);
1167 g_free (name);
1168 monitor_print_drive (drive);
1171 static void
1172 monitor_drive_eject_button (GVolumeMonitor *volume_monitor, GDrive *drive)
1174 char *name;
1175 name = g_drive_get_name (drive);
1176 g_print ("Drive eject button: '%s'\n", name);
1177 g_free (name);
1180 static void
1181 monitor (void)
1183 g_signal_connect (volume_monitor, "mount-added", (GCallback) monitor_mount_added, NULL);
1184 g_signal_connect (volume_monitor, "mount-removed", (GCallback) monitor_mount_removed, NULL);
1185 g_signal_connect (volume_monitor, "mount-changed", (GCallback) monitor_mount_changed, NULL);
1186 g_signal_connect (volume_monitor, "mount-pre-unmount", (GCallback) monitor_mount_pre_unmount, NULL);
1187 g_signal_connect (volume_monitor, "volume-added", (GCallback) monitor_volume_added, NULL);
1188 g_signal_connect (volume_monitor, "volume-removed", (GCallback) monitor_volume_removed, NULL);
1189 g_signal_connect (volume_monitor, "volume-changed", (GCallback) monitor_volume_changed, NULL);
1190 g_signal_connect (volume_monitor, "drive-connected", (GCallback) monitor_drive_connected, NULL);
1191 g_signal_connect (volume_monitor, "drive-disconnected", (GCallback) monitor_drive_disconnected, NULL);
1192 g_signal_connect (volume_monitor, "drive-changed", (GCallback) monitor_drive_changed, NULL);
1193 g_signal_connect (volume_monitor, "drive-eject-button", (GCallback) monitor_drive_eject_button, NULL);
1195 g_print ("Monitoring events. Press Ctrl+C to quit.\n");
1197 g_main_loop_run (main_loop);
1201 handle_mount (int argc, char *argv[], gboolean do_help)
1203 GOptionContext *context;
1204 gchar *param;
1205 GError *error = NULL;
1206 GFile *file;
1207 int i;
1209 g_set_prgname ("gio mount");
1211 /* Translators: commandline placeholder */
1212 param = g_strdup_printf ("[%s…]", _("LOCATION"));
1213 context = g_option_context_new (param);
1214 g_free (param);
1215 g_option_context_set_help_enabled (context, FALSE);
1216 g_option_context_set_summary (context, _("Mount or unmount the locations."));
1217 g_option_context_add_main_entries (context, entries, GETTEXT_PACKAGE);
1219 if (do_help)
1221 show_help (context, NULL);
1222 g_option_context_free (context);
1223 return 0;
1226 if (!g_option_context_parse (context, &argc, &argv, &error))
1228 show_help (context, error->message);
1229 g_error_free (error);
1230 g_option_context_free (context);
1231 return 1;
1234 main_loop = g_main_loop_new (NULL, FALSE);
1235 volume_monitor = g_volume_monitor_get ();
1237 if (mount_list)
1238 list_monitor_items ();
1239 else if (mount_device_file != NULL)
1240 mount_with_device_file (mount_device_file);
1241 else if (stop_device_file)
1242 stop_with_device_file (stop_device_file);
1243 else if (unmount_scheme != NULL)
1244 unmount_all_with_scheme (unmount_scheme);
1245 else if (mount_monitor)
1246 monitor ();
1247 else if (argc > 1)
1249 for (i = 1; i < argc; i++)
1251 file = g_file_new_for_commandline_arg (argv[i]);
1252 if (mount_unmount)
1253 unmount (file);
1254 else if (mount_eject)
1255 eject (file);
1256 else
1257 mount (file);
1258 g_object_unref (file);
1261 else
1263 show_help (context, _("No locations given"));
1264 g_option_context_free (context);
1265 g_object_unref (volume_monitor);
1266 return 1;
1269 g_option_context_free (context);
1271 if (outstanding_mounts > 0)
1272 g_main_loop_run (main_loop);
1274 g_object_unref (volume_monitor);
1276 return success ? 0 : 2;