static const char *udisks_next_dict_entry( DBusMessageIter *iter, DBusMessageIter *variant ) { DBusMessageIter sub; const char *name; if (p_dbus_message_iter_get_arg_type( iter ) != DBUS_TYPE_DICT_ENTRY) return NULL; p_dbus_message_iter_recurse( iter, &sub ); p_dbus_message_iter_next( iter ); p_dbus_message_iter_get_basic( &sub, &name ); p_dbus_message_iter_next( &sub ); p_dbus_message_iter_recurse( &sub, variant ); return name; }
/* to make things easier, UDisks2 stores strings as array of bytes instead of strings... */ static const char *udisks2_string_from_array( DBusMessageIter *iter ) { DBusMessageIter string; const char *array; int size; p_dbus_message_iter_recurse( iter, &string ); p_dbus_message_iter_get_fixed_array( &string, &array, &size ); return array; }
static enum device_type udisks_parse_media_compatibility( DBusMessageIter *iter ) { DBusMessageIter media; enum device_type drive_type = DEVICE_UNKNOWN; p_dbus_message_iter_recurse( iter, &media ); while (p_dbus_message_iter_get_arg_type( &media ) == DBUS_TYPE_STRING) { const char *media_type; p_dbus_message_iter_get_basic( &media, &media_type ); if (starts_with( media_type, "optical_dvd" )) drive_type = DEVICE_DVD; if (starts_with( media_type, "floppy" )) drive_type = DEVICE_FLOPPY; else if (starts_with( media_type, "optical_" ) && drive_type == DEVICE_UNKNOWN) drive_type = DEVICE_CDROM; p_dbus_message_iter_next( &media ); } return drive_type; }
/* UDisks2 is almost, but not quite, entirely unlike UDisks. * It would have been easy to make it backwards compatible, but where would be the fun in that? */ static BOOL udisks2_add_devices( const char *changed ) { DBusMessage *request, *reply; DBusMessageIter dict, iter, block; DBusError error; const char *udi; request = p_dbus_message_new_method_call( "org.freedesktop.UDisks2", "/org/freedesktop/UDisks2", "org.freedesktop.DBus.ObjectManager", "GetManagedObjects" ); if (!request) return FALSE; p_dbus_error_init( &error ); reply = p_dbus_connection_send_with_reply_and_block( connection, request, udisks_timeout, &error ); p_dbus_message_unref( request ); if (!reply) { WARN( "failed: %s\n", error.message ); p_dbus_error_free( &error ); return FALSE; } p_dbus_error_free( &error ); p_dbus_message_iter_init( reply, &dict ); if (p_dbus_message_iter_get_arg_type( &dict ) == DBUS_TYPE_ARRAY) { p_dbus_message_iter_recurse( &dict, &iter ); while ((udi = udisks_next_dict_entry( &iter, &block ))) { if (!starts_with( udi, "/org/freedesktop/UDisks2/block_devices/" )) continue; if (changed && strcmp( changed, udi )) continue; udisks2_add_device( udi, &dict, &block ); } } else WARN( "unexpected args in GetManagedObjects reply\n" ); p_dbus_message_unref( reply ); return TRUE; }
/* find the drive entry in the dictionary and get its parameters */ static void udisks2_get_drive_info( const char *drive_name, DBusMessageIter *dict, enum device_type *drive_type, int *removable ) { DBusMessageIter iter, drive, variant; const char *name; p_dbus_message_iter_recurse( dict, &iter ); while ((name = udisks_next_dict_entry( &iter, &drive ))) { if (strcmp( name, drive_name )) continue; while ((name = udisks_next_dict_entry( &drive, &iter ))) { if (strcmp( name, "org.freedesktop.UDisks2.Drive" )) continue; while ((name = udisks_next_dict_entry( &iter, &variant ))) { if (!strcmp( name, "Removable" )) p_dbus_message_iter_get_basic( &variant, removable ); else if (!strcmp( name, "MediaCompatibility" )) *drive_type = udisks_parse_media_compatibility( &variant ); } } } }
/* UDisks callback for new device */ static void udisks_new_device( const char *udi ) { static const char *dev_name = "org.freedesktop.UDisks.Device"; DBusMessage *request, *reply; DBusMessageIter iter, variant; DBusError error; const char *device = NULL; const char *mount_point = NULL; const char *type = NULL; GUID guid, *guid_ptr = NULL; int removable = FALSE; enum device_type drive_type = DEVICE_UNKNOWN; request = p_dbus_message_new_method_call( "org.freedesktop.UDisks", udi, "org.freedesktop.DBus.Properties", "GetAll" ); if (!request) return; p_dbus_message_iter_init_append( request, &iter ); p_dbus_message_iter_append_basic( &iter, DBUS_TYPE_STRING, &dev_name ); p_dbus_error_init( &error ); reply = p_dbus_connection_send_with_reply_and_block( connection, request, -1, &error ); p_dbus_message_unref( request ); if (!reply) { WARN( "failed: %s\n", error.message ); p_dbus_error_free( &error ); return; } p_dbus_error_free( &error ); p_dbus_message_iter_init( reply, &iter ); if (p_dbus_message_iter_get_arg_type( &iter ) == DBUS_TYPE_ARRAY) { const char *name; p_dbus_message_iter_recurse( &iter, &iter ); while ((name = udisks_next_dict_entry( &iter, &variant ))) { if (!strcmp( name, "DeviceFile" )) p_dbus_message_iter_get_basic( &variant, &device ); else if (!strcmp( name, "DeviceIsRemovable" )) p_dbus_message_iter_get_basic( &variant, &removable ); else if (!strcmp( name, "IdType" )) p_dbus_message_iter_get_basic( &variant, &type ); else if (!strcmp( name, "DriveMediaCompatibility" )) { DBusMessageIter media; p_dbus_message_iter_recurse( &variant, &media ); while (p_dbus_message_iter_get_arg_type( &media ) == DBUS_TYPE_STRING) { const char *media_type; p_dbus_message_iter_get_basic( &media, &media_type ); if (!strncmp( media_type, "optical_dvd", 11 )) drive_type = DEVICE_DVD; if (!strncmp( media_type, "floppy", 6 )) drive_type = DEVICE_FLOPPY; else if (!strncmp( media_type, "optical_", 8 ) && drive_type == DEVICE_UNKNOWN) drive_type = DEVICE_CDROM; p_dbus_message_iter_next( &media ); } } else if (!strcmp( name, "DeviceMountPaths" )) { DBusMessageIter paths; p_dbus_message_iter_recurse( &variant, &paths ); if (p_dbus_message_iter_get_arg_type( &paths ) == DBUS_TYPE_STRING) p_dbus_message_iter_get_basic( &paths, &mount_point ); } else if (!strcmp( name, "IdUuid" )) { char *uuid_str; p_dbus_message_iter_get_basic( &variant, &uuid_str ); guid_ptr = parse_uuid( &guid, uuid_str ); } } } TRACE( "udi %s device %s mount point %s uuid %s type %s removable %u\n", debugstr_a(udi), debugstr_a(device), debugstr_a(mount_point), debugstr_guid(guid_ptr), debugstr_a(type), removable ); if (type) { if (!strcmp( type, "iso9660" )) { removable = TRUE; drive_type = DEVICE_CDROM; } else if (!strcmp( type, "udf" )) { removable = TRUE; drive_type = DEVICE_DVD; } } if (device) { if (removable) add_dos_device( -1, udi, device, mount_point, drive_type, guid_ptr ); else if (guid_ptr) add_volume( udi, device, mount_point, DEVICE_HARDDISK_VOL, guid_ptr ); } p_dbus_message_unref( reply ); }
static void udisks2_add_device( const char *udi, DBusMessageIter *dict, DBusMessageIter *block ) { DBusMessageIter iter, variant, paths, string; const char *device = NULL; const char *mount_point = NULL; const char *type = NULL; const char *drive = NULL; GUID guid, *guid_ptr = NULL; const char *iface, *name; int removable = FALSE; enum device_type drive_type = DEVICE_UNKNOWN; while ((iface = udisks_next_dict_entry( block, &iter ))) { if (!strcmp( iface, "org.freedesktop.UDisks2.Filesystem" )) { while ((name = udisks_next_dict_entry( &iter, &variant ))) { if (!strcmp( name, "MountPoints" )) { p_dbus_message_iter_recurse( &variant, &paths ); if (p_dbus_message_iter_get_arg_type( &paths ) == DBUS_TYPE_ARRAY) { p_dbus_message_iter_recurse( &variant, &string ); mount_point = udisks2_string_from_array( &string ); } } } } if (!strcmp( iface, "org.freedesktop.UDisks2.Block" )) { while ((name = udisks_next_dict_entry( &iter, &variant ))) { if (!strcmp( name, "Device" )) device = udisks2_string_from_array( &variant ); else if (!strcmp( name, "IdType" )) p_dbus_message_iter_get_basic( &variant, &type ); else if (!strcmp( name, "Drive" )) { p_dbus_message_iter_get_basic( &variant, &drive ); udisks2_get_drive_info( drive, dict, &drive_type, &removable ); } else if (!strcmp( name, "IdUUID" )) { const char *uuid_str; if (p_dbus_message_iter_get_arg_type( &variant ) == DBUS_TYPE_ARRAY) uuid_str = udisks2_string_from_array( &variant ); else p_dbus_message_iter_get_basic( &variant, &uuid_str ); guid_ptr = parse_uuid( &guid, uuid_str ); } } } } TRACE( "udi %s device %s mount point %s uuid %s type %s removable %u\n", debugstr_a(udi), debugstr_a(device), debugstr_a(mount_point), debugstr_guid(guid_ptr), debugstr_a(type), removable ); if (type) { if (!strcmp( type, "iso9660" )) { removable = TRUE; drive_type = DEVICE_CDROM; } else if (!strcmp( type, "udf" )) { removable = TRUE; drive_type = DEVICE_DVD; } } if (device) { if (removable) add_dos_device( -1, udi, device, mount_point, drive_type, guid_ptr ); else if (guid_ptr) add_volume( udi, device, mount_point, DEVICE_HARDDISK_VOL, guid_ptr ); } }