#include "lsfd.h"
+static struct list_head partitions;
+
+struct partition {
+ struct list_head partitions;
+ dev_t dev;
+ char *name;
+};
+
static bool bdev_fill_column(struct proc *proc __attribute__((__unused__)),
struct file *file __attribute__((__unused__)),
struct libscols_line *ln,
sb, name, fd);
}
+static struct partition *make_partition(dev_t dev, const char *name)
+{
+ struct partition *partition = xcalloc(1, sizeof(*partition));
+
+ INIT_LIST_HEAD(&partition->partitions);
+
+ partition->dev = dev;
+ partition->name = xstrdup(name);
+
+ return partition;
+}
+
+static void free_partition(struct partition *partition)
+{
+ free(partition->name);
+ free(partition);
+}
+
+static void read_partitions(struct list_head *partitions_list, FILE *part_fp)
+{
+ unsigned int major, minor;
+ char line[256];
+ char name[sizeof(line)];
+
+ while (fgets(line, sizeof(line), part_fp)) {
+ struct partition *partition;
+
+ if (sscanf(line, "%u %u %*u %s", &major, &minor, name) != 3)
+ continue;
+ partition = make_partition(makedev(major, minor), name);
+ list_add_tail(&partition->partitions, partitions_list);
+ }
+}
+
+static void bdev_class_initialize(void)
+{
+ INIT_LIST_HEAD(&partitions);
+
+ FILE *part_fp = fopen("/proc/partitions", "r");
+ if (part_fp) {
+ read_partitions(&partitions, part_fp);
+ fclose(part_fp);
+ }
+}
+
+static void bdev_class_finalize(void)
+{
+ list_free(&partitions, struct partition, partitions, free_partition);
+}
+
+const char *get_partition(dev_t dev)
+{
+ struct list_head *p;
+ list_for_each(p, &partitions) {
+ struct partition *partition = list_entry(p, struct partition, partitions);
+ if (partition->dev == dev)
+ return partition->name;
+ }
+ return NULL;
+}
+
const struct file_class bdev_class = {
.super = &file_class,
.size = sizeof(struct file),
+ .initialize_class = bdev_class_initialize,
+ .finalize_class = bdev_class_finalize,
.fill_column = bdev_fill_column,
.free_content = NULL,
};