credential-cache--daemon: refactor check_socket_directory

This function does an early return, and therefore has to repeat its cleanup. We can stick the later bit of the function into an "else" and avoid duplicating the shared part (which will get bigger in a future patch). Let's also rename the function to init_socket_directory. It not only checks the directory but also creates it. Saying "init" is more accurate. Signed-off-by: Jon Griffiths <jon_p_griffiths@yahoo.com> Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jon Griffiths committed Feb 23, 2016 at 02:15 UTC a6e5e2864f96ebce9ec6656841c8965bcdf1b37f
1 file changed +13 -15
credential-cache--daemon.c
+13 -15
@@ -215,7 +215,7 @@ static const char permissions_advice[] =
215 "users may be able to read your cached credentials. Consider running:\n"
216 "\n"
217 " chmod 0700 %s";
218 -static void check_socket_directory(const char *path)
218 +static void init_socket_directory(const char *path)
219 {
220 struct stat st;
221 char *path_copy = xstrdup(path);
@@ -224,20 +224,18 @@ static void check_socket_directory(const char *path)
224 if (!stat(dir, &st)) {
225 if (st.st_mode & 077)
226 die(permissions_advice, dir);
227 - free(path_copy);
228 - return;
227 + } else {
228 + /*
229 + * We must be sure to create the directory with the correct mode,
230 + * not just chmod it after the fact; otherwise, there is a race
231 + * condition in which somebody can chdir to it, sleep, then try to open
232 + * our protected socket.
233 + */
234 + if (safe_create_leading_directories_const(dir) < 0)
235 + die_errno("unable to create directories for '%s'", dir);
236 + if (mkdir(dir, 0700) < 0)
237 + die_errno("unable to mkdir '%s'", dir);
238 }
230 -
231 - /*
232 - * We must be sure to create the directory with the correct mode,
233 - * not just chmod it after the fact; otherwise, there is a race
234 - * condition in which somebody can chdir to it, sleep, then try to open
235 - * our protected socket.
236 - */
237 - if (safe_create_leading_directories_const(dir) < 0)
238 - die_errno("unable to create directories for '%s'", dir);
239 - if (mkdir(dir, 0700) < 0)
240 - die_errno("unable to mkdir '%s'", dir);
239 free(path_copy);
240 }
241
@@ -264,7 +262,7 @@ int main(int argc, const char **argv)
262 if (!socket_path)
263 usage_with_options(usage, options);
264
267 - check_socket_directory(socket_path);
265 + init_socket_directory(socket_path);
266 register_tempfile(&socket_file, socket_path);
267
268 if (ignore_sighup)