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 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
},
75 prompt_for (const char *prompt
, const char *default_value
, gboolean echo
)
78 struct termios term_attr
;
80 gboolean restore_flags
;
85 if (default_value
&& *default_value
!= 0)
86 g_print ("%s [%s]: ", prompt
, default_value
);
88 g_print ("%s: ", prompt
);
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
;
100 if (tcsetattr (STDIN_FILENO
, TCSAFLUSH
, &term_attr
) != 0)
101 g_print ("Warning! Password will be echoed");
106 fgets(data
, sizeof (data
), stdin
);
108 #ifdef HAVE_TERMIOS_H
111 term_attr
.c_lflag
= old_flags
;
112 tcsetattr (STDIN_FILENO
, TCSAFLUSH
, &term_attr
);
122 if (data
[len
-1] == '\n')
128 if (*data
== 0 && default_value
)
129 return g_strdup (default_value
);
130 return g_strdup (data
);
134 ask_password_cb (GMountOperation
*op
,
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
);
147 g_print ("%s\n", message
);
149 if (flags
& G_ASK_PASSWORD_NEED_USERNAME
)
151 s
= prompt_for ("User", default_user
, TRUE
);
154 g_mount_operation_set_username (op
, s
);
158 if (flags
& G_ASK_PASSWORD_NEED_DOMAIN
)
160 s
= prompt_for ("Domain", default_domain
, TRUE
);
163 g_mount_operation_set_domain (op
, s
);
167 if (flags
& G_ASK_PASSWORD_NEED_PASSWORD
)
169 s
= prompt_for ("Password", NULL
, FALSE
);
172 g_mount_operation_set_password (op
, s
);
177 /* Only try anonymous access once. */
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
);
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
);
193 g_mount_operation_reply (op
, G_MOUNT_OPERATION_ABORTED
);
197 ask_question_cb (GMountOperation
*op
,
202 char **ptr
= choices
;
206 g_print ("%s\n", message
);
211 g_print ("[%d] %s\n", i
, *ptr
++);
215 s
= prompt_for ("Choice", NULL
, TRUE
);
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
);
230 g_mount_operation_reply (op
, G_MOUNT_OPERATION_ABORTED
);
234 mount_mountable_done_cb (GObject
*object
,
239 GError
*error
= NULL
;
240 GMountOperation
*op
= user_data
;
242 target
= g_file_mount_mountable_finish (G_FILE (object
), res
, &error
);
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
);
255 g_object_unref (target
);
259 outstanding_mounts
--;
261 if (outstanding_mounts
== 0)
262 g_main_loop_quit (main_loop
);
266 mount_done_cb (GObject
*object
,
271 GError
*error
= NULL
;
272 GMountOperation
*op
= user_data
;
274 succeeded
= g_file_mount_enclosing_volume_finish (G_FILE (object
), res
, &error
);
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
);
289 outstanding_mounts
--;
291 if (outstanding_mounts
== 0)
292 g_main_loop_quit (main_loop
);
295 static GMountOperation
*
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...
323 op
= new_mount_op ();
326 g_file_mount_mountable (file
, 0, op
, NULL
, mount_mountable_done_cb
, op
);
328 g_file_mount_enclosing_volume (file
, 0, op
, NULL
, mount_done_cb
, op
);
330 outstanding_mounts
++;
334 unmount_done_cb (GObject
*object
,
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
));
348 print_file_error (file
, error
->message
);
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
);
362 unmount (GFile
*file
)
365 GError
*error
= NULL
;
366 GMountOperation
*mount_op
;
367 GMountUnmountFlags flags
;
372 mount
= g_file_find_enclosing_mount (file
, NULL
, &error
);
375 print_file_error (file
, error
->message
);
377 g_error_free (error
);
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
++;
390 eject_done_cb (GObject
*object
,
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
));
404 print_file_error (file
, error
->message
);
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
);
421 GError
*error
= NULL
;
422 GMountOperation
*mount_op
;
423 GMountUnmountFlags flags
;
428 mount
= g_file_find_enclosing_mount (file
, NULL
, &error
);
431 print_file_error (file
, error
->message
);
433 g_error_free (error
);
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
++;
446 stop_with_device_file_cb (GObject
*object
,
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
);
460 g_free (device_path
);
462 outstanding_mounts
--;
464 if (outstanding_mounts
== 0)
465 g_main_loop_quit (main_loop
);
469 stop_with_device_file (const char *device_file
)
471 GVolumeMonitor
*volume_monitor
;
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
);
483 id
= g_drive_get_identifier (drive
, G_VOLUME_IDENTIFIER_KIND_UNIX_DEVICE
);
484 if (g_strcmp0 (id
, device_file
) == 0)
487 GMountUnmountFlags flags
;
489 op
= new_mount_op ();
490 flags
= force
? G_MOUNT_UNMOUNT_FORCE
: G_MOUNT_UNMOUNT_NONE
;
495 stop_with_device_file_cb
,
496 g_steal_pointer (&id
));
499 outstanding_mounts
++;
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"));
512 g_object_unref (volume_monitor
);
516 iterate_gmain_timeout_function (gpointer data
)
518 g_main_loop_quit (main_loop
);
525 g_timeout_add (500, iterate_gmain_timeout_function
, NULL
);
526 g_main_loop_run (main_loop
);
530 show_themed_icon_names (GThemedIcon
*icon
, gboolean symbolic
, int indent
)
535 g_print ("%*s%sthemed icons:", indent
, " ", symbolic
? "symbolic " : "");
539 g_object_get (icon
, "names", &names
, NULL
);
541 for (iter
= names
; *iter
; iter
++)
542 g_print (" [%s]", *iter
);
548 /* don't copy-paste this code */
550 get_type_name (gpointer object
)
552 const char *type_name
;
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)",
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)",
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)",
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)",
581 (const char *) g_object_get_data (G_OBJECT (object
),
582 "g-proxy-shadow-mount-volume-monitor-name"));
586 ret
= g_strdup (type_name
);
593 list_mounts (GList
*mounts
,
595 gboolean only_with_no_volume
)
601 char *name
, *uuid
, *uri
;
602 GFile
*root
, *default_location
;
604 char **x_content_types
;
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
);
617 g_object_unref (volume
);
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
);
634 uuid
= g_mount_get_uuid (mount
);
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
);
644 g_object_unref (default_location
);
647 icon
= g_mount_get_icon (mount
);
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
);
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)
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
]);
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
);
685 g_object_unref (root
);
692 list_volumes (GList
*volumes
,
694 gboolean only_with_no_drive
)
703 GFile
*activation_root
;
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
);
718 g_object_unref (drive
);
723 name
= g_volume_get_name (volume
);
725 g_print ("%*sVolume(%d): %s\n", indent
, "", c
, name
);
728 type_name
= get_type_name (volume
);
729 g_print ("%*sType: %s\n", indent
+2, "", type_name
);
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
,
742 g_print ("%*s %s: '%s'\n", indent
+2, "", ids
[i
], id
);
748 uuid
= g_volume_get_uuid (volume
);
750 g_print ("%*suuid=%s\n", indent
+ 2, "", uuid
);
751 activation_root
= g_volume_get_activation_root (volume
);
755 uri
= g_file_get_uri (activation_root
);
756 g_print ("%*sactivation_root=%s\n", indent
+ 2, "", uri
);
758 g_object_unref (activation_root
);
760 icon
= g_volume_get_icon (volume
);
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
);
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
);
787 mount
= g_volume_get_mount (volume
);
790 mounts
= g_list_prepend (NULL
, mount
);
791 list_mounts (mounts
, indent
+ 2, FALSE
);
792 g_list_free (mounts
);
793 g_object_unref (mount
);
799 list_drives (GList
*drives
,
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
);
819 type_name
= get_type_name (drive
);
820 g_print ("%*sType: %s\n", indent
+2, "", type_name
);
825 GEnumValue
*enum_value
;
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
,
836 g_print ("%*s %s: '%s'\n", indent
+2, "", ids
[i
], id
);
842 icon
= g_drive_get_icon (drive
);
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
);
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
));
869 klass
= g_type_class_ref (G_TYPE_DRIVE_START_STOP_TYPE
);
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
);
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 */
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
);
916 unmount_all_with_scheme (const char *scheme
)
918 GVolumeMonitor
*volume_monitor
;
922 volume_monitor
= g_volume_monitor_get();
924 /* populate gvfs network mounts */
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
);
932 root
= g_mount_get_root (mount
);
933 if (g_file_has_uri_scheme (root
, scheme
)) {
936 g_object_unref (root
);
938 g_list_free_full (mounts
, g_object_unref
);
940 g_object_unref (volume_monitor
);
944 mount_with_device_file_cb (GObject
*object
,
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
);
959 print_error ("%s: %s", device_path
, error
->message
);
960 g_error_free (error
);
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
);
980 g_free (device_path
);
982 outstanding_mounts
--;
984 if (outstanding_mounts
== 0)
985 g_main_loop_quit (main_loop
);
989 mount_with_device_file (const char *device_file
)
991 GVolumeMonitor
*volume_monitor
;
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
);
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
,
1014 mount_with_device_file_cb
,
1017 g_object_unref (op
);
1019 outstanding_mounts
++;
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"));
1032 g_object_unref (volume_monitor
);
1036 monitor_print_mount (GMount
*mount
)
1041 l
= g_list_prepend (NULL
, mount
);
1042 list_mounts (l
, 2, FALSE
);
1049 monitor_print_volume (GVolume
*volume
)
1054 l
= g_list_prepend (NULL
, volume
);
1055 list_volumes (l
, 2, FALSE
);
1062 monitor_print_drive (GDrive
*drive
)
1067 l
= g_list_prepend (NULL
, drive
);
1075 monitor_mount_added (GVolumeMonitor
*volume_monitor
, GMount
*mount
)
1078 name
= g_mount_get_name (mount
);
1079 g_print ("Mount added: '%s'\n", name
);
1081 monitor_print_mount (mount
);
1085 monitor_mount_removed (GVolumeMonitor
*volume_monitor
, GMount
*mount
)
1088 name
= g_mount_get_name (mount
);
1089 g_print ("Mount removed: '%s'\n", name
);
1091 monitor_print_mount (mount
);
1095 monitor_mount_changed (GVolumeMonitor
*volume_monitor
, GMount
*mount
)
1098 name
= g_mount_get_name (mount
);
1099 g_print ("Mount changed: '%s'\n", name
);
1101 monitor_print_mount (mount
);
1105 monitor_mount_pre_unmount (GVolumeMonitor
*volume_monitor
, GMount
*mount
)
1108 name
= g_mount_get_name (mount
);
1109 g_print ("Mount pre-unmount: '%s'\n", name
);
1111 monitor_print_mount (mount
);
1115 monitor_volume_added (GVolumeMonitor
*volume_monitor
, GVolume
*volume
)
1118 name
= g_volume_get_name (volume
);
1119 g_print ("Volume added: '%s'\n", name
);
1121 monitor_print_volume (volume
);
1125 monitor_volume_removed (GVolumeMonitor
*volume_monitor
, GVolume
*volume
)
1128 name
= g_volume_get_name (volume
);
1129 g_print ("Volume removed: '%s'\n", name
);
1131 monitor_print_volume (volume
);
1135 monitor_volume_changed (GVolumeMonitor
*volume_monitor
, GVolume
*volume
)
1138 name
= g_volume_get_name (volume
);
1139 g_print ("Volume changed: '%s'\n", name
);
1141 monitor_print_volume (volume
);
1145 monitor_drive_connected (GVolumeMonitor
*volume_monitor
, GDrive
*drive
)
1148 name
= g_drive_get_name (drive
);
1149 g_print ("Drive connected: '%s'\n", name
);
1151 monitor_print_drive (drive
);
1155 monitor_drive_disconnected (GVolumeMonitor
*volume_monitor
, GDrive
*drive
)
1158 name
= g_drive_get_name (drive
);
1159 g_print ("Drive disconnected: '%s'\n", name
);
1161 monitor_print_drive (drive
);
1165 monitor_drive_changed (GVolumeMonitor
*volume_monitor
, GDrive
*drive
)
1168 name
= g_drive_get_name (drive
);
1169 g_print ("Drive changed: '%s'\n", name
);
1171 monitor_print_drive (drive
);
1175 monitor_drive_eject_button (GVolumeMonitor
*volume_monitor
, GDrive
*drive
)
1178 name
= g_drive_get_name (drive
);
1179 g_print ("Drive eject button: '%s'\n", name
);
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
;
1212 GError
*error
= NULL
;
1216 g_set_prgname ("gio mount");
1218 /* Translators: commandline placeholder */
1219 param
= g_strdup_printf ("[%s…]", _("LOCATION"));
1220 context
= g_option_context_new (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
);
1228 show_help (context
, NULL
);
1229 g_option_context_free (context
);
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
);
1241 main_loop
= g_main_loop_new (NULL
, FALSE
);
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
)
1255 for (i
= 1; i
< argc
; i
++)
1257 file
= g_file_new_for_commandline_arg (argv
[i
]);
1260 else if (mount_eject
)
1264 g_object_unref (file
);
1269 show_help (context
, _("No locations given"));
1270 g_option_context_free (context
);
1274 g_option_context_free (context
);
1276 if (outstanding_mounts
> 0)
1277 g_main_loop_run (main_loop
);
1279 return success
? 0 : 2;