96dc52
autofs-5.1.7 - simplify cache_get_parent()
96dc52
96dc52
From: Ian Kent <raven@themaw.net>
96dc52
96dc52
Eliminate the list traversal from get_parent() and rename it to
96dc52
get_offset_parent() to better describe it's usage.
96dc52
96dc52
Signed-off-by: Ian Kent <raven@themaw.net>
96dc52
---
96dc52
 CHANGELOG   |    1 +
96dc52
 lib/cache.c |   46 ++++++++++++++++++++++++++++------------------
96dc52
 2 files changed, 29 insertions(+), 18 deletions(-)
96dc52
96dc52
diff --git a/CHANGELOG b/CHANGELOG
96dc52
index e55fd66a..ee746277 100644
96dc52
--- a/CHANGELOG
96dc52
+++ b/CHANGELOG
96dc52
@@ -7,6 +7,7 @@
96dc52
 - Fix option for master read wait.
96dc52
 - eliminate cache_lookup_offset() usage.
96dc52
 - fix is mounted check on non existent path.
96dc52
+- simplify cache_get_parent().
96dc52
 
96dc52
 25/01/2021 autofs-5.1.7
96dc52
 - make bind mounts propagation slave by default.
96dc52
diff --git a/lib/cache.c b/lib/cache.c
96dc52
index d3b6642b..53f290cd 100644
96dc52
--- a/lib/cache.c
96dc52
+++ b/lib/cache.c
96dc52
@@ -827,47 +827,57 @@ void cache_update_negative(struct mapent_cache *mc,
96dc52
 }
96dc52
 
96dc52
 
96dc52
-static struct mapent *get_parent(const char *key, struct list_head *head, struct list_head **pos)
96dc52
+static struct mapent *get_offset_parent(struct mapent_cache *mc,
96dc52
+					const char *key)
96dc52
 {
96dc52
-	struct list_head *next;
96dc52
-	struct mapent *this, *last;
96dc52
-	int eq;
96dc52
+	struct mapent *me;
96dc52
+	char *parent, *tail;
96dc52
+	int key_len;
96dc52
 
96dc52
-	last = NULL;
96dc52
-	next = *pos ? (*pos)->next : head->next;
96dc52
+	key_len = strlen(key);
96dc52
 
96dc52
-	list_for_each(next, head) {
96dc52
-		this = list_entry(next, struct mapent, multi_list);
96dc52
+	/* Check if this is the root offset */
96dc52
+	if (key[key_len - 1] == '/')
96dc52
+		return NULL;
96dc52
+
96dc52
+	parent = strdup(key);
96dc52
+	tail = &parent[key_len - 1];
96dc52
 
96dc52
-		if (!strcmp(this->key, key))
96dc52
+	while (*tail) {
96dc52
+		while (*tail != '/')
96dc52
+			tail--;
96dc52
+
96dc52
+		*tail = 0;
96dc52
+
96dc52
+		tail--;
96dc52
+		if (tail == parent)
96dc52
 			break;
96dc52
 
96dc52
-		eq = strncmp(this->key, key, strlen(this->key));
96dc52
-		if (eq == 0) {
96dc52
-			*pos = next;
96dc52
-			last = this;
96dc52
-			continue;
96dc52
+		me = cache_lookup_distinct(mc, parent);
96dc52
+		if (me) {
96dc52
+			free(parent);
96dc52
+			return me;
96dc52
 		}
96dc52
 	}
96dc52
+	free(parent);
96dc52
 
96dc52
-	return last;
96dc52
+	return NULL;
96dc52
 }
96dc52
 
96dc52
 int cache_set_parents(struct mapent *mm)
96dc52
 {
96dc52
-	struct list_head *multi_head, *p, *pos;
96dc52
+	struct list_head *multi_head, *p;
96dc52
 	struct mapent *this;
96dc52
 
96dc52
 	if (!mm->multi)
96dc52
 		return 0;
96dc52
 
96dc52
-	pos = NULL;
96dc52
 	multi_head = &mm->multi->multi_list;
96dc52
 
96dc52
 	list_for_each(p, multi_head) {
96dc52
 		struct mapent *parent;
96dc52
 		this = list_entry(p, struct mapent, multi_list);
96dc52
-		parent = get_parent(this->key, multi_head, &pos;;
96dc52
+		parent = get_offset_parent(mm->mc, this->key);
96dc52
 		if (parent)
96dc52
 			this->parent = parent;
96dc52
 		else