From: William Lallemand Date: Wed, 19 Mar 2025 15:16:52 +0000 (+0100) Subject: MINOR: tools: path_base() concatenates a path with a base path X-Git-Tag: v3.2-dev8~23 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=b0ad777902c8e51b127c275b3961e7baba14ec12;p=thirdparty%2Fhaproxy.git MINOR: tools: path_base() concatenates a path with a base path With the SSL configuration, crt-base, key-base are often used, these keywords concatenates the base path with the path when the path does not start by '/'. This is done at several places in the code, so a function to do this would be better to standardize the code. --- diff --git a/include/haproxy/tools.h b/include/haproxy/tools.h index bf2ca90a0..fa56373f6 100644 --- a/include/haproxy/tools.h +++ b/include/haproxy/tools.h @@ -1388,4 +1388,6 @@ static inline const char *errname(int err_num, char **out) } } +int path_base(const char *path, const char *base, char *dst, char **err); + #endif /* _HAPROXY_TOOLS_H */ diff --git a/src/tools.c b/src/tools.c index 1f107b372..ce927f90c 100644 --- a/src/tools.c +++ b/src/tools.c @@ -7215,6 +7215,32 @@ void free_all_file_names() HA_RWLOCK_WRUNLOCK(OTHER_LOCK, &file_names.lock); } + +/* + * Fill a buffer with a path. <*dst> must be at least of size PATH_MAX. + * If a is specified and the path does not start with "/", concatenate / + * + */ +int path_base(const char *path, const char *base, char *dst, char **err) +{ + int err_code = 0; + int rv = 0; + + if (base && *base && *path != '/') + rv = snprintf(dst, PATH_MAX, "%s/%s", base, path); + else + rv = snprintf(dst, PATH_MAX, "%s", path); + + if (rv >= PATH_MAX) { + memprintf(err, "'%s/%s' : path too long", base, path); + err_code |= ERR_ALERT | ERR_FATAL; + goto out; + } + +out: + return err_code; +} + /* * Local variables: * c-indent-level: 8