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>
32 #define STDIN_FILENO 0
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
},
73 prompt_for (const char *prompt
, const char *default_value
, gboolean echo
)
76 struct termios term_attr
;
78 gboolean restore_flags
;
83 if (default_value
&& *default_value
!= 0)
84 g_print ("%s [%s]: ", prompt
, default_value
);
86 g_print ("%s: ", prompt
);
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
;
98 if (tcsetattr (STDIN_FILENO
, TCSAFLUSH
, &term_attr
) != 0)
99 g_print ("Warning! Password will be echoed");
104 fgets(data
, sizeof (data
), stdin
);
106 #ifdef HAVE_TERMIOS_H
109 term_attr
.c_lflag
= old_flags
;
110 tcsetattr (STDIN_FILENO
, TCSAFLUSH
, &term_attr
);
120 if (data
[len
-1] == '\n')
126 if (*data
== 0 && default_value
)
127 return g_strdup (default_value
);
128 return g_strdup (data
);
132 ask_password_cb (GMountOperation
*op
,
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
);
145 g_print ("%s\n", message
);
147 if (flags
& G_ASK_PASSWORD_NEED_USERNAME
)
149 s
= prompt_for ("User", default_user
, TRUE
);
152 g_mount_operation_set_username (op
, s
);
156 if (flags
& G_ASK_PASSWORD_NEED_DOMAIN
)
158 s
= prompt_for ("Domain", default_domain
, TRUE
);
161 g_mount_operation_set_domain (op
, s
);
165 if (flags
& G_ASK_PASSWORD_NEED_PASSWORD
)
167 s
= prompt_for ("Password", NULL
, FALSE
);
170 g_mount_operation_set_password (op
, s
);
175 /* Only try anonymous access once. */
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
);
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
);
191 g_mount_operation_reply (op
, G_MOUNT_OPERATION_ABORTED
);
195 ask_question_cb (GMountOperation
*op
,
200 char **ptr
= choices
;
204 g_print ("%s\n", message
);
209 g_print ("[%d] %s\n", i
, *ptr
++);
213 s
= prompt_for ("Choice", NULL
, TRUE
);
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
);
228 g_mount_operation_reply (op
, G_MOUNT_OPERATION_ABORTED
);
232 mount_mountable_done_cb (GObject
*object
,
237 GError
*error
= NULL
;
238 GMountOperation
*op
= user_data
;
240 target
= g_file_mount_mountable_finish (G_FILE (object
), res
, &error
);
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
);
253 g_object_unref (target
);
255 outstanding_mounts
--;
257 if (outstanding_mounts
== 0)
258 g_main_loop_quit (main_loop
);
262 mount_done_cb (GObject
*object
,
267 GError
*error
= NULL
;
268 GMountOperation
*op
= user_data
;
270 succeeded
= g_file_mount_enclosing_volume_finish (G_FILE (object
), res
, &error
);
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
*
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...
317 op
= new_mount_op ();
320 g_file_mount_mountable (file
, 0, op
, NULL
, mount_mountable_done_cb
, op
);
322 g_file_mount_enclosing_volume (file
, 0, op
, NULL
, mount_done_cb
, op
);
324 outstanding_mounts
++;
328 unmount_done_cb (GObject
*object
,
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
));
342 print_file_error (file
, error
->message
);
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
);
356 unmount (GFile
*file
)
359 GError
*error
= NULL
;
360 GMountOperation
*mount_op
;
361 GMountUnmountFlags flags
;
366 mount
= g_file_find_enclosing_mount (file
, NULL
, &error
);
369 print_file_error (file
, error
->message
);
371 g_error_free (error
);
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
++;
384 eject_done_cb (GObject
*object
,
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
));
398 print_file_error (file
, error
->message
);
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
);
415 GError
*error
= NULL
;
416 GMountOperation
*mount_op
;
417 GMountUnmountFlags flags
;
422 mount
= g_file_find_enclosing_mount (file
, NULL
, &error
);
425 print_file_error (file
, error
->message
);
427 g_error_free (error
);
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
++;
440 iterate_gmain_timeout_function (gpointer data
)
442 g_main_loop_quit (main_loop
);
449 g_timeout_add (500, iterate_gmain_timeout_function
, NULL
);
450 g_main_loop_run (main_loop
);
454 show_themed_icon_names (GThemedIcon
*icon
, gboolean symbolic
, int indent
)
459 g_print ("%*s%sthemed icons:", indent
, " ", symbolic
? "symbolic " : "");
463 g_object_get (icon
, "names", &names
, NULL
);
465 for (iter
= names
; *iter
; iter
++)
466 g_print (" [%s]", *iter
);
472 /* don't copy-paste this code */
474 get_type_name (gpointer object
)
476 const char *type_name
;
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)",
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)",
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)",
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)",
505 (const char *) g_object_get_data (G_OBJECT (object
),
506 "g-proxy-shadow-mount-volume-monitor-name"));
510 ret
= g_strdup (type_name
);
517 list_mounts (GList
*mounts
,
519 gboolean only_with_no_volume
)
525 char *name
, *uuid
, *uri
;
526 GFile
*root
, *default_location
;
528 char **x_content_types
;
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
);
541 g_object_unref (volume
);
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
);
558 uuid
= g_mount_get_uuid (mount
);
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
);
568 g_object_unref (default_location
);
571 icon
= g_mount_get_icon (mount
);
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
);
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)
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
]);
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
);
609 g_object_unref (root
);
616 list_volumes (GList
*volumes
,
618 gboolean only_with_no_drive
)
627 GFile
*activation_root
;
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
);
642 g_object_unref (drive
);
647 name
= g_volume_get_name (volume
);
649 g_print ("%*sVolume(%d): %s\n", indent
, "", c
, name
);
652 type_name
= get_type_name (volume
);
653 g_print ("%*sType: %s\n", indent
+2, "", type_name
);
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
,
666 g_print ("%*s %s: '%s'\n", indent
+2, "", ids
[i
], id
);
672 uuid
= g_volume_get_uuid (volume
);
674 g_print ("%*suuid=%s\n", indent
+ 2, "", uuid
);
675 activation_root
= g_volume_get_activation_root (volume
);
679 uri
= g_file_get_uri (activation_root
);
680 g_print ("%*sactivation_root=%s\n", indent
+ 2, "", uri
);
682 g_object_unref (activation_root
);
684 icon
= g_volume_get_icon (volume
);
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
);
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
);
711 mount
= g_volume_get_mount (volume
);
714 mounts
= g_list_prepend (NULL
, mount
);
715 list_mounts (mounts
, indent
+ 2, FALSE
);
716 g_list_free (mounts
);
717 g_object_unref (mount
);
723 list_drives (GList
*drives
,
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
);
743 type_name
= get_type_name (drive
);
744 g_print ("%*sType: %s\n", indent
+2, "", type_name
);
749 GEnumValue
*enum_value
;
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
,
760 g_print ("%*s %s: '%s'\n", indent
+2, "", ids
[i
], id
);
766 icon
= g_drive_get_icon (drive
);
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
);
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
));
793 klass
= g_type_class_ref (G_TYPE_DRIVE_START_STOP_TYPE
);
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
);
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 */
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
);
840 unmount_all_with_scheme (const char *scheme
)
842 GVolumeMonitor
*volume_monitor
;
846 volume_monitor
= g_volume_monitor_get();
848 /* populate gvfs network mounts */
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
);
856 root
= g_mount_get_root (mount
);
857 if (g_file_has_uri_scheme (root
, scheme
)) {
860 g_object_unref (root
);
862 g_list_free_full (mounts
, g_object_unref
);
864 g_object_unref (volume_monitor
);
868 mount_with_device_file_cb (GObject
*object
,
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
);
883 print_error ("%s: %s", device_path
, error
->message
);
884 g_error_free (error
);
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
);
904 g_free (device_path
);
906 outstanding_mounts
--;
908 if (outstanding_mounts
== 0)
909 g_main_loop_quit (main_loop
);
913 mount_with_device_file (const char *device_file
)
915 GVolumeMonitor
*volume_monitor
;
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
);
927 id
= g_volume_get_identifier (volume
, G_VOLUME_IDENTIFIER_KIND_UNIX_DEVICE
);
928 if (g_strcmp0 (id
, device_file
) == 0)
932 op
= new_mount_op ();
934 g_volume_mount (volume
,
938 mount_with_device_file_cb
,
941 outstanding_mounts
++;
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"));
954 g_object_unref (volume_monitor
);
958 monitor_print_mount (GMount
*mount
)
963 l
= g_list_prepend (NULL
, mount
);
964 list_mounts (l
, 2, FALSE
);
971 monitor_print_volume (GVolume
*volume
)
976 l
= g_list_prepend (NULL
, volume
);
977 list_volumes (l
, 2, FALSE
);
984 monitor_print_drive (GDrive
*drive
)
989 l
= g_list_prepend (NULL
, drive
);
997 monitor_mount_added (GVolumeMonitor
*volume_monitor
, GMount
*mount
)
1000 name
= g_mount_get_name (mount
);
1001 g_print ("Mount added: '%s'\n", name
);
1003 monitor_print_mount (mount
);
1007 monitor_mount_removed (GVolumeMonitor
*volume_monitor
, GMount
*mount
)
1010 name
= g_mount_get_name (mount
);
1011 g_print ("Mount removed: '%s'\n", name
);
1013 monitor_print_mount (mount
);
1017 monitor_mount_changed (GVolumeMonitor
*volume_monitor
, GMount
*mount
)
1020 name
= g_mount_get_name (mount
);
1021 g_print ("Mount changed: '%s'\n", name
);
1023 monitor_print_mount (mount
);
1027 monitor_mount_pre_unmount (GVolumeMonitor
*volume_monitor
, GMount
*mount
)
1030 name
= g_mount_get_name (mount
);
1031 g_print ("Mount pre-unmount: '%s'\n", name
);
1033 monitor_print_mount (mount
);
1037 monitor_volume_added (GVolumeMonitor
*volume_monitor
, GVolume
*volume
)
1040 name
= g_volume_get_name (volume
);
1041 g_print ("Volume added: '%s'\n", name
);
1043 monitor_print_volume (volume
);
1047 monitor_volume_removed (GVolumeMonitor
*volume_monitor
, GVolume
*volume
)
1050 name
= g_volume_get_name (volume
);
1051 g_print ("Volume removed: '%s'\n", name
);
1053 monitor_print_volume (volume
);
1057 monitor_volume_changed (GVolumeMonitor
*volume_monitor
, GVolume
*volume
)
1060 name
= g_volume_get_name (volume
);
1061 g_print ("Volume changed: '%s'\n", name
);
1063 monitor_print_volume (volume
);
1067 monitor_drive_connected (GVolumeMonitor
*volume_monitor
, GDrive
*drive
)
1070 name
= g_drive_get_name (drive
);
1071 g_print ("Drive connected: '%s'\n", name
);
1073 monitor_print_drive (drive
);
1077 monitor_drive_disconnected (GVolumeMonitor
*volume_monitor
, GDrive
*drive
)
1080 name
= g_drive_get_name (drive
);
1081 g_print ("Drive disconnected: '%s'\n", name
);
1083 monitor_print_drive (drive
);
1087 monitor_drive_changed (GVolumeMonitor
*volume_monitor
, GDrive
*drive
)
1090 name
= g_drive_get_name (drive
);
1091 g_print ("Drive changed: '%s'\n", name
);
1093 monitor_print_drive (drive
);
1097 monitor_drive_eject_button (GVolumeMonitor
*volume_monitor
, GDrive
*drive
)
1100 name
= g_drive_get_name (drive
);
1101 g_print ("Drive eject button: '%s'\n", name
);
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
;
1134 GError
*error
= NULL
;
1138 g_set_prgname ("gio mount");
1140 /* Translators: commandline placeholder */
1141 param
= g_strdup_printf ("[%s...]", _("LOCATION"));
1142 context
= g_option_context_new (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
);
1150 show_help (context
, NULL
);
1151 g_option_context_free (context
);
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
);
1163 g_option_context_free (context
);
1165 main_loop
= g_main_loop_new (NULL
, FALSE
);
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
)
1177 for (i
= 1; i
< argc
; i
++)
1179 file
= g_file_new_for_commandline_arg (argv
[i
]);
1182 else if (mount_eject
)
1186 g_object_unref (file
);
1190 if (outstanding_mounts
> 0)
1191 g_main_loop_run (main_loop
);
1193 return success
? 0 : 2;