port to new DeviceKit interface
authorDavid Zeuthen <davidz@redhat.com>
Fri, 25 Apr 2008 18:00:38 +0000 (14:00 -0400)
committerDavid Zeuthen <davidz@redhat.com>
Fri, 25 Apr 2008 18:00:38 +0000 (14:00 -0400)
This still needs a bit of work...

configure.in
src/devkit-disks-daemon.c
src/devkit-disks-device.c
src/devkit-disks-device.h
src/main.c

index 31310c3..f1fbf5b 100644 (file)
@@ -95,9 +95,9 @@ if test "x$GCC" = "xyes"; then
   changequote([,])dnl
 fi
 
-PKG_CHECK_MODULES(DEVKIT, [devkit >= 001])
-AC_SUBST(DEVKIT_CFLAGS)
-AC_SUBST(DEVKIT_LIBS)
+PKG_CHECK_MODULES(DEVKIT, [devkit-gobject >= 002])
+AC_SUBST(DEVKIT_GOBJECT_CFLAGS)
+AC_SUBST(DEVKIT_GOBJECT_LIBS)
 
 PKG_CHECK_MODULES(GLIB, [glib-2.0 >= 2.6.0])
 AC_SUBST(GLIB_CFLAGS)
index e5b7004..8e259b4 100644 (file)
@@ -48,6 +48,7 @@
 #include <gio/gunixmounts.h>
 #include <dbus/dbus-glib.h>
 #include <dbus/dbus-glib-lowlevel.h>
+#include <devkit-gobject.h>
 
 #include "devkit-disks-daemon.h"
 #include "devkit-disks-device.h"
@@ -57,6 +58,7 @@
 #include "devkit-disks-daemon-glue.h"
 #include "devkit-disks-marshal.h"
 
+
 /*--------------------------------------------------------------------------------------------------------------*/
 
 enum
@@ -77,8 +79,8 @@ struct DevkitDisksDaemonPrivate
         PolKitContext     *pk_context;
         PolKitTracker     *pk_tracker;
 
-       int                udev_socket;
-       GIOChannel        *udev_channel;
+        DevkitClient      *devkit_client;
+
        GIOChannel        *mdstat_channel;
 
         GList             *inhibitors;
@@ -367,7 +369,6 @@ static void
 devkit_disks_daemon_init (DevkitDisksDaemon *daemon)
 {
         daemon->priv = DEVKIT_DISKS_DAEMON_GET_PRIVATE (daemon);
-        daemon->priv->udev_socket = -1;
         daemon->priv->map_native_path_to_device = g_hash_table_new_full (g_str_hash,
                                                                          g_str_equal,
                                                                          g_free,
@@ -411,15 +412,9 @@ devkit_disks_daemon_finalize (GObject *object)
                 g_source_remove (daemon->priv->killtimer_id);
         }
 
-        if (daemon->priv->udev_socket != -1)
-                close (daemon->priv->udev_socket);
-
         if (daemon->priv->mdstat_channel != NULL)
                 g_io_channel_unref (daemon->priv->mdstat_channel);
 
-        if (daemon->priv->udev_channel != NULL)
-                g_io_channel_unref (daemon->priv->udev_channel);
-
         if (daemon->priv->map_native_path_to_device != NULL) {
                 g_hash_table_unref (daemon->priv->map_native_path_to_device);
         }
@@ -431,6 +426,10 @@ devkit_disks_daemon_finalize (GObject *object)
                 g_object_unref (daemon->priv->mount_monitor);
         }
 
+        if (daemon->priv->devkit_client != NULL) {
+                g_object_unref (daemon->priv->devkit_client);
+        }
+
         G_OBJECT_CLASS (devkit_disks_daemon_parent_class)->finalize (object);
 }
 
@@ -550,6 +549,7 @@ device_went_away (gpointer user_data, GObject *where_the_object_was)
                                      where_the_object_was);
 }
 
+#if 0
 static void device_add    (DevkitDisksDaemon *daemon, const char *native_path, gboolean emit_event);
 static void device_remove (DevkitDisksDaemon *daemon, const char *native_path);
 
@@ -571,26 +571,35 @@ device_changed (DevkitDisksDaemon *daemon, const char *native_path, gboolean syn
                 device_add (daemon, native_path, TRUE);
         }
 }
+#endif
+
+static void
+device_changed (DevkitDisksDaemon *daemon, const char *native_path, gboolean synthesized)
+{
+        g_warning ("TODO");
+}
 
 void
 devkit_disks_daemon_local_synthesize_changed (DevkitDisksDaemon       *daemon,
                                               const char              *native_path)
 {
-        device_changed (daemon, native_path, TRUE);
+        //device_changed (daemon, native_path, TRUE);
 }
 
 static void
-device_add (DevkitDisksDaemon *daemon, const char *native_path, gboolean emit_event)
+device_add (DevkitDisksDaemon *daemon, DevkitDevice *d, gboolean emit_event)
 {
         DevkitDisksDevice *device;
+        const char *native_path;
 
+        native_path = devkit_device_get_native_path (d);
         device = g_hash_table_lookup (daemon->priv->map_native_path_to_device, native_path);
         if (device != NULL) {
                 /* we already have the device; treat as change event */
                 g_print ("treating add event as change event on %s\n", native_path);
-                device_changed (daemon, native_path, FALSE);
+                //device_changed (daemon, d, FALSE);
         } else {
-                device = devkit_disks_device_new (daemon, native_path);
+                device = devkit_disks_device_new (daemon, d);
 
                 if (device != NULL) {
                         /* only take a weak ref; the device will stay on the bus until
@@ -615,6 +624,7 @@ device_add (DevkitDisksDaemon *daemon, const char *native_path, gboolean emit_ev
         }
 }
 
+#if 0
 static void
 device_remove (DevkitDisksDaemon *daemon, const char *native_path)
 {
@@ -630,6 +640,7 @@ device_remove (DevkitDisksDaemon *daemon, const char *native_path)
                 g_object_unref (device);
         }
 }
+#endif
 
 DevkitDisksDevice *
 devkit_disks_daemon_local_find_by_native_path (DevkitDisksDaemon *daemon, const char *native_path)
@@ -650,104 +661,6 @@ devkit_disks_daemon_local_get_all_devices (DevkitDisksDaemon *daemon)
         return g_hash_table_get_values (daemon->priv->map_native_path_to_device);
 }
 
-static gboolean
-receive_udev_data (GIOChannel *source, GIOCondition condition, gpointer user_data)
-{
-        DevkitDisksDaemon *daemon = user_data;
-       int fd;
-       int retval;
-       struct msghdr smsg;
-       struct cmsghdr *cmsg;
-       struct iovec iov;
-       struct ucred *cred;
-       char cred_msg[CMSG_SPACE(sizeof(struct ucred))];
-       char buf[4096];
-       size_t bufpos = 0;
-       const char *action;
-       const char *devpath;
-       const char *subsystem;
-
-       memset(buf, 0x00, sizeof (buf));
-       iov.iov_base = &buf;
-       iov.iov_len = sizeof (buf);
-       memset (&smsg, 0x00, sizeof (struct msghdr));
-       smsg.msg_iov = &iov;
-       smsg.msg_iovlen = 1;
-       smsg.msg_control = cred_msg;
-       smsg.msg_controllen = sizeof (cred_msg);
-
-       fd = g_io_channel_unix_get_fd (source);
-
-       retval = recvmsg (fd, &smsg, 0);
-       if (retval <  0) {
-               if (errno != EINTR)
-                       g_warning ("Unable to receive message: %m");
-               goto out;
-       }
-       cmsg = CMSG_FIRSTHDR (&smsg);
-       cred = (struct ucred *) CMSG_DATA (cmsg);
-
-       if (cmsg == NULL || cmsg->cmsg_type != SCM_CREDENTIALS) {
-               g_warning ("No sender credentials received, message ignored");
-               goto out;
-       }
-
-       if (cred->uid != 0) {
-               g_warning ("Sender uid=%d, message ignored", cred->uid);
-               goto out;
-       }
-
-       if (!strstr(buf, "@/")) {
-               g_warning ("invalid message format");
-               goto out;
-       }
-
-        action = NULL;
-        devpath = NULL;
-        subsystem = NULL;
-       while (bufpos < sizeof (buf)) {
-               size_t keylen;
-               char *key;
-
-               key = &buf[bufpos];
-               keylen = strlen(key);
-               if (keylen == 0)
-                       break;
-               bufpos += keylen + 1;
-
-               if (strncmp (key, "ACTION=", 7) == 0) {
-                       action = key + 7;
-               } else if (strncmp (key, "DEVPATH=", 8) == 0) {
-                        devpath = key + 8;
-               } else if (strncmp(key, "SUBSYSTEM=", 10) == 0) {
-                        subsystem = key + 10;
-                }
-                /* TODO: collect other values */
-       }
-
-       if (action != NULL && devpath != NULL && subsystem != NULL) {
-                if (strcmp (subsystem, "block") == 0) {
-                        char *native_path;
-
-                        native_path = g_build_filename ("/sys", devpath, NULL);
-                        if (strcmp (action, "add") == 0) {
-                                device_add (daemon, native_path, TRUE);
-                        } else if (strcmp (action, "remove") == 0) {
-                                device_remove (daemon, native_path);
-                        } else if (strcmp (action, "change") == 0) {
-                                device_changed (daemon, native_path, FALSE);
-                        }
-                        g_free (native_path);
-                }
-
-       } else {
-                g_warning ("malformed message");
-        }
-
-out:
-       return TRUE;
-}
-
 static void
 mounts_changed (GUnixMountMonitor *monitor, gpointer user_data)
 {
@@ -809,9 +722,6 @@ out:
 static gboolean
 register_disks_daemon (DevkitDisksDaemon *daemon)
 {
-       struct sockaddr_un saddr;
-       socklen_t addrlen;
-       const int on = 1;
         DBusConnection *connection;
         DBusError dbus_error;
         GError *error = NULL;
@@ -904,28 +814,13 @@ register_disks_daemon (DevkitDisksDaemon *daemon)
                 error = NULL;
        }
 
-
-       /* setup socket for listening from messages from udev */
-       memset (&saddr, 0x00, sizeof(saddr));
-       saddr.sun_family = AF_LOCAL;
-       /* use abstract namespace for socket path */
-       strcpy (&saddr.sun_path[1], "/org/freedesktop/devicekit/disks/udev_event");
-       addrlen = offsetof (struct sockaddr_un, sun_path) + strlen(saddr.sun_path+1) + 1;
-       daemon->priv->udev_socket = socket (AF_LOCAL, SOCK_DGRAM, 0);
-       if (daemon->priv->udev_socket == -1) {
-               g_warning ("Couldn't open udev event socket: %m");
-                goto error;
-       }
-
-       if (bind (daemon->priv->udev_socket, (struct sockaddr *) &saddr, addrlen) < 0) {
-               g_warning ("Error binding to udev event socket: %m");
+        /* connect to the DeviceKit daemon */
+        daemon->priv->devkit_client = devkit_client_new (NULL); // TODO: subsystems
+        if (!devkit_client_connect (daemon->priv->devkit_client, &error)) {
+               g_warning ("Couldn't open connection to DeviceKit daemon: %s", error->message);
+                g_error_free (error);
                 goto error;
-       }
-       /* enable receiving of the sender credentials */
-       setsockopt (daemon->priv->udev_socket, SOL_SOCKET, SO_PASSCRED, &on, sizeof(on));
-       daemon->priv->udev_channel = g_io_channel_unix_new (daemon->priv->udev_socket);
-       g_io_add_watch (daemon->priv->udev_channel, G_IO_IN, receive_udev_data, daemon);
-       g_io_channel_unref (daemon->priv->udev_channel);
+        }
 
         /* monitor mounts */
         daemon->priv->mount_monitor = g_unix_mount_monitor_new ();
@@ -945,27 +840,36 @@ error:
 DevkitDisksDaemon *
 devkit_disks_daemon_new (gboolean no_exit)
 {
-        gboolean res;
         DevkitDisksDaemon *daemon;
-        GList *native_paths;
+        GError *error = NULL;
+        GList *devices;
         GList *l;
+        const char *subsystems[] = {"block", NULL};
 
         daemon = DEVKIT_DISKS_DAEMON (g_object_new (DEVKIT_TYPE_DISKS_DAEMON, NULL));
         daemon->priv->no_exit = no_exit;
 
-        res = register_disks_daemon (DEVKIT_DISKS_DAEMON (daemon));
-        if (! res) {
+        if (!register_disks_daemon (DEVKIT_DISKS_DAEMON (daemon))) {
                 g_object_unref (daemon);
                 return NULL;
         }
 
-        native_paths = devkit_disks_enumerate_native_paths ();
-        for (l = native_paths; l != NULL; l = l->next) {
-                char *native_path = l->data;
-                device_add (daemon, native_path, FALSE);
-                g_free (native_path);
+
+        devices = devkit_client_enumerate_by_subsystem (daemon->priv->devkit_client,
+                                                         subsystems,
+                                                         &error);
+        if (error != NULL) {
+                g_warning ("Cannot enumerate devices: %s", error->message);
+                g_error_free (error);
+                g_object_unref (daemon);
+                return NULL;
+        }
+        for (l = devices; l != NULL; l = l->next) {
+                DevkitDevice *device = l->data;
+                device_add (daemon, device, FALSE);
         }
-        g_list_free (native_paths);
+        g_list_foreach (devices, (GFunc) g_object_unref, NULL);
+        g_list_free (devices);
 
         /* clean stale directories in /media as well as stale
          * entries in /var/lib/DeviceKit-disks/mtab
index c088250..794687d 100644 (file)
@@ -46,7 +46,7 @@
 #include <gio/gunixmounts.h>
 #include <dbus/dbus-glib.h>
 #include <dbus/dbus-glib-lowlevel.h>
-#include <devkit/devkit.h>
+#include <devkit-gobject.h>
 #include <polkit-dbus/polkit-dbus.h>
 
 #include "devkit-disks-device.h"
@@ -63,7 +63,7 @@ static void     devkit_disks_device_finalize    (GObject     *object);
 
 static void     init_info                  (DevkitDisksDevice *device);
 static void     free_info                  (DevkitDisksDevice *device);
-static gboolean update_info                (DevkitDisksDevice *device);
+static gboolean update_info                (DevkitDisksDevice *device, DevkitDevice *d);
 
 /* Returns the cleartext device. If device==NULL, unlocking failed and an error has
  * been reported back to the caller
@@ -1174,14 +1174,6 @@ init_info (DevkitDisksDevice *device)
 }
 
 
-static devkit_bool_t
-update_info_add_ptr (DevKitInfo *info, const char *str, void *user_data)
-{
-        GPtrArray *ptr_array = user_data;
-        g_ptr_array_add (ptr_array, g_strdup (str));
-        return FALSE;
-}
-
 static char *
 _dupv8 (const char *s)
 {
@@ -1235,62 +1227,60 @@ sysfs_resolve_link (const char *sysfs_path, const char *name)
                 return NULL;
 }
 
-static devkit_bool_t
-update_info_properties_cb (DevKitInfo *info, const char *key, void *user_data)
+static gboolean
+update_info_properties_cb (DevkitDevice *d, const char *key, const char *value, void *user_data)
 {
+        int n;
         gboolean ignore_device;
         DevkitDisksDevice *device = user_data;
 
         ignore_device = FALSE;
 
         if (strcmp (key, "ID_FS_USAGE") == 0) {
-                device->priv->info.id_usage   = _dupv8 (devkit_info_property_get_string (info, key));
+                device->priv->info.id_usage   = g_strdup (value);
         } else if (strcmp (key, "ID_FS_TYPE") == 0) {
-                device->priv->info.id_type    = _dupv8 (devkit_info_property_get_string (info, key));
+                device->priv->info.id_type    = g_strdup (value);
         } else if (strcmp (key, "ID_FS_VERSION") == 0) {
-                device->priv->info.id_version = _dupv8 (devkit_info_property_get_string (info, key));
+                device->priv->info.id_version = g_strdup (value);
                 if (device->priv->info.device_is_linux_md_component) {
-                        device->priv->info.linux_md_component_version =
-                                _dupv8 (devkit_info_property_get_string (info, key));
+                        device->priv->info.linux_md_component_version = g_strdup (value);
                 }
         } else if (strcmp (key, "ID_FS_UUID") == 0) {
-                device->priv->info.id_uuid    = _dupv8 (devkit_info_property_get_string (info, key));
+                device->priv->info.id_uuid    = g_strdup (value);
                 if (device->priv->info.device_is_linux_md_component) {
-                        device->priv->info.linux_md_component_uuid =
-                                _dupv8 (devkit_info_property_get_string (info, key));
+                        device->priv->info.linux_md_component_uuid = g_strdup (value);
                 }
         } else if (strcmp (key, "ID_FS_LABEL") == 0) {
-                device->priv->info.id_label   = _dupv8 (devkit_info_property_get_string (info, key));
+                device->priv->info.id_label   = g_strdup (value);
                 if (device->priv->info.device_is_linux_md_component) {
-                        device->priv->info.linux_md_component_name =
-                                _dupv8 (devkit_info_property_get_string (info, key));
+                        device->priv->info.linux_md_component_name = g_strdup (value);
                 }
 
         } else if (strcmp (key, "ID_VENDOR") == 0) {
                 if (device->priv->info.device_is_drive && device->priv->info.drive_vendor == NULL)
-                        device->priv->info.drive_vendor = _dupv8 (devkit_info_property_get_string (info, key));
+                        device->priv->info.drive_vendor = g_strdup (value);
         } else if (strcmp (key, "ID_MODEL") == 0) {
                 if (device->priv->info.device_is_drive && device->priv->info.drive_model == NULL)
-                        device->priv->info.drive_model = _dupv8 (devkit_info_property_get_string (info, key));
+                        device->priv->info.drive_model = g_strdup (value);
         } else if (strcmp (key, "ID_REVISION") == 0) {
                 if (device->priv->info.device_is_drive && device->priv->info.drive_revision == NULL)
-                        device->priv->info.drive_revision = _dupv8 (devkit_info_property_get_string (info, key));
+                        device->priv->info.drive_revision = g_strdup (value);
         } else if (strcmp (key, "ID_SERIAL_SHORT") == 0) {
                 if (device->priv->info.device_is_drive && device->priv->info.drive_serial == NULL)
-                        device->priv->info.drive_serial = _dupv8 (devkit_info_property_get_string (info, key));
+                        device->priv->info.drive_serial = g_strdup (value);
 
         } else if (strcmp (key, "PART_SCHEME") == 0) {
 
                 if (device->priv->info.device_is_partition) {
                         device->priv->info.partition_scheme =
-                                _dupv8 (devkit_info_property_get_string (info, key));
+                                g_strdup (value);
                 } else {
                         device->priv->info.device_is_partition_table = TRUE;
                         device->priv->info.partition_table_scheme =
-                                _dupv8 (devkit_info_property_get_string (info, key));
+                                g_strdup (value);
                 }
         } else if (strcmp (key, "PART_COUNT") == 0) {
-                device->priv->info.partition_table_count = devkit_info_property_get_int (info, key);
+                device->priv->info.partition_table_count = devkit_device_get_property_as_int (d, key);
         } else if (g_str_has_prefix (key, "PART_P") && g_ascii_isdigit (key[6])) {
                 char *endp;
                 int part_number = strtol (key + 6, &endp, 10);
@@ -1306,7 +1296,7 @@ update_info_properties_cb (DevKitInfo *info, const char *key, void *user_data)
 
                                 array = NULL;
                                 index = 0;
-                                value = devkit_info_property_get_uint64 (info, key);
+                                value = devkit_device_get_property_as_uint64 (d, key);
                                 if (g_str_has_prefix (endp, "_OFFSET")) {
                                         array = device->priv->info.partition_table_offsets;
                                         index = part_number - 1;
@@ -1324,53 +1314,52 @@ update_info_properties_cb (DevKitInfo *info, const char *key, void *user_data)
 
                                 if (g_str_has_prefix (endp, "_LABEL")) {
                                         device->priv->info.partition_label =
-                                                _dupv8 (devkit_info_property_get_string (info, key));
+                                                g_strdup (value);
                                 } else if (g_str_has_prefix (endp, "_UUID")) {
                                         device->priv->info.partition_uuid =
-                                                _dupv8 (devkit_info_property_get_string (info, key));
+                                                g_strdup (value);
                                 } else if (g_str_has_prefix (endp, "_TYPE")) {
                                         device->priv->info.partition_type =
-                                                _dupv8 (devkit_info_property_get_string (info, key));
+                                                g_strdup (value);
                                 } else if (g_str_has_prefix (endp, "_OFFSET")) {
                                         device->priv->info.partition_offset =
-                                                devkit_info_property_get_uint64 (info, key);
+                                                devkit_device_get_property_as_uint64 (d, key);
                                 } else if (g_str_has_prefix (endp, "_SIZE")) {
                                         device->priv->info.partition_size =
-                                                devkit_info_property_get_uint64 (info, key);
+                                                devkit_device_get_property_as_uint64 (d, key);
                                 } else if (g_str_has_prefix (endp, "_FLAGS")) {
-                                        devkit_info_property_strlist_foreach (info, key, update_info_add_ptr,
-                                                                              device->priv->info.partition_flags);
+                                        char **tokens = devkit_device_dup_property_as_strv (d, key);
+                                        for (n = 0; tokens[n] != NULL; n++)
+                                                g_ptr_array_add (device->priv->info.partition_flags, tokens[n]);
+                                        g_free (tokens);
                                 }
                         }
                 }
 
         } else if (strcmp (key, "MD_DEVICES") == 0) {
-                device->priv->info.linux_md_component_num_raid_devices = devkit_info_property_get_int (info, key);
+                device->priv->info.linux_md_component_num_raid_devices = devkit_device_get_property_as_int (d, key);
 
         } else if (strcmp (key, "MD_LEVEL") == 0) {
-                device->priv->info.linux_md_component_level = g_strdup (devkit_info_property_get_string (info, key));
+                device->priv->info.linux_md_component_level = g_strdup (value);
 
         } else if (strcmp (key, "MD_UPDATE_TIME") == 0) {
-                device->priv->info.linux_md_component_update_time = devkit_info_property_get_uint64 (info, key);
+                device->priv->info.linux_md_component_update_time = devkit_device_get_property_as_uint64 (d, key);
 
         } else if (strcmp (key, "MD_EVENTS") == 0) {
-                device->priv->info.linux_md_component_events = devkit_info_property_get_uint64 (info, key);
+                device->priv->info.linux_md_component_events = devkit_device_get_property_as_uint64 (d, key);
 
         } else if (strcmp (key, "DM_NAME") == 0) {
-                const char *dm_name;
-                dm_name = devkit_info_property_get_string (info, key);
-
-                if (g_str_has_prefix (dm_name, "temporary-cryptsetup-")) {
+                if (g_str_has_prefix (value, "temporary-cryptsetup-")) {
                         /* ignore temporary devices created by /sbin/cryptsetup */
                         ignore_device = TRUE;
                         goto out;
                 } else {
                         /* TODO: export this at some point */
-                        device->priv->info.dm_name = g_strdup (dm_name);
+                        device->priv->info.dm_name = g_strdup (value);
                 }
 
         } else if (strcmp (key, "DM_TARGET_TYPES") == 0) {
-                if (strcmp (devkit_info_property_get_string (info, key), "crypt") == 0) {
+                if (strcmp (value, "crypt") == 0) {
                         /* we're a dm-crypt target and can, by design, then only have one slave */
                         if (device->priv->info.slaves_objpath->len == 1) {
                                 /* avoid claiming we are a drive since we want to be related
@@ -1385,16 +1374,17 @@ update_info_properties_cb (DevKitInfo *info, const char *key, void *user_data)
                 }
 
         } else if (strcmp (key, "COMPAT_MEDIA_TYPE") == 0) {
-                devkit_info_property_strlist_foreach (info, key, update_info_add_ptr,
-                                                      device->priv->info.drive_media_compatibility);
-
+                char **tokens = devkit_device_dup_property_as_strv (d, key);
+                for (n = 0; tokens[n] != NULL; n++)
+                        g_ptr_array_add (device->priv->info.drive_media_compatibility, tokens[n]);
+                g_free (tokens);
         } else if (strcmp (key, "MEDIA_TYPE") == 0) {
-                device->priv->info.drive_media = _dupv8 (devkit_info_property_get_string (info, key));
+                device->priv->info.drive_media = g_strdup (value);
 
 
         } else if (strcmp (key, "MEDIA_AVAILABLE") == 0) {
                 if (device->priv->info.device_is_removable) {
-                        device->priv->info.device_is_media_available = devkit_info_property_get_bool (info, key);
+                        device->priv->info.device_is_media_available = devkit_device_get_property_as_boolean (d, key);
                 }
         }
 
@@ -1402,20 +1392,6 @@ out:
         return ignore_device;
 }
 
-static gboolean
-update_info_symlinks_cb (DevKitInfo *info, const char *value, void *user_data)
-{
-        DevkitDisksDevice *device = user_data;
-
-        if (g_str_has_prefix (value, "/dev/disk/by-id/") || g_str_has_prefix (value, "/dev/disk/by-uuid/")) {
-                g_ptr_array_add (device->priv->info.device_file_by_id, _dupv8 (value));
-        } else if (g_str_has_prefix (value, "/dev/disk/by-path/")) {
-                g_ptr_array_add (device->priv->info.device_file_by_path, _dupv8 (value));
-        }
-
-        return FALSE;
-}
-
 static void
 update_slaves (DevkitDisksDevice *device)
 {
@@ -1441,7 +1417,7 @@ update_slaves (DevkitDisksDevice *device)
 
                 slave = devkit_disks_daemon_local_find_by_object_path (device->priv->daemon, slave_objpath);
                 if (slave != NULL) {
-                        update_info (slave);
+                        update_info (slave, NULL);
                 }
         }
 }
@@ -1613,7 +1589,7 @@ strv_has_str (char **strv, char *str)
  * Returns: #TRUE to keep (or add) the device; #FALSE to ignore (or remove) the device
  **/
 static gboolean
-update_info (DevkitDisksDevice *device)
+update_info (DevkitDisksDevice *device, DevkitDevice *d)
 {
         guint64 start, size;
         char *s;
@@ -1622,7 +1598,6 @@ update_info (DevkitDisksDevice *device)
         gboolean ret;
         int fd;
         int block_size;
-        DevKitInfo *info;
         char *path;
         GDir *dir;
         const char *name;
@@ -1630,7 +1605,13 @@ update_info (DevkitDisksDevice *device)
         const char *fstype;
 
         ret = FALSE;
-        info = NULL;
+
+        if (d != NULL) {
+                g_object_ref (d);
+        } else {
+                /* TODO */
+                goto out;
+        }
 
         /* md is special; we don't get "remove" events from the kernel when an array is
          * stopped; so catch it very early before erasing our existing slave variable (we
@@ -1663,28 +1644,37 @@ update_info (DevkitDisksDevice *device)
                 device->priv->info.device_is_drive = FALSE;
         }
 
-        info = devkit_info_new (device->priv->native_path);
-        if (info == NULL) {
+        device->priv->info.device_file = g_strdup (devkit_device_get_device_file (d));
+        if (device->priv->info.device_file == NULL) {
+               g_warning ("No device file for %s", device->priv->native_path);
                 goto out;
         }
 
-        device->priv->info.device_file = _dupv8 (devkit_info_get_device_file (info));
-        devkit_info_device_file_symlinks_foreach (info, update_info_symlinks_cb, device);
+        const char * const * symlinks;
+        symlinks = devkit_device_get_device_file_symlinks (d);
+        for (n = 0; symlinks[n] != NULL; n++) {
+                if (g_str_has_prefix (symlinks[n], "/dev/disk/by-id/") ||
+                    g_str_has_prefix (symlinks[n], "/dev/disk/by-uuid/")) {
+                        g_ptr_array_add (device->priv->info.device_file_by_id, g_strdup (symlinks[n]));
+                } else if (g_str_has_prefix (symlinks[n], "/dev/disk/by-path/")) {
+                        g_ptr_array_add (device->priv->info.device_file_by_path, g_strdup (symlinks[n]));
+                }
+        }
 
         /* TODO: hmm.. it would be really nice if sysfs could export this. There's a
-         *       queue/hw_sector_size in sysfs but that's not available for e.g. RAID
+         *       queue/hw_sector_size in sysfs but that's not available for e.g. Linux md devices
          */
         errno = 0;
-        fd = open (devkit_info_get_device_file (info), O_RDONLY);
+        fd = open (device->priv->info.device_file, O_RDONLY);
         if (fd < 0 && errno != ENOMEDIUM) {
-               g_warning ("Cannot open %s read only", devkit_info_get_device_file (info));
+               g_warning ("Cannot open %s read only", device->priv->info.device_file);
                 goto out;
         }
         if (errno == ENOMEDIUM) {
                 block_size = 0;
         } else {
                 if (ioctl (fd, BLKSSZGET, &block_size) != 0) {
-                        g_warning ("Cannot determine block size for %s", devkit_info_get_device_file (info));
+                        g_warning ("Cannot determine block size for %s", device->priv->info.device_file);
                         goto out;
                 }
                 close (fd);
@@ -1694,13 +1684,12 @@ update_info (DevkitDisksDevice *device)
                  * (e.g. write-protect on SD cards, optical drives etc.)
                  */
                 errno = 0;
-                fd = open (devkit_info_get_device_file (info), O_WRONLY);
+                fd = open (device->priv->info.device_file, O_WRONLY);
                 if (fd < 0) {
                         if (errno == EROFS) {
                                 device->priv->info.device_is_read_only = TRUE;
                         } else {
-                                g_warning ("Cannot determine if %s is read only: %m",
-                                           devkit_info_get_device_file (info));
+                                g_warning ("Cannot determine if %s is read only: %m", device->priv->info.device_file);
                                 goto out;
                         }
                 } else {
@@ -1780,12 +1769,12 @@ update_info (DevkitDisksDevice *device)
         /* ------------------------------------- */
 
         /* set this first since e.g. ID_FS_LABEL et. al. needs to be redirected/copied */
-        fstype = devkit_info_property_get_string (info, "ID_FS_TYPE");
+        fstype = devkit_device_get_property (d, "ID_FS_TYPE");
         if (fstype != NULL && strcmp (fstype, "linux_raid_member") == 0) {
                 device->priv->info.device_is_linux_md_component = TRUE;
         }
 
-        if (devkit_info_property_foreach (info, update_info_properties_cb, device)) {
+        if (devkit_device_properties_foreach (d, update_info_properties_cb, device)) {
                 goto out;
         }
 
@@ -1928,8 +1917,8 @@ update_info (DevkitDisksDevice *device)
         ret = TRUE;
 
 out:
-        if (info != NULL)
-                devkit_info_unref (info);
+        if (d != NULL)
+                g_object_unref (d);
         return ret;
 }
 
@@ -1981,23 +1970,30 @@ devkit_disks_device_removed (DevkitDisksDevice *device)
 }
 
 DevkitDisksDevice *
-devkit_disks_device_new (DevkitDisksDaemon *daemon, const char *native_path)
+devkit_disks_device_new (DevkitDisksDaemon *daemon, DevkitDevice *d)
 {
         DevkitDisksDevice *device;
-        gboolean res;
+        const char *native_path;
+
+        device = NULL;
+        native_path = devkit_device_get_native_path (d);
+
+        /* ignore ram and loop devices */
+        if (g_str_has_prefix (native_path, "/sys/devices/virtual/block/ram") ||
+            g_str_has_prefix (native_path, "/sys/devices/virtual/block/loop"))
+                goto out;
 
         device = DEVKIT_DISKS_DEVICE (g_object_new (DEVKIT_TYPE_DISKS_DEVICE, NULL));
 
         device->priv->daemon = g_object_ref (daemon);
         device->priv->native_path = g_strdup (native_path);
-        if (!update_info (device)) {
+        if (!update_info (device, d)) {
                 g_object_unref (device);
                 device = NULL;
                 goto out;
         }
 
-        res = register_disks_device (DEVKIT_DISKS_DEVICE (device));
-        if (! res) {
+        if (! register_disks_device (DEVKIT_DISKS_DEVICE (device))) {
                 g_object_unref (device);
                 device = NULL;
                 goto out;
@@ -2055,7 +2051,7 @@ devkit_disks_device_changed (DevkitDisksDevice *device, gboolean synthesized)
 {
         gboolean keep_device;
 
-        keep_device = update_info (device);
+        keep_device = update_info (device, NULL);
 
         /* if we're a linux md device.. then a change event might mean some metadata on the
          * components changed. So trigger a change on each slave
index 67a5165..cfd20a5 100644 (file)
@@ -23,6 +23,7 @@
 
 #include <glib-object.h>
 #include <polkit-dbus/polkit-dbus.h>
+#include <devkit-gobject.h>
 
 #include "devkit-disks-daemon.h"
 
@@ -87,7 +88,7 @@ GType devkit_disks_device_error_get_type (void);
 GQuark             devkit_disks_device_error_quark           (void);
 GType              devkit_disks_device_get_type              (void);
 
-DevkitDisksDevice *devkit_disks_device_new                   (DevkitDisksDaemon *daemon, const char *native_path);
+DevkitDisksDevice *devkit_disks_device_new                   (DevkitDisksDaemon *daemon, DevkitDevice *d);
 gboolean           devkit_disks_device_changed               (DevkitDisksDevice *device, gboolean synthesized);
 void               devkit_disks_device_removed               (DevkitDisksDevice *device);
 
index 4281fa8..73f70fb 100644 (file)
 #define DBUS_API_SUBJECT_TO_CHANGE
 #include <dbus/dbus-glib.h>
 #include <dbus/dbus-glib-lowlevel.h>
+#include <devkit-gobject.h>
 
 #include "devkit-disks-daemon.h"
 
+
 #define NAME_TO_CLAIM "org.freedesktop.DeviceKit.Disks"
 
 static gboolean
@@ -101,6 +103,44 @@ acquire_name_on_proxy (DBusGProxy *bus_proxy)
         return ret;
 }
 
+#if 0
+static gboolean
+print_prop (DevkitDevice *device, const char *key, const char *value, gpointer user_data)
+{
+        g_print ("  %s -> %s\n", key, value);
+        return FALSE;
+}
+
+static void
+print_device (DevkitDevice *device)
+{
+        int n;
+        const char * const *symlinks;
+
+        g_print (" subsys=%s\n"
+                 " native_path=%s\n"
+                 " device=%s\n",
+                 devkit_device_get_subsystem (device),
+                 devkit_device_get_native_path (device),
+                 devkit_device_get_device_file (device));
+
+        symlinks = devkit_device_get_device_file_symlinks (device);
+        for (n = 0; symlinks[n] != NULL; n++)
+                g_print ("  symlink %d: %s\n", n, symlinks[n]);
+        devkit_device_properties_foreach (device, print_prop, NULL);
+}
+
+static void
+device_event_signal_handler (DevkitClient *client,
+                             const char   *action,
+                             DevkitDevice *device,
+                             gpointer      user_data)
+{
+        g_print ("action=%s\n", action);
+        print_device (device);
+}
+#endif
+
 int
 main (int argc, char **argv)
 {
@@ -170,6 +210,20 @@ main (int argc, char **argv)
 
         loop = g_main_loop_new (NULL, FALSE);
 
+#if 0
+        DevkitClient *c;
+        const char *subsystems[] = {"block", "usb_endpoint", NULL};
+        c = devkit_client_new (NULL);//subsystems);
+        g_signal_connect (c, "device-event", G_CALLBACK (device_event_signal_handler), NULL);
+        devkit_client_connect (c, NULL);
+        GList *devs;
+        devs = devkit_client_enumerate_by_subsystem (c, subsystems, NULL);
+        g_list_foreach (devs, (GFunc) print_device, NULL);
+        g_list_foreach (devs, (GFunc) devkit_device_free, NULL);
+        g_list_free (devs);
+        g_object_unref (c);
+#endif
+
         g_main_loop_run (loop);
 
         g_object_unref (disks_daemon);