Quoting Christian Seiler (christ...@iwakd.de): > Instead of duplicating the code for parsing the global config file for > each option, write one main function, lxc_global_config_value, that > does the parsing for an arbitrary option name and just call that > function from the existing ones. > > Signed-off-by: Christian Seiler <christ...@iwakd.de>
Acked-by: Serge E. Hallyn <serge.hal...@ubuntu.com> > --- > src/lxc/utils.c | 137 > ++++++++++++++++++++++++------------------------------- > src/lxc/utils.h | 6 ++- > 2 files changed, 63 insertions(+), 80 deletions(-) > > diff --git a/src/lxc/utils.c b/src/lxc/utils.c > index 9a7a41d..fd892c1 100644 > --- a/src/lxc/utils.c > +++ b/src/lxc/utils.c > @@ -198,7 +198,7 @@ extern int mkdir_p(const char *dir, mode_t mode) > return 0; > } > > -static char *copypath(char *p) > +static char *copy_global_config_value(char *p) > { > int len = strlen(p); > char *retbuf; > @@ -216,116 +216,97 @@ static char *copypath(char *p) > return retbuf; > } > > -char *default_lxcpath; > #define DEFAULT_VG "lxc" > -char *default_lvmvg; > #define DEFAULT_ZFSROOT "lxc" > -char *default_zfsroot; > > -const char *default_lvm_vg(void) > +const char *lxc_global_config_value(const char *option_name) > { > - char buf[1024], *p; > - FILE *fin; > - > - if (default_lvmvg) > - return default_lvmvg; > + static const char *options[][2] = { > + { "lvm_vg", DEFAULT_VG }, > + { "zfsroot", DEFAULT_ZFSROOT }, > + { "lxcpath", LXCPATH }, > + { NULL, NULL }, > + }; > + static const char *values[sizeof(options) / sizeof(options[0])] = { 0 }; > + const char *(*ptr)[2]; > + size_t i; > + char buf[1024], *p, *p2; > + FILE *fin = NULL; > + > + for (i = 0, ptr = options; (*ptr)[0]; ptr++, i++) { > + if (!strcmp(option_name, (*ptr)[0])) > + break; > + } > + if (!(*ptr)[0]) { > + errno = EINVAL; > + return NULL; > + } > + if (values[i]) > + return values[i]; > > fin = fopen(LXC_GLOBAL_CONF, "r"); > if (fin) { > while (fgets(buf, 1024, fin)) { > if (buf[0] == '#') > continue; > - p = strstr(buf, "lvm_vg"); > + p = strstr(buf, option_name); > if (!p) > continue; > + /* see if there was just white space in front > + * of the option name > + */ > + for (p2 = buf; p2 < p; p2++) { > + if (*p2 != ' ' && *p2 != '\t') > + break; > + } > + if (p2 < p) > + continue; > p = strchr(p, '='); > if (!p) > continue; > + /* see if there was just white space after > + * the option name > + */ > + for (p2 += strlen(option_name); p2 < p; p2++) { > + if (*p2 != ' ' && *p2 != '\t') > + break; > + } > + if (p2 < p) > + continue; > p++; > while (*p && (*p == ' ' || *p == '\t')) p++; > if (!*p) > continue; > - default_lvmvg = copypath(p); > + values[i] = copy_global_config_value(p); > goto out; > } > } > - default_lvmvg = DEFAULT_VG; > + /* could not find value, use default */ > + values[i] = (*ptr)[1]; > + /* special case: if default value is NULL, > + * and there is no config, don't view that > + * as an error... */ > + if (!values[i]) > + errno = 0; > > out: > if (fin) > fclose(fin); > - return default_lvmvg; > + return values[i]; > } > > -const char *default_zfs_root(void) > +const char *default_lvm_vg(void) > { > - char buf[1024], *p; > - FILE *fin; > - > - if (default_zfsroot) > - return default_zfsroot; > - > - fin = fopen(LXC_GLOBAL_CONF, "r"); > - if (fin) { > - while (fgets(buf, 1024, fin)) { > - if (buf[0] == '#') > - continue; > - p = strstr(buf, "zfsroot"); > - if (!p) > - continue; > - p = strchr(p, '='); > - if (!p) > - continue; > - p++; > - while (*p && (*p == ' ' || *p == '\t')) p++; > - if (!*p) > - continue; > - default_zfsroot = copypath(p); > - goto out; > - } > - } > - default_zfsroot = DEFAULT_ZFSROOT; > + return lxc_global_config_value("lvm_vg"); > +} > > -out: > - if (fin) > - fclose(fin); > - return default_zfsroot; > +const char *default_zfs_root(void) > +{ > + return lxc_global_config_value("zfsroot"); > } > const char *default_lxc_path(void) > { > - char buf[1024], *p; > - FILE *fin; > - > - if (default_lxcpath) > - return default_lxcpath; > - > - fin = fopen(LXC_GLOBAL_CONF, "r"); > - if (fin) { > - while (fgets(buf, 1024, fin)) { > - if (buf[0] == '#') > - continue; > - p = strstr(buf, "lxcpath"); > - if (!p) > - continue; > - p = strchr(p, '='); > - if (!p) > - continue; > - p++; > - while (*p && (*p == ' ' || *p == '\t')) p++; > - if (!*p) > - continue; > - default_lxcpath = copypath(p); > - goto out; > - } > - } > - /* we couldn't open the file, or didn't find a lxcpath > - * entry there. Return @LXCPATH@ */ > - default_lxcpath = LXCPATH; > - > -out: > - if (fin) > - fclose(fin); > - return default_lxcpath; > + return lxc_global_config_value("lxcpath"); > } > > int wait_for_pid(pid_t pid) > diff --git a/src/lxc/utils.h b/src/lxc/utils.h > index 2c53da4..0ad9505 100644 > --- a/src/lxc/utils.h > +++ b/src/lxc/utils.h > @@ -25,6 +25,7 @@ > > #include <errno.h> > #include <stdarg.h> > +#include <stdio.h> > #include <sys/syscall.h> > #include <sys/types.h> > #include <unistd.h> > @@ -36,9 +37,10 @@ extern int lxc_setup_fs(void); > extern int get_u16(unsigned short *val, const char *arg, int base); > extern int mkdir_p(const char *dir, mode_t mode); > /* > - * Return a newly allocated buffer containing the default container > - * path. Caller must free this buffer. > + * Return a buffer containing the default container path. > + * Caller must NOT free this buffer, since it may be static. > */ > +extern const char *lxc_global_config_value(const char *option_name); > extern const char *default_lxc_path(void); > extern const char *default_zfs_root(void); > extern const char *default_lvm_vg(void); > -- > 1.7.10.4 > > > ------------------------------------------------------------------------------ > Learn the latest--Visual Studio 2012, SharePoint 2013, SQL 2012, more! > Discover the easy way to master current and previous Microsoft technologies > and advance your career. Get an incredible 1,500+ hours of step-by-step > tutorial videos with LearnDevNow. Subscribe today and save! > http://pubads.g.doubleclick.net/gampad/clk?id=58041391&iu=/4140/ostg.clktrk > _______________________________________________ > Lxc-devel mailing list > Lxc-devel@lists.sourceforge.net > https://lists.sourceforge.net/lists/listinfo/lxc-devel ------------------------------------------------------------------------------ How ServiceNow helps IT people transform IT departments: 1. Consolidate legacy IT systems to a single system of record for IT 2. Standardize and globalize service processes across IT 3. Implement zero-touch automation to replace manual, redundant tasks http://pubads.g.doubleclick.net/gampad/clk?id=51271111&iu=/4140/ostg.clktrk _______________________________________________ Lxc-devel mailing list Lxc-devel@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/lxc-devel