Blame SOURCES/autofs-5.0.9-amd-lookup-add-key-matching-helper-function.patch

306fa1
autofs-5.0.9 - amd lookup add key matching helper function
306fa1
306fa1
From: Ian Kent <raven@themaw.net>
306fa1
306fa1
Add helper function and match_cached_key() that perform the progressive
306fa1
key+prefix matching procedure.
306fa1
---
306fa1
 include/parse_subs.h |    2 +
306fa1
 lib/parse_subs.c     |   84 ++++++++++++++++++++++++++++++++++++++++++++++++++
306fa1
 2 files changed, 86 insertions(+)
306fa1
306fa1
diff --git a/include/parse_subs.h b/include/parse_subs.h
306fa1
index 43da182..1e87716 100644
306fa1
--- a/include/parse_subs.h
306fa1
+++ b/include/parse_subs.h
306fa1
@@ -113,6 +113,8 @@ struct map_type_info {
306fa1
 unsigned int get_proximity(struct sockaddr *);
306fa1
 unsigned int get_network_proximity(const char *);
306fa1
 unsigned int in_network(char *);
306fa1
+struct mapent *match_cached_key(struct autofs_point *, const char *,
306fa1
+				struct map_source *, const char *);
306fa1
 const char *skipspace(const char *);
306fa1
 int check_colon(const char *);
306fa1
 int chunklen(const char *, int);
306fa1
diff --git a/lib/parse_subs.c b/lib/parse_subs.c
306fa1
index 421d18c..1e4825d 100644
306fa1
--- a/lib/parse_subs.c
306fa1
+++ b/lib/parse_subs.c
306fa1
@@ -498,6 +498,90 @@ unsigned int in_network(char *network)
306fa1
 	return 1;
306fa1
 }
306fa1
 
306fa1
+struct mapent *match_cached_key(struct autofs_point *ap,
306fa1
+				const char *err_prefix,
306fa1
+				struct map_source *source,
306fa1
+				const char *key)
306fa1
+{
306fa1
+	char buf[MAX_ERR_BUF];
306fa1
+	struct mapent_cache *mc;
306fa1
+	struct mapent *me;
306fa1
+
306fa1
+	mc = source->mc;
306fa1
+
306fa1
+	if (!(source->flags & MAP_FLAG_FORMAT_AMD)) {
306fa1
+		int ret;
306fa1
+
306fa1
+		me = cache_lookup(mc, key);
306fa1
+		/*
306fa1
+		 * Stale mapent => check for entry in alternate source or
306fa1
+		 * wildcard. Note, plus included direct mount map entries
306fa1
+		 * are included as an instance (same map entry cache), not
306fa1
+		 * in a distinct source.
306fa1
+		 */
306fa1
+		if (me && (!me->mapent ||
306fa1
+		   (me->source != source && *me->key != '/'))) {
306fa1
+			while ((me = cache_lookup_key_next(me)))
306fa1
+				if (me->source == source)
306fa1
+					break;
306fa1
+			if (!me)
306fa1
+				me = cache_lookup_distinct(mc, "*");
306fa1
+		}
306fa1
+
306fa1
+		if (!me)
306fa1
+			goto done;
306fa1
+
306fa1
+		/*
306fa1
+		 * If this is a lookup add wildcard match for later validation
306fa1
+		 * checks and negative cache lookups.
306fa1
+		 */
306fa1
+		if (!(ap->flags & MOUNT_FLAG_REMOUNT) &&
306fa1
+		    ap->type == LKP_INDIRECT && *me->key == '*') {
306fa1
+			ret = cache_update(mc, source, key, me->mapent, me->age);
306fa1
+			if (!(ret & (CHE_OK | CHE_UPDATED)))
306fa1
+				me = NULL;
306fa1
+		}
306fa1
+	} else {
306fa1
+		char *lkp_key = strdup(key);
306fa1
+		if (!lkp_key) {
306fa1
+			char *estr = strerror_r(errno, buf, MAX_ERR_BUF);
306fa1
+			error(ap->logopt, "%s strdup: %s", err_prefix, estr);
306fa1
+			return NULL;
306fa1
+		}
306fa1
+
306fa1
+		/* If it's found we're done */
306fa1
+		me = cache_lookup_distinct(mc, lkp_key);
306fa1
+		if (me)
306fa1
+			goto free;
306fa1
+
306fa1
+		/*
306fa1
+		 * Otherwise strip successive directory components and try
306fa1
+		 * a match against map entries ending with a wildcard and
306fa1
+		 * finally try the wilcard entry itself.
306fa1
+		*/
306fa1
+		while (!me) {
306fa1
+			char *prefix;
306fa1
+
306fa1
+			while ((prefix = strrchr(lkp_key, '/'))) {
306fa1
+				*prefix = '\0';
306fa1
+				me = cache_partial_match_wild(mc, lkp_key);
306fa1
+				if (me)
306fa1
+					goto free;
306fa1
+			}
306fa1
+
306fa1
+			me = cache_lookup_distinct(mc, "*");
306fa1
+			if (me)
306fa1
+				goto free;
306fa1
+
306fa1
+			break;
306fa1
+		}
306fa1
+free:
306fa1
+		free(lkp_key);
306fa1
+	}
306fa1
+done:
306fa1
+	return me;
306fa1
+}
306fa1
+
306fa1
 /*
306fa1
  * Skip whitespace in a string; if we hit a #, consider the rest of the
306fa1
  * entry a comment.