chiark / gitweb /
libudev: use sysfs attr ilist interface for attribute walk
[elogind.git] / udev / udevadm-info.c
index 7206f4fcf621ed8f3c32cf35ed78d221b3b44a2e..f60ad2c4506b727a3b4ee474fb91827c674c679c 100644 (file)
 
 static void print_all_attributes(struct udev_device *device, const char *key)
 {
-       struct udev *udev = udev_device_get_udev(device);
-       DIR *dir;
-       struct dirent *dent;
-
-       dir = opendir(udev_device_get_syspath(device));
-       if (dir != NULL) {
-               for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) {
-                       struct stat statbuf;
-                       const char *value;
-                       size_t len;
-
-                       if (dent->d_name[0] == '.')
-                               continue;
-
-                       if (strcmp(dent->d_name, "uevent") == 0)
-                               continue;
-                       if (strcmp(dent->d_name, "dev") == 0)
-                               continue;
-
-                       if (fstatat(dirfd(dir), dent->d_name, &statbuf, AT_SYMLINK_NOFOLLOW) != 0)
-                               continue;
-                       if (S_ISLNK(statbuf.st_mode))
-                               continue;
-
-                       value = udev_device_get_sysattr_value(device, dent->d_name);
-                       if (value == NULL)
-                               continue;
-                       dbg(udev, "attr '%s'='%s'\n", dent->d_name, value);
-
-                       /* skip nonprintable attributes */
-                       len = strlen(value);
-                       while (len > 0 && isprint(value[len-1]))
-                               len--;
-                       if (len > 0) {
-                               dbg(udev, "attribute value of '%s' non-printable, skip\n", dent->d_name);
-                               continue;
-                       }
-
-                       printf("    %s{%s}==\"%s\"\n", key, dent->d_name, value);
+       struct udev_list_entry *sysattr;
+
+       udev_list_entry_foreach(sysattr, udev_device_get_sysattr_list_entry(device)) {
+               const char *name;
+               const char *value;
+               size_t len;
+
+               /* skip symlinks */
+               if (0 == strcmp("s", udev_list_entry_get_value(sysattr)))
+                       continue;
+
+               name = udev_list_entry_get_name(sysattr);
+               if (strcmp(name, "uevent") == 0)
+                       continue;
+               if (strcmp(name, "dev") == 0)
+                       continue;
+
+               value = udev_device_get_sysattr_value(device, name);
+               if (value == NULL)
+                       continue;
+               dbg(udev_device_get_udev(device), "attr '%s'='%s'\n", name, value);
+
+               /* skip nonprintable attributes */
+               len = strlen(value);
+               while (len > 0 && isprint(value[len-1]))
+                       len--;
+               if (len > 0) {
+                       dbg(udev_device_get_udev(device),
+                                       "attribute value of '%s' non-printable, skip\n", name);
+                       continue;
                }
-               closedir(dir);
+
+               printf("    %s{%s}==\"%s\"\n", key, name, value);
        }
        printf("\n");
 }
@@ -210,38 +202,44 @@ static int convert_db(struct udev *udev)
                device = udev_device_new_from_syspath(udev, udev_list_entry_get_name(list_entry));
                if (device != NULL) {
                        const char *id;
-                       struct stat statbuf;
+                       struct stat stats;
                        char to[UTIL_PATH_SIZE];
                        char devpath[UTIL_PATH_SIZE];
                        char from[UTIL_PATH_SIZE];
 
                        id = udev_device_get_id_filename(device);
-                       if (id == NULL)
-                               goto next;
+                       if (id == NULL) {
+                               udev_device_unref(device);
+                               continue;
+                       }
                        util_strscpyl(to, sizeof(to), udev_get_dev_path(udev), "/.udev/db/", id, NULL);
 
-                       /* do not overwrite a new database file */
-                       if (lstat(to, &statbuf) == 0)
-                               goto next;
-
                        /* find old database with $subsys:$sysname */
                        util_strscpyl(from, sizeof(from), udev_get_dev_path(udev),
                                     "/.udev/db/", udev_device_get_subsystem(device), ":",
                                     udev_device_get_sysname(device), NULL);
-                       if (lstat(from, &statbuf) == 0) {
-                               rename(from, to);
-                               goto next;
+                       if (lstat(from, &stats) == 0) {
+                               if (lstat(to, &stats) == 0)
+                                       unlink(from);
+                               else
+                                       rename(from, to);
                        }
 
                        /* find old database with the encoded devpath */
                        util_path_encode(udev_device_get_devpath(device), devpath, sizeof(devpath));
                        util_strscpyl(from, sizeof(from), udev_get_dev_path(udev),
                                      "/.udev/db/", devpath, NULL);
-                       if (lstat(from, &statbuf) == 0) {
-                               rename(from, to);
-                               goto next;
+                       if (lstat(from, &stats) == 0) {
+                               if (lstat(to, &stats) == 0)
+                                       unlink(from);
+                               else
+                                       rename(from, to);
                        }
-next:
+
+                       /* read the old database, and write out a new one */
+                       udev_device_read_db(device);
+                       udev_device_update_db(device);
+
                        udev_device_unref(device);
                }
        }