finish port to new DeviceKit
authorDavid Zeuthen <davidz@redhat.com>
Sun, 27 Apr 2008 18:49:46 +0000 (14:49 -0400)
committerDavid Zeuthen <davidz@redhat.com>
Sun, 27 Apr 2008 18:49:46 +0000 (14:49 -0400)
src/devkit-disks-daemon.c
src/devkit-disks-daemon.h
src/devkit-disks-device-private.h
src/devkit-disks-device.c
src/devkit-disks-device.h

index 8e259b4..28939ff 100644 (file)
@@ -549,41 +549,37 @@ 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);
+static void device_add (DevkitDisksDaemon *daemon, DevkitDevice *d, gboolean emit_event);
+static void device_remove (DevkitDisksDaemon *daemon, DevkitDevice *d);
 
 static void
-device_changed (DevkitDisksDaemon *daemon, const char *native_path, gboolean synthesized)
+device_changed (DevkitDisksDaemon *daemon, DevkitDevice *d, gboolean synthesized)
 {
         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) {
-                if (!devkit_disks_device_changed (device, synthesized)) {
+                if (!devkit_disks_device_changed (device, d, synthesized)) {
                         g_print ("changed triggered remove on %s\n", native_path);
-                        device_remove (daemon, native_path);
+                        device_remove (daemon, d);
                 } else {
                         g_print ("changed %s\n", native_path);
                 }
         } else {
                 g_print ("treating change event as add on %s\n", native_path);
-                device_add (daemon, native_path, TRUE);
+                device_add (daemon, d, 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)
+                                              DevkitDevice            *d)
 {
-        //device_changed (daemon, native_path, TRUE);
+        g_object_ref (d);
+        device_changed (daemon, d, TRUE);
+        g_object_unref (d);
 }
 
 static void
@@ -597,7 +593,7 @@ device_add (DevkitDisksDaemon *daemon, DevkitDevice *d, gboolean emit_event)
         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, d, FALSE);
+                device_changed (daemon, d, FALSE);
         } else {
                 device = devkit_disks_device_new (daemon, d);
 
@@ -624,12 +620,13 @@ device_add (DevkitDisksDaemon *daemon, DevkitDevice *d, gboolean emit_event)
         }
 }
 
-#if 0
 static void
-device_remove (DevkitDisksDaemon *daemon, const char *native_path)
+device_remove (DevkitDisksDaemon *daemon, DevkitDevice *d)
 {
         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) {
                 g_print ("ignoring remove event on %s\n", native_path);
@@ -640,7 +637,25 @@ device_remove (DevkitDisksDaemon *daemon, const char *native_path)
                 g_object_unref (device);
         }
 }
-#endif
+
+static void
+device_event_signal_handler (DevkitClient *client,
+                             const char   *action,
+                             DevkitDevice *device,
+                             gpointer      user_data)
+{
+        DevkitDisksDaemon *daemon = DEVKIT_DISKS_DAEMON (user_data);
+
+        if (strcmp (action, "add") == 0) {
+                device_add (daemon, device, TRUE);
+        } else if (strcmp (action, "remove") == 0) {
+                device_remove (daemon, device);
+        } else if (strcmp (action, "change") == 0) {
+                device_changed (daemon, device, FALSE);
+        } else {
+                g_warning ("unhandled action '%s' on %s", action, devkit_device_get_native_path (device));
+        }
+}
 
 DevkitDisksDevice *
 devkit_disks_daemon_local_find_by_native_path (DevkitDisksDaemon *daemon, const char *native_path)
@@ -701,15 +716,15 @@ mdstat_changed_event (GIOChannel *channel, GIOCondition cond, gpointer user_data
         g_hash_table_iter_init (&iter, daemon->priv->map_native_path_to_device);
         while (g_hash_table_iter_next (&iter, (gpointer *) &native_path, (gpointer *) &device)) {
                 if (device->priv->info.device_is_linux_md) {
-                        g_ptr_array_add (a, g_strdup (native_path));
+                        g_ptr_array_add (a, g_object_ref (device->priv->d));
                 }
         }
 
         for (n = 0; n < (int) a->len; n++) {
-                char *native_path = a->pdata[n];
+                DevkitDevice *d = a->pdata[n];
                 g_warning ("using change on /proc/mdstat to trigger change event on %s", native_path);
-                device_changed (daemon, native_path, FALSE);
-                g_free (native_path);
+                device_changed (daemon, d, FALSE);
+                g_object_unref (d);
         }
 
         g_ptr_array_free (a, TRUE);
@@ -725,6 +740,7 @@ register_disks_daemon (DevkitDisksDaemon *daemon)
         DBusConnection *connection;
         DBusError dbus_error;
         GError *error = NULL;
+        const char *subsystems[] = {"block", NULL};
 
         daemon->priv->pk_context = polkit_context_new ();
         polkit_context_set_io_watch_functions (daemon->priv->pk_context, pk_io_add_watch, pk_io_remove_watch);
@@ -748,7 +764,7 @@ register_disks_daemon (DevkitDisksDaemon *daemon)
         polkit_tracker_set_system_bus_connection (daemon->priv->pk_tracker, connection);
         polkit_tracker_init (daemon->priv->pk_tracker);
 
-        dbus_g_connection_register_g_object (daemon->priv->system_bus_connection, "/", 
+        dbus_g_connection_register_g_object (daemon->priv->system_bus_connection, "/",
                                              G_OBJECT (daemon));
 
         daemon->priv->system_bus_proxy = dbus_g_proxy_new_for_name (daemon->priv->system_bus_connection,
@@ -815,12 +831,14 @@ register_disks_daemon (DevkitDisksDaemon *daemon)
        }
 
         /* connect to the DeviceKit daemon */
-        daemon->priv->devkit_client = devkit_client_new (NULL); // TODO: subsystems
+        daemon->priv->devkit_client = devkit_client_new (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;
         }
+        g_signal_connect (daemon->priv->devkit_client, "device-event",
+                          G_CALLBACK (device_event_signal_handler), daemon);
 
         /* monitor mounts */
         daemon->priv->mount_monitor = g_unix_mount_monitor_new ();
index f6ce787..1b449f6 100644 (file)
@@ -90,7 +90,7 @@ void               devkit_disks_daemon_local_update_mount_state  (DevkitDisksDae
                                                                   gboolean                 emit_changed);
 
 void               devkit_disks_daemon_local_synthesize_changed  (DevkitDisksDaemon       *daemon,
-                                                                  const char              *native_path);
+                                                                  DevkitDevice            *d);
 
 /* exported methods */
 
index b57b10c..9418da4 100644 (file)
@@ -36,6 +36,7 @@ struct DevkitDisksDevicePrivate
         DBusGConnection *system_bus_connection;
         DBusGProxy      *system_bus_proxy;
         DevkitDisksDaemon *daemon;
+        DevkitDevice *d;
 
         Job *job;
 
index 794687d..16ce4d4 100644 (file)
@@ -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, DevkitDevice *d);
+static gboolean update_info                (DevkitDisksDevice *device);
 
 /* Returns the cleartext device. If device==NULL, unlocking failed and an error has
  * been reported back to the caller
@@ -893,6 +893,7 @@ devkit_disks_device_finalize (GObject *object)
         device = DEVKIT_DISKS_DEVICE (object);
         g_return_if_fail (device->priv != NULL);
 
+        g_object_unref (device->priv->d);
         g_object_unref (device->priv->daemon);
         g_free (device->priv->object_path);
 
@@ -1417,7 +1418,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, NULL);
+                        update_info (slave);
                 }
         }
 }
@@ -1562,7 +1563,7 @@ poll_syncing_md_device (gpointer user_data)
         g_warning ("polling md device");
 
         device->priv->linux_md_poll_timeout_id = 0;
-        devkit_disks_daemon_local_synthesize_changed (device->priv->daemon, device->priv->native_path);
+        devkit_disks_daemon_local_synthesize_changed (device->priv->daemon, device->priv->d);
         return FALSE;
 }
 
@@ -1589,7 +1590,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, DevkitDevice *d)
+update_info (DevkitDisksDevice *device)
 {
         guint64 start, size;
         char *s;
@@ -1606,13 +1607,6 @@ update_info (DevkitDisksDevice *device, DevkitDevice *d)
 
         ret = FALSE;
 
-        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
          * need this to set them slaves free)
@@ -1644,14 +1638,14 @@ update_info (DevkitDisksDevice *device, DevkitDevice *d)
                 device->priv->info.device_is_drive = FALSE;
         }
 
-        device->priv->info.device_file = g_strdup (devkit_device_get_device_file (d));
+        device->priv->info.device_file = g_strdup (devkit_device_get_device_file (device->priv->d));
         if (device->priv->info.device_file == NULL) {
                g_warning ("No device file for %s", device->priv->native_path);
                 goto out;
         }
 
         const char * const * symlinks;
-        symlinks = devkit_device_get_device_file_symlinks (d);
+        symlinks = devkit_device_get_device_file_symlinks (device->priv->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/")) {
@@ -1769,12 +1763,12 @@ update_info (DevkitDisksDevice *device, DevkitDevice *d)
         /* ------------------------------------- */
 
         /* set this first since e.g. ID_FS_LABEL et. al. needs to be redirected/copied */
-        fstype = devkit_device_get_property (d, "ID_FS_TYPE");
+        fstype = devkit_device_get_property (device->priv->d, "ID_FS_TYPE");
         if (fstype != NULL && strcmp (fstype, "linux_raid_member") == 0) {
                 device->priv->info.device_is_linux_md_component = TRUE;
         }
 
-        if (devkit_device_properties_foreach (d, update_info_properties_cb, device)) {
+        if (devkit_device_properties_foreach (device->priv->d, update_info_properties_cb, device)) {
                 goto out;
         }
 
@@ -1917,8 +1911,6 @@ update_info (DevkitDisksDevice *device, DevkitDevice *d)
         ret = TRUE;
 
 out:
-        if (d != NULL)
-                g_object_unref (d);
         return ret;
 }
 
@@ -1984,10 +1976,10 @@ devkit_disks_device_new (DevkitDisksDaemon *daemon, DevkitDevice *d)
                 goto out;
 
         device = DEVKIT_DISKS_DEVICE (g_object_new (DEVKIT_TYPE_DISKS_DEVICE, NULL));
-
+        device->priv->d = g_object_ref (d);
         device->priv->daemon = g_object_ref (daemon);
         device->priv->native_path = g_strdup (native_path);
-        if (!update_info (device, d)) {
+        if (!update_info (device)) {
                 g_object_unref (device);
                 device = NULL;
                 goto out;
@@ -2047,11 +2039,14 @@ emit_changed (DevkitDisksDevice *device)
 }
 
 gboolean
-devkit_disks_device_changed (DevkitDisksDevice *device, gboolean synthesized)
+devkit_disks_device_changed (DevkitDisksDevice *device, DevkitDevice *d, gboolean synthesized)
 {
         gboolean keep_device;
 
-        keep_device = update_info (device, NULL);
+        g_object_unref (device->priv->d);
+        device->priv->d = g_object_ref (d);
+
+        keep_device = update_info (device);
 
         /* 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
@@ -2106,78 +2101,6 @@ out:
 
 /*--------------------------------------------------------------------------------------------------------------*/
 
-/**
- * devkit_disks_enumerate_native_paths:
- *
- * Enumerates all block devices on the system.
- *
- * Returns: A #GList of native paths for devices (on Linux the sysfs path)
- */
-GList *
-devkit_disks_enumerate_native_paths (void)
-{
-        GList *ret;
-        GDir *dir;
-        gboolean have_class_block;
-        const char *name;
-
-        ret = 0;
-
-        /* TODO: rip out support for running without /sys/class/block */
-
-        have_class_block = FALSE;
-        if (g_file_test ("/sys/class/block", G_FILE_TEST_EXISTS))
-                have_class_block = TRUE;
-
-        dir = g_dir_open (have_class_block ? "/sys/class/block" : "/sys/block", 0, NULL);
-        if (dir == NULL)
-                goto out;
-
-        while ((name = g_dir_read_name (dir)) != NULL) {
-                char *s;
-                char sysfs_path[PATH_MAX];
-
-                /* skip all ram%d block devices */
-                if (g_str_has_prefix (name, "ram"))
-                        continue;
-
-                s = g_build_filename (have_class_block ? "/sys/class/block" : "/sys/block", name, NULL);
-                if (realpath (s, sysfs_path) == NULL) {
-                        g_free (s);
-                        continue;
-                }
-                g_free (s);
-
-                ret = g_list_prepend (ret, g_strdup (sysfs_path));
-
-                if (!have_class_block) {
-                        GDir *part_dir;
-                        const char *part_name;
-
-                        if((part_dir = g_dir_open (sysfs_path, 0, NULL)) != NULL) {
-                                while ((part_name = g_dir_read_name (part_dir)) != NULL) {
-                                        if (g_str_has_prefix (part_name, name)) {
-                                                char *part_sysfs_path;
-                                                part_sysfs_path = g_build_filename (sysfs_path, part_name, NULL);
-                                                ret = g_list_prepend (ret, part_sysfs_path);
-                                        }
-                                }
-                                g_dir_close (part_dir);
-                        }
-                }
-        }
-        g_dir_close (dir);
-
-        /* TODO: probing order.. might be tricky.. right now we just
-         *       sort the list
-         */
-        ret = g_list_sort (ret, (GCompareFunc) strcmp);
-out:
-        return ret;
-}
-
-/*--------------------------------------------------------------------------------------------------------------*/
-
 const char *
 devkit_disks_device_local_get_object_path (DevkitDisksDevice *device)
 {
index cfd20a5..9f3f950 100644 (file)
@@ -88,11 +88,14 @@ 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, DevkitDevice *d);
-gboolean           devkit_disks_device_changed               (DevkitDisksDevice *device, gboolean synthesized);
-void               devkit_disks_device_removed               (DevkitDisksDevice *device);
+DevkitDisksDevice *devkit_disks_device_new                   (DevkitDisksDaemon *daemon,
+                                                              DevkitDevice      *d);
+
+gboolean           devkit_disks_device_changed               (DevkitDisksDevice *device,
+                                                              DevkitDevice      *d,
+                                                              gboolean           synthesized);
 
-GList *devkit_disks_enumerate_native_paths (void);
+void               devkit_disks_device_removed               (DevkitDisksDevice *device);
 
 /* local methods */