Listing devices using udisks2

Christopher Chittleborough cchittleborough at cluemail.com
Wed May 16 21:00:05 PDT 2012


On Mon, May 14, 2012, at 05:01 PM, David Zeuthen wrote (in part):
> On Mon, May 14, 2012 at 4:54 PM, Anant Kamath <kamathanant at gmail.com> wrote:
> > I'm using udisks 1.97
> >
> > Since the EnumarateDevices DBus method is not available in udisks2, is the
> > org.freedesktop.DBus.ObjectManager.GetManagedObjects method the best way to
> > list drives using udisks2 ?
> 
> The best way is to use libudisks2 (LGPLv2), see
> http://udisks.freedesktop.org/docs/latest/
> which is accessible from C/C++ and any language supporting
> GObjectIntrospection. If that is not an option, the next best choice
> is to use GDBusObjectManagerClient, see
> http://developer.gnome.org/gio/unstable/GDBusObjectManagerClient.html
> or any other D-Bus ObjectManager implementation. If you don't have a
> D-Bus ObjectManager implementation available, you can (relatively)
> easily write one yourself <SNIP>

You can also list the known drives via D-Bus, with a little work. Use
org.freedesktop.DBus.Introspectable.Introspect() on a D-Bus object, and
scan the resulting XML text for "<node" followed by whitespace followed
by "name=" followed by a single or double quote. (This is guaranteed to
work because the XML will be well-formed and the DBus spec requires
<node> elements other than the document root to have name attributes.

I tested this idea under udisks 1.91. Using Introspect() on
org.freedesktop.UDisks2 showed child nodes named "drives" (ie.,
org.freedesktop.UDisks2.drives) and "block_devices". Introspect() on
org.freedesktop.UDisks2.block_devices showed child nodes for every block
device on my system (to be precise, everything in /sys/block/).

Even in pure ISO C, it's not that hard to get the list of nodes from the
XML:

const char * p = xml_string;
while ( ((p = strstr(p, "<node")) != NULL ) {
	p += 5;
	if ( ! isspace(*p) ) continue;          // Ignore "<node>"
	do { ++p; } while (isspace(*p));
	if ( strncmp(p, "name=", 5) ) continue; // Should not happen
	p+= 5;
	char q = *p;            // Should be quote character: ' or "
	if ( q != '"' && q != '\'' ) continue;  // Should not happen
	++p;
	const char * name_end = strchr(p, q);
	if ( name_end == NULL ) continue;       // Should not happen
	process_node_by_name_len(p, (name_end - p));
}

Hope this helps -- Chris


More information about the devkit-devel mailing list