From 4ef89d213be3b32c84511cd952a0286869aef3ea Mon Sep 17 00:00:00 2001 From: Markus Makela Date: Thu, 31 Dec 2015 10:33:53 +0200 Subject: [PATCH] MXS-463: Filepaths are now properly formatted for printing The various global directory setter functions now process the input they receive and remove redundant and trailing forward slashes from the directory paths. --- server/core/gwdirs.c | 113 ++++++++++++++++++++++++++++-------------- server/core/secrets.c | 5 +- server/core/utils.c | 41 +++++++++++++++ server/include/gw.h | 1 + 4 files changed, 123 insertions(+), 37 deletions(-) diff --git a/server/core/gwdirs.c b/server/core/gwdirs.c index 29699ed11..19b21ad3f 100644 --- a/server/core/gwdirs.c +++ b/server/core/gwdirs.c @@ -17,66 +17,101 @@ */ #include +#include +/** + * Set the configuration file directory + * @param str Path to directory + */ void set_configdir(char* str) { free(configdir); + clean_up_pathname(str); configdir = str; } +/** + * Set the log file directory + * @param str Path to directory + */ void set_logdir(char* str) { free(logdir); + clean_up_pathname(str); logdir = str; } +/** + * Set the language file directory + * @param str Path to directory + */ void set_langdir(char* str) { free(langdir); + clean_up_pathname(str); langdir = str; } +/** + * Set the PID file directory + * @param str Path to directory + */ void set_piddir(char* str) { free(piddir); + clean_up_pathname(str); piddir = str; } +/** + * Set the cache directory + * @param str Path to directory + */ +void set_cachedir(char* param) +{ + free(cachedir); + clean_up_pathname(param); + cachedir = param; +} + +/** + * Set the data directory + * @param str Path to directory + */ +void set_datadir(char* param) +{ + free(maxscaledatadir); + clean_up_pathname(param); + maxscaledatadir = param; +} + +/** + * Set the library directory. Modules will be loaded from here. + * @param str Path to directory + */ +void set_libdir(char* param) +{ + free(libdir); + clean_up_pathname(param); + libdir = param; +} + /** * Get the directory with all the modules. * @return The module directory */ char* get_libdir() { - return libdir ? libdir : (char*)default_libdir; + return libdir ? libdir : (char*) default_libdir; } -void set_libdir(char* param) -{ - if (libdir) - { - free(libdir); - } - - libdir = param; -} /** * Get the service cache directory * @return The path to the cache directory */ char* get_cachedir() { - return cachedir ? cachedir : (char*)default_cachedir; -} - -void set_cachedir(char* param) -{ - if (cachedir) - { - free(cachedir); - } - - cachedir = param; + return cachedir ? cachedir : (char*) default_cachedir; } /** @@ -85,35 +120,41 @@ void set_cachedir(char* param) */ char* get_datadir() { - return maxscaledatadir ? maxscaledatadir : (char*)default_datadir; -} - -void set_datadir(char* param) -{ - if (maxscaledatadir) - { - free(maxscaledatadir); - } - - maxscaledatadir = param; + return maxscaledatadir ? maxscaledatadir : (char*) default_datadir; } +/** + * Get the configuration file directory + * @return The path to the configuration file directory + */ char* get_configdir() { - return configdir ? configdir : (char*)default_configdir; + return configdir ? configdir : (char*) default_configdir; } +/** + * Get the PID file directory which contains maxscale.pid + * @return Path to the PID file directory + */ char* get_piddir() { - return piddir ? piddir : (char*)default_piddir; + return piddir ? piddir : (char*) default_piddir; } +/** + * Return the log file directory + * @return Path to the log file directory + */ char* get_logdir() { - return logdir ? logdir : (char*)default_logdir; + return logdir ? logdir : (char*) default_logdir; } +/** + * Path to the directory which contains the errmsg.sys language file + * @return Path to the language file directory + */ char* get_langdir() { - return langdir ? langdir : (char*)default_langdir; + return langdir ? langdir : (char*) default_langdir; } diff --git a/server/core/secrets.c b/server/core/secrets.c index acc8ab226..1cb9b3c4c 100644 --- a/server/core/secrets.c +++ b/server/core/secrets.c @@ -25,6 +25,8 @@ #include #include +#include "gw.h" + /** * Generate a random printable character * @@ -67,6 +69,7 @@ secrets_readKeys(const char* path) if (path != NULL) { snprintf(secret_file, PATH_MAX, "%s/.secrets", path); + clean_up_pathname(secret_file); } else { @@ -224,7 +227,7 @@ int secrets_writeKeys(const char *path) } snprintf(secret_file, PATH_MAX + 9, "%s/.secrets", path); - secret_file[PATH_MAX + 9] = '\0'; + clean_up_pathname(secret_file); /* Open for writing | Create | Truncate the file for writing */ if ((fd = open(secret_file, O_CREAT | O_WRONLY | O_TRUNC, S_IRUSR)) < 0) diff --git a/server/core/utils.c b/server/core/utils.c index b2441e847..f71b5a1e2 100644 --- a/server/core/utils.c +++ b/server/core/utils.c @@ -278,3 +278,44 @@ char *create_hex_sha1_sha1_passwd(char *passwd) return hexpasswd; } + +/** + * Remove duplicate and trailing forward slashes from a path. + * @param path Path to clean up + */ +void clean_up_pathname(char *path) +{ + char *data = path; + size_t len = strlen(path); + + if (len > PATH_MAX) + { + MXS_WARNING("Pathname too long: %s", path); + } + + while (*data != '\0') + { + if (*data == '/') + { + if (*(data + 1) == '/') + { + memmove(data, data + 1, len); + len--; + } + else if (*(data + 1) == '\0' && data != path) + { + *data = '\0'; + } + else + { + data++; + len--; + } + } + else + { + data++; + len--; + } + } +} diff --git a/server/include/gw.h b/server/include/gw.h index 3571968ea..b686709bc 100644 --- a/server/include/gw.h +++ b/server/include/gw.h @@ -89,4 +89,5 @@ int parse_bindconfig(char *, unsigned short, struct sockaddr_in *); int setipaddress(struct in_addr *, char *); char* get_libdir(); long get_processor_count(); +void clean_up_pathname(char *path); #endif