Skip to content

Commit 6a97da3

Browse files
peffgitster
authored andcommitted
daemon: use an argv_array to exec children
Our struct child_process already has its own argv_array. Let's use that to avoid having to format options into separate buffers. Note that we'll need to declare the child process outside of the run_service_command() helper to do this. But that opens up a further simplification, which is that the helper can append to our argument list, saving each caller from specifying "." manually. Signed-off-by: Jeff King <[email protected]>
1 parent 07af889 commit 6a97da3

File tree

1 file changed

+17
-21
lines changed

1 file changed

+17
-21
lines changed

daemon.c

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -449,46 +449,42 @@ static void copy_to_log(int fd)
449449
fclose(fp);
450450
}
451451

452-
static int run_service_command(const char **argv)
452+
static int run_service_command(struct child_process *cld)
453453
{
454-
struct child_process cld = CHILD_PROCESS_INIT;
455-
456-
cld.argv = argv;
457-
cld.git_cmd = 1;
458-
cld.err = -1;
459-
if (start_command(&cld))
454+
argv_array_push(&cld->args, ".");
455+
cld->git_cmd = 1;
456+
cld->err = -1;
457+
if (start_command(cld))
460458
return -1;
461459

462460
close(0);
463461
close(1);
464462

465-
copy_to_log(cld.err);
463+
copy_to_log(cld->err);
466464

467-
return finish_command(&cld);
465+
return finish_command(cld);
468466
}
469467

470468
static int upload_pack(void)
471469
{
472-
/* Timeout as string */
473-
char timeout_buf[64];
474-
const char *argv[] = { "upload-pack", "--strict", NULL, ".", NULL };
475-
476-
argv[2] = timeout_buf;
477-
478-
snprintf(timeout_buf, sizeof timeout_buf, "--timeout=%u", timeout);
479-
return run_service_command(argv);
470+
struct child_process cld = CHILD_PROCESS_INIT;
471+
argv_array_pushl(&cld.args, "upload-pack", "--strict", NULL);
472+
argv_array_pushf(&cld.args, "--timeout=%u", timeout);
473+
return run_service_command(&cld);
480474
}
481475

482476
static int upload_archive(void)
483477
{
484-
static const char *argv[] = { "upload-archive", ".", NULL };
485-
return run_service_command(argv);
478+
struct child_process cld = CHILD_PROCESS_INIT;
479+
argv_array_push(&cld.args, "upload-archive");
480+
return run_service_command(&cld);
486481
}
487482

488483
static int receive_pack(void)
489484
{
490-
static const char *argv[] = { "receive-pack", ".", NULL };
491-
return run_service_command(argv);
485+
struct child_process cld = CHILD_PROCESS_INIT;
486+
argv_array_push(&cld.args, "receive-pack");
487+
return run_service_command(&cld);
492488
}
493489

494490
static struct daemon_service daemon_service[] = {

0 commit comments

Comments
 (0)