]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
lslocks: factor out code getting and setting the data from add_scols_line
authorMasatake YAMATO <yamato@redhat.com>
Wed, 10 Dec 2025 13:36:05 +0000 (22:36 +0900)
committerMasatake YAMATO <yamato@redhat.com>
Mon, 29 Dec 2025 07:36:45 +0000 (16:36 +0900)
Signed-off-by: Masatake YAMATO <yamato@redhat.com>
misc-utils/lslocks.c

index 2a7261f0c585cc49003da6af8ee7d098532311bd..fbd42403a81feb46adbdaf9b91a7cc794d71bc3d 100644 (file)
@@ -608,10 +608,10 @@ static void xstrcoholder(char **str, struct lock *l)
                    l->pid, l->cmdname, l->fd);
 }
 
-static void add_scols_line(struct lslocks *lslocks,
-                          struct libscols_table *table, struct lock *l)
+static char *get_data(struct lslocks *lslocks, struct lock *l, int num)
 {
-       struct libscols_line *line;
+       char *str = NULL;
+
        /*
         * Whenever cmdname or filename is NULL it is most
         * likely  because there's no read permissions
@@ -619,93 +619,106 @@ static void add_scols_line(struct lslocks *lslocks,
         */
        const char *notfnd = "";
 
-       assert(l);
-       assert(table);
+       switch (get_column_id(num)) {
+       case COL_SRC:
+               xasprintf(&str, "%s", l->cmdname ? l->cmdname : notfnd);
+               break;
+       case COL_PID:
+               xasprintf(&str, "%d", l->pid);
+               break;
+       case COL_TYPE:
+               xasprintf(&str, "%s", l->type);
+               break;
+       case COL_INODE:
+               xasprintf(&str, "%ju", (uintmax_t) l->inode);
+               break;
+       case COL_MAJMIN:
+               if (lslocks->json || lslocks->raw)
+                       xasprintf(&str, "%u:%u", major(l->dev), minor(l->dev));
+               else
+                       xasprintf(&str, "%3u:%-3u", major(l->dev), minor(l->dev));
+               break;
+       case COL_SIZE:
+               if (!l->size)
+                       break;
+               if (lslocks->bytes)
+                       xasprintf(&str, "%ju", l->size);
+               else
+                       str = size_to_human_string(SIZE_SUFFIX_1LETTER, l->size);
+               break;
+       case COL_MODE:
+               xasprintf(&str, "%s%s", l->mode, l->blocked ? "*" : "");
+               break;
+       case COL_M:
+               xasprintf(&str, "%d", l->mandatory ? 1 : 0);
+               break;
+       case COL_START:
+               xasprintf(&str, "%jd", l->start);
+               break;
+       case COL_END:
+               xasprintf(&str, "%jd", l->end);
+               break;
+       case COL_PATH:
+               xasprintf(&str, "%s", l->path ? l->path : notfnd);
+               break;
+       case COL_BLOCKER:
+       {
+               pid_t bl = l->blocked && l->id ?
+                       get_blocker(l->id, &lslocks->proc_locks) : 0;
+               if (bl)
+                       xasprintf(&str, "%d", (int) bl);
+               break;
+       }
+       case COL_HOLDERS:
+       {
+               struct lock_tnode tmp = { .dev = l->dev, .inode = l->inode, };
+               struct lock_tnode **head = tfind(&tmp, &lslocks->pid_locks, lock_tnode_compare);
+               struct list_head *p;
 
-       line = scols_table_new_line(table, NULL);
-       if (!line)
-               err(EXIT_FAILURE, _("failed to allocate output line"));
+               if (!head)
+                       break;
 
-       for (size_t i = 0; i < ncolumns; i++) {
-               char *str = NULL;
+               list_for_each(p, &(*head)->chain) {
+                       struct lock *m = list_entry(p, struct lock, locks);
 
-               switch (get_column_id(i)) {
-               case COL_SRC:
-                       xasprintf(&str, "%s", l->cmdname ? l->cmdname : notfnd);
-                       break;
-               case COL_PID:
-                       xasprintf(&str, "%d", l->pid);
-                       break;
-               case COL_TYPE:
-                       xasprintf(&str, "%s", l->type);
-                       break;
-               case COL_INODE:
-                       xasprintf(&str, "%ju", (uintmax_t) l->inode);
-                       break;
-               case COL_MAJMIN:
-                       if (lslocks->json || lslocks->raw)
-                               xasprintf(&str, "%u:%u", major(l->dev), minor(l->dev));
-                       else
-                               xasprintf(&str, "%3u:%-3u", major(l->dev), minor(l->dev));
-                       break;
-               case COL_SIZE:
-                       if (!l->size)
-                               break;
-                       if (lslocks->bytes)
-                               xasprintf(&str, "%ju", l->size);
-                       else
-                               str = size_to_human_string(SIZE_SUFFIX_1LETTER, l->size);
-                       break;
-               case COL_MODE:
-                       xasprintf(&str, "%s%s", l->mode, l->blocked ? "*" : "");
-                       break;
-               case COL_M:
-                       xasprintf(&str, "%d", l->mandatory ? 1 : 0);
-                       break;
-               case COL_START:
-                       xasprintf(&str, "%jd", l->start);
-                       break;
-               case COL_END:
-                       xasprintf(&str, "%jd", l->end);
-                       break;
-               case COL_PATH:
-                       xasprintf(&str, "%s", l->path ? l->path : notfnd);
-                       break;
-               case COL_BLOCKER:
-               {
-                       pid_t bl = l->blocked && l->id ?
-                                               get_blocker(l->id, &lslocks->proc_locks) : 0;
-                       if (bl)
-                               xasprintf(&str, "%d", (int) bl);
-                       break;
+                       if (!is_holder(l, m))
+                               continue;
+
+                       if (str)
+                               xstrputc(&str, '\n');
+                       xstrcoholder(&str, m);
                }
-               case COL_HOLDERS:
-               {
-                       struct lock_tnode tmp = { .dev = l->dev, .inode = l->inode, };
-                       struct lock_tnode **head = tfind(&tmp, &lslocks->pid_locks, lock_tnode_compare);
-                       struct list_head *p;
+               break;
+       }
+       default:
+               break;
+       }
+
+       return str;
+}
 
-                       if (!head)
-                               break;
+static void set_line_data(struct libscols_line *line, size_t i, char *data)
+{
+       if (data && scols_line_refer_data(line, i, data))
+               err(EXIT_FAILURE, _("failed to add output data"));
+}
 
-                       list_for_each(p, &(*head)->chain) {
-                               struct lock *m = list_entry(p, struct lock, locks);
+static void add_scols_line(struct lslocks *lslocks,
+                          struct libscols_table *table, struct lock *l)
+{
+       struct libscols_line *line;
 
-                               if (!is_holder(l, m))
-                                       continue;
+       assert(l);
+       assert(table);
 
-                               if (str)
-                                       xstrputc(&str, '\n');
-                               xstrcoholder(&str, m);
-                       }
-                       break;
-               }
-               default:
-                       break;
-               }
+       line = scols_table_new_line(table, NULL);
+       if (!line)
+               err(EXIT_FAILURE, _("failed to allocate output line"));
 
-               if (str && scols_line_refer_data(line, i, str))
-                       err(EXIT_FAILURE, _("failed to add output data"));
+       for (size_t i = 0; i < ncolumns; i++) {
+               char *str = get_data(lslocks, l, i);
+               if (str)
+                       set_line_data(line, i, str);
        }
 }