Skip to content

Commit 34bee81

Browse files
committed
Call OpenProcess() from within __archive_create_child()
Move the Windows specific to the Windows version of the helper, making the existing code shorter and simpler. This means that the child "handle" type will vary across the two platforms - something that we've already been doing with waitpid. Signed-off-by: Emil Velikov <emil.l.velikov@gmail.com>
1 parent 4626d75 commit 34bee81

4 files changed

Lines changed: 15 additions & 35 deletions

File tree

libarchive/archive_read_support_filter_program.c

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -400,7 +400,6 @@ __archive_read_program(struct archive_read_filter *self, const char *cmd)
400400
static const size_t out_buf_len = 65536;
401401
char *out_buf;
402402
const char *prefix = "Program: ";
403-
pid_t child;
404403
int ret;
405404
size_t l;
406405

@@ -428,7 +427,7 @@ __archive_read_program(struct archive_read_filter *self, const char *cmd)
428427
state->out_buf_len = out_buf_len;
429428

430429
ret = __archive_create_child(cmd, &state->child_stdin,
431-
&state->child_stdout, &child);
430+
&state->child_stdout, &state->child);
432431
if (ret != ARCHIVE_OK) {
433432
free(state->out_buf);
434433
archive_string_free(&state->description);
@@ -438,21 +437,6 @@ __archive_read_program(struct archive_read_filter *self, const char *cmd)
438437
cmd);
439438
return (ARCHIVE_FATAL);
440439
}
441-
#if defined(_WIN32) && !defined(__CYGWIN__)
442-
state->child = OpenProcess(PROCESS_QUERY_INFORMATION, FALSE, child);
443-
if (state->child == NULL) {
444-
child_stop(self, state);
445-
free(state->out_buf);
446-
archive_string_free(&state->description);
447-
free(state);
448-
archive_set_error(&self->archive->archive, EINVAL,
449-
"Can't initialize filter; unable to run program \"%s\"",
450-
cmd);
451-
return (ARCHIVE_FATAL);
452-
}
453-
#else
454-
state->child = child;
455-
#endif
456440

457441
self->data = state;
458442
self->read = program_filter_read;

libarchive/archive_write_add_filter_program.c

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,6 @@ int
211211
__archive_write_program_open(struct archive_write_filter *f,
212212
struct archive_write_program_data *data, const char *cmd)
213213
{
214-
pid_t child;
215214
int ret;
216215

217216
if (data->child_buf == NULL) {
@@ -227,26 +226,12 @@ __archive_write_program_open(struct archive_write_filter *f,
227226
}
228227

229228
ret = __archive_create_child(cmd, &data->child_stdin,
230-
&data->child_stdout, &child);
229+
&data->child_stdout, &data->child);
231230
if (ret != ARCHIVE_OK) {
232231
archive_set_error(f->archive, EINVAL,
233232
"Can't launch external program: %s", cmd);
234233
return (ARCHIVE_FATAL);
235234
}
236-
#if defined(_WIN32) && !defined(__CYGWIN__)
237-
data->child = OpenProcess(PROCESS_QUERY_INFORMATION, FALSE, child);
238-
if (data->child == NULL) {
239-
close(data->child_stdin);
240-
data->child_stdin = -1;
241-
close(data->child_stdout);
242-
data->child_stdout = -1;
243-
archive_set_error(f->archive, EINVAL,
244-
"Can't launch external program: %s", cmd);
245-
return (ARCHIVE_FATAL);
246-
}
247-
#else
248-
data->child = child;
249-
#endif
250235
return (ARCHIVE_OK);
251236
}
252237

libarchive/filter_fork.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,11 @@
3434

3535
int
3636
__archive_create_child(const char *cmd, int *child_stdin, int *child_stdout,
37+
#if defined(_WIN32) && !defined(__CYGWIN__)
38+
HANDLE *out_child);
39+
#else
3740
pid_t *out_child);
41+
#endif
3842

3943
void
4044
__archive_check_child(int in, int out);

libarchive/filter_fork_windows.c

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333

3434
int
3535
__archive_create_child(const char *cmd, int *child_stdin, int *child_stdout,
36-
pid_t *out_child)
36+
HANDLE *out_child)
3737
{
3838
HANDLE childStdout[2], childStdin[2],childStderr;
3939
SECURITY_ATTRIBUTES secAtts;
@@ -45,6 +45,7 @@ __archive_create_child(const char *cmd, int *child_stdin, int *child_stdout,
4545
char *arg0, *ext;
4646
int i, l;
4747
DWORD fl, fl_old;
48+
HANDLE child;
4849

4950
childStdout[0] = childStdout[1] = INVALID_HANDLE_VALUE;
5051
childStdin[0] = childStdin[1] = INVALID_HANDLE_VALUE;
@@ -155,13 +156,19 @@ __archive_create_child(const char *cmd, int *child_stdin, int *child_stdout,
155156
*child_stdout = _open_osfhandle((intptr_t)childStdout[0], _O_RDONLY);
156157
*child_stdin = _open_osfhandle((intptr_t)childStdin[1], _O_WRONLY);
157158

159+
child = OpenProcess(PROCESS_QUERY_INFORMATION, FALSE,
160+
childInfo.dwProcessId);
161+
if (child == NULL) // INVALID_HANDLE_VALUE ?
162+
goto fail;
163+
164+
*out_child = child;
165+
158166
CloseHandle(childStdout[1]);
159167
CloseHandle(childStdin[0]);
160168

161169
archive_string_free(&cmdline);
162170
archive_string_free(&fullpath);
163171
__archive_cmdline_free(acmd);
164-
*out_child = childInfo.dwProcessId;
165172
return ARCHIVE_OK;
166173

167174
fail:

0 commit comments

Comments
 (0)