From 6eee03ef45fa3b04cf7ac94fd5bc5f982a33bfb7 Mon Sep 17 00:00:00 2001 From: Kay Sievers Date: Fri, 16 Mar 2007 17:24:39 +0100 Subject: [PATCH] cleanup already existing db-entries and db-index on device update --- udev_db.c | 19 +++++++++++-------- udev_device.c | 47 +++++++++++++++++++++++------------------------ 2 files changed, 34 insertions(+), 32 deletions(-) diff --git a/udev_db.c b/udev_db.c index 728d74a0..40082abf 100644 --- a/udev_db.c +++ b/udev_db.c @@ -64,13 +64,13 @@ static int name_index(const char *devpath, const char *name, int add) strlcat(filename, device, sizeof(filename)); if (add) { - dbg("creating: '%s'", filename); + info("creating index: '%s'", filename); create_path(filename); fd = open(filename, O_WRONLY|O_TRUNC|O_CREAT, 0644); if (fd > 0) close(fd); } else { - dbg("removing: '%s'", filename); + info("removing index: '%s'", filename); unlink(filename); delete_path(filename); } @@ -86,25 +86,23 @@ int udev_db_add_device(struct udevice *udev) devpath_to_db_path(udev->dev->devpath, filename, sizeof(filename)); create_path(filename); - name_index(udev->dev->devpath, udev->name, 1); + unlink(filename); /* - * create only a symlink with the name as the target - * if we don't have any interesting data to remember + * don't waste tmpfs memory pages, if we don't have any data to store + * create fake db-file; store the node-name in a symlink target */ if (list_empty(&udev->symlink_list) && list_empty(&udev->env_list) && !udev->partitions && !udev->ignore_remove) { dbg("nothing interesting to store, create symlink"); - unlink(filename); if (symlink(udev->name, filename) != 0) { err("unable to create db link '%s': %s", filename, strerror(errno)); return -1; } } else { - struct name_entry *name_loop; FILE *f; + struct name_entry *name_loop; - unlink(filename); f = fopen(filename, "w"); if (f == NULL) { err("unable to create db file '%s': %s", filename, strerror(errno)); @@ -115,6 +113,7 @@ int udev_db_add_device(struct udevice *udev) fprintf(f, "N:%s\n", udev->name); list_for_each_entry(name_loop, &udev->symlink_list, node) { fprintf(f, "S:%s\n", name_loop->name); + /* add symlink-name to index */ name_index(udev->dev->devpath, name_loop->name, 1); } fprintf(f, "M:%u:%u\n", major(udev->devt), minor(udev->devt)); @@ -128,6 +127,10 @@ int udev_db_add_device(struct udevice *udev) fprintf(f, "E:%s\n", name_loop->name); fclose(f); } + + /* add name to index */ + name_index(udev->dev->devpath, udev->name, 1); + return 0; } diff --git a/udev_device.c b/udev_device.c index 91fa6554..daf94a57 100644 --- a/udev_device.c +++ b/udev_device.c @@ -162,40 +162,39 @@ int udev_device_event(struct udev_rules *rules, struct udevice *udev) goto exit; } - /* read current database entry, we may want to cleanup symlinks */ + /* read current database entry, we may need to cleanup */ udev_old = udev_device_init(); if (udev_old != NULL) { - if (udev_db_get_device(udev_old, udev->dev->devpath) != 0) { + if (udev_db_get_device(udev_old, udev->dev->devpath) == 0) { + info("device '%s' already in database, cleanup", udev->dev->devpath); + udev_db_delete_device(udev_old); + } else { udev_device_cleanup(udev_old); udev_old = NULL; - } else - info("device '%s' already in database, validate currently present symlinks", - udev->dev->devpath); + } } /* create node and symlinks */ retval = udev_node_add(udev, udev_old); - if (retval == 0) { - /* store record in database */ + if (retval == 0) udev_db_add_device(udev); - /* remove possibly left-over symlinks */ - if (udev_old != NULL) { - struct name_entry *link_loop; - struct name_entry *link_old_loop; - struct name_entry *link_old_tmp_loop; - - /* remove still valid symlinks from old list */ - list_for_each_entry_safe(link_old_loop, link_old_tmp_loop, &udev_old->symlink_list, node) - list_for_each_entry(link_loop, &udev->symlink_list, node) - if (strcmp(link_old_loop->name, link_loop->name) == 0) { - dbg("symlink '%s' still valid, keep it", link_old_loop->name); - list_del(&link_old_loop->node); - free(link_old_loop); - } - udev_node_remove_symlinks(udev_old); - udev_device_cleanup(udev_old); - } + /* remove possibly left-over symlinks */ + if (udev_old != NULL) { + struct name_entry *link_loop; + struct name_entry *link_old_loop; + struct name_entry *link_old_tmp_loop; + + /* remove still valid symlinks from old list */ + list_for_each_entry_safe(link_old_loop, link_old_tmp_loop, &udev_old->symlink_list, node) + list_for_each_entry(link_loop, &udev->symlink_list, node) + if (strcmp(link_old_loop->name, link_loop->name) == 0) { + dbg("symlink '%s' still valid, keep it", link_old_loop->name); + list_del(&link_old_loop->node); + free(link_old_loop); + } + udev_node_remove_symlinks(udev_old); + udev_device_cleanup(udev_old); } goto exit; } -- 2.39.5