chiark / gitweb /
libudev: device - add get_parent_with_subsystem()
[elogind.git] / udev / lib / libudev-device.c
index f5465e9892fc5e9c2f8ef654656c43b595dd26d3..8e4360a7f4c50a5d96f3d83646ce1251a75a9c6d 100644 (file)
@@ -193,7 +193,7 @@ void device_set_info_loaded(struct udev_device *device)
        device->info_loaded = 1;
 }
 
-struct udev_device *device_init(struct udev *udev)
+struct udev_device *device_new(struct udev *udev)
 {
        struct udev_device *udev_device;
 
@@ -231,8 +231,8 @@ struct udev_device *udev_device_new_from_syspath(struct udev *udev, const char *
 {
        size_t len;
        const char *subdir;
-       const char *pos;
        char path[UTIL_PATH_SIZE];
+       char *pos;
        struct stat statbuf;
        struct udev_device *udev_device;
 
@@ -260,6 +260,23 @@ struct udev_device *udev_device_new_from_syspath(struct udev *udev, const char *
        util_strlcpy(path, syspath, sizeof(path));
        util_resolve_sys_link(udev, path, sizeof(path));
 
+       /* try to resolve the silly block layout if needed */
+       if (strncmp(&path[len], "/block/", 7) == 0) {
+               char block[UTIL_PATH_SIZE];
+               char part[UTIL_PATH_SIZE];
+
+               util_strlcpy(block, path, sizeof(block));
+               pos = strrchr(block, '/');
+               if (pos == NULL)
+                       return NULL;
+               util_strlcpy(part, pos, sizeof(part));
+               pos[0] = '\0';
+               if (util_resolve_sys_link(udev, block, sizeof(block)) == 0) {
+                       util_strlcpy(path, block, sizeof(path));
+                       util_strlcat(path, part, sizeof(path));
+               }
+       }
+
        /* path exists in sys */
        if (strncmp(&syspath[len], "/devices/", 9) == 0 ||
            strncmp(&syspath[len], "/class/", 7) == 0 ||
@@ -281,7 +298,7 @@ struct udev_device *udev_device_new_from_syspath(struct udev *udev, const char *
                }
        }
 
-       udev_device = device_init(udev);
+       udev_device = device_new(udev);
        if (udev_device == NULL)
                return NULL;
 
@@ -387,6 +404,22 @@ struct udev_device *udev_device_get_parent(struct udev_device *udev_device)
        return udev_device->parent_device;
 }
 
+struct udev_device *udev_device_get_parent_with_subsystem(struct udev_device *udev_device, const char *subsystem)
+{
+       struct udev_device *parent;
+
+       parent = udev_device_get_parent(udev_device);
+       while (parent != NULL) {
+               const char *parent_subsystem;
+
+               parent_subsystem = udev_device_get_subsystem(parent);
+               if (parent_subsystem != NULL && strcmp(parent_subsystem, subsystem) == 0)
+                       break;
+               parent = udev_device_get_parent(parent);
+       }
+       return parent;
+}
+
 /**
  * udev_device_get_udev:
  * @udev_device: udev device