cleanup already existing db-entries and db-index on device update

This commit is contained in:
Kay Sievers 2007-03-16 17:24:39 +01:00
parent 2afb8cb37a
commit 6eee03ef45
2 changed files with 33 additions and 31 deletions

View file

@ -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;
}

View file

@ -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 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 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;
}