[PATCH] Allow uci to work with symlinked config files

Arjun AK arjunak234 at gmail.com
Mon May 24 05:42:37 PDT 2021


Instead of creating the temp file in "/etc/config" it should be created
where the config file actually exists (if the file is symlinked, it
might be in another mount point and "rename()" will fail)

Signed-off-by: Arjun AK <arjunak234 at gmail.com>
---
 file.c | 14 ++++++++++++--
 1 file changed, 12 insertions(+), 2 deletions(-)

diff --git a/file.c b/file.c
index 93abfae..f7dfa2d 100644
--- a/file.c
+++ b/file.c
@@ -29,6 +29,7 @@
 #include <string.h>
 #include <stdlib.h>
 #include <errno.h>
+#include <libgen.h>
 
 #include "uci.h"
 #include "uci_internal.h"
@@ -737,6 +738,7 @@ static void uci_file_commit(struct uci_context *ctx, struct uci_package **packag
 	char *volatile name = NULL;
 	char *volatile path = NULL;
 	char *filename = NULL;
+	char *conf_dir = NULL;
 	struct stat statbuf;
 	volatile bool do_rename = false;
 	int fd, sz;
@@ -748,9 +750,17 @@ static void uci_file_commit(struct uci_context *ctx, struct uci_package **packag
 			UCI_THROW(ctx, UCI_ERR_INVAL);
 	}
 
-	sz = snprintf(NULL, 0, "%s/.%s.uci-XXXXXX", ctx->confdir, p->e.name);
+	path = realpath(p->path, NULL);
+	if (path == NULL) {
+		UCI_THROW(ctx, UCI_ERR_INVAL);
+	}
+	conf_dir = dirname(path);
+
+	sz = snprintf(NULL, 0, "%s/.%s.uci-XXXXXX", conf_dir, p->e.name);
 	filename = alloca(sz + 1);
-	snprintf(filename, sz + 1, "%s/.%s.uci-XXXXXX", ctx->confdir, p->e.name);
+	snprintf(filename, sz + 1, "%s/.%s.uci-XXXXXX", conf_dir, p->e.name);
+
+	free(path);
 
 	/* open the config file for writing now, so that it is locked */
 	f1 = uci_open_stream(ctx, p->path, NULL, SEEK_SET, true, true);
-- 
2.20.1




More information about the openwrt-devel mailing list