]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: tools: path_base() concatenates a path with a base path
authorWilliam Lallemand <wlallemand@haproxy.com>
Wed, 19 Mar 2025 15:16:52 +0000 (16:16 +0100)
committerWilliam Lallemand <wlallemand@haproxy.com>
Wed, 19 Mar 2025 16:59:31 +0000 (17:59 +0100)
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.

include/haproxy/tools.h
src/tools.c

index bf2ca90a0ff7e30016908db46c20baba9eb91d84..fa56373f6d09117d7b63ec141f45b903a7bf3e5b 100644 (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 */
index 1f107b3728a5240b4278d3d57b6d64659bf9cd8c..ce927f90c534bd91121beeeb03fe6d149a4d7766 100644 (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