Skip to content

Commit dc8edc8

Browse files
rscharfegitster
authored andcommitted
daemon: use callback to build interpolated path
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent edef953 commit dc8edc8

File tree

1 file changed

+45
-9
lines changed

1 file changed

+45
-9
lines changed

daemon.c

Lines changed: 45 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,46 @@ static void NORETURN daemon_die(const char *err, va_list params)
122122
exit(1);
123123
}
124124

125+
static void strbuf_addstr_or_null(struct strbuf *sb, const char *s)
126+
{
127+
if (s)
128+
strbuf_addstr(sb, s);
129+
}
130+
131+
struct expand_path_context {
132+
const char *directory;
133+
};
134+
135+
static size_t expand_path(struct strbuf *sb, const char *placeholder, void *ctx)
136+
{
137+
struct expand_path_context *context = ctx;
138+
139+
switch (placeholder[0]) {
140+
case 'H':
141+
strbuf_addstr_or_null(sb, hostname);
142+
return 1;
143+
case 'C':
144+
if (placeholder[1] == 'H') {
145+
strbuf_addstr_or_null(sb, get_canon_hostname());
146+
return 2;
147+
}
148+
break;
149+
case 'I':
150+
if (placeholder[1] == 'P') {
151+
strbuf_addstr_or_null(sb, get_ip_address());
152+
return 2;
153+
}
154+
break;
155+
case 'P':
156+
strbuf_addstr_or_null(sb, tcp_port);
157+
return 1;
158+
case 'D':
159+
strbuf_addstr(sb, context->directory);
160+
return 1;
161+
}
162+
return 0;
163+
}
164+
125165
static const char *path_ok(const char *directory)
126166
{
127167
static char rpath[PATH_MAX];
@@ -160,22 +200,18 @@ static const char *path_ok(const char *directory)
160200
}
161201
else if (interpolated_path && saw_extended_args) {
162202
struct strbuf expanded_path = STRBUF_INIT;
163-
struct strbuf_expand_dict_entry dict[6];
164-
165-
dict[0].placeholder = "H"; dict[0].value = hostname;
166-
dict[1].placeholder = "CH"; dict[1].value = get_canon_hostname();
167-
dict[2].placeholder = "IP"; dict[2].value = get_ip_address();
168-
dict[3].placeholder = "P"; dict[3].value = tcp_port;
169-
dict[4].placeholder = "D"; dict[4].value = directory;
170-
dict[5].placeholder = NULL; dict[5].value = NULL;
203+
struct expand_path_context context;
204+
205+
context.directory = directory;
206+
171207
if (*dir != '/') {
172208
/* Allow only absolute */
173209
logerror("'%s': Non-absolute path denied (interpolated-path active)", dir);
174210
return NULL;
175211
}
176212

177213
strbuf_expand(&expanded_path, interpolated_path,
178-
strbuf_expand_dict_cb, &dict);
214+
expand_path, &context);
179215
strlcpy(interp_path, expanded_path.buf, PATH_MAX);
180216
strbuf_release(&expanded_path);
181217
loginfo("Interpolated dir '%s'", interp_path);

0 commit comments

Comments
 (0)