1
0
mirror of https://github.com/systemd/systemd.git synced 2025-01-26 14:04:03 +03:00

delete old tags when configuration changes

This commit is contained in:
Kay Sievers 2010-04-23 09:01:56 +02:00
parent 1dbfbfbea6
commit c1dbe11dc3
3 changed files with 49 additions and 23 deletions

View File

@ -22,34 +22,58 @@
#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;
udev_list_entry_foreach(list_entry, udev_device_get_tags_list_entry(udev_device)) {
struct udev *udev = udev_device_get_udev(dev);
char filename[UTIL_PATH_SIZE];
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);
util_strscpyl(filename, sizeof(filename), udev_get_dev_path(udev), "/.udev/tags/", tag, "/",
udev_device_get_subsystem(dev), ":", udev_device_get_sysname(dev), NULL);
if (add) {
util_create_path(udev, filename);
symlink(udev_device_get_devpath(udev_device), filename);
if (udev_device_get_sysname_old(udev_device) != NULL) {
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];
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);
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_delete_path(udev, filename);
}
}
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;
}
}
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);
}

View 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);

View 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 */