Skip to contentSkip to conversation

batctl: avoid parsing bat-hosts multiple times

PATCH Superseded Daniel Seither 2 participants 3 messages Mar 4, 2010

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 batctl: avoid parsing bat-hosts multiple times

Currently, running batctl from the home directory leads to warnings ("Warning - mac already known") if ~/bat-hosts exists. This is caused by batctl parsing both "~/bat-hosts" and "bat-hosts" which happen to be the same file when the working directory is ~

This patch adds duplicate file name detection to bat_hosts_init() to avoid these warnings.

---

bat-hosts.c +21 -4
Index: batctl/bat-hosts.c
===================================================================
@@ -23,6 +23,7 @@
 
 #include <stdio.h>
 #include <stdint.h>
+#include <limits.h>
 #include <stdlib.h>
 #include <errno.h>
 #include <string.h>
@@ -144,9 +145,11 @@
 
 void bat_hosts_init(void)
 {
-	unsigned int i;
+	unsigned int i, j, parse;
 	char confdir[CONF_DIR_LEN];
 	char *homedir;
+	size_t locations = sizeof(bat_hosts_path) / sizeof(char *);
+	char normalized[locations][PATH_MAX];
 
 	host_hash = hash_new(64, compare_mac, choose_mac);
 
@@ -157,7 +160,7 @@
 
 	homedir = getenv("HOME");
 
-	for (i = 0; i < sizeof(bat_hosts_path) / sizeof(char *); i++) {
+	for (i = 0; i < locations; i++) {
 		strcpy(confdir, "");
 
 		if (strlen(bat_hosts_path[i]) >= 2
@@ -169,8 +172,22 @@
 			strncpy(confdir, bat_hosts_path[i], CONF_DIR_LEN);
 			confdir[CONF_DIR_LEN - 1] = '\0';
 		}
-
-		parse_hosts_file(&host_hash, confdir);
+		
+		if (realpath(confdir, normalized[i]) == NULL) {
+			normalized[i][0] = '\0';
+			continue;
+		}
+		
+		/* check for duplicates: don't parse the same file twice */	
+		parse = 1;
+		for (j = 0; j < i; j++)
+			if (strncmp(normalized[i], normalized[j], CONF_DIR_LEN) == 0) {
+				parse = 0;
+				break;
+			}
+	
+		if (parse && (normalized[i][0] != '\0'))
+			parse_hosts_file(&host_hash, normalized[i]);
 	}
 }
 
#1 lore raw
Quoted 7 lines · On Thursday 04 March 2010 20:23:21 Daniel Seither wrote:

Currently, running batctl from the home directory leads to warnings ("Warning - mac already known") if ~/bat-hosts exists. This is caused by batctl parsing both "~/bat-hosts" and "bat-hosts" which happen to be the same file when the working directory is ~

This patch adds duplicate file name detection to bat_hosts_init() to avoid these warnings.

I tested your patch and it works very well. That is a nice catch! I did not stumble over this problem before.
However, I wonder whether it would be better to let realpath() allocate the normalized file path buffer instead of using the static normalized array, simply to save some disk space. What do you think ?

Cheers,
Marek

#2 lore raw
Quoted 3 lines · Marek Lindner

However, I wonder whether it would be better to let realpath() allocate the normalized file path buffer instead of using the static normalized array, simply to save some disk space. What do you think ?

Yes, would be better; I didn't think about this issue. I'll send an updated patch in a few seconds.

Regards,
Daniel