+static size_t devpath_to_db_path(struct udev *udev, const char *devpath, char *filename, size_t len)
+{
+ size_t start;
+
+ /* translate to location of db file */
+ util_strlcpy(filename, udev_get_dev_path(udev), len);
+ start = util_strlcat(filename, "/.udev/db/", len);
+ util_strlcat(filename, devpath, len);
+ return util_path_encode(&filename[start], len - start);
+}
+
+int udev_device_read_db(struct udev_device *udev_device)
+{
+ struct stat stats;
+ char filename[UTIL_PATH_SIZE];
+ char line[UTIL_LINE_SIZE];
+ FILE *f;
+
+ devpath_to_db_path(udev_device->udev, udev_device->devpath, filename, sizeof(filename));
+
+ if (lstat(filename, &stats) != 0) {
+ dbg(udev_device->udev, "no db file to read %s: %m\n", filename);
+ return -1;
+ }
+ if ((stats.st_mode & S_IFMT) == S_IFLNK) {
+ char target[UTIL_PATH_SIZE];
+ char devnode[UTIL_PATH_SIZE];
+ int target_len;
+ char *next;
+
+ target_len = readlink(filename, target, sizeof(target));
+ if (target_len > 0)
+ target[target_len] = '\0';
+ else {
+ dbg(udev_device->udev, "error reading db link %s: %m\n", filename);
+ return -1;
+ }
+
+ next = strchr(target, ' ');
+ if (next != NULL) {
+ next[0] = '\0';
+ next = &next[1];
+ }
+ util_strlcpy(devnode, udev_get_dev_path(udev_device->udev), sizeof(devnode));
+ util_strlcat(devnode, "/", sizeof(devnode));
+ util_strlcat(devnode, target, sizeof(devnode));
+ udev_device_set_devnode(udev_device, devnode);
+ while (next != NULL) {
+ char devlink[UTIL_PATH_SIZE];
+ const char *lnk;
+
+ lnk = next;
+ next = strchr(next, ' ');
+ if (next != NULL) {
+ next[0] = '\0';
+ next = &next[1];
+ }
+ util_strlcpy(devlink, udev_get_dev_path(udev_device->udev), sizeof(devlink));
+ util_strlcat(devlink, "/", sizeof(devlink));
+ util_strlcat(devlink, lnk, sizeof(devlink));
+ udev_device_add_devlink(udev_device, devlink);
+ }
+ info(udev_device->udev, "device %p filled with db symlink data '%s'\n", udev_device, udev_device->devnode);
+ return 0;
+ }
+
+ f = fopen(filename, "r");
+ if (f == NULL) {
+ dbg(udev_device->udev, "error reading db file %s: %m\n", filename);
+ return -1;
+ }
+ while (fgets(line, sizeof(line), f)) {
+ ssize_t len;
+ const char *val;
+
+ len = strlen(line);
+ if (len < 4)
+ break;
+ line[len-1] = '\0';
+ val = &line[2];
+ switch(line[0]) {
+ case 'N':
+ util_strlcpy(filename, udev_get_dev_path(udev_device->udev), sizeof(filename));
+ util_strlcat(filename, "/", sizeof(filename));
+ util_strlcat(filename, val, sizeof(filename));
+ udev_device_set_devnode(udev_device, filename);
+ break;
+ case 'S':
+ util_strlcpy(filename, udev_get_dev_path(udev_device->udev), sizeof(filename));
+ util_strlcat(filename, "/", sizeof(filename));
+ util_strlcat(filename, val, sizeof(filename));
+ udev_device_add_devlink(udev_device, filename);
+ break;
+ case 'L':
+ udev_device_set_devlink_priority(udev_device, atoi(val));
+ break;
+ case 'T':
+ udev_device_set_event_timeout(udev_device, atoi(val));
+ break;
+ case 'A':
+ udev_device_set_num_fake_partitions(udev_device, atoi(val));
+ break;
+ case 'R':
+ udev_device_set_ignore_remove(udev_device, atoi(val));
+ break;
+ case 'E':
+ udev_device_add_property_from_string(udev_device, val);
+ break;
+ }
+ }
+ fclose(f);
+
+ info(udev_device->udev, "device %p filled with db file data\n", udev_device);
+ return 0;
+}
+
+int udev_device_read_uevent_file(struct udev_device *udev_device)
+{
+ char filename[UTIL_PATH_SIZE];
+ FILE *f;
+ char line[UTIL_LINE_SIZE];
+ int maj = 0;
+ int min = 0;
+
+ util_strlcpy(filename, udev_device->syspath, sizeof(filename));
+ util_strlcat(filename, "/uevent", sizeof(filename));
+ f = fopen(filename, "r");
+ if (f == NULL)
+ return -1;
+
+ while (fgets(line, sizeof(line), f)) {
+ char *pos;
+
+ pos = strchr(line, '\n');
+ if (pos == NULL)
+ continue;
+ pos[0] = '\0';
+
+ if (strncmp(line, "MAJOR=", 6) == 0)
+ maj = strtoull(&line[6], NULL, 10);
+ else if (strncmp(line, "MINOR=", 6) == 0)
+ min = strtoull(&line[6], NULL, 10);
+
+ udev_device_add_property_from_string(udev_device, line);
+ }
+
+ udev_device->devnum = makedev(maj, min);
+
+ fclose(f);
+ return 0;
+}
+
+static void device_load_info(struct udev_device *device)
+{
+ device->info_loaded = 1;
+ udev_device_read_uevent_file(device);
+ udev_device_read_db(device);
+}
+
+void udev_device_set_info_loaded(struct udev_device *device)
+{
+ device->info_loaded = 1;
+}
+
+struct udev_device *device_new(struct udev *udev)