From 13bfcda8de68c6347d0ce15f5dcdca25f782b6b3 Mon Sep 17 00:00:00 2001 From: Roland Mainz Date: Tue, 21 Oct 2014 12:06:42 -0400 Subject: [PATCH] Add support for directories in profile paths If a profile path component is a directory, process files in the directory as we would for an "includedir" directive. [ghudson@mit.edu: don't change default profile path; simplify profile_process_directory using prior commit; only check stat bits, not final character of pathname; misc style changes; commit message] ticket: 8030 (new) --- src/util/profile/prof_file.c | 29 +++++++++++++++++++---------- src/util/profile/prof_int.h | 3 +++ src/util/profile/prof_parse.c | 19 +++++++++++++++++++ 3 files changed, 41 insertions(+), 10 deletions(-) diff --git a/src/util/profile/prof_file.c b/src/util/profile/prof_file.c index 5611233704..08c0dd0824 100644 --- a/src/util/profile/prof_file.c +++ b/src/util/profile/prof_file.c @@ -307,6 +307,7 @@ errcode_t profile_update_file_data_locked(prf_data_t data, char **ret_modspec) time_t now; #endif FILE *f; + int isdir = 0; #ifdef HAVE_STAT now = time(0); @@ -345,19 +346,27 @@ errcode_t profile_update_file_data_locked(prf_data_t data, char **ret_modspec) return 0; } #endif - errno = 0; - f = fopen(data->filespec, "r"); - if (f == NULL) { - retval = errno; - if (retval == 0) - retval = ENOENT; - return retval; + +#ifdef HAVE_STAT + isdir = S_ISDIR(st.st_mode); +#endif + if (!isdir) { + errno = 0; + f = fopen(data->filespec, "r"); + if (f == NULL) + return (errno != 0) ? errno : ENOENT; + set_cloexec_file(f); } - set_cloexec_file(f); + data->upd_serial++; data->flags &= PROFILE_FILE_SHARED; /* FIXME same as '=' operator */ - retval = profile_parse_file(f, &data->root, ret_modspec); - fclose(f); + + if (isdir) { + retval = profile_process_directory(data->filespec, &data->root); + } else { + retval = profile_parse_file(f, &data->root, ret_modspec); + (void)fclose(f); + } if (retval) { return retval; } diff --git a/src/util/profile/prof_int.h b/src/util/profile/prof_int.h index 38ea59a1f7..73f7fad47b 100644 --- a/src/util/profile/prof_int.h +++ b/src/util/profile/prof_int.h @@ -123,6 +123,9 @@ struct _profile_t { errcode_t profile_parse_file (FILE *f, struct profile_node **root, char **ret_modspec); +errcode_t profile_process_directory + (const char *dirname, struct profile_node **root); + errcode_t profile_write_tree_file (struct profile_node *root, FILE *dstfile); diff --git a/src/util/profile/prof_parse.c b/src/util/profile/prof_parse.c index 07605be326..1c2a270cbe 100644 --- a/src/util/profile/prof_parse.c +++ b/src/util/profile/prof_parse.c @@ -451,6 +451,25 @@ errcode_t profile_parse_file(FILE *f, struct profile_node **root, return 0; } +errcode_t profile_process_directory(const char *dirname, + struct profile_node **root) +{ + errcode_t retval; + struct profile_node *node; + + *root = NULL; + retval = profile_create_node("(root)", 0, &node); + if (retval) + return retval; + retval = parse_include_dir(dirname, node); + if (retval) { + profile_free_node(node); + return retval; + } + *root = node; + return 0; +} + /* * Return TRUE if the string begins or ends with whitespace */ -- 2.47.2