build: Include host_machine.cpu_family() in tapset directory (Meson)
[glib.git] / gio / gio-tool-mount.c
blob9522713e9e69d4f797c95be8cc3e911ce319dfc5
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 gboolean success = TRUE;
56 static const GOptionEntry entries[] =
58 { "mountable", 'm', 0, G_OPTION_ARG_NONE, &mount_mountable, N_("Mount as mountable"), NULL },
59 { "device", 'd', 0, G_OPTION_ARG_STRING, &mount_device_file, N_("Mount volume with device file"), N_("DEVICE") },
60 { "unmount", 'u', 0, G_OPTION_ARG_NONE, &mount_unmount, N_("Unmount"), NULL},
61 { "eject", 'e', 0, G_OPTION_ARG_NONE, &mount_eject, N_("Eject"), NULL},
62 { "unmount-scheme", 's', 0, G_OPTION_ARG_STRING, &unmount_scheme, N_("Unmount all mounts with the given scheme"), N_("SCHEME") },
63 { "force", 'f', 0, G_OPTION_ARG_NONE, &force, N_("Ignore outstanding file operations when unmounting or ejecting"), NULL },
64 { "anonymous", 'a', 0, G_OPTION_ARG_NONE, &anonymous, N_("Use an anonymous user when authenticating"), NULL },
65 /* Translator: List here is a verb as in 'List all mounts' */
66 { "list", 'l', 0, G_OPTION_ARG_NONE, &mount_list, N_("List"), NULL},
67 { "monitor", 'o', 0, G_OPTION_ARG_NONE, &mount_monitor, N_("Monitor events"), NULL},
68 { "detail", 'i', 0, G_OPTION_ARG_NONE, &extra_detail, N_("Show extra information"), NULL},
69 { NULL }
72 static char *
73 prompt_for (const char *prompt, const char *default_value, gboolean echo)
75 #ifdef HAVE_TERMIOS_H
76 struct termios term_attr;
77 int old_flags;
78 gboolean restore_flags;
79 #endif
80 char data[256];
81 int len;
83 if (default_value && *default_value != 0)
84 g_print ("%s [%s]: ", prompt, default_value);
85 else
86 g_print ("%s: ", prompt);
88 data[0] = 0;
90 #ifdef HAVE_TERMIOS_H
91 restore_flags = FALSE;
92 if (!echo && tcgetattr (STDIN_FILENO, &term_attr) == 0)
94 old_flags = term_attr.c_lflag;
95 term_attr.c_lflag &= ~ECHO;
96 restore_flags = TRUE;
98 if (tcsetattr (STDIN_FILENO, TCSAFLUSH, &term_attr) != 0)
99 g_print ("Warning! Password will be echoed");
102 #endif
104 fgets(data, sizeof (data), stdin);
106 #ifdef HAVE_TERMIOS_H
107 if (restore_flags)
109 term_attr.c_lflag = old_flags;
110 tcsetattr (STDIN_FILENO, TCSAFLUSH, &term_attr);
112 #endif
114 len = strlen (data);
115 if (len == 0)
117 g_print ("\n");
118 return NULL;
120 if (data[len-1] == '\n')
121 data[len-1] = 0;
123 if (!echo)
124 g_print ("\n");
126 if (*data == 0 && default_value)
127 return g_strdup (default_value);
128 return g_strdup (data);
131 static void
132 ask_password_cb (GMountOperation *op,
133 const char *message,
134 const char *default_user,
135 const char *default_domain,
136 GAskPasswordFlags flags)
138 if ((flags & G_ASK_PASSWORD_ANONYMOUS_SUPPORTED) && anonymous)
140 g_mount_operation_set_anonymous (op, TRUE);
142 else
144 char *s;
145 g_print ("%s\n", message);
147 if (flags & G_ASK_PASSWORD_NEED_USERNAME)
149 s = prompt_for ("User", default_user, TRUE);
150 if (!s)
151 goto error;
152 g_mount_operation_set_username (op, s);
153 g_free (s);
156 if (flags & G_ASK_PASSWORD_NEED_DOMAIN)
158 s = prompt_for ("Domain", default_domain, TRUE);
159 if (!s)
160 goto error;
161 g_mount_operation_set_domain (op, s);
162 g_free (s);
165 if (flags & G_ASK_PASSWORD_NEED_PASSWORD)
167 s = prompt_for ("Password", NULL, FALSE);
168 if (!s)
169 goto error;
170 g_mount_operation_set_password (op, s);
171 g_free (s);
175 /* Only try anonymous access once. */
176 if (anonymous &&
177 GPOINTER_TO_INT (g_object_get_data (G_OBJECT (op), "state")) == MOUNT_OP_ASKED)
179 g_object_set_data (G_OBJECT (op), "state", GINT_TO_POINTER (MOUNT_OP_ABORTED));
180 g_mount_operation_reply (op, G_MOUNT_OPERATION_ABORTED);
182 else
184 g_object_set_data (G_OBJECT (op), "state", GINT_TO_POINTER (MOUNT_OP_ASKED));
185 g_mount_operation_reply (op, G_MOUNT_OPERATION_HANDLED);
188 return;
190 error:
191 g_mount_operation_reply (op, G_MOUNT_OPERATION_ABORTED);
194 static void
195 ask_question_cb (GMountOperation *op,
196 char *message,
197 char **choices,
198 gpointer user_data)
200 char **ptr = choices;
201 char *s;
202 int i, choice;
204 g_print ("%s\n", message);
206 i = 1;
207 while (*ptr)
209 g_print ("[%d] %s\n", i, *ptr++);
210 i++;
213 s = prompt_for ("Choice", NULL, TRUE);
214 if (!s)
215 goto error;
217 choice = atoi (s);
218 if (choice > 0 && choice < i)
220 g_mount_operation_set_choice (op, choice - 1);
221 g_mount_operation_reply (op, G_MOUNT_OPERATION_HANDLED);
223 g_free (s);
225 return;
227 error:
228 g_mount_operation_reply (op, G_MOUNT_OPERATION_ABORTED);
231 static void
232 mount_mountable_done_cb (GObject *object,
233 GAsyncResult *res,
234 gpointer user_data)
236 GFile *target;
237 GError *error = NULL;
238 GMountOperation *op = user_data;
240 target = g_file_mount_mountable_finish (G_FILE (object), res, &error);
242 if (target == NULL)
244 success = FALSE;
245 if (GPOINTER_TO_INT (g_object_get_data (G_OBJECT (op), "state")) == MOUNT_OP_ABORTED)
246 print_file_error (G_FILE (object), _("Anonymous access denied"));
247 else if (!g_error_matches (error, G_IO_ERROR, G_IO_ERROR_FAILED_HANDLED))
248 print_file_error (G_FILE (object), error->message);
250 g_error_free (error);
252 else
253 g_object_unref (target);
255 outstanding_mounts--;
257 if (outstanding_mounts == 0)
258 g_main_loop_quit (main_loop);
261 static void
262 mount_done_cb (GObject *object,
263 GAsyncResult *res,
264 gpointer user_data)
266 gboolean succeeded;
267 GError *error = NULL;
268 GMountOperation *op = user_data;
270 succeeded = g_file_mount_enclosing_volume_finish (G_FILE (object), res, &error);
272 if (!succeeded)
274 success = FALSE;
275 if (GPOINTER_TO_INT (g_object_get_data (G_OBJECT (op), "state")) == MOUNT_OP_ABORTED)
276 print_file_error (G_FILE (object), _("Anonymous access denied"));
277 else if (!g_error_matches (error, G_IO_ERROR, G_IO_ERROR_FAILED_HANDLED))
278 print_file_error (G_FILE (object), error->message);
280 g_error_free (error);
283 outstanding_mounts--;
285 if (outstanding_mounts == 0)
286 g_main_loop_quit (main_loop);
289 static GMountOperation *
290 new_mount_op (void)
292 GMountOperation *op;
294 op = g_mount_operation_new ();
296 g_object_set_data (G_OBJECT (op), "state", GINT_TO_POINTER (MOUNT_OP_NONE));
298 g_signal_connect (op, "ask_password", G_CALLBACK (ask_password_cb), NULL);
299 g_signal_connect (op, "ask_question", G_CALLBACK (ask_question_cb), NULL);
301 /* TODO: we *should* also connect to the "aborted" signal but since the
302 * main thread is blocked handling input we won't get that signal anyway...
305 return op;
309 static void
310 mount (GFile *file)
312 GMountOperation *op;
314 if (file == NULL)
315 return;
317 op = new_mount_op ();
319 if (mount_mountable)
320 g_file_mount_mountable (file, 0, op, NULL, mount_mountable_done_cb, op);
321 else
322 g_file_mount_enclosing_volume (file, 0, op, NULL, mount_done_cb, op);
324 outstanding_mounts++;
327 static void
328 unmount_done_cb (GObject *object,
329 GAsyncResult *res,
330 gpointer user_data)
332 gboolean succeeded;
333 GError *error = NULL;
334 GFile *file = G_FILE (user_data);
336 succeeded = g_mount_unmount_with_operation_finish (G_MOUNT (object), res, &error);
338 g_object_unref (G_MOUNT (object));
340 if (!succeeded)
342 print_file_error (file, error->message);
343 success = FALSE;
344 g_error_free (error);
347 g_object_unref (file);
349 outstanding_mounts--;
351 if (outstanding_mounts == 0)
352 g_main_loop_quit (main_loop);
355 static void
356 unmount (GFile *file)
358 GMount *mount;
359 GError *error = NULL;
360 GMountOperation *mount_op;
361 GMountUnmountFlags flags;
363 if (file == NULL)
364 return;
366 mount = g_file_find_enclosing_mount (file, NULL, &error);
367 if (mount == NULL)
369 print_file_error (file, error->message);
370 success = FALSE;
371 g_error_free (error);
372 return;
375 mount_op = new_mount_op ();
376 flags = force ? G_MOUNT_UNMOUNT_FORCE : G_MOUNT_UNMOUNT_NONE;
377 g_mount_unmount_with_operation (mount, flags, mount_op, NULL, unmount_done_cb, g_object_ref (file));
378 g_object_unref (mount_op);
380 outstanding_mounts++;
383 static void
384 eject_done_cb (GObject *object,
385 GAsyncResult *res,
386 gpointer user_data)
388 gboolean succeeded;
389 GError *error = NULL;
390 GFile *file = G_FILE (user_data);
392 succeeded = g_mount_eject_with_operation_finish (G_MOUNT (object), res, &error);
394 g_object_unref (G_MOUNT (object));
396 if (!succeeded)
398 print_file_error (file, error->message);
399 success = FALSE;
400 g_error_free (error);
403 g_object_unref (file);
405 outstanding_mounts--;
407 if (outstanding_mounts == 0)
408 g_main_loop_quit (main_loop);
411 static void
412 eject (GFile *file)
414 GMount *mount;
415 GError *error = NULL;
416 GMountOperation *mount_op;
417 GMountUnmountFlags flags;
419 if (file == NULL)
420 return;
422 mount = g_file_find_enclosing_mount (file, NULL, &error);
423 if (mount == NULL)
425 print_file_error (file, error->message);
426 success = FALSE;
427 g_error_free (error);
428 return;
431 mount_op = new_mount_op ();
432 flags = force ? G_MOUNT_UNMOUNT_FORCE : G_MOUNT_UNMOUNT_NONE;
433 g_mount_eject_with_operation (mount, flags, mount_op, NULL, eject_done_cb, g_object_ref (file));
434 g_object_unref (mount_op);
436 outstanding_mounts++;
439 static gboolean
440 iterate_gmain_timeout_function (gpointer data)
442 g_main_loop_quit (main_loop);
443 return FALSE;
446 static void
447 iterate_gmain(void)
449 g_timeout_add (500, iterate_gmain_timeout_function, NULL);
450 g_main_loop_run (main_loop);
453 static void
454 show_themed_icon_names (GThemedIcon *icon, gboolean symbolic, int indent)
456 char **names;
457 char **iter;
459 g_print ("%*s%sthemed icons:", indent, " ", symbolic ? "symbolic " : "");
461 names = NULL;
463 g_object_get (icon, "names", &names, NULL);
465 for (iter = names; *iter; iter++)
466 g_print (" [%s]", *iter);
468 g_print ("\n");
469 g_strfreev (names);
472 /* don't copy-paste this code */
473 static char *
474 get_type_name (gpointer object)
476 const char *type_name;
477 char *ret;
479 type_name = g_type_name (G_TYPE_FROM_INSTANCE (object));
480 if (strcmp ("GProxyDrive", type_name) == 0)
482 ret = g_strdup_printf ("%s (%s)",
483 type_name,
484 (const char *) g_object_get_data (G_OBJECT (object),
485 "g-proxy-drive-volume-monitor-name"));
487 else if (strcmp ("GProxyVolume", type_name) == 0)
489 ret = g_strdup_printf ("%s (%s)",
490 type_name,
491 (const char *) g_object_get_data (G_OBJECT (object),
492 "g-proxy-volume-volume-monitor-name"));
494 else if (strcmp ("GProxyMount", type_name) == 0)
496 ret = g_strdup_printf ("%s (%s)",
497 type_name,
498 (const char *) g_object_get_data (G_OBJECT (object),
499 "g-proxy-mount-volume-monitor-name"));
501 else if (strcmp ("GProxyShadowMount", type_name) == 0)
503 ret = g_strdup_printf ("%s (%s)",
504 type_name,
505 (const char *) g_object_get_data (G_OBJECT (object),
506 "g-proxy-shadow-mount-volume-monitor-name"));
508 else
510 ret = g_strdup (type_name);
513 return ret;
516 static void
517 list_mounts (GList *mounts,
518 int indent,
519 gboolean only_with_no_volume)
521 GList *l;
522 int c;
523 GMount *mount;
524 GVolume *volume;
525 char *name, *uuid, *uri;
526 GFile *root, *default_location;
527 GIcon *icon;
528 char **x_content_types;
529 char *type_name;
530 const gchar *sort_key;
532 for (c = 0, l = mounts; l != NULL; l = l->next, c++)
534 mount = (GMount *) l->data;
536 if (only_with_no_volume)
538 volume = g_mount_get_volume (mount);
539 if (volume != NULL)
541 g_object_unref (volume);
542 continue;
546 name = g_mount_get_name (mount);
547 root = g_mount_get_root (mount);
548 uri = g_file_get_uri (root);
550 g_print ("%*sMount(%d): %s -> %s\n", indent, "", c, name, uri);
552 type_name = get_type_name (mount);
553 g_print ("%*sType: %s\n", indent+2, "", type_name);
554 g_free (type_name);
556 if (extra_detail)
558 uuid = g_mount_get_uuid (mount);
559 if (uuid)
560 g_print ("%*suuid=%s\n", indent + 2, "", uuid);
562 default_location = g_mount_get_default_location (mount);
563 if (default_location)
565 char *loc_uri = g_file_get_uri (default_location);
566 g_print ("%*sdefault_location=%s\n", indent + 2, "", loc_uri);
567 g_free (loc_uri);
568 g_object_unref (default_location);
571 icon = g_mount_get_icon (mount);
572 if (icon)
574 if (G_IS_THEMED_ICON (icon))
575 show_themed_icon_names (G_THEMED_ICON (icon), FALSE, indent + 2);
577 g_object_unref (icon);
580 icon = g_mount_get_symbolic_icon (mount);
581 if (icon)
583 if (G_IS_THEMED_ICON (icon))
584 show_themed_icon_names (G_THEMED_ICON (icon), TRUE, indent + 2);
586 g_object_unref (icon);
589 x_content_types = g_mount_guess_content_type_sync (mount, FALSE, NULL, NULL);
590 if (x_content_types != NULL && g_strv_length (x_content_types) > 0)
592 int n;
593 g_print ("%*sx_content_types:", indent + 2, "");
594 for (n = 0; x_content_types[n] != NULL; n++)
595 g_print (" %s", x_content_types[n]);
596 g_print ("\n");
598 g_strfreev (x_content_types);
600 g_print ("%*scan_unmount=%d\n", indent + 2, "", g_mount_can_unmount (mount));
601 g_print ("%*scan_eject=%d\n", indent + 2, "", g_mount_can_eject (mount));
602 g_print ("%*sis_shadowed=%d\n", indent + 2, "", g_mount_is_shadowed (mount));
603 sort_key = g_mount_get_sort_key (mount);
604 if (sort_key != NULL)
605 g_print ("%*ssort_key=%s\n", indent + 2, "", sort_key);
606 g_free (uuid);
609 g_object_unref (root);
610 g_free (name);
611 g_free (uri);
615 static void
616 list_volumes (GList *volumes,
617 int indent,
618 gboolean only_with_no_drive)
620 GList *l, *mounts;
621 int c, i;
622 GMount *mount;
623 GVolume *volume;
624 GDrive *drive;
625 char *name;
626 char *uuid;
627 GFile *activation_root;
628 char **ids;
629 GIcon *icon;
630 char *type_name;
631 const gchar *sort_key;
633 for (c = 0, l = volumes; l != NULL; l = l->next, c++)
635 volume = (GVolume *) l->data;
637 if (only_with_no_drive)
639 drive = g_volume_get_drive (volume);
640 if (drive != NULL)
642 g_object_unref (drive);
643 continue;
647 name = g_volume_get_name (volume);
649 g_print ("%*sVolume(%d): %s\n", indent, "", c, name);
650 g_free (name);
652 type_name = get_type_name (volume);
653 g_print ("%*sType: %s\n", indent+2, "", type_name);
654 g_free (type_name);
656 if (extra_detail)
658 ids = g_volume_enumerate_identifiers (volume);
659 if (ids && ids[0] != NULL)
661 g_print ("%*sids:\n", indent+2, "");
662 for (i = 0; ids[i] != NULL; i++)
664 char *id = g_volume_get_identifier (volume,
665 ids[i]);
666 g_print ("%*s %s: '%s'\n", indent+2, "", ids[i], id);
667 g_free (id);
670 g_strfreev (ids);
672 uuid = g_volume_get_uuid (volume);
673 if (uuid)
674 g_print ("%*suuid=%s\n", indent + 2, "", uuid);
675 activation_root = g_volume_get_activation_root (volume);
676 if (activation_root)
678 char *uri;
679 uri = g_file_get_uri (activation_root);
680 g_print ("%*sactivation_root=%s\n", indent + 2, "", uri);
681 g_free (uri);
682 g_object_unref (activation_root);
684 icon = g_volume_get_icon (volume);
685 if (icon)
687 if (G_IS_THEMED_ICON (icon))
688 show_themed_icon_names (G_THEMED_ICON (icon), FALSE, indent + 2);
690 g_object_unref (icon);
693 icon = g_volume_get_symbolic_icon (volume);
694 if (icon)
696 if (G_IS_THEMED_ICON (icon))
697 show_themed_icon_names (G_THEMED_ICON (icon), TRUE, indent + 2);
699 g_object_unref (icon);
702 g_print ("%*scan_mount=%d\n", indent + 2, "", g_volume_can_mount (volume));
703 g_print ("%*scan_eject=%d\n", indent + 2, "", g_volume_can_eject (volume));
704 g_print ("%*sshould_automount=%d\n", indent + 2, "", g_volume_should_automount (volume));
705 sort_key = g_volume_get_sort_key (volume);
706 if (sort_key != NULL)
707 g_print ("%*ssort_key=%s\n", indent + 2, "", sort_key);
708 g_free (uuid);
711 mount = g_volume_get_mount (volume);
712 if (mount)
714 mounts = g_list_prepend (NULL, mount);
715 list_mounts (mounts, indent + 2, FALSE);
716 g_list_free (mounts);
717 g_object_unref (mount);
722 static void
723 list_drives (GList *drives,
724 int indent)
726 GList *volumes, *l;
727 int c, i;
728 GDrive *drive;
729 char *name;
730 char **ids;
731 GIcon *icon;
732 char *type_name;
733 const gchar *sort_key;
735 for (c = 0, l = drives; l != NULL; l = l->next, c++)
737 drive = (GDrive *) l->data;
738 name = g_drive_get_name (drive);
740 g_print ("%*sDrive(%d): %s\n", indent, "", c, name);
741 g_free (name);
743 type_name = get_type_name (drive);
744 g_print ("%*sType: %s\n", indent+2, "", type_name);
745 g_free (type_name);
747 if (extra_detail)
749 GEnumValue *enum_value;
750 gpointer klass;
752 ids = g_drive_enumerate_identifiers (drive);
753 if (ids && ids[0] != NULL)
755 g_print ("%*sids:\n", indent+2, "");
756 for (i = 0; ids[i] != NULL; i++)
758 char *id = g_drive_get_identifier (drive,
759 ids[i]);
760 g_print ("%*s %s: '%s'\n", indent+2, "", ids[i], id);
761 g_free (id);
764 g_strfreev (ids);
766 icon = g_drive_get_icon (drive);
767 if (icon)
769 if (G_IS_THEMED_ICON (icon))
770 show_themed_icon_names (G_THEMED_ICON (icon), FALSE, indent + 2);
771 g_object_unref (icon);
774 icon = g_drive_get_symbolic_icon (drive);
775 if (icon)
777 if (G_IS_THEMED_ICON (icon))
778 show_themed_icon_names (G_THEMED_ICON (icon), TRUE, indent + 2);
780 g_object_unref (icon);
783 g_print ("%*sis_removable=%d\n", indent + 2, "", g_drive_is_removable (drive));
784 g_print ("%*sis_media_removable=%d\n", indent + 2, "", g_drive_is_media_removable (drive));
785 g_print ("%*shas_media=%d\n", indent + 2, "", g_drive_has_media (drive));
786 g_print ("%*sis_media_check_automatic=%d\n", indent + 2, "", g_drive_is_media_check_automatic (drive));
787 g_print ("%*scan_poll_for_media=%d\n", indent + 2, "", g_drive_can_poll_for_media (drive));
788 g_print ("%*scan_eject=%d\n", indent + 2, "", g_drive_can_eject (drive));
789 g_print ("%*scan_start=%d\n", indent + 2, "", g_drive_can_start (drive));
790 g_print ("%*scan_stop=%d\n", indent + 2, "", g_drive_can_stop (drive));
792 enum_value = NULL;
793 klass = g_type_class_ref (G_TYPE_DRIVE_START_STOP_TYPE);
794 if (klass != NULL)
796 enum_value = g_enum_get_value (klass, g_drive_get_start_stop_type (drive));
797 g_print ("%*sstart_stop_type=%s\n", indent + 2, "",
798 enum_value != NULL ? enum_value->value_nick : "UNKNOWN");
799 g_type_class_unref (klass);
802 sort_key = g_drive_get_sort_key (drive);
803 if (sort_key != NULL)
804 g_print ("%*ssort_key=%s\n", indent + 2, "", sort_key);
806 volumes = g_drive_get_volumes (drive);
807 list_volumes (volumes, indent + 2, FALSE);
808 g_list_free_full (volumes, g_object_unref);
813 static void
814 list_monitor_items (void)
816 GVolumeMonitor *volume_monitor;
817 GList *drives, *volumes, *mounts;
819 volume_monitor = g_volume_monitor_get();
821 /* populate gvfs network mounts */
822 iterate_gmain();
824 drives = g_volume_monitor_get_connected_drives (volume_monitor);
825 list_drives (drives, 0);
826 g_list_free_full (drives, g_object_unref);
828 volumes = g_volume_monitor_get_volumes (volume_monitor);
829 list_volumes (volumes, 0, TRUE);
830 g_list_free_full (volumes, g_object_unref);
832 mounts = g_volume_monitor_get_mounts (volume_monitor);
833 list_mounts (mounts, 0, TRUE);
834 g_list_free_full (mounts, g_object_unref);
836 g_object_unref (volume_monitor);
839 static void
840 unmount_all_with_scheme (const char *scheme)
842 GVolumeMonitor *volume_monitor;
843 GList *mounts;
844 GList *l;
846 volume_monitor = g_volume_monitor_get();
848 /* populate gvfs network mounts */
849 iterate_gmain();
851 mounts = g_volume_monitor_get_mounts (volume_monitor);
852 for (l = mounts; l != NULL; l = l->next) {
853 GMount *mount = G_MOUNT (l->data);
854 GFile *root;
856 root = g_mount_get_root (mount);
857 if (g_file_has_uri_scheme (root, scheme)) {
858 unmount (root);
860 g_object_unref (root);
862 g_list_free_full (mounts, g_object_unref);
864 g_object_unref (volume_monitor);
867 static void
868 mount_with_device_file_cb (GObject *object,
869 GAsyncResult *res,
870 gpointer user_data)
872 GVolume *volume;
873 gboolean succeeded;
874 GError *error = NULL;
875 gchar *device_path = (gchar *)user_data;
877 volume = G_VOLUME (object);
879 succeeded = g_volume_mount_finish (volume, res, &error);
881 if (!succeeded)
883 print_error ("%s: %s", device_path, error->message);
884 g_error_free (error);
885 success = FALSE;
887 else
889 GMount *mount;
890 GFile *root;
891 char *mount_path;
893 mount = g_volume_get_mount (volume);
894 root = g_mount_get_root (mount);
895 mount_path = g_file_get_path (root);
897 g_print (_("Mounted %s at %s\n"), device_path, mount_path);
899 g_object_unref (mount);
900 g_object_unref (root);
901 g_free (mount_path);
904 g_free (device_path);
906 outstanding_mounts--;
908 if (outstanding_mounts == 0)
909 g_main_loop_quit (main_loop);
912 static void
913 mount_with_device_file (const char *device_file)
915 GVolumeMonitor *volume_monitor;
916 GList *volumes;
917 GList *l;
919 volume_monitor = g_volume_monitor_get();
921 volumes = g_volume_monitor_get_volumes (volume_monitor);
922 for (l = volumes; l != NULL; l = l->next)
924 GVolume *volume = G_VOLUME (l->data);
925 gchar *id;
927 id = g_volume_get_identifier (volume, G_VOLUME_IDENTIFIER_KIND_UNIX_DEVICE);
928 if (g_strcmp0 (id, device_file) == 0)
930 GMountOperation *op;
932 op = new_mount_op ();
934 g_volume_mount (volume,
935 G_MOUNT_MOUNT_NONE,
937 NULL,
938 mount_with_device_file_cb,
939 id);
941 outstanding_mounts++;
943 else
944 g_free (id);
946 g_list_free_full (volumes, g_object_unref);
948 if (outstanding_mounts == 0)
950 print_error ("%s: %s", device_file, _("No volume for device file"));
951 success = FALSE;
954 g_object_unref (volume_monitor);
957 static void
958 monitor_print_mount (GMount *mount)
960 if (extra_detail)
962 GList *l;
963 l = g_list_prepend (NULL, mount);
964 list_mounts (l, 2, FALSE);
965 g_list_free (l);
966 g_print ("\n");
970 static void
971 monitor_print_volume (GVolume *volume)
973 if (extra_detail)
975 GList *l;
976 l = g_list_prepend (NULL, volume);
977 list_volumes (l, 2, FALSE);
978 g_list_free (l);
979 g_print ("\n");
983 static void
984 monitor_print_drive (GDrive *drive)
986 if (extra_detail)
988 GList *l;
989 l = g_list_prepend (NULL, drive);
990 list_drives (l, 2);
991 g_list_free (l);
992 g_print ("\n");
996 static void
997 monitor_mount_added (GVolumeMonitor *volume_monitor, GMount *mount)
999 char *name;
1000 name = g_mount_get_name (mount);
1001 g_print ("Mount added: '%s'\n", name);
1002 g_free (name);
1003 monitor_print_mount (mount);
1006 static void
1007 monitor_mount_removed (GVolumeMonitor *volume_monitor, GMount *mount)
1009 char *name;
1010 name = g_mount_get_name (mount);
1011 g_print ("Mount removed: '%s'\n", name);
1012 g_free (name);
1013 monitor_print_mount (mount);
1016 static void
1017 monitor_mount_changed (GVolumeMonitor *volume_monitor, GMount *mount)
1019 char *name;
1020 name = g_mount_get_name (mount);
1021 g_print ("Mount changed: '%s'\n", name);
1022 g_free (name);
1023 monitor_print_mount (mount);
1026 static void
1027 monitor_mount_pre_unmount (GVolumeMonitor *volume_monitor, GMount *mount)
1029 char *name;
1030 name = g_mount_get_name (mount);
1031 g_print ("Mount pre-unmount: '%s'\n", name);
1032 g_free (name);
1033 monitor_print_mount (mount);
1036 static void
1037 monitor_volume_added (GVolumeMonitor *volume_monitor, GVolume *volume)
1039 char *name;
1040 name = g_volume_get_name (volume);
1041 g_print ("Volume added: '%s'\n", name);
1042 g_free (name);
1043 monitor_print_volume (volume);
1046 static void
1047 monitor_volume_removed (GVolumeMonitor *volume_monitor, GVolume *volume)
1049 char *name;
1050 name = g_volume_get_name (volume);
1051 g_print ("Volume removed: '%s'\n", name);
1052 g_free (name);
1053 monitor_print_volume (volume);
1056 static void
1057 monitor_volume_changed (GVolumeMonitor *volume_monitor, GVolume *volume)
1059 char *name;
1060 name = g_volume_get_name (volume);
1061 g_print ("Volume changed: '%s'\n", name);
1062 g_free (name);
1063 monitor_print_volume (volume);
1066 static void
1067 monitor_drive_connected (GVolumeMonitor *volume_monitor, GDrive *drive)
1069 char *name;
1070 name = g_drive_get_name (drive);
1071 g_print ("Drive connected: '%s'\n", name);
1072 g_free (name);
1073 monitor_print_drive (drive);
1076 static void
1077 monitor_drive_disconnected (GVolumeMonitor *volume_monitor, GDrive *drive)
1079 char *name;
1080 name = g_drive_get_name (drive);
1081 g_print ("Drive disconnected: '%s'\n", name);
1082 g_free (name);
1083 monitor_print_drive (drive);
1086 static void
1087 monitor_drive_changed (GVolumeMonitor *volume_monitor, GDrive *drive)
1089 char *name;
1090 name = g_drive_get_name (drive);
1091 g_print ("Drive changed: '%s'\n", name);
1092 g_free (name);
1093 monitor_print_drive (drive);
1096 static void
1097 monitor_drive_eject_button (GVolumeMonitor *volume_monitor, GDrive *drive)
1099 char *name;
1100 name = g_drive_get_name (drive);
1101 g_print ("Drive eject button: '%s'\n", name);
1102 g_free (name);
1105 static void
1106 monitor (void)
1108 GVolumeMonitor *volume_monitor;
1110 volume_monitor = g_volume_monitor_get ();
1112 g_signal_connect (volume_monitor, "mount-added", (GCallback) monitor_mount_added, NULL);
1113 g_signal_connect (volume_monitor, "mount-removed", (GCallback) monitor_mount_removed, NULL);
1114 g_signal_connect (volume_monitor, "mount-changed", (GCallback) monitor_mount_changed, NULL);
1115 g_signal_connect (volume_monitor, "mount-pre-unmount", (GCallback) monitor_mount_pre_unmount, NULL);
1116 g_signal_connect (volume_monitor, "volume-added", (GCallback) monitor_volume_added, NULL);
1117 g_signal_connect (volume_monitor, "volume-removed", (GCallback) monitor_volume_removed, NULL);
1118 g_signal_connect (volume_monitor, "volume-changed", (GCallback) monitor_volume_changed, NULL);
1119 g_signal_connect (volume_monitor, "drive-connected", (GCallback) monitor_drive_connected, NULL);
1120 g_signal_connect (volume_monitor, "drive-disconnected", (GCallback) monitor_drive_disconnected, NULL);
1121 g_signal_connect (volume_monitor, "drive-changed", (GCallback) monitor_drive_changed, NULL);
1122 g_signal_connect (volume_monitor, "drive-eject-button", (GCallback) monitor_drive_eject_button, NULL);
1124 g_print ("Monitoring events. Press Ctrl+C to quit.\n");
1126 g_main_loop_run (main_loop);
1130 handle_mount (int argc, char *argv[], gboolean do_help)
1132 GOptionContext *context;
1133 gchar *param;
1134 GError *error = NULL;
1135 GFile *file;
1136 int i;
1138 g_set_prgname ("gio mount");
1140 /* Translators: commandline placeholder */
1141 param = g_strdup_printf ("[%s...]", _("LOCATION"));
1142 context = g_option_context_new (param);
1143 g_free (param);
1144 g_option_context_set_help_enabled (context, FALSE);
1145 g_option_context_set_summary (context, _("Mount or unmount the locations."));
1146 g_option_context_add_main_entries (context, entries, GETTEXT_PACKAGE);
1148 if (do_help)
1150 show_help (context, NULL);
1151 g_option_context_free (context);
1152 return 0;
1155 if (!g_option_context_parse (context, &argc, &argv, &error))
1157 show_help (context, error->message);
1158 g_error_free (error);
1159 g_option_context_free (context);
1160 return 1;
1163 g_option_context_free (context);
1165 main_loop = g_main_loop_new (NULL, FALSE);
1167 if (mount_list)
1168 list_monitor_items ();
1169 else if (mount_device_file != NULL)
1170 mount_with_device_file (mount_device_file);
1171 else if (unmount_scheme != NULL)
1172 unmount_all_with_scheme (unmount_scheme);
1173 else if (mount_monitor)
1174 monitor ();
1175 else if (argc > 1)
1177 for (i = 1; i < argc; i++)
1179 file = g_file_new_for_commandline_arg (argv[i]);
1180 if (mount_unmount)
1181 unmount (file);
1182 else if (mount_eject)
1183 eject (file);
1184 else
1185 mount (file);
1186 g_object_unref (file);
1190 if (outstanding_mounts > 0)
1191 g_main_loop_run (main_loop);
1193 return success ? 0 : 2;