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 <l.s.r@web.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>
René Scharfe committed
Feb 15, 2015 at 19:33 UTC
dc8edc8f7d503b96dc4ceb275f7f6ca7637be5a9
1 file changed
+45
-9
daemon.c
+45
-9
@@ -122,6 +122,46 @@ static void NORETURN daemon_die(const char *err, va_list params)
122
exit(1);
123
}
124
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
+
165
static const char *path_ok(const char *directory)
166
{
167
static char rpath[PATH_MAX];
@@ -160,14 +200,10 @@ static const char *path_ok(const char *directory)
200
}
201
else if (interpolated_path && saw_extended_args) {
202
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
+
207
if (*dir != '/') {
208
/* Allow only absolute */
209
logerror("'%s': Non-absolute path denied (interpolated-path active)", dir);
@@ -175,7 +211,7 @@ static const char *path_ok(const char *directory)
211
}
212
213
strbuf_expand(&expanded_path, interpolated_path,
178
- strbuf_expand_dict_cb, &dict);
214
+ expand_path, &context);
215
strlcpy(interp_path, expanded_path.buf, PATH_MAX);
216
strbuf_release(&expanded_path);
217
loginfo("Interpolated dir '%s'", interp_path);