From: Masatake YAMATO Date: Fri, 7 May 2021 18:25:30 +0000 (+0900) Subject: lsfd: read /proc/partitions X-Git-Tag: v2.38-rc1~144^2~141 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=353151b005cb481276999bc9661474e2d8b9097f;p=thirdparty%2Futil-linux.git lsfd: read /proc/partitions Signed-off-by: Masatake YAMATO --- diff --git a/misc-utils/lsfd-bdev.c b/misc-utils/lsfd-bdev.c index e40c0c739c..56fa6dbfb6 100644 --- a/misc-utils/lsfd-bdev.c +++ b/misc-utils/lsfd-bdev.c @@ -25,6 +25,14 @@ #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, @@ -60,9 +68,72 @@ struct file *make_bdev(const struct file_class *class, 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, }; diff --git a/misc-utils/lsfd.h b/misc-utils/lsfd.h index c800e38f59..b07bd4b9e4 100644 --- a/misc-utils/lsfd.h +++ b/misc-utils/lsfd.h @@ -158,4 +158,6 @@ void free_name_manager(struct name_manager *nm); const char *get_name(struct name_manager *nm, unsigned long id); unsigned long add_name(struct name_manager *nm, const char *name); +const char *get_partition(dev_t dev); + #endif /* UTIL_LINUX_LSFD_H */