Re: [RFC PATCH 0/1] Enable config.d directory to be processed.

Alice Mitchell
2020-11-02T15:57:09Z

On Mon, 2020-11-02 at 09:23 -0500, Steve Dickson wrote:
> Hello,
>
> On 11/2/20 8:24 AM, Steve Dickson wrote:
> > > You would need to write an equivalent of conf_load_file() that
> > > created a new transaction id and read in all the files before
> > > committing them to do it this way.
> >

How about the following as an alternative...

It changes none of the past behaviour, but if you wanted to add an
optional directory structure to a config file then simply add this to
the default single config file that we ship.

/etc/nfsmount.conf:
[NFSMount_Global_Options]
include=-/etc/nfsmount.conf.d/*.conf

The leading minus tells it that it isnt an error if its empty, and it
will load all of the fragments it finds in there as well as the
existing single file. Apply the same structure to any existing config
file that you want to also have a directory for.

-Alice

diff --git a/support/nfs/conffile.c b/support/nfs/conffile.c
index 58c03911..aade50c8 100644
--- a/support/nfs/conffile.c
+++ b/support/nfs/conffile.c
@@ -53,6 +53,7 @@
#include <libgen.h>
#include <sys/file.h>
#include <time.h>
+#include <glob.h>

#include "conffile.h"
#include "xlog.h"
@@ -690,6 +691,7 @@ conf_parse_line(int trans, char *line, const char *filename, int lineno, char **
if (strcasecmp(line, "include")==0) {
/* load and parse subordinate config files */
_Bool optional = false;
+ glob_t globdata;

if (val && *val == '-') {
optional = true;
@@ -704,33 +706,46 @@ conf_parse_line(int trans, char *line, const char *filename, int lineno, char **
return;
}

- subconf = conf_readfile(relpath);
- if (subconf == NULL) {
- if (!optional)
- xlog_warn("config error at %s:%d: error loading included config",
- filename, lineno);
- if (relpath)
- free(relpath);
- return;
- }
+ if (glob(relpath, GLOB_MARK|GLOB_NOCHECK, NULL, &globdata)) {
+ xlog_warn("config error at %s:%d: error with matching pattern", filename, lineno);
+ } else
+ {
+ for (size_t g=0; g<globdata.gl_pathc; g++) {
+ const char * subpath = globdata.gl_pathv[g];

- /* copy the section data so the included file can inherit it
- * without accidentally changing it for us */
- if (*section != NULL) {
- inc_section = strdup(*section);
- if (*subsection != NULL)
- inc_subsection = strdup(*subsection);
- }
+ if (subpath[strlen(subpath)-1] == '/') {
+ continue;
+ }
+ subconf = conf_readfile(subpath);
+ if (subconf == NULL) {
+ if (!optional)
+ xlog_warn("config error at %s:%d: error loading included config",
+ filename, lineno);
+ if (relpath)
+ free(relpath);
+ return;
+ }
+
+ /* copy the section data so the included file can inherit it
+ * without accidentally changing it for us */
+ if (*section != NULL) {
+ inc_section = strdup(*section);
+ if (*subsection != NULL)
+ inc_subsection = strdup(*subsection);
+ }

- conf_parse(trans, subconf, &inc_section, &inc_subsection, relpath);
+ conf_parse(trans, subconf, &inc_section, &inc_subsection, relpath);

- if (inc_section)
- free(inc_section);
- if (inc_subsection)
- free(inc_subsection);
+ if (inc_section)
+ free(inc_section);
+ if (inc_subsection)
+ free(inc_subsection);
+ free(subconf);
+ }
+ }
if (relpath)
free(relpath);
- free(subconf);
+ globfree(&globdata);
} else {
/* XXX Perhaps should we not ignore errors? */
conf_set(trans, *section, *subsection, line, val, 0, 0);