Welcome to mirror list, hosted at ThFree Co, Russian Federation.

git.kernel.org/pub/scm/git/git.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRené Scharfe <l.s.r@web.de>2023-06-17 23:43:17 +0300
committerJunio C Hamano <gitster@pobox.com>2023-06-18 22:55:30 +0300
commit6f1e2d52797161c7effe4d1388765de3dbd80bbf (patch)
treef00abffad5e1956a300923be11f2ba8ef2f53d1d /daemon.c
parent39dbd49b4138b6cdc9fb73e317d4e9f06df0c5c5 (diff)
replace strbuf_expand() with strbuf_expand_step()
Avoid the overhead of passing context to a callback function of strbuf_expand() by using strbuf_expand_step() in a loop instead. It requires explicit handling of %% and unrecognized placeholders, but is simpler, more direct and avoids void pointers. Signed-off-by: René Scharfe <l.s.r@web.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Diffstat (limited to 'daemon.c')
-rw-r--r--daemon.c61
1 files changed, 19 insertions, 42 deletions
diff --git a/daemon.c b/daemon.c
index 7139cc201d..3682bfdd08 100644
--- a/daemon.c
+++ b/daemon.c
@@ -144,42 +144,6 @@ static void NORETURN daemon_die(const char *err, va_list params)
exit(1);
}
-struct expand_path_context {
- const char *directory;
- struct hostinfo *hostinfo;
-};
-
-static size_t expand_path(struct strbuf *sb, const char *placeholder, void *ctx)
-{
- struct expand_path_context *context = ctx;
- struct hostinfo *hi = context->hostinfo;
-
- switch (placeholder[0]) {
- case 'H':
- strbuf_addbuf(sb, &hi->hostname);
- return 1;
- case 'C':
- if (placeholder[1] == 'H') {
- strbuf_addstr(sb, get_canon_hostname(hi));
- return 2;
- }
- break;
- case 'I':
- if (placeholder[1] == 'P') {
- strbuf_addstr(sb, get_ip_address(hi));
- return 2;
- }
- break;
- case 'P':
- strbuf_addbuf(sb, &hi->tcp_port);
- return 1;
- case 'D':
- strbuf_addstr(sb, context->directory);
- return 1;
- }
- return 0;
-}
-
static const char *path_ok(const char *directory, struct hostinfo *hi)
{
static char rpath[PATH_MAX];
@@ -223,10 +187,7 @@ static const char *path_ok(const char *directory, struct hostinfo *hi)
}
else if (interpolated_path && hi->saw_extended_args) {
struct strbuf expanded_path = STRBUF_INIT;
- struct expand_path_context context;
-
- context.directory = directory;
- context.hostinfo = hi;
+ const char *format = interpolated_path;
if (*dir != '/') {
/* Allow only absolute */
@@ -234,8 +195,24 @@ static const char *path_ok(const char *directory, struct hostinfo *hi)
return NULL;
}
- strbuf_expand(&expanded_path, interpolated_path,
- expand_path, &context);
+ while (strbuf_expand_step(&expanded_path, &format)) {
+ if (skip_prefix(format, "%", &format))
+ strbuf_addch(&expanded_path, '%');
+ else if (skip_prefix(format, "H", &format))
+ strbuf_addbuf(&expanded_path, &hi->hostname);
+ else if (skip_prefix(format, "CH", &format))
+ strbuf_addstr(&expanded_path,
+ get_canon_hostname(hi));
+ else if (skip_prefix(format, "IP", &format))
+ strbuf_addstr(&expanded_path,
+ get_ip_address(hi));
+ else if (skip_prefix(format, "P", &format))
+ strbuf_addbuf(&expanded_path, &hi->tcp_port);
+ else if (skip_prefix(format, "D", &format))
+ strbuf_addstr(&expanded_path, directory);
+ else
+ strbuf_addch(&expanded_path, '%');
+ }
rlen = strlcpy(interp_path, expanded_path.buf,
sizeof(interp_path));