Forráskód Böngészése

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.
Andrei Pelinescu-Onciul 15 éve
szülő
commit
139913192c
2 módosított fájl, 24 hozzáadás és 10 törlés
  1. 23 9
      cfg_parser.c
  2. 1 1
      cfg_parser.h

+ 23 - 9
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;
 }
 

+ 1 - 1
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);