Drive
Object Hierarchy:
Description:
`GDrive` represents a piece of hardware connected to the machine.
It’s generally only created for removable hardware or hardware with removable media.
`GDrive` is a container class for [iface@Gio.Volume] objects that stem from the same piece of media. As such, `GDrive` abstracts a drive with ( or without) removable media and provides operations for querying whether media is available, determining whether media change is automatically detected and ejecting the media.
If the `GDrive` reports that media isn’t automatically detected, one can poll for media; typically one should not do this periodically as a poll for media operation is potentially expensive and may spin up the drive creating noise.
`GDrive` supports starting and stopping drives with authentication support for the former. This can be used to support a diverse set of use cases including connecting/disconnecting iSCSI devices, powering down external disk enclosures and starting/stopping multi-disk devices such as RAID devices. Note that the actual semantics and side-effects of starting/stopping a `GDrive` may vary according to implementation. To choose the correct verbs in e.g. a file manager, use [method@Gio.Drive.get_start_stop_type].
For [porting from GnomeVFS](migrating-gnome-vfs.html) note that there is no equivalent of `GDrive` in that API.
Example: Drives:
public void print_drive (Drive drive, string title) {
print (@"$title:\n");
print (" name: %s\n", drive.get_name ());
print (" can-eject: %s\n", drive.can_eject ().to_string ());
print (" can-poll-for-media: %s\n", drive.can_poll_for_media ().to_string ());
print (" can-start: %s\n", drive.can_start ().to_string ());
print (" can-start-degraded: %s\n", drive.can_start_degraded ().to_string ());
print (" can-stop: %s\n", drive.can_stop ().to_string ());
print (" has-volumes: %s\n", drive.has_volumes ().to_string ());
print (" has-media: %s\n", drive.has_media ().to_string ());
print (" is-media-check-automatic: %s\n", drive.is_media_check_automatic ().to_string ());
print (" is-media-removable: %s\n", drive.is_media_removable ().to_string ());
print (" start-stop-type: %s\n", drive.get_start_stop_type ().to_string ());
string[] kinds = drive.enumerate_identifiers ();
foreach (unowned string kind in kinds) {
print (" %s = %s\n", kind, drive.get_identifier (kind));
}
}
public static int main (string[] args) {
MainLoop loop = new MainLoop ();
VolumeMonitor monitor = VolumeMonitor.get ();
// Print a list of drives connected to the system:
List<Drive> drives = monitor.get_connected_drives ();
foreach (Drive drive in drives) {
print_drive (drive, "Connected");
}
// Emitted when a drive is connected to the system:
monitor.drive_connected.connect ((drive) => {
print_drive (drive, "Drive connected");
// Reject all newly connected drives:
drive.eject_with_operation.begin (MountUnmountFlags.FORCE, null, null, (obj, res) => {
try {
bool status = drive.eject_with_operation.end (res);
print ("Eject: %s: %s\n", drive.get_name (), status.to_string ());
} catch (Error e) {
print ("Error: %s\n", e.message);
}
});
});
// Emitted when a drive changes:
monitor.drive_changed.connect ((drive) => {
// See GLib.Drive.changed
print_drive (drive, "Drive changed");
});
// Emitted when a drive is disconnected from the system:
monitor.drive_disconnected.connect ((drive) => {
// See GLib.Drive.disconnected
print_drive (drive, "Drive disconnected");
});
// Emitted when the eject button is pressed on drive:
monitor.drive_eject_button.connect ((drive) => {
// See GLib.Drive.eject_button
print_drive (drive, "Drive eject-button");
});
// Emitted when the stop button is pressed on drive:
monitor.drive_stop_button.connect ((drive) => {
// See GLib.Drive.stop_button
print_drive (drive, "Drive stop-button");
});
loop.run ();
return 0;
}
valac --pkg gio-2.0 GLib.Drive.vala