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

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