chiark / gitweb /
udevadm: info - make attribute array static and const
[elogind.git] / udev / udevadm-info.c
index 7206f4fcf621ed8f3c32cf35ed78d221b3b44a2e..187e74d50eb795257e1424fb059cfcde555e1c72 100644 (file)
 
 #include "udev.h"
 
-static void print_all_attributes(struct udev_device *device, const char *key)
+static bool skip_attribute(const char *name)
 {
-       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;
+       static const char const *skip[] = {
+               "uevent",
+               "dev",
+               "modalias",
+               "resource",
+               "driver",
+               "subsystem",
+               "module",
+       };
+       unsigned int i;
 
-                       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;
-                       }
+       for (i = 0; i < ARRAY_SIZE(skip); i++)
+               if (strcmp(name, skip[i]) == 0)
+                       return true;
+       return false;
+}
 
-                       printf("    %s{%s}==\"%s\"\n", key, dent->d_name, value);
+static void print_all_attributes(struct udev_device *device, const char *key)
+{
+       struct udev_list_entry *sysattr;
+
+       udev_list_entry_foreach(sysattr, udev_device_get_sysattr_list_entry(device)) {
+               struct udev *udev = udev_device_get_udev(device);
+               const char *name;
+               const char *value;
+               size_t len;
+
+               name = udev_list_entry_get_name(sysattr);
+               if (skip_attribute(name))
+                       continue;
+
+               value = udev_device_get_sysattr_value(device, name);
+               if (value == NULL)
+                       continue;
+               dbg(udev, "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, "attribute value of '%s' non-printable, skip\n", name);
+                       continue;
                }
-               closedir(dir);
+
+               printf("    %s{%s}==\"%s\"\n", key, name, value);
        }
        printf("\n");
 }
@@ -210,38 +215,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);
                }
        }