]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
lslocks: (refactor) add a helper function returning JSON type for a given column
authorMasatake YAMATO <yamato@redhat.com>
Thu, 30 Nov 2023 02:41:11 +0000 (11:41 +0900)
committerMasatake YAMATO <yamato@redhat.com>
Thu, 30 Nov 2023 02:58:18 +0000 (11:58 +0900)
Signed-off-by: Masatake YAMATO <yamato@redhat.com>
misc-utils/lslocks.c

index f4f6618bf9a297dd81266613d0d0ea4313c59328..6eb7f245a1921b0a4ca71d986f9e2201a0af4886 100644 (file)
@@ -721,6 +721,28 @@ static void rem_tnode(void *node)
        free(node);
 }
 
+static int get_json_type_for_column(int column_id, int representing_in_bytes)
+{
+       switch (column_id) {
+       case COL_SIZE:
+               if (!representing_in_bytes)
+                       return SCOLS_JSON_STRING;
+               /* fallthrough */
+       case COL_PID:
+       case COL_START:
+       case COL_END:
+       case COL_BLOCKER:
+       case COL_INODE:
+               return SCOLS_JSON_NUMBER;
+       case COL_M:
+               return SCOLS_JSON_BOOLEAN;
+       case COL_HOLDERS:
+               return SCOLS_JSON_ARRAY_STRING;
+       default:
+               return SCOLS_JSON_STRING;
+       }
+}
+
 static int show_locks(struct list_head *locks, pid_t target_pid, void *pid_locks)
 {
        int rc = 0;
@@ -757,31 +779,8 @@ static int show_locks(struct list_head *locks, pid_t target_pid, void *pid_locks
 
                if (json) {
                        int id = get_column_id(i);
-
-                       switch (id) {
-                       case COL_SIZE:
-                               if (!bytes) {
-                                       scols_column_set_json_type(cl, SCOLS_JSON_STRING);
-                                       break;
-                               }
-                               /* fallthrough */
-                       case COL_PID:
-                       case COL_START:
-                       case COL_END:
-                       case COL_BLOCKER:
-                       case COL_INODE:
-                               scols_column_set_json_type(cl, SCOLS_JSON_NUMBER);
-                               break;
-                       case COL_M:
-                               scols_column_set_json_type(cl, SCOLS_JSON_BOOLEAN);
-                               break;
-                       case COL_HOLDERS:
-                               scols_column_set_json_type(cl, SCOLS_JSON_ARRAY_STRING);
-                               break;
-                       default:
-                               scols_column_set_json_type(cl, SCOLS_JSON_STRING);
-                               break;
-                       }
+                       int json_type = get_json_type_for_column(id, bytes);
+                       scols_column_set_json_type(cl, json_type);
                }
 
        }