]> err.no Git - systemd/commitdiff
delete old tags when configuration changes
authorKay Sievers <kay.sievers@vrfy.org>
Fri, 23 Apr 2010 07:01:56 +0000 (09:01 +0200)
committerKay Sievers <kay.sievers@vrfy.org>
Fri, 23 Apr 2010 07:01:56 +0000 (09:01 +0200)
libudev/libudev-device-private.c
libudev/libudev-private.h
udev/udev-event.c

index 13f1ebf88304316369d451ecedf00d3ff2f07fc6..7e5fcbc29375a1403c13e2cd893a21b01530afbf 100644 (file)
 #include "libudev.h"
 #include "libudev-private.h"
 
-static int udev_device_tag_index(struct udev_device *udev_device, bool add)
+static void udev_device_tag(struct udev_device *dev, const char *tag, bool add)
 {
-       struct udev *udev = udev_device_get_udev(udev_device);
-       struct udev_list_entry *list_entry;
+       struct udev *udev = udev_device_get_udev(dev);
+       char filename[UTIL_PATH_SIZE];
 
-       udev_list_entry_foreach(list_entry, udev_device_get_tags_list_entry(udev_device)) {
-               char filename[UTIL_PATH_SIZE];
+       util_strscpyl(filename, sizeof(filename), udev_get_dev_path(udev), "/.udev/tags/", tag, "/",
+                     udev_device_get_subsystem(dev), ":", udev_device_get_sysname(dev), NULL);
 
-               util_strscpyl(filename, sizeof(filename), udev_get_dev_path(udev), "/.udev/tags/",
-                             udev_list_entry_get_name(list_entry), "/",
-                             udev_device_get_subsystem(udev_device), ":", udev_device_get_sysname(udev_device), NULL);
+       if (add) {
+               util_create_path(udev, filename);
+               symlink(udev_device_get_devpath(dev), filename);
+               /* possibly cleanup old entries after a device renaming */
+               if (udev_device_get_sysname_old(dev) != NULL) {
+                       char filename_old[UTIL_PATH_SIZE];
 
-               if (add) {
-                       util_create_path(udev, filename);
-                       symlink(udev_device_get_devpath(udev_device), filename);
-                       if (udev_device_get_sysname_old(udev_device) != NULL) {
-                               char filename_old[UTIL_PATH_SIZE];
+                       util_strscpyl(filename_old, sizeof(filename_old), udev_get_dev_path(udev), "/.udev/tags/", tag, "/",
+                                     udev_device_get_subsystem(dev), ":", udev_device_get_sysname_old(dev), NULL);
+                       unlink(filename_old);
+               }
+       } else {
+               unlink(filename);
+       }
+}
 
-                               util_strscpyl(filename, sizeof(filename), udev_get_dev_path(udev), "/.udev/tags/",
-                                             udev_list_entry_get_name(list_entry),
-                                             udev_device_get_subsystem(udev_device), ":", udev_device_get_sysname_old(udev_device), NULL);
-                               unlink(filename_old);
+int udev_device_tag_index(struct udev_device *dev, struct udev_device *dev_old, bool add)
+{
+       struct udev_list_entry *list_entry;
+       bool found;
+
+       if (add) {
+               /* delete possible left-over tags */
+               udev_list_entry_foreach(list_entry, udev_device_get_tags_list_entry(dev_old)) {
+                       const char *tag_old = udev_list_entry_get_name(list_entry);
+                       struct udev_list_entry *list_entry_current;
+
+                       found = false;
+                       udev_list_entry_foreach(list_entry_current, udev_device_get_tags_list_entry(dev)) {
+                               const char *tag = udev_list_entry_get_name(list_entry_current);
+
+                               if (strcmp(tag, tag_old) == 0) {
+                                       found = true;
+                                       break;
+                               }
                        }
-               } else {
-                       unlink(filename);
-                       util_delete_path(udev, filename);
+                       if (!found)
+                               udev_device_tag(dev_old, tag_old, false);
                }
        }
+
+       udev_list_entry_foreach(list_entry, udev_device_get_tags_list_entry(dev))
+               udev_device_tag(dev, udev_list_entry_get_name(list_entry), add);
+
        return 0;
 }
 
@@ -140,7 +164,6 @@ file:
        rename(filename_tmp, filename);
        info(udev, "created db file for '%s' in '%s'\n", udev_device_get_devpath(udev_device), filename);
 out:
-       udev_device_tag_index(udev_device, true);
        return 0;
 }
 
@@ -149,7 +172,6 @@ int udev_device_delete_db(struct udev_device *udev_device)
        struct udev *udev = udev_device_get_udev(udev_device);
        char filename[UTIL_PATH_SIZE];
 
-       udev_device_tag_index(udev_device, false);
        util_strscpyl(filename, sizeof(filename), udev_get_dev_path(udev), "/.udev/db/",
                      udev_device_get_subsystem(udev_device), ":", udev_device_get_sysname(udev_device), NULL);
        unlink(filename);
@@ -169,6 +191,5 @@ int udev_device_rename_db(struct udev_device *udev_device)
                      udev_device_get_subsystem(udev_device), ":", udev_device_get_sysname_old(udev_device), NULL);
        util_strscpyl(filename, sizeof(filename), udev_get_dev_path(udev), "/.udev/db/",
                      udev_device_get_subsystem(udev_device), ":", udev_device_get_sysname(udev_device), NULL);
-       udev_device_tag_index(udev_device, true);
        return rename(filename_old, filename);
 }
index 548f4adbef0c0c3d47d8537f5bfd96512eeda834..3f38a326f1ba6d0b4de4e0092734e28967fef937 100644 (file)
@@ -106,6 +106,7 @@ void udev_device_set_info_loaded(struct udev_device *device);
 int udev_device_update_db(struct udev_device *udev_device);
 int udev_device_delete_db(struct udev_device *udev_device);
 int udev_device_rename_db(struct udev_device *udev_device);
+int udev_device_tag_index(struct udev_device *dev, struct udev_device *dev_old, bool add);
 
 /* libudev-monitor.c - netlink/unix socket communication  */
 int udev_monitor_disconnect(struct udev_monitor *udev_monitor);
index 212ccc795073511c6b0b47111b60fb233642993c..2d8becd86a66fd303dd41109e1a9c3f03f2983ed 100644 (file)
@@ -544,6 +544,7 @@ int udev_event_execute_rules(struct udev_event *event, struct udev_rules *rules)
        if (strcmp(udev_device_get_action(dev), "remove") == 0) {
                udev_device_read_db(dev);
                udev_device_delete_db(dev);
+               udev_device_tag_index(dev, NULL, false);
 
                if (major(udev_device_get_devnum(dev)) != 0)
                        udev_watch_end(event->udev, dev);
@@ -577,6 +578,7 @@ int udev_event_execute_rules(struct udev_event *event, struct udev_rules *rules)
 
                                /* delete stale db file */
                                udev_device_delete_db(dev);
+                               udev_device_tag_index(dev, NULL, false);
 
                                /* remember old name */
                                udev_device_add_property(dev, "INTERFACE_OLD", udev_device_get_sysname(dev));
@@ -618,6 +620,7 @@ int udev_event_execute_rules(struct udev_event *event, struct udev_rules *rules)
                        if (event->name == NULL) {
                                /* things went wrong */
                                udev_device_delete_db(dev);
+                               udev_device_tag_index(dev, NULL, false);
                                udev_device_unref(event->dev_db);
                                err = -ENOMEM;
                                goto out;
@@ -629,6 +632,7 @@ int udev_event_execute_rules(struct udev_event *event, struct udev_rules *rules)
                }
 
                udev_device_update_db(dev);
+               udev_device_tag_index(dev, event->dev_db, true);
 
                if (major(udev_device_get_devnum(dev)) != 0) {
                        /* remove/update possible left-over symlinks from old database entry */