git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: "René Scharfe" <l.s.r@web.de>
To: Git Mailing List <git@vger.kernel.org>
Cc: Junio C Hamano <gitster@pobox.com>, Jeff King <peff@peff.net>
Subject: [PATCH 2/2] daemon: use callback to build interpolated path
Date: Sun, 15 Feb 2015 19:33:52 +0100	[thread overview]
Message-ID: <54E0E690.8000607@web.de> (raw)
In-Reply-To: <54E0E60D.6000305@web.de>

Provide a callback function for strbuf_expand() instead of using the
helper strbuf_expand_dict_cb().  While the resulting code is longer, it
only looks up the canonical hostname and IP address if at least one of
the placeholders %CH and %IP are used with --interpolated-path.

Use a struct for passing the directory to the callback function instead
of passing it directly to avoid having to cast away its const qualifier.

Signed-off-by: Rene Scharfe <l.s.r@web.de>
---
 daemon.c | 54 +++++++++++++++++++++++++++++++++++++++++++++---------
 1 file changed, 45 insertions(+), 9 deletions(-)

diff --git a/daemon.c b/daemon.c
index ef41943..09fa652 100644
--- a/daemon.c
+++ b/daemon.c
@@ -122,6 +122,46 @@ static void NORETURN daemon_die(const char *err, va_list params)
 	exit(1);
 }
 
+static void strbuf_addstr_or_null(struct strbuf *sb, const char *s)
+{
+	if (s)
+		strbuf_addstr(sb, s);
+}
+
+struct expand_path_context {
+	const char *directory;
+};
+
+static size_t expand_path(struct strbuf *sb, const char *placeholder, void *ctx)
+{
+	struct expand_path_context *context = ctx;
+
+	switch (placeholder[0]) {
+	case 'H':
+		strbuf_addstr_or_null(sb, hostname);
+		return 1;
+	case 'C':
+		if (placeholder[1] == 'H') {
+			strbuf_addstr_or_null(sb, get_canon_hostname());
+			return 2;
+		}
+		break;
+	case 'I':
+		if (placeholder[1] == 'P') {
+			strbuf_addstr_or_null(sb, get_ip_address());
+			return 2;
+		}
+		break;
+	case 'P':
+		strbuf_addstr_or_null(sb, tcp_port);
+		return 1;
+	case 'D':
+		strbuf_addstr(sb, context->directory);
+		return 1;
+	}
+	return 0;
+}
+
 static const char *path_ok(const char *directory)
 {
 	static char rpath[PATH_MAX];
@@ -160,14 +200,10 @@ static const char *path_ok(const char *directory)
 	}
 	else if (interpolated_path && saw_extended_args) {
 		struct strbuf expanded_path = STRBUF_INIT;
-		struct strbuf_expand_dict_entry dict[6];
-
-		dict[0].placeholder = "H"; dict[0].value = hostname;
-		dict[1].placeholder = "CH"; dict[1].value = get_canon_hostname();
-		dict[2].placeholder = "IP"; dict[2].value = get_ip_address();
-		dict[3].placeholder = "P"; dict[3].value = tcp_port;
-		dict[4].placeholder = "D"; dict[4].value = directory;
-		dict[5].placeholder = NULL; dict[5].value = NULL;
+		struct expand_path_context context;
+
+		context.directory = directory;
+
 		if (*dir != '/') {
 			/* Allow only absolute */
 			logerror("'%s': Non-absolute path denied (interpolated-path active)", dir);
@@ -175,7 +211,7 @@ static const char *path_ok(const char *directory)
 		}
 
 		strbuf_expand(&expanded_path, interpolated_path,
-				strbuf_expand_dict_cb, &dict);
+			      expand_path, &context);
 		strlcpy(interp_path, expanded_path.buf, PATH_MAX);
 		strbuf_release(&expanded_path);
 		loginfo("Interpolated dir '%s'", interp_path);
-- 
2.3.0

      reply	other threads:[~2015-02-15 18:34 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-02-15 18:31 [PATCH 1/2] daemon: look up client-supplied hostname lazily René Scharfe
2015-02-15 18:33 ` René Scharfe [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=54E0E690.8000607@web.de \
    --to=l.s.r@web.de \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    --cc=peff@peff.net \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).