network: switch to single find + allocation fun

parent ecbb3790
...@@ -166,17 +166,19 @@ bool lxc_config_value_empty(const char *value) ...@@ -166,17 +166,19 @@ bool lxc_config_value_empty(const char *value)
return true; return true;
} }
struct lxc_netdev *lxc_find_netdev_by_idx(struct lxc_conf *conf, /* Takes care of finding the correct netdev struct in the networks list or
unsigned int idx) * allocates a new one if it couldn't be found.
*/
struct lxc_netdev *lxc_get_netdev_by_idx(struct lxc_conf *conf,
unsigned int idx, bool allocate)
{ {
struct lxc_list *newlist;
struct lxc_netdev *netdev = NULL; struct lxc_netdev *netdev = NULL;
struct lxc_list *networks = &conf->network; struct lxc_list *networks = &conf->network;
struct lxc_list *insert = networks; struct lxc_list *insert = networks;
/* lookup network */ /* lookup network */
if (lxc_list_empty(networks)) if (!lxc_list_empty(networks)) {
return NULL;
lxc_list_for_each(insert, networks) { lxc_list_for_each(insert, networks) {
netdev = insert->elem; netdev = insert->elem;
if (netdev->idx >= idx) if (netdev->idx >= idx)
...@@ -186,25 +188,10 @@ struct lxc_netdev *lxc_find_netdev_by_idx(struct lxc_conf *conf, ...@@ -186,25 +188,10 @@ struct lxc_netdev *lxc_find_netdev_by_idx(struct lxc_conf *conf,
/* network already exists */ /* network already exists */
if (netdev->idx == idx) if (netdev->idx == idx)
return netdev; return netdev;
}
if (!allocate)
return NULL; return NULL;
}
/* Takes care of finding the correct netdev struct in the networks list or
* allocates a new one if it couldn't be found.
*/
struct lxc_netdev *lxc_get_netdev_by_idx(struct lxc_conf *conf,
unsigned int idx)
{
struct lxc_list *newlist;
struct lxc_netdev *netdev = NULL;
struct lxc_list *networks = &conf->network;
struct lxc_list *insert = networks;
/* lookup network */
netdev = lxc_find_netdev_by_idx(conf, idx);
if (netdev)
return netdev;
/* network does not exist */ /* network does not exist */
netdev = malloc(sizeof(*netdev)); netdev = malloc(sizeof(*netdev));
......
...@@ -28,10 +28,8 @@ extern int parse_idmaps(const char *idmap, char *type, unsigned long *nsid, ...@@ -28,10 +28,8 @@ extern int parse_idmaps(const char *idmap, char *type, unsigned long *nsid,
unsigned long *hostid, unsigned long *range); unsigned long *hostid, unsigned long *range);
extern bool lxc_config_value_empty(const char *value); extern bool lxc_config_value_empty(const char *value);
extern struct lxc_netdev *lxc_find_netdev_by_idx(struct lxc_conf *conf, extern struct lxc_netdev *
unsigned int idx); lxc_get_netdev_by_idx(struct lxc_conf *conf, unsigned int idx, bool allocate);
extern struct lxc_netdev *lxc_get_netdev_by_idx(struct lxc_conf *conf,
unsigned int idx);
extern void lxc_log_configured_netdevs(const struct lxc_conf *conf); extern void lxc_log_configured_netdevs(const struct lxc_conf *conf);
extern bool lxc_remove_nic_by_idx(struct lxc_conf *conf, unsigned int idx); extern bool lxc_remove_nic_by_idx(struct lxc_conf *conf, unsigned int idx);
extern void lxc_free_networks(struct lxc_conf *conf); extern void lxc_free_networks(struct lxc_conf *conf);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment