Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix show-xml filtering by interface name (issue #735,bsc#954758) #744

Merged
merged 3 commits into from
Mar 23, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 45 additions & 20 deletions client/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -522,50 +522,68 @@ __dump_fake_xml(const ni_dbus_variant_t *variant, unsigned int indent, const cha
}
}

static xml_node_t *
__dump_object_xml(const char *object_path, const ni_dbus_variant_t *variant, ni_xs_scope_t *schema, xml_node_t *parent)
static ni_bool_t
__dump_object_xml(const char *object_path, const ni_dbus_variant_t *variant,
ni_xs_scope_t *schema, xml_node_t *parent, const ni_string_array_t *filter)
{
xml_node_t *object_node;
ni_dbus_dict_entry_t *entry;
unsigned int index;
const char *ifname, *interface_name;

if (!ni_dbus_variant_is_dict(variant)) {
ni_error("%s: dbus data is not a dict", __func__);
return NULL;
return FALSE;
}

object_node = xml_node_new("object", parent);
object_node = xml_node_new("object", NULL);
xml_node_add_attr(object_node, "path", object_path);

if (filter && !filter->count)
filter = NULL;

for (entry = variant->dict_array_value, index = 0; index < variant->array.len; ++index, ++entry) {
const char *interface_name = entry->key;
interface_name = entry->key;
if (filter
&& ni_string_eq(interface_name, NI_OBJECTMODEL_NETIF_INTERFACE)
&& ni_dbus_dict_get_string(&entry->datum, "name", &ifname)
&& ni_string_array_index(filter, ifname) == -1) {
xml_node_free(object_node);
return TRUE;
}

/* Ignore well-known interfaces that never have properties */
if (!strcmp(interface_name, "org.freedesktop.DBus.ObjectManager")
|| !strcmp(interface_name, "org.freedesktop.DBus.Properties"))
if (!ni_string_startswith(interface_name, NI_OBJECTMODEL_NAMESPACE))
continue;

ni_dbus_xml_deserialize_properties(schema, interface_name, &entry->datum, object_node);
}

return object_node;
if (object_node->children)
xml_node_add_child(parent, object_node);
else
xml_node_free(object_node);
return TRUE;
}

static xml_node_t *
__dump_schema_xml(const ni_dbus_variant_t *variant, ni_xs_scope_t *schema)
__dump_schema_xml(const ni_dbus_variant_t *variant, ni_xs_scope_t *schema, const ni_string_array_t *filter)
{
xml_node_t *root = xml_node_new(NULL, NULL);
ni_dbus_dict_entry_t *entry;
unsigned int index;

if (!ni_dbus_variant_is_dict(variant)) {
ni_error("%s: dbus data is not a dict", __func__);
xml_node_free(root);
return NULL;
}

for (entry = variant->dict_array_value, index = 0; index < variant->array.len; ++index, ++entry) {
if (!__dump_object_xml(entry->key, &entry->datum, schema, root))
if (!__dump_object_xml(entry->key, &entry->datum, schema, root, filter)) {
xml_node_free(root);
return NULL;
}
}

return root;
Expand Down Expand Up @@ -598,6 +616,7 @@ do_show_xml(int argc, char **argv)
int opt_modems = 0;
#endif
int c, rv = 1;
ni_string_array_t ifnames = NI_STRING_ARRAY_INIT;

optind = 1;
while ((c = getopt_long(argc, argv, "", local_options, NULL)) != EOF) {
Expand All @@ -616,12 +635,14 @@ do_show_xml(int argc, char **argv)
case OPT_HELP:
usage:
fprintf(stderr,
"wicked [options] show-xml <ifname|all>\n"
"\nSupported options:\n"
"wicked show-xml [options] [ifname ... |all]\n"
"\n"
"Supported options:\n"
" --help\n"
" Show this help text.\n"
" --raw\n"
" Show raw dbus reply in pseudo-xml, rather than using the schema\n"
" Show raw dbus reply in pseudo-xml, rather than using the schema.\n"
" This option effectively disables the ifname filter. \n"
#ifdef MODEM
" --modem\n"
" List Modems\n"
Expand All @@ -631,13 +652,18 @@ do_show_xml(int argc, char **argv)
}
}

if (optind < argc)
ifname = argv[optind++];
(void)ifname; /* FIXME; not used yet */

if (optind != argc)
if (opt_raw && optind != argc)
goto usage;

/* warning: this is a shallow-copy from argv,
* use this only with _index() for filtering */
ifnames.count = argc - optind;
ifnames.data = argv + optind;
if (ni_string_array_index(&ifnames, "all") != -1) {
ifnames.count = 0;
ifnames.data = NULL;
}

if (!(object = ni_call_create_client()))
return 1;
#ifdef MODEM
Expand Down Expand Up @@ -670,7 +696,7 @@ do_show_xml(int argc, char **argv)
ni_xs_scope_t *schema = ni_objectmodel_init(NULL);
xml_node_t *tree;

tree = __dump_schema_xml(&result, schema);
tree = __dump_schema_xml(&result, schema, &ifnames);
if (tree == NULL) {
ni_error("unable to represent properties as xml");
goto out;
Expand Down Expand Up @@ -1737,4 +1763,3 @@ do_convert(int argc, char **argv)

return do_show_config(argc, argv, "compat:");
}