]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
libsmartcols: add scols_filter_has_holder()
authorKarel Zak <kzak@redhat.com>
Mon, 10 Mar 2025 14:32:58 +0000 (15:32 +0100)
committerKarel Zak <kzak@redhat.com>
Mon, 10 Mar 2025 14:32:58 +0000 (15:32 +0100)
Signed-off-by: Karel Zak <kzak@redhat.com>
libsmartcols/docs/libsmartcols-sections.txt
libsmartcols/src/filter-param.c
libsmartcols/src/libsmartcols.h.in
libsmartcols/src/libsmartcols.sym
sys-utils/lsns.c

index 94bb9ef0584122022ee5eae796bdc8e224c5bb71..e7fe4b75d5d940b9dd61404f44872ff3089a14f9 100644 (file)
@@ -89,6 +89,7 @@ scols_counter_set_param
 scols_dump_filter
 scols_filter_assign_column
 scols_filter_get_errmsg
+scols_filter_has_holder
 scols_filter_new_counter
 scols_filter_next_counter
 scols_filter_next_holder
index 4a67cbfaff0fce08be10b9d3c9fb55e662f8d735..17757f0d9011c8247d940bb037017f7a19124b4a 100644 (file)
@@ -887,3 +887,30 @@ int scols_filter_next_holder(struct libscols_filter *fltr,
 
        return rc;
 }
+
+/**
+ * scols_filter_has_holder:
+ * @fltr: filter instance
+ * @name: wanted holder
+ *
+ * Returns: 0 or 1
+ *
+ * Since: 2.42
+ */
+int scols_filter_has_holder(struct libscols_filter *fltr, const char *name)
+{
+       struct libscols_iter itr;
+       const char *n = NULL;
+
+       if (!fltr || !name)
+               return 0;
+
+       scols_reset_iter(&itr, SCOLS_ITER_FORWARD);
+
+       while (scols_filter_next_holder(fltr, &itr, &n, 0) == 0) {
+               if (strcmp(n, name) == 0)
+                       return 1;
+       }
+
+       return 0;
+}
index 1acff2f0e9b06d2d02c8c3f8fc4d7e5bfeef859d..abb9cdd6c1c9bfc57cfd1cce0d647b6dd2b6571d 100644 (file)
@@ -452,6 +452,8 @@ extern int scols_line_apply_filter(struct libscols_line *ln,
 
 extern int scols_filter_next_holder(struct libscols_filter *fltr,
                         struct libscols_iter *itr, const char **name, int type);
+extern int scols_filter_has_holder(struct libscols_filter *fltr, const char *name);
+
 extern int scols_filter_assign_column(struct libscols_filter *fltr,
                        struct libscols_iter *itr,
                         const char *name, struct libscols_column *col);
index ce77b0df3c3f042b4463ff6c2e3ca395e2d32b75..6fe8f618790f4b7d4e3fffe879333ed011987e87 100644 (file)
@@ -256,3 +256,8 @@ SMARTCOLS_2.41 {
        scols_column_set_uri;
        scols_column_get_uri;
 } SMARTCOLS_2.40;
+
+SMARTCOLS_2.42 {
+       scols_filter_has_holder;
+} SMARTCOLS_2.41;
+
index a97e7ef3c64a25e6b9b6062180f76056c8c54aa6..b64f704064fdf9ff99282ac8163c633c49c9c460 100644 (file)
@@ -278,24 +278,12 @@ static int column_name_to_id(const char *name, size_t namesz)
        return -1;
 }
 
-static int is_column_in_filter(int id, struct libscols_filter *filter)
+static inline const char *column_id_to_name(int id)
 {
-       int r = 0;
-       const char *name = NULL;
-       struct libscols_iter *itr = scols_new_iter(SCOLS_ITER_FORWARD);
-
-       if (!itr)
-               err(EXIT_FAILURE, _("failed to allocate iterator"));
-
-       while (scols_filter_next_holder(filter, itr, &name, 0) == 0) {
-               if (strcmp(infos[id].name, name) == 0) {
-                       r = 1;
-                       break;
-               }
-       }
+       assert(id >= 0);
+       assert(id < (int) ARRAY_SIZE(infos));
 
-       scols_free_iter(itr);
-       return r;
+       return infos[ id ].name;
 }
 
 static int has_column(int id)
@@ -323,6 +311,7 @@ static inline const struct colinfo *get_column_info(unsigned num)
        return &infos[ get_column_id(num) ];
 }
 
+
 #ifdef USE_NS_GET_API
 /* Get the inode number for the parent namespace of the namespace `fd' specifies.
  * If `pfd' is non-null, the file descriptor opening the parent namespace.*/
@@ -1812,7 +1801,8 @@ int main(int argc, char *argv[])
 
 #ifdef HAVE_LINUX_NET_NAMESPACE_H
        if (has_column(COL_NETNSID)
-           || (ls.filter && is_column_in_filter(COL_NETNSID, ls.filter)))
+           || (ls.filter && scols_filter_has_holder(ls.filter,
+                                                    column_id_to_name(COL_NETNSID))))
                netlink_fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
 #endif
        ls.tab = mnt_new_table_from_file(_PATH_PROC_MOUNTINFO);