- strlcpy(dbpath, udev_root, sizeof(dbpath));
- strlcat(dbpath, "/"DB_DIR, sizeof(dbpath));
- dir = opendir(dbpath);
- if (dir == NULL) {
- info("no udev_db available '%s': %s", dbpath, strerror(errno));
- return -1;
- }
-
- while (!found) {
- struct dirent *ent;
- char filename[PATH_SIZE];
- char nodename[PATH_SIZE];
- struct stat stats;
- char *bufline;
- char *buf;
- size_t bufsize;
- size_t cur;
- size_t count;
-
- ent = readdir(dir);
- if (ent == NULL || ent->d_name[0] == '\0')
- break;
- if (ent->d_name[0] == '.')
- continue;
-
- snprintf(filename, sizeof(filename), "%s/%s", dbpath, ent->d_name);
- filename[sizeof(filename)-1] = '\0';
- dbg("looking at '%s'", filename);
-
- if (lstat(filename, &stats) != 0) {
- info("unable to read %s: %s", filename, strerror(errno));
- continue;
- }
- if ((stats.st_mode & S_IFMT) == S_IFLNK) {
- char target[NAME_SIZE];
- int target_len;
-
- info("found a symlink as db file");
- target_len = readlink(filename, target, sizeof(target));
- if (target_len > 0)
- target[target_len] = '\0';
- else {
- info("error reading db link %s: %s", filename, strerror(errno));
- return -1;
- }
- dbg("db link points to '%s'", target);
- if (strcmp(name, target) == 0) {
- db_file_to_devpath(ent->d_name, devpath, len);
- found =1;
- }
- continue;
- }
-
- if (file_map(filename, &buf, &bufsize) != 0) {
- info("unable to read db file '%s': %s", filename, strerror(errno));
- continue;
- }
-
- cur = 0;
- while (cur < bufsize && !found) {
- count = buf_get_line(buf, bufsize, cur);
- bufline = &buf[cur];
- cur += count+1;
-
- switch(bufline[0]) {
- case 'N':
- case 'S':
- if (count > sizeof(nodename))
- count = sizeof(nodename);
- memcpy(nodename, &bufline[2], count-2);
- nodename[count-2] = '\0';
- dbg("compare '%s' '%s'", nodename, name);
- if (strcmp(nodename, name) == 0) {
- db_file_to_devpath(ent->d_name, devpath, len);
- found = 1;
- }
- break;
- default:
- continue;
- }
- }
- file_unmap(buf, bufsize);
- }
-
- closedir(dir);
- if (found)
- return 0;
- else
- return -1;