1 diff --git a/exo-mount/exo-mount-hal.c b/exo-mount/exo-mount-hal.c
2 index 791a536..4084719 100644
3 --- a/exo-mount/exo-mount-hal.c
4 +++ b/exo-mount/exo-mount-hal.c
5 @@ -145,6 +145,42 @@ exo_mount_hal_propagate_error (GError **error,
10 +string_in_list(gchar * const *haystack, const gchar *needle)
17 + for (n=0; haystack[n]; ++n) {
18 + if (!strcmp (haystack[n], needle))
26 +device_has_interface(const gchar *udi, const gchar *iface,
32 + /* determine the info.interfaces property of the device */
33 + interfaces = libhal_device_get_property_strlist (hal_context, udi,
34 + "info.interfaces", derror);
36 + /* check for the interface we need */
37 + result = string_in_list(interfaces, iface);
38 + libhal_free_string_array(interfaces);
47 * exo_mount_hal_device_from_udi:
48 @@ -158,18 +194,15 @@ exo_mount_hal_propagate_error (GError **error,
49 * or %NULL in case of an error.
52 -exo_mount_hal_device_from_udi (const gchar *udi,
53 +exo_mount_hal_device_from_udi (const gchar *in_udi,
56 ExoMountHalDevice *device = NULL;
59 - gchar **volume_udis;
60 - gchar *volume_udi = NULL;
65 - g_return_val_if_fail (udi != NULL, NULL);
66 + g_return_val_if_fail (in_udi != NULL, NULL);
67 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
69 /* make sure the HAL support is initialized */
70 @@ -179,55 +212,60 @@ exo_mount_hal_device_from_udi (const gchar *udi,
71 /* initialize D-Bus error */
72 dbus_error_init (&derror);
75 - /* determine the info.interfaces property of the device */
76 - interfaces = libhal_device_get_property_strlist (hal_context, udi, "info.interfaces", &derror);
77 - if (G_UNLIKELY (interfaces == NULL))
78 + udi = g_strdup (in_udi);
79 + /* at this point, we own udi */
81 + /* maybe we have a mountable device here */
82 + while(G_UNLIKELY (!device_has_interface (udi,
83 + "org.freedesktop.Hal.Device.Volume", &derror)))
85 - /* reset D-Bus error */
86 - dbus_error_free (&derror);
87 + gchar **volume_udis;
89 - /* release any previous volume UDI */
90 - g_free (volume_udi);
92 + /* maybe there was a D-Bus error? gotta check */
93 + if (G_UNLIKELY (dbus_error_is_set (&derror)))
95 + exo_mount_hal_propagate_error (error, &derror);
100 + /* maybe we have a volume whose parent is identified by the udi */
101 + volume_udis = libhal_manager_find_device_string_match (hal_context,
102 + "info.parent", udi, &n_volume_udis, &derror);
104 - /* ok, but maybe we have a volume whose parent is identified by the udi */
105 - volume_udis = libhal_manager_find_device_string_match (hal_context, "info.parent", udi, &n_volume_udis, &derror);
106 if (G_UNLIKELY (volume_udis == NULL))
108 -err0: exo_mount_hal_propagate_error (error, &derror);
110 + exo_mount_hal_propagate_error (error, &derror);
114 else if (G_UNLIKELY (n_volume_udis < 1))
116 - /* no match, we cannot handle that device */
117 libhal_free_string_array (volume_udis);
119 + dbus_error_free (&derror);
120 + /* definitely not a device that we're able to
121 + * mount, eject or unmount */
122 + g_set_error (error, G_FILE_ERROR, G_FILE_ERROR_FAILED,
123 + _("Given device \"%s\" is not a volume or drive"), udi);
130 /* use the first volume UDI... */
131 - volume_udi = g_strdup (volume_udis[0]);
132 + udi = g_strdup (volume_udis[0]);
133 libhal_free_string_array (volume_udis);
135 /* ..and try again using that UDI */
136 - udi = (const gchar *) volume_udi;
140 - /* verify that we have a mountable device here */
141 - for (n = 0; interfaces[n] != NULL; ++n)
142 - if (strcmp (interfaces[n], "org.freedesktop.Hal.Device.Volume") == 0)
144 - if (G_UNLIKELY (interfaces[n] == NULL))
146 - /* definitely not a device that we're able to mount, eject or unmount */
147 -err1: g_set_error (error, G_FILE_ERROR, G_FILE_ERROR_FAILED, _("Given device \"%s\" is not a volume or drive"), udi);
150 + /* at this point, udi contains the UDI of something
151 + * that implements Hal.Device.Volume.
152 + * udi is the only resource that we hold here. */
154 /* setup the device struct */
155 device = g_new0 (ExoMountHalDevice, 1);
156 - device->udi = g_strdup (udi);
159 /* check if we have a volume here */
160 device->volume = libhal_volume_from_udi (hal_context, udi);
161 @@ -269,8 +307,8 @@ err1: g_set_error (error, G_FILE_ERROR, G_FILE_ERROR_FAILED, _("Given device \"%
162 if (G_UNLIKELY (device->file == NULL || device->name == NULL))
164 exo_mount_hal_device_free (device);
167 + exo_mount_hal_propagate_error(error, &derror);
171 /* check if we failed */
172 @@ -282,11 +320,7 @@ err1: g_set_error (error, G_FILE_ERROR, G_FILE_ERROR_FAILED, _("Given device \"%
178 - libhal_free_string_array (interfaces);
179 - g_free (volume_udi);
181 + dbus_error_free (&derror);
185 @@ -313,7 +347,7 @@ exo_mount_hal_device_from_file (const gchar *file,
192 g_return_val_if_fail (g_path_is_absolute (file), NULL);
193 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
194 @@ -347,12 +381,7 @@ exo_mount_hal_device_from_file (const gchar *file,
197 /* check if we have a mountable device here */
198 - for (m = 0; interfaces[m] != NULL; ++m)
199 - if (strcmp (interfaces[m], "org.freedesktop.Hal.Device.Volume") == 0)
202 - /* check if it's a usable device */
203 - if (interfaces[m] != NULL)
204 + if (string_in_list (interfaces, "org.freedesktop.Hal.Device.Volume"))
206 libhal_free_string_array (interfaces);