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.
This commit is contained in:
William Lallemand 2025-03-19 16:16:52 +01:00
parent 21601f4a27
commit b0ad777902
2 changed files with 28 additions and 0 deletions

View file

@ -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 */

View file

@ -7215,6 +7215,32 @@ void free_all_file_names()
HA_RWLOCK_WRUNLOCK(OTHER_LOCK, &file_names.lock);
}
/*
* Fill a <dst> buffer with a path. <*dst> must be at least of size PATH_MAX.
* If a <base> is specified and the path does not start with "/", concatenate <base>/<path>
*
*/
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