Use macros for refcount types API
[glib.git] / gio / gio-tool-mount.c
blob224ff89901b790256a3cc77e91410c849ec4c0a9
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;
43 static gboolean mount_mountable = FALSE;
44 static gboolean mount_unmount = FALSE;
45 static gboolean mount_eject = FALSE;
46 static gboolean force = FALSE;
47 static gboolean anonymous = FALSE;
48 static gboolean mount_list = FALSE;
49 static gboolean extra_detail = FALSE;
50 static gboolean mount_monitor = FALSE;
51 static const char *unmount_scheme = NULL;
52 static const char *mount_device_file = NULL;
53 static const char *stop_device_file = NULL;
54 static gboolean success = TRUE;
57 static const GOptionEntry entries[] =
59 { "mountable", 'm', 0, G_OPTION_ARG_NONE, &mount_mountable, N_("Mount as mountable"), NULL },
60 { "device", 'd', 0, G_OPTION_ARG_STRING, &mount_device_file, N_("Mount volume with device file"), N_("DEVICE") },
61 { "unmount", 'u', 0, G_OPTION_ARG_NONE, &mount_unmount, N_("Unmount"), NULL},
62 { "eject", 'e', 0, G_OPTION_ARG_NONE, &mount_eject, N_("Eject"), NULL},
63 { "stop", 't', 0, G_OPTION_ARG_STRING, &stop_device_file, N_("Stop drive with device file"), N_("DEVICE") },
64 { "unmount-scheme", 's', 0, G_OPTION_ARG_STRING, &unmount_scheme, N_("Unmount all mounts with the given scheme"), N_("SCHEME") },
65 { "force", 'f', 0, G_OPTION_ARG_NONE, &force, N_("Ignore outstanding file operations when unmounting or ejecting"), NULL },
66 { "anonymous", 'a', 0, G_OPTION_ARG_NONE, &anonymous, N_("Use an anonymous user when authenticating"), NULL },
67 /* Translator: List here is a verb as in 'List all mounts' */
68 { "list", 'l', 0, G_OPTION_ARG_NONE, &mount_list, N_("List"), NULL},
69 { "monitor", 'o', 0, G_OPTION_ARG_NONE, &mount_monitor, N_("Monitor events"), NULL},
70 { "detail", 'i', 0, G_OPTION_ARG_NONE, &extra_detail, N_("Show extra information"), NULL},
71 { NULL }
74 static char *
75 prompt_for (const char *prompt, const char *default_value, gboolean echo)
77 #ifdef HAVE_TERMIOS_H
78 struct termios term_attr;
79 int old_flags;
80 gboolean restore_flags;
81 #endif
82 char data[256];
83 int len;
85 if (default_value && *default_value != 0)
86 g_print ("%s [%s]: ", prompt, default_value);
87 else
88 g_print ("%s: ", prompt);
90 data[0] = 0;
92 #ifdef HAVE_TERMIOS_H
93 restore_flags = FALSE;
94 if (!echo && tcgetattr (STDIN_FILENO, &term_attr) == 0)
96 old_flags = term_attr.c_lflag;
97 term_attr.c_lflag &= ~ECHO;
98 restore_flags = TRUE;
100 if (tcsetattr (STDIN_FILENO, TCSAFLUSH, &term_attr) != 0)
101 g_print ("Warning! Password will be echoed");
104 #endif
106 fgets(data, sizeof (data), stdin);
108 #ifdef HAVE_TERMIOS_H
109 if (restore_flags)
111 term_attr.c_lflag = old_flags;
112 tcsetattr (STDIN_FILENO, TCSAFLUSH, &term_attr);
114 #endif
116 len = strlen (data);
117 if (len == 0)
119 g_print ("\n");
120 return NULL;
122 if (data[len-1] == '\n')
123 data[len-1] = 0;
125 if (!echo)
126 g_print ("\n");
128 if (*data == 0 && default_value)
129 return g_strdup (default_value);
130 return g_strdup (data);
133 static void
134 ask_password_cb (GMountOperation *op,
135 const char *message,
136 const char *default_user,
137 const char *default_domain,
138 GAskPasswordFlags flags)
140 if ((flags & G_ASK_PASSWORD_ANONYMOUS_SUPPORTED) && anonymous)
142 g_mount_operation_set_anonymous (op, TRUE);
144 else
146 char *s;
147 g_print ("%s\n", message);
149 if (flags & G_ASK_PASSWORD_NEED_USERNAME)
151 s = prompt_for ("User", default_user, TRUE);
152 if (!s)
153 goto error;
154 g_mount_operation_set_username (op, s);
155 g_free (s);
158 if (flags & G_ASK_PASSWORD_NEED_DOMAIN)
160 s = prompt_for ("Domain", default_domain, TRUE);
161 if (!s)
162 goto error;
163 g_mount_operation_set_domain (op, s);
164 g_free (s);
167 if (flags & G_ASK_PASSWORD_NEED_PASSWORD)
169 s = prompt_for ("Password", NULL, FALSE);
170 if (!s)
171 goto error;
172 g_mount_operation_set_password (op, s);
173 g_free (s);
177 /* Only try anonymous access once. */
178 if (anonymous &&
179 GPOINTER_TO_INT (g_object_get_data (G_OBJECT (op), "state")) == MOUNT_OP_ASKED)
181 g_object_set_data (G_OBJECT (op), "state", GINT_TO_POINTER (MOUNT_OP_ABORTED));
182 g_mount_operation_reply (op, G_MOUNT_OPERATION_ABORTED);
184 else
186 g_object_set_data (G_OBJECT (op), "state", GINT_TO_POINTER (MOUNT_OP_ASKED));
187 g_mount_operation_reply (op, G_MOUNT_OPERATION_HANDLED);
190 return;
192 error:
193 g_mount_operation_reply (op, G_MOUNT_OPERATION_ABORTED);
196 static void
197 ask_question_cb (GMountOperation *op,
198 char *message,
199 char **choices,
200 gpointer user_data)
202 char **ptr = choices;
203 char *s;
204 int i, choice;
206 g_print ("%s\n", message);
208 i = 1;
209 while (*ptr)
211 g_print ("[%d] %s\n", i, *ptr++);
212 i++;
215 s = prompt_for ("Choice", NULL, TRUE);
216 if (!s)
217 goto error;
219 choice = atoi (s);
220 if (choice > 0 && choice < i)
222 g_mount_operation_set_choice (op, choice - 1);
223 g_mount_operation_reply (op, G_MOUNT_OPERATION_HANDLED);
225 g_free (s);
227 return;
229 error:
230 g_mount_operation_reply (op, G_MOUNT_OPERATION_ABORTED);
233 static void
234 mount_mountable_done_cb (GObject *object,
235 GAsyncResult *res,
236 gpointer user_data)
238 GFile *target;
239 GError *error = NULL;
240 GMountOperation *op = user_data;
242 target = g_file_mount_mountable_finish (G_FILE (object), res, &error);
244 if (target == NULL)
246 success = FALSE;
247 if (GPOINTER_TO_INT (g_object_get_data (G_OBJECT (op), "state")) == MOUNT_OP_ABORTED)
248 print_file_error (G_FILE (object), _("Anonymous access denied"));
249 else if (!g_error_matches (error, G_IO_ERROR, G_IO_ERROR_FAILED_HANDLED))
250 print_file_error (G_FILE (object), error->message);
252 g_error_free (error);
254 else
255 g_object_unref (target);
257 g_object_unref (op);
259 outstanding_mounts--;
261 if (outstanding_mounts == 0)
262 g_main_loop_quit (main_loop);
265 static void
266 mount_done_cb (GObject *object,
267 GAsyncResult *res,
268 gpointer user_data)
270 gboolean succeeded;
271 GError *error = NULL;
272 GMountOperation *op = user_data;
274 succeeded = g_file_mount_enclosing_volume_finish (G_FILE (object), res, &error);
276 if (!succeeded)
278 success = FALSE;
279 if (GPOINTER_TO_INT (g_object_get_data (G_OBJECT (op), "state")) == MOUNT_OP_ABORTED)
280 print_file_error (G_FILE (object), _("Anonymous access denied"));
281 else if (!g_error_matches (error, G_IO_ERROR, G_IO_ERROR_FAILED_HANDLED))
282 print_file_error (G_FILE (object), error->message);
284 g_error_free (error);
287 g_object_unref (op);
289 outstanding_mounts--;
291 if (outstanding_mounts == 0)
292 g_main_loop_quit (main_loop);
295 static GMountOperation *
296 new_mount_op (void)
298 GMountOperation *op;
300 op = g_mount_operation_new ();
302 g_object_set_data (G_OBJECT (op), "state", GINT_TO_POINTER (MOUNT_OP_NONE));
304 g_signal_connect (op, "ask_password", G_CALLBACK (ask_password_cb), NULL);
305 g_signal_connect (op, "ask_question", G_CALLBACK (ask_question_cb), NULL);
307 /* TODO: we *should* also connect to the "aborted" signal but since the
308 * main thread is blocked handling input we won't get that signal anyway...
311 return op;
315 static void
316 mount (GFile *file)
318 GMountOperation *op;
320 if (file == NULL)
321 return;
323 op = new_mount_op ();
325 if (mount_mountable)
326 g_file_mount_mountable (file, 0, op, NULL, mount_mountable_done_cb, op);
327 else
328 g_file_mount_enclosing_volume (file, 0, op, NULL, mount_done_cb, op);
330 outstanding_mounts++;
333 static void
334 unmount_done_cb (GObject *object,
335 GAsyncResult *res,
336 gpointer user_data)
338 gboolean succeeded;
339 GError *error = NULL;
340 GFile *file = G_FILE (user_data);
342 succeeded = g_mount_unmount_with_operation_finish (G_MOUNT (object), res, &error);
344 g_object_unref (G_MOUNT (object));
346 if (!succeeded)
348 print_file_error (file, error->message);
349 success = FALSE;
350 g_error_free (error);
353 g_object_unref (file);
355 outstanding_mounts--;
357 if (outstanding_mounts == 0)
358 g_main_loop_quit (main_loop);
361 static void
362 unmount (GFile *file)
364 GMount *mount;
365 GError *error = NULL;
366 GMountOperation *mount_op;
367 GMountUnmountFlags flags;
369 if (file == NULL)
370 return;
372 mount = g_file_find_enclosing_mount (file, NULL, &error);
373 if (mount == NULL)
375 print_file_error (file, error->message);
376 success = FALSE;
377 g_error_free (error);
378 return;
381 mount_op = new_mount_op ();
382 flags = force ? G_MOUNT_UNMOUNT_FORCE : G_MOUNT_UNMOUNT_NONE;
383 g_mount_unmount_with_operation (mount, flags, mount_op, NULL, unmount_done_cb, g_object_ref (file));
384 g_object_unref (mount_op);
386 outstanding_mounts++;
389 static void
390 eject_done_cb (GObject *object,
391 GAsyncResult *res,
392 gpointer user_data)
394 gboolean succeeded;
395 GError *error = NULL;
396 GFile *file = G_FILE (user_data);
398 succeeded = g_mount_eject_with_operation_finish (G_MOUNT (object), res, &error);
400 g_object_unref (G_MOUNT (object));
402 if (!succeeded)
404 print_file_error (file, error->message);
405 success = FALSE;
406 g_error_free (error);
409 g_object_unref (file);
411 outstanding_mounts--;
413 if (outstanding_mounts == 0)
414 g_main_loop_quit (main_loop);
417 static void
418 eject (GFile *file)
420 GMount *mount;
421 GError *error = NULL;
422 GMountOperation *mount_op;
423 GMountUnmountFlags flags;
425 if (file == NULL)
426 return;
428 mount = g_file_find_enclosing_mount (file, NULL, &error);
429 if (mount == NULL)
431 print_file_error (file, error->message);
432 success = FALSE;
433 g_error_free (error);
434 return;
437 mount_op = new_mount_op ();
438 flags = force ? G_MOUNT_UNMOUNT_FORCE : G_MOUNT_UNMOUNT_NONE;
439 g_mount_eject_with_operation (mount, flags, mount_op, NULL, eject_done_cb, g_object_ref (file));
440 g_object_unref (mount_op);
442 outstanding_mounts++;
445 static void
446 stop_with_device_file_cb (GObject *object,
447 GAsyncResult *res,
448 gpointer user_data)
450 GError *error = NULL;
451 gchar *device_path = user_data;
453 if (!g_drive_stop_finish (G_DRIVE (object), res, &error))
455 print_error ("%s: %s", device_path, error->message);
456 g_error_free (error);
457 success = FALSE;
460 g_free (device_path);
462 outstanding_mounts--;
464 if (outstanding_mounts == 0)
465 g_main_loop_quit (main_loop);
468 static void
469 stop_with_device_file (const char *device_file)
471 GVolumeMonitor *volume_monitor;
472 GList *drives;
473 GList *l;
475 volume_monitor = g_volume_monitor_get ();
477 drives = g_volume_monitor_get_connected_drives (volume_monitor);
478 for (l = drives; l != NULL; l = l->next)
480 GDrive *drive = G_DRIVE (l->data);
481 gchar *id;
483 id = g_drive_get_identifier (drive, G_VOLUME_IDENTIFIER_KIND_UNIX_DEVICE);
484 if (g_strcmp0 (id, device_file) == 0)
486 GMountOperation *op;
487 GMountUnmountFlags flags;
489 op = new_mount_op ();
490 flags = force ? G_MOUNT_UNMOUNT_FORCE : G_MOUNT_UNMOUNT_NONE;
491 g_drive_stop (drive,
492 flags,
494 NULL,
495 stop_with_device_file_cb,
496 g_steal_pointer (&id));
497 g_object_unref (op);
499 outstanding_mounts++;
502 g_free (id);
504 g_list_free_full (drives, g_object_unref);
506 if (outstanding_mounts == 0)
508 print_error ("%s: %s", device_file, _("No drive for device file"));
509 success = FALSE;
512 g_object_unref (volume_monitor);
515 static gboolean
516 iterate_gmain_timeout_function (gpointer data)
518 g_main_loop_quit (main_loop);
519 return FALSE;
522 static void
523 iterate_gmain(void)
525 g_timeout_add (500, iterate_gmain_timeout_function, NULL);
526 g_main_loop_run (main_loop);
529 static void
530 show_themed_icon_names (GThemedIcon *icon, gboolean symbolic, int indent)
532 char **names;
533 char **iter;
535 g_print ("%*s%sthemed icons:", indent, " ", symbolic ? "symbolic " : "");
537 names = NULL;
539 g_object_get (icon, "names", &names, NULL);
541 for (iter = names; *iter; iter++)
542 g_print (" [%s]", *iter);
544 g_print ("\n");
545 g_strfreev (names);
548 /* don't copy-paste this code */
549 static char *
550 get_type_name (gpointer object)
552 const char *type_name;
553 char *ret;
555 type_name = g_type_name (G_TYPE_FROM_INSTANCE (object));
556 if (strcmp ("GProxyDrive", type_name) == 0)
558 ret = g_strdup_printf ("%s (%s)",
559 type_name,
560 (const char *) g_object_get_data (G_OBJECT (object),
561 "g-proxy-drive-volume-monitor-name"));
563 else if (strcmp ("GProxyVolume", type_name) == 0)
565 ret = g_strdup_printf ("%s (%s)",
566 type_name,
567 (const char *) g_object_get_data (G_OBJECT (object),
568 "g-proxy-volume-volume-monitor-name"));
570 else if (strcmp ("GProxyMount", type_name) == 0)
572 ret = g_strdup_printf ("%s (%s)",
573 type_name,
574 (const char *) g_object_get_data (G_OBJECT (object),
575 "g-proxy-mount-volume-monitor-name"));
577 else if (strcmp ("GProxyShadowMount", type_name) == 0)
579 ret = g_strdup_printf ("%s (%s)",
580 type_name,
581 (const char *) g_object_get_data (G_OBJECT (object),
582 "g-proxy-shadow-mount-volume-monitor-name"));
584 else
586 ret = g_strdup (type_name);
589 return ret;
592 static void
593 list_mounts (GList *mounts,
594 int indent,
595 gboolean only_with_no_volume)
597 GList *l;
598 int c;
599 GMount *mount;
600 GVolume *volume;
601 char *name, *uuid, *uri;
602 GFile *root, *default_location;
603 GIcon *icon;
604 char **x_content_types;
605 char *type_name;
606 const gchar *sort_key;
608 for (c = 0, l = mounts; l != NULL; l = l->next, c++)
610 mount = (GMount *) l->data;
612 if (only_with_no_volume)
614 volume = g_mount_get_volume (mount);
615 if (volume != NULL)
617 g_object_unref (volume);
618 continue;
622 name = g_mount_get_name (mount);
623 root = g_mount_get_root (mount);
624 uri = g_file_get_uri (root);
626 g_print ("%*sMount(%d): %s -> %s\n", indent, "", c, name, uri);
628 type_name = get_type_name (mount);
629 g_print ("%*sType: %s\n", indent+2, "", type_name);
630 g_free (type_name);
632 if (extra_detail)
634 uuid = g_mount_get_uuid (mount);
635 if (uuid)
636 g_print ("%*suuid=%s\n", indent + 2, "", uuid);
638 default_location = g_mount_get_default_location (mount);
639 if (default_location)
641 char *loc_uri = g_file_get_uri (default_location);
642 g_print ("%*sdefault_location=%s\n", indent + 2, "", loc_uri);
643 g_free (loc_uri);
644 g_object_unref (default_location);
647 icon = g_mount_get_icon (mount);
648 if (icon)
650 if (G_IS_THEMED_ICON (icon))
651 show_themed_icon_names (G_THEMED_ICON (icon), FALSE, indent + 2);
653 g_object_unref (icon);
656 icon = g_mount_get_symbolic_icon (mount);
657 if (icon)
659 if (G_IS_THEMED_ICON (icon))
660 show_themed_icon_names (G_THEMED_ICON (icon), TRUE, indent + 2);
662 g_object_unref (icon);
665 x_content_types = g_mount_guess_content_type_sync (mount, FALSE, NULL, NULL);
666 if (x_content_types != NULL && g_strv_length (x_content_types) > 0)
668 int n;
669 g_print ("%*sx_content_types:", indent + 2, "");
670 for (n = 0; x_content_types[n] != NULL; n++)
671 g_print (" %s", x_content_types[n]);
672 g_print ("\n");
674 g_strfreev (x_content_types);
676 g_print ("%*scan_unmount=%d\n", indent + 2, "", g_mount_can_unmount (mount));
677 g_print ("%*scan_eject=%d\n", indent + 2, "", g_mount_can_eject (mount));
678 g_print ("%*sis_shadowed=%d\n", indent + 2, "", g_mount_is_shadowed (mount));
679 sort_key = g_mount_get_sort_key (mount);
680 if (sort_key != NULL)
681 g_print ("%*ssort_key=%s\n", indent + 2, "", sort_key);
682 g_free (uuid);
685 g_object_unref (root);
686 g_free (name);
687 g_free (uri);
691 static void
692 list_volumes (GList *volumes,
693 int indent,
694 gboolean only_with_no_drive)
696 GList *l, *mounts;
697 int c, i;
698 GMount *mount;
699 GVolume *volume;
700 GDrive *drive;
701 char *name;
702 char *uuid;
703 GFile *activation_root;
704 char **ids;
705 GIcon *icon;
706 char *type_name;
707 const gchar *sort_key;
709 for (c = 0, l = volumes; l != NULL; l = l->next, c++)
711 volume = (GVolume *) l->data;
713 if (only_with_no_drive)
715 drive = g_volume_get_drive (volume);
716 if (drive != NULL)
718 g_object_unref (drive);
719 continue;
723 name = g_volume_get_name (volume);
725 g_print ("%*sVolume(%d): %s\n", indent, "", c, name);
726 g_free (name);
728 type_name = get_type_name (volume);
729 g_print ("%*sType: %s\n", indent+2, "", type_name);
730 g_free (type_name);
732 if (extra_detail)
734 ids = g_volume_enumerate_identifiers (volume);
735 if (ids && ids[0] != NULL)
737 g_print ("%*sids:\n", indent+2, "");
738 for (i = 0; ids[i] != NULL; i++)
740 char *id = g_volume_get_identifier (volume,
741 ids[i]);
742 g_print ("%*s %s: '%s'\n", indent+2, "", ids[i], id);
743 g_free (id);
746 g_strfreev (ids);
748 uuid = g_volume_get_uuid (volume);
749 if (uuid)
750 g_print ("%*suuid=%s\n", indent + 2, "", uuid);
751 activation_root = g_volume_get_activation_root (volume);
752 if (activation_root)
754 char *uri;
755 uri = g_file_get_uri (activation_root);
756 g_print ("%*sactivation_root=%s\n", indent + 2, "", uri);
757 g_free (uri);
758 g_object_unref (activation_root);
760 icon = g_volume_get_icon (volume);
761 if (icon)
763 if (G_IS_THEMED_ICON (icon))
764 show_themed_icon_names (G_THEMED_ICON (icon), FALSE, indent + 2);
766 g_object_unref (icon);
769 icon = g_volume_get_symbolic_icon (volume);
770 if (icon)
772 if (G_IS_THEMED_ICON (icon))
773 show_themed_icon_names (G_THEMED_ICON (icon), TRUE, indent + 2);
775 g_object_unref (icon);
778 g_print ("%*scan_mount=%d\n", indent + 2, "", g_volume_can_mount (volume));
779 g_print ("%*scan_eject=%d\n", indent + 2, "", g_volume_can_eject (volume));
780 g_print ("%*sshould_automount=%d\n", indent + 2, "", g_volume_should_automount (volume));
781 sort_key = g_volume_get_sort_key (volume);
782 if (sort_key != NULL)
783 g_print ("%*ssort_key=%s\n", indent + 2, "", sort_key);
784 g_free (uuid);
787 mount = g_volume_get_mount (volume);
788 if (mount)
790 mounts = g_list_prepend (NULL, mount);
791 list_mounts (mounts, indent + 2, FALSE);
792 g_list_free (mounts);
793 g_object_unref (mount);
798 static void
799 list_drives (GList *drives,
800 int indent)
802 GList *volumes, *l;
803 int c, i;
804 GDrive *drive;
805 char *name;
806 char **ids;
807 GIcon *icon;
808 char *type_name;
809 const gchar *sort_key;
811 for (c = 0, l = drives; l != NULL; l = l->next, c++)
813 drive = (GDrive *) l->data;
814 name = g_drive_get_name (drive);
816 g_print ("%*sDrive(%d): %s\n", indent, "", c, name);
817 g_free (name);
819 type_name = get_type_name (drive);
820 g_print ("%*sType: %s\n", indent+2, "", type_name);
821 g_free (type_name);
823 if (extra_detail)
825 GEnumValue *enum_value;
826 gpointer klass;
828 ids = g_drive_enumerate_identifiers (drive);
829 if (ids && ids[0] != NULL)
831 g_print ("%*sids:\n", indent+2, "");
832 for (i = 0; ids[i] != NULL; i++)
834 char *id = g_drive_get_identifier (drive,
835 ids[i]);
836 g_print ("%*s %s: '%s'\n", indent+2, "", ids[i], id);
837 g_free (id);
840 g_strfreev (ids);
842 icon = g_drive_get_icon (drive);
843 if (icon)
845 if (G_IS_THEMED_ICON (icon))
846 show_themed_icon_names (G_THEMED_ICON (icon), FALSE, indent + 2);
847 g_object_unref (icon);
850 icon = g_drive_get_symbolic_icon (drive);
851 if (icon)
853 if (G_IS_THEMED_ICON (icon))
854 show_themed_icon_names (G_THEMED_ICON (icon), TRUE, indent + 2);
856 g_object_unref (icon);
859 g_print ("%*sis_removable=%d\n", indent + 2, "", g_drive_is_removable (drive));
860 g_print ("%*sis_media_removable=%d\n", indent + 2, "", g_drive_is_media_removable (drive));
861 g_print ("%*shas_media=%d\n", indent + 2, "", g_drive_has_media (drive));
862 g_print ("%*sis_media_check_automatic=%d\n", indent + 2, "", g_drive_is_media_check_automatic (drive));
863 g_print ("%*scan_poll_for_media=%d\n", indent + 2, "", g_drive_can_poll_for_media (drive));
864 g_print ("%*scan_eject=%d\n", indent + 2, "", g_drive_can_eject (drive));
865 g_print ("%*scan_start=%d\n", indent + 2, "", g_drive_can_start (drive));
866 g_print ("%*scan_stop=%d\n", indent + 2, "", g_drive_can_stop (drive));
868 enum_value = NULL;
869 klass = g_type_class_ref (G_TYPE_DRIVE_START_STOP_TYPE);
870 if (klass != NULL)
872 enum_value = g_enum_get_value (klass, g_drive_get_start_stop_type (drive));
873 g_print ("%*sstart_stop_type=%s\n", indent + 2, "",
874 enum_value != NULL ? enum_value->value_nick : "UNKNOWN");
875 g_type_class_unref (klass);
878 sort_key = g_drive_get_sort_key (drive);
879 if (sort_key != NULL)
880 g_print ("%*ssort_key=%s\n", indent + 2, "", sort_key);
882 volumes = g_drive_get_volumes (drive);
883 list_volumes (volumes, indent + 2, FALSE);
884 g_list_free_full (volumes, g_object_unref);
889 static void
890 list_monitor_items (void)
892 GVolumeMonitor *volume_monitor;
893 GList *drives, *volumes, *mounts;
895 volume_monitor = g_volume_monitor_get();
897 /* populate gvfs network mounts */
898 iterate_gmain();
900 drives = g_volume_monitor_get_connected_drives (volume_monitor);
901 list_drives (drives, 0);
902 g_list_free_full (drives, g_object_unref);
904 volumes = g_volume_monitor_get_volumes (volume_monitor);
905 list_volumes (volumes, 0, TRUE);
906 g_list_free_full (volumes, g_object_unref);
908 mounts = g_volume_monitor_get_mounts (volume_monitor);
909 list_mounts (mounts, 0, TRUE);
910 g_list_free_full (mounts, g_object_unref);
912 g_object_unref (volume_monitor);
915 static void
916 unmount_all_with_scheme (const char *scheme)
918 GVolumeMonitor *volume_monitor;
919 GList *mounts;
920 GList *l;
922 volume_monitor = g_volume_monitor_get();
924 /* populate gvfs network mounts */
925 iterate_gmain();
927 mounts = g_volume_monitor_get_mounts (volume_monitor);
928 for (l = mounts; l != NULL; l = l->next) {
929 GMount *mount = G_MOUNT (l->data);
930 GFile *root;
932 root = g_mount_get_root (mount);
933 if (g_file_has_uri_scheme (root, scheme)) {
934 unmount (root);
936 g_object_unref (root);
938 g_list_free_full (mounts, g_object_unref);
940 g_object_unref (volume_monitor);
943 static void
944 mount_with_device_file_cb (GObject *object,
945 GAsyncResult *res,
946 gpointer user_data)
948 GVolume *volume;
949 gboolean succeeded;
950 GError *error = NULL;
951 gchar *device_path = (gchar *)user_data;
953 volume = G_VOLUME (object);
955 succeeded = g_volume_mount_finish (volume, res, &error);
957 if (!succeeded)
959 print_error ("%s: %s", device_path, error->message);
960 g_error_free (error);
961 success = FALSE;
963 else
965 GMount *mount;
966 GFile *root;
967 char *mount_path;
969 mount = g_volume_get_mount (volume);
970 root = g_mount_get_root (mount);
971 mount_path = g_file_get_path (root);
973 g_print (_("Mounted %s at %s\n"), device_path, mount_path);
975 g_object_unref (mount);
976 g_object_unref (root);
977 g_free (mount_path);
980 g_free (device_path);
982 outstanding_mounts--;
984 if (outstanding_mounts == 0)
985 g_main_loop_quit (main_loop);
988 static void
989 mount_with_device_file (const char *device_file)
991 GVolumeMonitor *volume_monitor;
992 GList *volumes;
993 GList *l;
995 volume_monitor = g_volume_monitor_get();
997 volumes = g_volume_monitor_get_volumes (volume_monitor);
998 for (l = volumes; l != NULL; l = l->next)
1000 GVolume *volume = G_VOLUME (l->data);
1001 gchar *id;
1003 id = g_volume_get_identifier (volume, G_VOLUME_IDENTIFIER_KIND_UNIX_DEVICE);
1004 if (g_strcmp0 (id, device_file) == 0)
1006 GMountOperation *op;
1008 op = new_mount_op ();
1010 g_volume_mount (volume,
1011 G_MOUNT_MOUNT_NONE,
1013 NULL,
1014 mount_with_device_file_cb,
1015 id);
1017 g_object_unref (op);
1019 outstanding_mounts++;
1021 else
1022 g_free (id);
1024 g_list_free_full (volumes, g_object_unref);
1026 if (outstanding_mounts == 0)
1028 print_error ("%s: %s", device_file, _("No volume for device file"));
1029 success = FALSE;
1032 g_object_unref (volume_monitor);
1035 static void
1036 monitor_print_mount (GMount *mount)
1038 if (extra_detail)
1040 GList *l;
1041 l = g_list_prepend (NULL, mount);
1042 list_mounts (l, 2, FALSE);
1043 g_list_free (l);
1044 g_print ("\n");
1048 static void
1049 monitor_print_volume (GVolume *volume)
1051 if (extra_detail)
1053 GList *l;
1054 l = g_list_prepend (NULL, volume);
1055 list_volumes (l, 2, FALSE);
1056 g_list_free (l);
1057 g_print ("\n");
1061 static void
1062 monitor_print_drive (GDrive *drive)
1064 if (extra_detail)
1066 GList *l;
1067 l = g_list_prepend (NULL, drive);
1068 list_drives (l, 2);
1069 g_list_free (l);
1070 g_print ("\n");
1074 static void
1075 monitor_mount_added (GVolumeMonitor *volume_monitor, GMount *mount)
1077 char *name;
1078 name = g_mount_get_name (mount);
1079 g_print ("Mount added: '%s'\n", name);
1080 g_free (name);
1081 monitor_print_mount (mount);
1084 static void
1085 monitor_mount_removed (GVolumeMonitor *volume_monitor, GMount *mount)
1087 char *name;
1088 name = g_mount_get_name (mount);
1089 g_print ("Mount removed: '%s'\n", name);
1090 g_free (name);
1091 monitor_print_mount (mount);
1094 static void
1095 monitor_mount_changed (GVolumeMonitor *volume_monitor, GMount *mount)
1097 char *name;
1098 name = g_mount_get_name (mount);
1099 g_print ("Mount changed: '%s'\n", name);
1100 g_free (name);
1101 monitor_print_mount (mount);
1104 static void
1105 monitor_mount_pre_unmount (GVolumeMonitor *volume_monitor, GMount *mount)
1107 char *name;
1108 name = g_mount_get_name (mount);
1109 g_print ("Mount pre-unmount: '%s'\n", name);
1110 g_free (name);
1111 monitor_print_mount (mount);
1114 static void
1115 monitor_volume_added (GVolumeMonitor *volume_monitor, GVolume *volume)
1117 char *name;
1118 name = g_volume_get_name (volume);
1119 g_print ("Volume added: '%s'\n", name);
1120 g_free (name);
1121 monitor_print_volume (volume);
1124 static void
1125 monitor_volume_removed (GVolumeMonitor *volume_monitor, GVolume *volume)
1127 char *name;
1128 name = g_volume_get_name (volume);
1129 g_print ("Volume removed: '%s'\n", name);
1130 g_free (name);
1131 monitor_print_volume (volume);
1134 static void
1135 monitor_volume_changed (GVolumeMonitor *volume_monitor, GVolume *volume)
1137 char *name;
1138 name = g_volume_get_name (volume);
1139 g_print ("Volume changed: '%s'\n", name);
1140 g_free (name);
1141 monitor_print_volume (volume);
1144 static void
1145 monitor_drive_connected (GVolumeMonitor *volume_monitor, GDrive *drive)
1147 char *name;
1148 name = g_drive_get_name (drive);
1149 g_print ("Drive connected: '%s'\n", name);
1150 g_free (name);
1151 monitor_print_drive (drive);
1154 static void
1155 monitor_drive_disconnected (GVolumeMonitor *volume_monitor, GDrive *drive)
1157 char *name;
1158 name = g_drive_get_name (drive);
1159 g_print ("Drive disconnected: '%s'\n", name);
1160 g_free (name);
1161 monitor_print_drive (drive);
1164 static void
1165 monitor_drive_changed (GVolumeMonitor *volume_monitor, GDrive *drive)
1167 char *name;
1168 name = g_drive_get_name (drive);
1169 g_print ("Drive changed: '%s'\n", name);
1170 g_free (name);
1171 monitor_print_drive (drive);
1174 static void
1175 monitor_drive_eject_button (GVolumeMonitor *volume_monitor, GDrive *drive)
1177 char *name;
1178 name = g_drive_get_name (drive);
1179 g_print ("Drive eject button: '%s'\n", name);
1180 g_free (name);
1183 static void
1184 monitor (void)
1186 GVolumeMonitor *volume_monitor;
1188 volume_monitor = g_volume_monitor_get ();
1190 g_signal_connect (volume_monitor, "mount-added", (GCallback) monitor_mount_added, NULL);
1191 g_signal_connect (volume_monitor, "mount-removed", (GCallback) monitor_mount_removed, NULL);
1192 g_signal_connect (volume_monitor, "mount-changed", (GCallback) monitor_mount_changed, NULL);
1193 g_signal_connect (volume_monitor, "mount-pre-unmount", (GCallback) monitor_mount_pre_unmount, NULL);
1194 g_signal_connect (volume_monitor, "volume-added", (GCallback) monitor_volume_added, NULL);
1195 g_signal_connect (volume_monitor, "volume-removed", (GCallback) monitor_volume_removed, NULL);
1196 g_signal_connect (volume_monitor, "volume-changed", (GCallback) monitor_volume_changed, NULL);
1197 g_signal_connect (volume_monitor, "drive-connected", (GCallback) monitor_drive_connected, NULL);
1198 g_signal_connect (volume_monitor, "drive-disconnected", (GCallback) monitor_drive_disconnected, NULL);
1199 g_signal_connect (volume_monitor, "drive-changed", (GCallback) monitor_drive_changed, NULL);
1200 g_signal_connect (volume_monitor, "drive-eject-button", (GCallback) monitor_drive_eject_button, NULL);
1202 g_print ("Monitoring events. Press Ctrl+C to quit.\n");
1204 g_main_loop_run (main_loop);
1208 handle_mount (int argc, char *argv[], gboolean do_help)
1210 GOptionContext *context;
1211 gchar *param;
1212 GError *error = NULL;
1213 GFile *file;
1214 int i;
1216 g_set_prgname ("gio mount");
1218 /* Translators: commandline placeholder */
1219 param = g_strdup_printf ("[%s…]", _("LOCATION"));
1220 context = g_option_context_new (param);
1221 g_free (param);
1222 g_option_context_set_help_enabled (context, FALSE);
1223 g_option_context_set_summary (context, _("Mount or unmount the locations."));
1224 g_option_context_add_main_entries (context, entries, GETTEXT_PACKAGE);
1226 if (do_help)
1228 show_help (context, NULL);
1229 g_option_context_free (context);
1230 return 0;
1233 if (!g_option_context_parse (context, &argc, &argv, &error))
1235 show_help (context, error->message);
1236 g_error_free (error);
1237 g_option_context_free (context);
1238 return 1;
1241 main_loop = g_main_loop_new (NULL, FALSE);
1243 if (mount_list)
1244 list_monitor_items ();
1245 else if (mount_device_file != NULL)
1246 mount_with_device_file (mount_device_file);
1247 else if (stop_device_file)
1248 stop_with_device_file (stop_device_file);
1249 else if (unmount_scheme != NULL)
1250 unmount_all_with_scheme (unmount_scheme);
1251 else if (mount_monitor)
1252 monitor ();
1253 else if (argc > 1)
1255 for (i = 1; i < argc; i++)
1257 file = g_file_new_for_commandline_arg (argv[i]);
1258 if (mount_unmount)
1259 unmount (file);
1260 else if (mount_eject)
1261 eject (file);
1262 else
1263 mount (file);
1264 g_object_unref (file);
1267 else
1269 show_help (context, _("No locations given"));
1270 g_option_context_free (context);
1271 return 1;
1274 g_option_context_free (context);
1276 if (outstanding_mounts > 0)
1277 g_main_loop_run (main_loop);
1279 return success ? 0 : 2;