From: Karel Zak Date: Mon, 10 Mar 2025 14:32:58 +0000 (+0100) Subject: libsmartcols: add scols_filter_has_holder() X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=8097dd31c24cb7f10d404ec5f756f9f5a7c248a1;p=thirdparty%2Futil-linux.git libsmartcols: add scols_filter_has_holder() Signed-off-by: Karel Zak --- diff --git a/libsmartcols/docs/libsmartcols-sections.txt b/libsmartcols/docs/libsmartcols-sections.txt index 94bb9ef05..e7fe4b75d 100644 --- a/libsmartcols/docs/libsmartcols-sections.txt +++ b/libsmartcols/docs/libsmartcols-sections.txt @@ -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 diff --git a/libsmartcols/src/filter-param.c b/libsmartcols/src/filter-param.c index 4a67cbfaf..17757f0d9 100644 --- a/libsmartcols/src/filter-param.c +++ b/libsmartcols/src/filter-param.c @@ -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; +} diff --git a/libsmartcols/src/libsmartcols.h.in b/libsmartcols/src/libsmartcols.h.in index 1acff2f0e..abb9cdd6c 100644 --- a/libsmartcols/src/libsmartcols.h.in +++ b/libsmartcols/src/libsmartcols.h.in @@ -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); diff --git a/libsmartcols/src/libsmartcols.sym b/libsmartcols/src/libsmartcols.sym index ce77b0df3..6fe8f6187 100644 --- a/libsmartcols/src/libsmartcols.sym +++ b/libsmartcols/src/libsmartcols.sym @@ -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; + diff --git a/sys-utils/lsns.c b/sys-utils/lsns.c index a97e7ef3c..b64f70406 100644 --- a/sys-utils/lsns.c +++ b/sys-utils/lsns.c @@ -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);