UDisks Versus UDisks2

Lawrence D'Oliveiro ldo at geek-central.gen.nz
Thu Sep 21 00:45:36 UTC 2017


A while back, I put together this D-Bus-based utility
<https://github.com/ldo/waitdisc> to get around the problem of
optical-disc-burning programs seemingly not being able to wait for
media to become ready for starting to write. I originally wrote it to
interface to the UDisks service. Then later I discovered this might not
be available by default, but there was thing called UDisks2, so perhaps
I should be using that instead.

And I must say, I wonder why we have UDisks2 at all. Did somebody have
something against the original UDisks? For comparison, here is the
routine I wrote to find available optical writer drives with UDisks:

    udisks_bus = "org.freedesktop.UDisks"
    udisks_device_interface = "org.freedesktop.UDisks.Device"
    udisks_devices_root = "/org/freedesktop/UDisks/devices"

    def find_optical_writer_drives() :
        try :
            udisks_devices = \
                dbus.SystemBus().get_object(udisks_bus, udisks_devices_root)
        except dbus.exceptions.DBusException :
            # assume udisks daemon not running
            # udisks_devices = None
            raise # don't bother recovering
        #end try
        if udisks_devices != None :
            for device_node in introspect(udisks_devices).findall("node") :
                device_name = udisks_devices_root + "/" + device_node.get("name")
                device = dbus.SystemBus().get_object(udisks_bus, device_name)
                if (
                        not get_prop(device, udisks_device_interface, "DeviceIsReadOnly", to_bool)
                    and
                        any
                          (
                            "optical" in str(s)
                            for s in
                                get_prop(device, udisks_device_interface, "DriveMediaCompatibility")
                          )
                ) :
                    yield device_name, device
                #end if
            #end for
        #end if
    #end find_optical_writer_drives

and here is the equivalent with UDisks2:

    udisks_bus = "org.freedesktop.UDisks2"
    udisks_block_device_interface = "org.freedesktop.UDisks2.Block"
    udisks_block_devices_root = "/org/freedesktop/UDisks2/block_devices"
    udisks_drive_interface = "org.freedesktop.UDisks2.Drive"
    udisks_drives_root = "/org/freedesktop/UDisks2/drives"

    def find_optical_writer_drives() :
        device_names = {}
        udisks_devices = dbus.SystemBus().get_object(udisks_bus, udisks_block_devices_root)
        for device_node in introspect(udisks_devices).findall("node") :
            device_name = udisks_block_devices_root + "/" + device_node.get("name")
            device = dbus.SystemBus().get_object(udisks_bus, device_name)
            props = {}
            for \
                name, cvt \
            in \
                (
                    ("Drive", str),
                    ("PreferredDevice", str_nullterm),
                ) \
            :
                props[name] = cvt(get_prop(device, udisks_block_device_interface, name))
            #end for
            device_names[props["Drive"]] = props["PreferredDevice"]
        #end for
        udisks_devices = dbus.SystemBus().get_object(udisks_bus, udisks_drives_root)
        for device_node in introspect(udisks_devices).findall("node") :
            device_name = udisks_drives_root + "/" + device_node.get("name")
            device = dbus.SystemBus().get_object(udisks_bus, device_name)
            if (
                any
                  (
                    str(s).startswith("optical")
                    for s in get_prop(device, udisks_drive_interface, "MediaCompatibility")
                  )
            ) :
                yield device_name, device, device_names[device_name]
            #end if
        #end for
    #end find_optical_writer_drives

Not only is the latter longer and more complicated, it also isn’t an
exact equivalent. Because I could not find any way to test that an
optical drive actually supports writing in UDisks2.

Have I missed something?


More information about the dbus mailing list