[sr-dev] git:master: cfg_parser: allow relative paths for the cfg file

Andrei Pelinescu-Onciul andrei at iptel.org
Tue Sep 7 18:40:13 CEST 2010


Module: sip-router
Branch: master
Commit: 139913192cb9e9616680a3a430a4039da9beb718
URL:    http://git.sip-router.org/cgi-bin/gitweb.cgi/sip-router/?a=commit;h=139913192cb9e9616680a3a430a4039da9beb718

Author: Andrei Pelinescu-Onciul <andrei at iptel.org>
Committer: Andrei Pelinescu-Onciul <andrei at iptel.org>
Date:   Tue Sep  7 17:03:53 2010 +0200

cfg_parser: allow relative paths for the cfg file

Allow relative parhs in the cfg_parser framework (used by the tls
module, ldap(s) and iptrtpproxy), by adding a new "basedir"
parameter to cfg_parser_init().
If basedir == 0 and the filename does not start with '/', the
filename path will be considered to be relative to the main ser
config file (e.g. ser -f /etc/ser/ser.cfg => relative to /etc/ser/ ).
This was the previous behaviour.
If basedir == "" the filename path will be considered to be
relative to the working directory (ser -w /tmp => relative to
/tmp).
For other basedir values, the filename path will be considered to
be relative to basedir.

---

 cfg_parser.c |   32 +++++++++++++++++++++++---------
 cfg_parser.h |    2 +-
 2 files changed, 24 insertions(+), 10 deletions(-)

diff --git a/cfg_parser.c b/cfg_parser.c
index 0827939..fa8721b 100644
--- a/cfg_parser.c
+++ b/cfg_parser.c
@@ -612,19 +612,33 @@ static char* get_base_name(str* filename)
 }
 
 
-cfg_parser_t* cfg_parser_init(str* filename)
+
+/** intialize the config parser.
+ * @param basedir - path to the config file name. If 0 the path
+ *               (base directory) of the main ser.cfg file will be used, else
+ *               basedir will be concatenated to the filename. It will be
+ *               used only if filename is not an absolute path.
+ * @param filename - config filename (can include path elements).
+ * @return 0 on error, !=0 on success.
+ */
+cfg_parser_t* cfg_parser_init(str* basedir, str* filename)
 {
 	cfg_parser_t* st;
-	char* pathname, *base;
+	char* pathname, *base, *abs_pathname;
 
-	pathname = NULL;
+	abs_pathname = NULL;
+	pathname = filename->s;
 	st = NULL;
 	base = NULL;
 	
-	if ((pathname = get_abs_pathname(NULL, filename)) == NULL) {
-		ERR("cfg_parser: Error while converting %.*s to absolute pathname\n", 
-			STR_FMT(filename));
-		goto error;
+	/* if basedir == 0 or != "" get_abs_pathname */
+	if (basedir == 0  || basedir->len != 0) {
+		if ((abs_pathname = get_abs_pathname(basedir, filename)) == NULL) {
+			ERR("cfg_parser: Error while converting %.*s to absolute"
+					" pathname\n", STR_FMT(filename));
+			goto error;
+		}
+		pathname = abs_pathname;
 	}
 
 	if ((base = get_base_name(filename)) == NULL) goto error;
@@ -640,7 +654,7 @@ cfg_parser_t* cfg_parser_init(str* filename)
 		goto error;
 	}
 
-	pkg_free(pathname);
+	if (abs_pathname) pkg_free(abs_pathname);
 
 	st->file = base;
 	st->line = 1;
@@ -653,7 +667,7 @@ cfg_parser_t* cfg_parser_init(str* filename)
 		pkg_free(st);
 	}
 	if (base) pkg_free(base);
-	if (pathname) pkg_free(pathname);
+	if (abs_pathname) pkg_free(abs_pathname);
 	return NULL;
 }
 
diff --git a/cfg_parser.h b/cfg_parser.h
index 4399b6c..11296c5 100644
--- a/cfg_parser.h
+++ b/cfg_parser.h
@@ -151,7 +151,7 @@ typedef struct cfg_parser {
 
 extern struct cfg_option cfg_bool_values[];
 
-struct cfg_parser* cfg_parser_init(str* filename);
+struct cfg_parser* cfg_parser_init(str* basedir, str* filename);
 
 void cfg_section_parser(struct cfg_parser* st, cfg_func_f parser, void* param);
 




More information about the sr-dev mailing list