1

I'm iterating package sections and I want to delete one of them:

struct uci_package * pkg;
struct uci_context * ctx;

//...

struct uci_element *elem;
uci_foreach_element(&(pkg->sections), elem)
{
        struct uci_section *section = uci_to_section(elem);
        if(!section)
            continue;
        if(match_foo(ctx, section))
        {
            //TODO uci_delete
            break;
        }
}

//...

but uci_delete input argument is struct uci_ptr which can be created only from parsing cli string (there is no conversion function from struct uci_section to struct uci_ptr).

How to delete section without playing with cli strings?

1 Answers1

0

I couldn't find a straight forward way to do that. But a simple solution is to create address of the node based on package name and element that you have in iteration loop. It should be something like this:

struct uci_package * pkg;
struct uci_context * ctx;

//...

char packageName[] = ""; // <--- Package name goes here
struct uci_element *elem;
uci_foreach_element(&(pkg->sections), elem)
{
        struct uci_section *section = uci_to_section(elem);
        if(!section)
            continue;
        if(match_foo(ctx, section))
        {
            //TODO uci_delete
            struct uci_ptr ptrPath;
            char tmp[strlen(packageName) + strlen(elem->name) + 2];
            sprintf(tmp, "%s.%s", packageName, elem->name);

            if (uci_lookup_ptr(ctx, &ptrPath, tmp, true) == UCI_OK) {
                uci_delete(ctx, &ptrPath);
                uci_save(ctx, ptrPath.p);
            }

            break;
        }
}