state: migrate to lif_state_record from a bare dictionary containing the mapped ifs
This commit is contained in:
parent
923b96fab8
commit
8e4eb5d00c
3 changed files with 30 additions and 8 deletions
|
@ -215,7 +215,8 @@ list_state(struct lif_dict *state, struct match_options *opts)
|
|||
fnmatch(opts->include_pattern, entry->key, 0))
|
||||
continue;
|
||||
|
||||
printf("%s=%s\n", entry->key, (const char *) entry->data);
|
||||
struct lif_state_record *rec = entry->data;
|
||||
printf("%s=%s %zu\n", entry->key, rec->mapped_if, rec->refcount);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -27,16 +27,21 @@ lif_state_read(struct lif_dict *state, FILE *fd)
|
|||
while (lif_fgetline(linebuf, sizeof linebuf, fd))
|
||||
{
|
||||
char *ifname = lif_next_token(&bufp);
|
||||
char *refcount = lif_next_token(&bufp);
|
||||
size_t rc = 1;
|
||||
char *equals_p = strchr(linebuf, '=');
|
||||
|
||||
if (*refcount)
|
||||
rc = strtoul(refcount, NULL, 10);
|
||||
|
||||
if (equals_p == NULL)
|
||||
{
|
||||
lif_state_upsert(state, ifname, &(struct lif_interface){ .ifname = ifname });
|
||||
lif_state_upsert(state, ifname, &(struct lif_interface){ .ifname = ifname, .refcount = rc });
|
||||
continue;
|
||||
}
|
||||
|
||||
*equals_p++ = '\0';
|
||||
lif_state_upsert(state, ifname, &(struct lif_interface){ .ifname = equals_p });
|
||||
lif_state_upsert(state, ifname, &(struct lif_interface){ .ifname = equals_p, .refcount = rc });
|
||||
}
|
||||
|
||||
return true;
|
||||
|
@ -61,7 +66,12 @@ lif_state_read_path(struct lif_dict *state, const char *path)
|
|||
void
|
||||
lif_state_upsert(struct lif_dict *state, const char *ifname, struct lif_interface *iface)
|
||||
{
|
||||
lif_dict_add(state, ifname, strdup(iface->ifname));
|
||||
struct lif_state_record *rec = calloc(1, sizeof(*rec));
|
||||
|
||||
rec->mapped_if = strdup(iface->ifname);
|
||||
rec->refcount = iface->refcount;
|
||||
|
||||
lif_dict_add(state, ifname, rec);
|
||||
}
|
||||
|
||||
void
|
||||
|
@ -72,7 +82,10 @@ lif_state_delete(struct lif_dict *state, const char *ifname)
|
|||
if (entry == NULL)
|
||||
return;
|
||||
|
||||
free(entry->data);
|
||||
struct lif_state_record *rec = entry->data;
|
||||
free(rec->mapped_if);
|
||||
free(rec);
|
||||
|
||||
lif_dict_delete_entry(state, entry);
|
||||
}
|
||||
|
||||
|
@ -84,8 +97,9 @@ lif_state_write(const struct lif_dict *state, FILE *f)
|
|||
LIF_DICT_FOREACH(iter, state)
|
||||
{
|
||||
struct lif_dict_entry *entry = iter->data;
|
||||
struct lif_state_record *rec = entry->data;
|
||||
|
||||
fprintf(f, "%s=%s\n", entry->key, (const char *) entry->data);
|
||||
fprintf(f, "%s=%s %zu\n", entry->key, rec->mapped_if, rec->refcount);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -111,7 +125,8 @@ lif_state_lookup(struct lif_dict *state, struct lif_dict *if_collection, const c
|
|||
if (entry == NULL)
|
||||
return NULL;
|
||||
|
||||
struct lif_dict_entry *if_entry = lif_dict_find(if_collection, (const char *) entry->data);
|
||||
struct lif_state_record *rec = entry->data;
|
||||
struct lif_dict_entry *if_entry = lif_dict_find(if_collection, rec->mapped_if);
|
||||
|
||||
if (if_entry == NULL)
|
||||
return NULL;
|
||||
|
@ -127,9 +142,10 @@ lif_state_sync(struct lif_dict *state, struct lif_dict *if_collection)
|
|||
LIF_DICT_FOREACH(iter, state)
|
||||
{
|
||||
struct lif_dict_entry *entry = iter->data;
|
||||
struct lif_state_record *rec = entry->data;
|
||||
struct lif_interface *iface = lif_interface_collection_find(if_collection, entry->key);
|
||||
|
||||
iface->refcount++;
|
||||
iface->refcount = rec->refcount;
|
||||
}
|
||||
|
||||
return true;
|
||||
|
|
|
@ -19,6 +19,11 @@
|
|||
#include <stdio.h>
|
||||
#include "libifupdown/interface.h"
|
||||
|
||||
struct lif_state_record {
|
||||
char *mapped_if;
|
||||
size_t refcount;
|
||||
};
|
||||
|
||||
extern bool lif_state_read(struct lif_dict *state, FILE *f);
|
||||
extern bool lif_state_read_path(struct lif_dict *state, const char *path);
|
||||
extern void lif_state_upsert(struct lif_dict *state, const char *ifname, struct lif_interface *iface);
|
||||
|
|
Loading…
Reference in a new issue