]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
lsdf: make the code for filling SOURCE, PARTITION, and MAJMIN reusable
authorMasatake YAMATO <yamato@redhat.com>
Fri, 9 Jun 2023 05:52:18 +0000 (14:52 +0900)
committerMasatake YAMATO <yamato@redhat.com>
Tue, 13 Jun 2023 10:18:26 +0000 (19:18 +0900)
Signed-off-by: Masatake YAMATO <yamato@redhat.com>
misc-utils/lsfd-file.c
misc-utils/lsfd.h

index 345d97103b254418d9c9ebc022554f555d9e19ac..3826532ba06f4d84a853ced147f86c13b3284f0d 100644 (file)
@@ -112,6 +112,40 @@ static uint64_t get_map_length(struct file *file)
        return res;
 }
 
+void decode_source(char *buf, size_t bufsize,
+                 unsigned int dev_major, unsigned int dev_minor,
+                 enum decode_source_level level)
+{
+       if (bufsize == 0)
+               return;
+
+       buf[0] = '\0';
+
+       if (level & DECODE_SOURCE_FILESYS_BIT) {
+               if (dev_major == 0) {
+                       const char *filesystem = get_nodev_filesystem(dev_minor);
+                       if (filesystem) {
+                               xstrncpy(buf, filesystem, bufsize);
+                               return;
+                       }
+               }
+       }
+
+       if (level & DECODE_SOURCE_PARTITION_BIT) {
+               dev_t dev = makedev(dev_major, dev_minor);
+               const char *partition = get_partition(dev);
+               if (partition) {
+                       xstrncpy(buf, partition, bufsize);
+                       return;
+               }
+       }
+
+       if (level & DECODE_SOURCE_MAJMIN_BIT)
+               snprintf(buf, bufsize, "%u:%u",
+                        dev_major,
+                        dev_minor);
+}
+
 static bool file_fill_column(struct proc *proc,
                             struct file *file,
                             struct libscols_line *ln,
@@ -120,7 +154,7 @@ static bool file_fill_column(struct proc *proc,
 {
        char *str = NULL;
        mode_t ftype;
-       const char *partition;
+       char buf[BUFSIZ];
 
        switch(column_id) {
        case COL_COMMAND:
@@ -177,26 +211,20 @@ static bool file_fill_column(struct proc *proc,
                xasprintf(&str, "%llu", (unsigned long long)file->stat.st_ino);
                break;
        case COL_SOURCE:
-               if (major(file->stat.st_dev) == 0) {
-                       const char *filesystem = get_nodev_filesystem(minor(file->stat.st_dev));
-                       if (filesystem) {
-                               xasprintf(&str, "%s", filesystem);
-                               break;
-                       }
-               }
-               /* FALL THROUGH */
+               decode_source(buf, sizeof(buf), major(file->stat.st_dev), minor(file->stat.st_dev),
+                             DECODE_SOURCE_FILESYS);
+               str = xstrdup(buf);
+               break;
        case COL_PARTITION:
-               partition = get_partition(file->stat.st_dev);
-               if (partition) {
-                       str = xstrdup(partition);
-                       break;
-               }
-               /* FALL THROUGH */
+               decode_source(buf, sizeof(buf), major(file->stat.st_dev), minor(file->stat.st_dev),
+                             DECODE_SOURCE_PARTITION);
+               str = xstrdup(buf);
+               break;
        case COL_DEV:
        case COL_MAJMIN:
-               xasprintf(&str, "%u:%u",
-                         major(file->stat.st_dev),
-                         minor(file->stat.st_dev));
+               decode_source(buf, sizeof(buf), major(file->stat.st_dev), minor(file->stat.st_dev),
+                             DECODE_SOURCE_MAJMIN);
+               str = xstrdup(buf);
                break;
        case COL_RDEV:
                xasprintf(&str, "%u:%u",
index aeda9ef85774dd5c31d69d97c04fab66b7d50f0a..f300c0cc223b9d32d2d2b3c3d281dd2dd4a98182 100644 (file)
@@ -221,6 +221,21 @@ void init_endpoint(struct ipc_endpoint *endpoint);
 void add_endpoint(struct ipc_endpoint *endpoint, struct ipc *ipc);
 #define foreach_endpoint(E,ENDPOINT) list_for_each_backwardly(E, &((ENDPOINT).ipc->endpoints))
 
+enum decode_source_bit {
+       DECODE_SOURCE_MAJMIN_BIT    = 1 << 0,
+       DECODE_SOURCE_PARTITION_BIT = 1 << 1,
+       DECODE_SOURCE_FILESYS_BIT   = 1 << 2,
+};
+
+enum decode_source_level {
+       DECODE_SOURCE_MAJMIN    = DECODE_SOURCE_MAJMIN_BIT,
+       DECODE_SOURCE_PARTITION = DECODE_SOURCE_PARTITION_BIT | DECODE_SOURCE_MAJMIN,
+       DECODE_SOURCE_FILESYS   = DECODE_SOURCE_FILESYS_BIT   | DECODE_SOURCE_PARTITION,
+       DECODE_SOURCE_FULL      = DECODE_SOURCE_FILESYS,
+};
+
+void decode_source(char *buf, size_t bufsize, unsigned int dev_major, unsigned int dev_minor,
+                  enum decode_source_level level);
 /*
  * Name managing
  */