blob: 24d19fbfdccd07b4dab2fd3740be46b091c5ca6a [file] [log] [blame]
Glenn L McGrath7ca04f32002-09-25 02:47:48 +00001/*
2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 2 of the License, or
5 * (at your option) any later version.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU Library General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
15 */
16
17#include <sys/types.h>
18#include <signal.h>
19#include <stdio.h>
20#include <stdlib.h>
21#include <string.h>
22#include <unistd.h>
23#include "libbb.h"
24#include "unarchive.h"
25
26extern char get_header_tar_gz(archive_handle_t *archive_handle)
27{
28 int fd_pipe[2];
29 int pid;
Glenn L McGrath2e41d0c2002-09-27 06:46:02 +000030 unsigned char magic[2];
31
32 xread_all(archive_handle->src_fd, &magic, 2);
33 if ((magic[0] != 0x1f) || (magic[1] != 0x8b)) {
34 error_msg_and_die("Invalid gzip magic");
35 }
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000036
37 check_header_gzip(archive_handle->src_fd);
38
39 if (pipe(fd_pipe) != 0) {
Glenn L McGrath2e41d0c2002-09-27 06:46:02 +000040 error_msg_and_die("Can't create pipe");
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000041 }
42
43 pid = fork();
44 if (pid == -1) {
45 error_msg_and_die("Fork failed\n");
46 }
47
48 if (pid == 0) {
49 /* child process */
50 close(fd_pipe[0]); /* We don't wan't to read from the pipe */
51 inflate(archive_handle->src_fd, fd_pipe[1]);
52 check_trailer_gzip(archive_handle->src_fd);
53 close(fd_pipe[1]); /* Send EOF */
54 exit(0);
55 /* notreached */
56 }
57 /* parent process */
58 close(fd_pipe[1]); /* Don't want to write down the pipe */
59 close(archive_handle->src_fd);
60
61 archive_handle->src_fd = fd_pipe[0];
62
Glenn L McGrath2e41d0c2002-09-27 06:46:02 +000063 archive_handle->offset = 0;
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000064 while (get_header_tar(archive_handle) == EXIT_SUCCESS);
65
66 if (kill(pid, SIGTERM) == -1) {
67 error_msg_and_die("Couldnt kill gunzip process");
68 }
69
70 /* I dont think this is needed */
71#if 0
72 if (waitpid(pid, NULL, 0) == -1) {
73 error_msg("Couldnt wait ?");
74 }
75#endif
76
77 /* Can only do one file at a time */
78 return(EXIT_FAILURE);
79}
80