add a bunch more of the state stuff

This commit is contained in:
Ariadne Conill 2020-07-20 08:19:23 -06:00
parent 6fe4630f52
commit aa1cc0815c
4 changed files with 111 additions and 7 deletions

View file

@ -22,5 +22,6 @@
#include "libifupdown/interface-file.h"
#include "libifupdown/fgetline.h"
#include "libifupdown/version.h"
#include "libifupdown/state.h"
#endif

View file

@ -1,5 +1,5 @@
/*
* libifupdown/state.h
* libifupdown/state.c
* Purpose: state management
*
* Copyright (c) 2020 Ariadne Conill <ariadne@dereferenced.org>
@ -15,6 +15,44 @@
#include <string.h>
#include "libifupdown/state.h"
#include "libifupdown/fgetline.h"
bool
lif_state_read(struct lif_dict *state, FILE *fd)
{
char linebuf[4096];
while (lif_fgetline(linebuf, sizeof linebuf, fd))
{
char *ifname = linebuf;
char *equals_p = strchr(linebuf, '=');
if (equals_p == NULL)
{
lif_state_upsert(state, ifname, &(struct lif_interface){ .ifname = ifname });
continue;
}
*equals_p++ = '\0';
lif_state_upsert(state, ifname, &(struct lif_interface){ .ifname = equals_p });
}
return true;
}
bool
lif_state_read_path(struct lif_dict *state, const char *path)
{
FILE *fd = fopen(path, "r");
bool ret;
if (fd == NULL)
return false;
ret = lif_state_read(state, fd);
fclose(fd);
return ret;
}
void
lif_state_upsert(struct lif_dict *state, const char *ifname, struct lif_interface *iface)
@ -47,6 +85,20 @@ lif_state_write(const struct lif_dict *state, FILE *f)
}
}
bool
lif_state_write_path(const struct lif_dict *state, const char *path)
{
FILE *fd = fopen(path, "w");
if (fd == NULL)
return false;
lif_state_write(state, fd);
fclose(fd);
return true;
}
struct lif_interface *
lif_state_lookup(struct lif_dict *state, struct lif_dict *if_collection, const char *ifname)
{

View file

@ -19,9 +19,12 @@
#include <stdio.h>
#include "libifupdown/interface.h"
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);
extern void lif_state_delete(struct lif_dict *state, const char *ifname);
extern void lif_state_write(const struct lif_dict *state, FILE *f);
extern bool lif_state_write_path(const struct lif_dict *state, const char *path);
extern struct lif_interface *lif_state_lookup(struct lif_dict *state, struct lif_dict *if_collection, const char *ifname);
#endif