From 46172c8bc24799ae35cf300b0b5ba765bd40896a Mon Sep 17 00:00:00 2001 From: Joel Rosdahl Date: Wed, 17 Jun 2020 21:40:02 +0200 Subject: [PATCH] Allow the config file to be a symlink Fixes #607. --- src/conf.c | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/src/conf.c b/src/conf.c index 4d0420e7a..72da237fa 100644 --- a/src/conf.c +++ b/src/conf.c @@ -296,8 +296,8 @@ conf_update_from_environment(struct conf *conf, char **errmsg) } bool -conf_set_value_in_file(const char *path, const char *key, const char *value, - char **errmsg) +conf_set_value_in_file(const char *conf_path, const char *key, + const char *value, char **errmsg) { const struct conf_item *item = find_conf(key); if (!item) { @@ -311,9 +311,14 @@ conf_set_value_in_file(const char *path, const char *key, const char *value, return false; } + char *path = x_realpath(conf_path); + if (!path) { + path = x_strdup(conf_path); + } FILE *infile = fopen(path, "r"); if (!infile) { *errmsg = format("%s: %s", path, strerror(errno)); + free(path); return false; } @@ -322,6 +327,7 @@ conf_set_value_in_file(const char *path, const char *key, const char *value, if (!outfile) { *errmsg = format("%s: %s", outpath, strerror(errno)); free(outpath); + free(path); fclose(infile); return false; } @@ -351,9 +357,12 @@ conf_set_value_in_file(const char *path, const char *key, const char *value, fclose(outfile); if (x_rename(outpath, path) != 0) { *errmsg = format("rename %s to %s: %s", outpath, path, strerror(errno)); + free(outpath); + free(path); return false; } - free(outpath); + free(outpath); + free(path); return true; } -- 2.47.2