Skip to content

Commit 2d71608

Browse files
rscharfegitster
authored andcommitted
run-command: factor out child_process_clear()
Avoid duplication by moving the code to release allocated memory for arguments and environment to its own function, child_process_clear(). Export it to provide a counterpart to child_process_init(). Signed-off-by: Rene Scharfe <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent a2558fb commit 2d71608

File tree

3 files changed

+17
-6
lines changed

3 files changed

+17
-6
lines changed

Documentation/technical/api-run-command.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,13 @@ Functions
4646
The argument dir corresponds the member .dir. The argument env
4747
corresponds to the member .env.
4848

49+
`child_process_clear`::
50+
51+
Release the memory associated with the struct child_process.
52+
Most users of the run-command API don't need to call this
53+
function explicitly because `start_command` invokes it on
54+
failure and `finish_command` calls it automatically already.
55+
4956
The functions above do the following:
5057

5158
. If a system call failed, errno is set and -1 is returned. A diagnostic

run-command.c

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@ void child_process_init(struct child_process *child)
1111
argv_array_init(&child->env_array);
1212
}
1313

14+
void child_process_clear(struct child_process *child)
15+
{
16+
argv_array_clear(&child->args);
17+
argv_array_clear(&child->env_array);
18+
}
19+
1420
struct child_to_clean {
1521
pid_t pid;
1622
struct child_to_clean *next;
@@ -336,8 +342,7 @@ int start_command(struct child_process *cmd)
336342
fail_pipe:
337343
error("cannot create %s pipe for %s: %s",
338344
str, cmd->argv[0], strerror(failed_errno));
339-
argv_array_clear(&cmd->args);
340-
argv_array_clear(&cmd->env_array);
345+
child_process_clear(cmd);
341346
errno = failed_errno;
342347
return -1;
343348
}
@@ -523,8 +528,7 @@ int start_command(struct child_process *cmd)
523528
close_pair(fderr);
524529
else if (cmd->err)
525530
close(cmd->err);
526-
argv_array_clear(&cmd->args);
527-
argv_array_clear(&cmd->env_array);
531+
child_process_clear(cmd);
528532
errno = failed_errno;
529533
return -1;
530534
}
@@ -550,8 +554,7 @@ int start_command(struct child_process *cmd)
550554
int finish_command(struct child_process *cmd)
551555
{
552556
int ret = wait_or_whine(cmd->pid, cmd->argv[0]);
553-
argv_array_clear(&cmd->args);
554-
argv_array_clear(&cmd->env_array);
557+
child_process_clear(cmd);
555558
return ret;
556559
}
557560

run-command.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ struct child_process {
4747

4848
#define CHILD_PROCESS_INIT { NULL, ARGV_ARRAY_INIT, ARGV_ARRAY_INIT }
4949
void child_process_init(struct child_process *);
50+
void child_process_clear(struct child_process *);
5051

5152
int start_command(struct child_process *);
5253
int finish_command(struct child_process *);

0 commit comments

Comments
 (0)